# Space sorting script

## Recommended Posts

Hi all,

I'm pretty new to direct scripting in Vectorworks, but always wanted to learn. As a first attempt I wanted to create a simple script that searches for "spaces" and assigns them to appropriate class depending on Net Area. In my file there is an example that should assign Spaces with area <10m2 to the RED class and rest to the BLUE class.

But, obviously, it fails since I' pretty green in this Area 🙂 Can someone give me a hint, not the solution, something that will help me to write this script?

For the record, I dislike Python and am not very good at it.

But it looks like you are getting the area, but never actually getting the handle to the Space Object so it has nothing to set the class of.

Posted (edited)

@Pat Stanford, thanks

So I think I don't really get the "handle" idea.

I tried to write a similar code for rectangles:

```RectCount = int (vs.Count('Rect'))
RectList = []

for i in range (RectCount):
obj = vs.GetObject((T=RECT))
area = vs.HAreaN(obj)
RectList.append([obj,area])

for j in range (len(RectList)):
if RectList[j][1] < 10.00:
vs.SetClass(RectList[j], 'RED')
elif RectList[j][1] >= 10.00:
vs.SetClass(RectList[j], 'BLUE')```

but I don't know how to describe a criteria to search for specified object (e.g. rectangle).

Edited by Grzegorz Krzemien
Posted (edited)

Managed to do this in a different way:

First - same idea, but with Rectangles:

```import vs

def RectSort (h):
area = vs.HAreaN(h)
if area < 10000000:
vs.SetClass(h, 'RED')
if area >= 10000000:
vs.SetClass(h, 'BLUE')

vs.ForEachObject(RectSort, "T=RECT")```

Works great.

I tried to recreate this, but instead of using rectangles, I wanted to use Spaces.

```import vs

def SpaceSort(h):
area = vs.Space_GetNetArea(h)
if area < 10000000:
vs.SetClass(h, 'RED')
if area >= 10000000:
vs.SetClass(h, 'BLUE')

vs.ForEachObject(SpaceSort, PON='Space')```

But it fails. There is something wrong with criteria I think, but I don't know what.

so I assume, I create a criteria in a wrong way.

Edited by Grzegorz Krzemien

Try this:

```import vs

def SpaceSort(h):
area = vs.Space_GetNetArea(h)
if area < 10000000:
vs.SetClass(h, 'RED')
if area >= 10000000:
vs.SetClass(h, 'BLUE')

vs.ForEachObject(SpaceSort,(("PON=Space")))```

Like Pat, I'm sort of feeling my way in the dark with Python, but this works for me.

Tried all of these combinations:

vs.ForEachObject(SpaceSort, PON='Space')

vs.ForEachObject(SpaceSort, (PON='Space'))

vs.ForEachObject(SpaceSort, ((PON='Space')))

but all of the fails.

First version gives error that suggests that there are no keyword arguments in vs.ForEachObject(), second and third that syntax is wrong.

GOT IT. The correct combination of quotes is:

vs.ForEachObject(RectSort, "PON='Space'")

Full script:

```import vs

def SpaceSort (h):
area = vs.Space_GetNetArea(h)
if area < 10: #value in m2
vs.SetClass(h, 'RED')
if area >= 10: #value in m2
vs.SetClass(h, 'BLUE')

vs.ForEachObject(SpaceSort, "PON='Space'")```

Thanks for help!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• KBASE
• #### MARIONETTE

×
• Create New...