Retrieving parent definition
-
I can go through the definition list and get an array of all the components but I'd like to get an array of only the parent components. How can the parent definition of a component with subcomponents be retrieved from the definition list if the definition doesn't have an instance?
Sorry if this has been asked before but I can't seem to find anything searching the forum.
Thanks,
Frank -
Thats a little trickier than it might sound. Just because a component is inside another component, that doesn't mean that is the only place it exists.
But I think you could still figure out what you're looking for by iterating through all the definitions, and then iterate over all instances and check what their parent is. It would be possible that some instances will be directly in the modelspace while others will be nested. Its up to you to determine how you want to treat the ones that have mixed parents.
Something like that should work, depending on what you're thinking to do with the components you find.
-
Or maybe this is an instance where simply finding all component instances inside the model space is a good way to go. Use the cool grep feature:
outer_instances = Sketchup.active_model.active_entities.grep(Sketchup;;ComponentInstance))
-
You would test the definitions'
entities
collections to see if it contains any instances.dl = Sketchup.active_model.definitions() pars = dl.reject {|d| d.entities.grep(Sketchup::ComponentInstance).empty? }
-
model=Sketchup.active_model definitions_containing_instances = [] model.definitions.each{|d| next if d.group? || d.image? ### for compos only d.entities.each{|e| if e.is_a?(Sketchup;;ComponentInstance) definitions_containing_instances << d break end } }
This code makes an array named '
definitions_containing_instances
', which contains only component-definitions that contain another component-instance.
Groups and Images are ignored.
It will always contain any component-definition that matches this simple rule... so an instance of defn A inside defn B adds def B to the list, AND and instance of defn C inside defn A also adds defn A to the list...
It you only want one level of nested to return positive, then you you can check for the defn having an instance with a parent that is not the model and the skip it...model=Sketchup.active_model definitions_containing_instances = [] model.definitions.each{|d| next if d.group? || d.image? ### for compos only nested=false d.instances.each{|i| if i.parent != model nested=true break end } next if nested d.entities.each{|e| if e.is_a?(Sketchup;;ComponentInstance) definitions_containing_instances << d break end } }
-
Thanks for taking the time to reply,
@Chris, I've never heard of grep before, which isn't all that surprising since I'm still very much a newbie to all this. I'll have to look into it and see how helpful it can be in certain instances. I tried to run your code but it only returns instances... I'm thinking there's something you might of left out which is cool it'll give me something to play with and see if I can figure it out on my own which is how I seem to learn the best.
@Dan, thanks for the simple solution which works great! Again, you're using grep which I have no clue of what it does but this line of code I'm sure will be helpful in understanding it.
@Tig, Thanks for the more detailed code, it works just as desribed and does exactly what I need it to do. I'll study the code later on to try and fully understand what's going on there. One question regarding your code, any reason or advantage to using << instead of .push()?
It's always interesting to see that there are many ways to accomplish the same task. Thanks for your time guys.
One suggestion if I may do so, maybe this should be added to the code snippets thread? Asking is always my last resort so I'm getting pretty good at searching for answers and I couldn't for the life of me find anything pertaining to this.
Frank
-
<< IS .push() !
Ruby has many dialects... -
@frankn said:
@Chris, I've never heard of
grep
before, ...
@Dan, ... Again, you're usinggrep
which I have no clue of what it does ...The docs for SketchUp API classes do not list standard Ruby methods that are inherited. (This is normal also in the Standard Ruby docs, however the latter lists what libraries are "mixed" in. The SketchUp docs do not. [I know I have requested this be changed.])
To list the ancestors of a class, use the
ancestors()
method:
Sketchup::Entities.ancestors %(#008000)[>> [Sketchup::Entities, Enumerable, Object, Kernel]]
Checking on
Enumerable
shows that it is actually a library "mixin" module, that is mixed into collection type objects, to give them extended common functionality.Consult the Standard Ruby Documentation to read the reference on the
Enumerable
library. (It is online, and I also posted the downloadable CHM (compiled HTML Help Markup,) in the Ruby Resources thread. (Follow the link in signature line.) -
@tig said:
<<
IS.push()
!
Ruby has many dialects...The correct term would be aliases, ...
... however this example is not a case of a simple method alias.
In fact looking at the C code.. both methods call the C-side function%(#0080BF)[rb_ary_push]
with different arguments.Synopsis:
BOTH Ruby methods return the receiver array, so call chaining can be used.
<<()
takes ONE argument.push()
can take a multiple argument list.
a = []
a << 1,2,3 %(#008000)[Error: #<SyntaxError: (eval): compile error (eval): syntax error, unexpected ',', expecting $end a << 1,2,3 _______^>]
a <<( 1,2,3 ) %(#008000)[Error: #<SyntaxError: (eval): compile error (eval): syntax error, unexpected ',', expecting $end a <<( 1,2,3 ) ________^>]
a.<<( 1,2,3 ) %(#008000)[Error: #<ArgumentError: (eval):0:in
<<': wrong number of arguments (3 for 1)>
(eval)
(eval):0]`a.push( 1,2,3 ) %(#008000)[[1, 2, 3]]
-
@frankn said:
One suggestion if I may do so, maybe this should be added to the code snippets thread?
I added a link to TIG's post, to the "Code Snippets" Subject Index, under the title:
"Collecting definitions that ARE parents of component instances"
My one-liner is not worthy, it will slow down as the model gets bigger (and does not ignore images & groups.)
-
Frankn, just to clarify for myself, but you really want only components that have sub-components in them? or is it that you want a list of definitions whose instances are NOT sub-components?
-
The parent of every
ComponentInstance
Sketchup.active_model.definitions.map() {|e| e.instances }.flatten.map() {|e| e.parent}.uniq
Every
ComponentDefinition
that has sub-components
Sketchup.active_model.definitions.find_all() {|e| e.entities.find() {|e| e.class == Sketchup::ComponentInstance } }
Every
ComponentInstance
that has sub-components
Sketchup.active_model.definitions.find_all() {|e| e.entities.find() {|e| e.class == Sketchup::ComponentInstance } }.map() {|e| e.instances }.flatten
To get the parent of a
Entity
, useentity.parent
. -
@chris fullmer said:
Frankn, just to clarify for myself, but you really want only components that have sub-components in them? or is it that you want a list of definitions whose instances are NOT sub-components?
Hey Chris, I wanted to be able to find the parent definition of components with subcomponets that aren't in the model but still in the definition list. Hope that makes sense... but if it doesn't, basically the 2nd code that Tig shared is what I wanted to accomplish.
Advertisement