Jump to content
Developer Wiki and Function Reference Links Read more... ×
Sign in to follow this  
michaelk

syntax for vs.ForEachObject(callback, c):

Recommended Posts

Can someone please put me out of my misery?

What is the exact syntax for calling a procedure in Python?

For example, how would this VS script be written in Python:


PROCEDURE MoveOver;

PROCEDURE Moveit(h :HANDLE);
BEGIN
HMove(h,10,10);
END;

BEGIN
ForEachObject(Moveit, T=RECT);
END;
RUN(MoveOver);


Thanks!

mk

Share this post


Link to post

Still struggling with this Python-module, but this code seems to work:

vs. SelectAll ()
vs. ForEachObject (vs. HMove (vs. LSActLayer (), 100, 100), vs. ObjectType (3))
vs. DSelectAll ()

or

vs. SelectAll ()

Obj_H = vs. LSActLayer ()
Crit = vs. ObjectType (3)

vs. ForEachObject (vs. HMove (Obj_H, 100, 100), Crit)

vs. DSelectAll ()

Edit: Seems to work for just one rectangle at the time ...

Edited by DeSignature

Share this post


Link to post

Glad I'm not the only one struggling with it!

Thanks for taking a swing at this. I see what you're trying to do. But it doesn't have the same effect as a user defined procedure.

It only moves the top rectangle on the active layer. Not every rectangle on every layer.

The online Python guide doesn't list a ForEachObject(callback,c): function. But it's in the VS reference guide.

Python:

def vs.ForEachObject(callback, c):

return None

Description:

Calls a user defined procedure to operate on each object matching the specified search criteria.

The procedure subroutine specified by the callback parameter must have one parameter of type HANDLE, which is passed the handle to an object by the ForEachObject call.

Parameters:

callback Name of action procedure to be applied to matching objects

c Search criteria for locating objects.

My question is how to write the "action procedure to be applied to matching objects".

Really wish there was a VW-specific Python language guide and more examples of scripts.

mk

Share this post


Link to post

I'm not at my desk, but some tidbits:

In the Python examples, download the Interactive Calls demo. There is an FEO example there.

FEO works identically to VectorScript, except that the procedure definition should follow a Python format, and criterium is not a native data type, so you need to enclose it in quotes like a string. Think of criteria in Python as a string that has significance when parsed by VW's criteria handler.

-Josh

Share this post


Link to post

Michael,

You're right, I see the example file I referenced didn't make it to the developer site. I'll have to ask why. Here is the example for FEO:

import vs;

def SelectThem(h):
vs.AlrtDialog( "we're in", h );

vs.AlrtDialog( 'should show three consecutive dialogs' )
vs.ForEachObject( SelectThem, '((T=WALL))' );

or to answer your original question

import vs

def Moveit(h):
vs.HMove(h,10,10)

vs.ForEachObject(Moveit, "T=RECT")

-Josh

Share this post


Link to post

Thanks. That's exactly what I was looking for!

What is the 'import vs' line for? The script runs without it.

mk

PS. Please feel free to send along any other python examples that didn't make it to the developer site!

Share this post


Link to post
Thanks.

PS. Please feel free to send along any other python examples that didn't make it to the developer site!

I second these.

By the way: where did you define the handle in this code?

Share this post


Link to post

The way I understand it, you don't have to define the variable type in Python.

ForEachObject passes a handle variable, so therefore h is a handle type variable.

hth

mk

Share this post


Link to post

One thing to remember is that VS came first, so many of the existing calls are more logical in VS than Python. Here are a couple ways to make ForEachObject more Pyhton-like.

def ListEachObject( C ):
   outList = []

   def AddToList( h ):
       outList.append( h )

   vs.ForEachObject( AddToList, C )
   return outList

for h in ListEachObject( "T = RECT" ):
vs.HMove(h, 0, 1)

vs.ForEachObject(lambda h: vs.HMove(h, 0, -1), 'T = RECT')

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×