Group components from array
-
When I import a Autocad DWG my blocks get components in SU.
The outliner show them in his list.
Now I want to group some components together.
(like manual selecting them and choose 'Make Group')
I get it so far that I have an array of all wanted groups
and an array for each group with the wanted components.
With this code I create the empty groups:grouplist.each{|c| group = entities.add_group group.name = '<'+c.rjust(5)+'>' }
The empty groups show up in the outliner at the expected positions.
But how do I add the existing components from array to their groups? -
Use the array of entities as argument for
.add_group
.http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html#add_group
-
But waht about the quote in the doc:
@unknownuser said:
NOTE: calling add_group with entities in its parameters has been known to crash SketchUp. It is preferable to create an empty group and then add things to its Entities collection.
-
Anyway I give it a try and it works nicely.
grouplist.each{|c| str = '@my_group_list = GroupPrefix'+ c eval str group = entities.add_group(@my_group_list) group.name = '<['+c.rjust(5)+'>' }
By the way: Is there a better way to create dynamicly array names and recall them?
The Grouplist is an array of strings like [ "100" "200" ...]
The Group-arrays are like 'GroupPrefix100' etc. -
Ruby is kind of cool in that you can increment Strings.
name = "Group100" name.next #(also .next! and .succ & .succ!) ==> "Group101"
Maybe useful?
-
@hpw said:
But waht about the quote in the doc:
@unknownuser said:
NOTE: calling add_group with entities in its parameters has been known to crash SketchUp. It is preferable to create an empty group and then add things to its Entities collection.
Yea, I've asked about that myself, but I've yet to hear about any concrete examples where it fails. And I don't know how you would move existing geometry into a group without rebuilding it from scratch.
-
group=Sketchup.active_model.active_entities.add_group() ### now we must assume you have an array of instances... instances.each{|instance| group.entities.add_instance(instance.definition,instance.transformation) instance.erase! ### remove the original instance - it's now been repeated in the group... }
I think this is what you want ?
-
@unknownuser said:
I think this is what you want ?
Thanks TIG, works also nicely.
But what is now the better or safer solution?Hans-Peter
-
@unknownuser said:
But what is now the better or safer solution?
Why, mine of course ! -
Very convincing reason!
But is the warning in the doc correct of from an older version of SU.
Both shows me an identical result.
The first version is one code-line shorter. -
Another observation: The first solution seems much faster.
When I leave the outliner open and run the code, the outliner flickers as long the code run.
But the first is much faster than the second one. -
Having the Outliner open during script execution is risky as it can cause crashes when groups/instances are getting changed...
Adding stuff to groups immediately as they are made, in scripts can cause crashes, although I often do it - safest not to... Do whatever works for you best...
-
@unknownuser said:
Ruby is kind of cool in that you can increment Strings.
Thanks Jim for the tip.
In this case I can not use it, since my block-numbering-steps comes from the imported autocad-blocknames.
But maybe it gets usefull in the future.@unknownuser said:
Do whatever works for you best...
Thanks again TIG.
Hans-Peter
Advertisement