Fastest way to select component by definition.name
-
What is the fasted way of getting a component by its definition.name?
Must I iterate through the entities every time:entities=model.entities entities.each{|c| mylist.push(c) if c.definition.name=="WantedComponentname" }
-
defn = Sketchup.active_model.definitions['WantedComponentname'] # Returns nil if the definition doesn't exist,
-
@hpw said:
What is the fasted way of getting a component by its definition.name?
Must I iterate through the entities every time:entities=model.entities > entities.each{|c| > mylist.push(c) if c.definition.name=="WantedComponentname" > } >
name="WantedComponentName" ### for example defn=nil ### empty variable Sketchup.active_model.definitions.each{|definition| defn=definition if definition.name==name} puts defn ### prints it in ruby console so we see if it works... Sketchup.active_model.selection.clear Sketchup.active_model.selection.add(defn.instances) ### selects all component instances with that name...
-
@thomthom said:
> defn = Sketchup.active_model.definitions['WantedComponentname'] > # Returns nil if the definition doesn't exist, >
Well done - much shorter...
For some reason I prefer the 'name' to be either a variable [string] or inside "...", though your '...' does work...Sketchup.active_model.selection.add((Sketchup.active_model.definitions["WantedComponentName"]).instances) ### as a one liner ?
-
I prefer to use single quotes out of habit from coding PHP where single quoted strings require less parsing resources as they don't allow variables inside them as double quoted does. (Same thing is for Ruby strings but in the SU environment as oppose to web environment I don't think it matters much.)
-
Thanks for both valuable Tips.
I am making slow progress in learning SU-ruby and importing my Autocad-Models in a half-automatic fashion.
Just finished a Autolisp-Function to write ruby-scripts to TempDir with attributs for non-editable DC's.
(I noticed a few problems with DC attributes. Capital letters in field names are not liked by the DC, and also german Umlauts gives problem, they has to be converted to HTML syntax)
A ruby function restored the block-hierarchy using groups.Now I will have a look at setting interact behaviour to this components.
By the way: An idea to move component axes by ruby-code?Regards
Hans-Peter
-
@unknownuser said:
By the way: An idea to move component axes by ruby-code?
Find my ComponentInstance-add_entities.rb that has some instance/definition transformation stuff in it. It has to add entities to the instance definition without changing its origin - it does it by transforming the contents of an instance back to the origin, making a new definition and, swapping names, then overwriting the old one with new one, and purging the old one...
-
Hello TIG,
I tried a different approach with your componentinstance.add_entities
(Your ComponentInstance-add_entities.rb is loaded)
(Instead of creating a new Master-Group I want to make a existing component to the master-component of it's Sub-components)sel_def = Sketchup.active_model.definitions['MyDefName'] sel_def.add_entities(MySubCompArray)
This throws the error ' undefined method `add_entities' '
I think I need the instance.
So what's the way back from the definition to the instance?
(Still miss the deep understanding of SU data-model) ;-( -
@unknownuser said:
sel_def = Sketchup.active_model.definitions['MyDefName']
sel_def.add_entities(MySubCompArray)Doesn't work...
My thing only adds given entities into an instance - with flags*. If you only have one instance you could usesel_def.instances[0].add_entities(MySubCompArray)
If you want to add some entities to a definition itself use...
sel_def = Sketchup.active_model.definitions['MyDefName'] sel_def.entities.add(MySubCompArray)
These new entities need to be where you want them in the component. My instance based method lets you add things to an instance as it is placed and then *either make it unique or update the original definition and all of its instances etc...
-
Hello TIG,
Again many thanks for the hint.
sel_def.instances[0].add_entities(MySubCompArray)
does the job for me.
Now I can use the imported dummy-block from autocad as the hierarchy-master instead of creating a separate new group. Will keep the outliner structure more readable. I also use now Jim's togglewindows.rb to keep the outliner close during execution.
Advertisement