How can I determine entity level?
-
@jim said:
Model.active_path should do it. (If the Component is selected as shown.)
level = Sketchup.active_model.active_path.nitems
If not, I think you need to check the parent and compare that to see if it is the model. Repeat until you get to the model level.
Something like this (not tried)
level = 0 > while entity.parent != active_model > entity = entity.parent > level += 1 > end
As I can see, both methods work just if my component is selected. In fact, what I want is simple: to make a list with all components/groups that have not inside them (and also are not included in other components or groups). I'd like to avoid .each method because I'm afraid that could take long time to iterate trough all entities in a complex model.
-
The second should work for you.
-
I don't know of a built in way of doing this. For my purposes, I recursively iterate through each top level group and component while maintaining a list of records containing the level spec and other relevant data for each child entity. Classes are perfect for this BTW. I use tests of the form:
entity.instance_of?( Sketchup;;Group ) ... entity.instance_of?( Sketchup;;ComponentInstance )
It would be nice if the Sketchup Ruby API supported a built in utility to do this.
-
If you want to find components that do not contain other components then inspect the model's definitions...
what_I_want=[] Sketchup.active_model.definitions.each{|d| components=false d.entities.each{|e| if e.class==Sketchup;;ComponentInstance components=true break end#if } what_I_want<<d if not components } if what_I_want[0] puts what_I_want end#if
This returns & 'puts' an array of the definitions containing components...
You can also expand it for Groups... -
Many thanks TIG! Just one thing I can't figure out how to do it: I want to apply this jut to that definitions which are used in model, but I don't want to purge unused definitions. Unfortunately, active_model gives just active entities. Everything is inside a group or component is not accesible.
-
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array. -
@thomthom said:
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array.AND?? This is what TIG's idea does... I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
TIG's script reports me ALL definitions. -
@newone said:
@thomthom said:
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array.AND?? This is what TIG's idea does... I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
TIG's script reports me ALL definitions.At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.
-
definitions have
.instances
. -
@newone said:
@thomthom said:
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array.AND?? This is what TIG's idea does... I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
TIG's script reports me ALL definitions.Unused definitions doesn't have any instances. So when you iterate over all the definitions you check for those that have instances. No need to iterate the entire model.
Sketchup.active_model.definitions.each { |d| if !d.group? && !d.image? && d.count_instances # Perform your actions to the definition here. # Note the check for group and image definitions - they are also included in the definition list. end }
-
@david. said:
@newone said:
@thomthom said:
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array.AND?? This is what TIG's idea does... I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
TIG's script reports me ALL definitions.At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.
Unless you need to know it's parents, or location in the model entity tree, there is no need to parse the whole model. To iterate only used definitions, the definition list is all you need.
-
@thomthom said:
@david. said:
@newone said:
@thomthom said:
Iterate all
model.definitions
- for eachdefinition
iterate through its.instances
array.AND?? This is what TIG's idea does... I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
TIG's script reports me ALL definitions.At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.
Unless you need to know it's parents, or location in the model entity tree, there is no need to parse the whole model. To iterate only used definitions, the definition list is all you need.
If you re-read to OP, that is what I understood that he wants...
@newone said:
Hy, I was playing a little with Entity.parent and model methods and I can't figure out how can I determine the "depth" of an entity. If I have a component (or group) that is encapsulated deep in other component, how can I find how deep (in numbers from 0 to n, where 0 means the component is topmost)
-
Thanks thomthom, sorry... I was tired whed I read your post and I didn't understood exactly what you mean. But, Jim opened my eyes.
Finally this is how I did it:model = Sketchup.active_model all_defs = model.definitions in_model = 0 out_model = 0 t1 = Time.new all_defs.each do |defn| count = defn.count_instances if count == 0 out_model += 1 else in_model += 1 end end t2 = Time.new dt = t2 - t1 puts 'time for completion; ' + dt.to_s puts 'all definitions; ' + all_defs.length.to_s puts 'in model; ' + in_model.to_s puts 'out_model; ' + out_model.to_s
I was surprised that with 1600 component definitions , time for completion was 0.0
Advertisement