Grzegorz Krzemien Posted April 19, 2021 Share Posted April 19, 2021 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? Space sorting script.vwx Quote Link to comment
Pat Stanford Posted April 23, 2021 Share Posted April 23, 2021 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. Quote Link to comment
Grzegorz Krzemien Posted April 23, 2021 Author Share Posted April 23, 2021 (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 April 23, 2021 by Grzegorz Krzemien Quote Link to comment
Grzegorz Krzemien Posted April 24, 2021 Author Share Posted April 24, 2021 (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. I couldn't find "Space" object on this page: https://developer.vectorworks.net/index.php/VS:Search_Criteria#Search_Criteria_Sub_Types so I assume, I create a criteria in a wrong way. Edited April 25, 2021 by Grzegorz Krzemien Quote Link to comment
michaelk Posted April 25, 2021 Share Posted April 25, 2021 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. Quote Link to comment
Grzegorz Krzemien Posted April 25, 2021 Author Share Posted April 25, 2021 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. Quote Link to comment
Grzegorz Krzemien Posted April 25, 2021 Author Share Posted April 25, 2021 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! Quote Link to comment
Recommended Posts
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.