Get Group Owner
-
The following Code makes groups in groups.
width = 200 depth = 100 pts = [] pts[0] = [0, 0, 0] pts[1] = [width, 0, 0] pts[2] = [width, depth, 0] pts[3] = [0, depth, 0] model = Sketchup.active_model g1 = model.entities.add_group g1.entities.add_face pts g1.transformation = Geom;;Transformation.new([10,0,10]) g2 = g1.entities.add_group g2.entities.add_face pts g2.transformation = Geom;;Transformation.new([20,0,20]) g3 = g2.entities.add_group g3.entities.add_face pts g3.transformation = Geom;;Transformation.new([30,0,30]) puts "model; " + model.entityID.to_s puts "g1; " + g1.entityID.to_s puts "g2; " + g2.entityID.to_s puts "g3; " + g3.entityID.to_s puts "g3.parent; " + g3.parent.entityID.to_s + ", " + g3.parent.class.name puts "g3.parent.parent; " + g3.parent.parent.entityID.to_s + ", " + g3.parent.parent.class.name puts "g3.parent.parent.parent; " + g3.parent.parent.parent.entityID.to_s + ", " + g3.parent.parent.parent.class.name
And results in
model; Model g1; 25 g2; 49 g3; 73 g3.parent; 40, Sketchup;;ComponentDefinition g3.parent.parent; 3, Sketchup;;DefinitionList g3.parent.parent.parent; Model, Sketchup;;Model
Now question:
Given g3, how can I get the owning/creating group g2 by code? g3.parent is not the right way. -
You can't traverse backwards like that.
Given an instance, that instance can appear many places in the model, if any of it's parent nodes have multiple instance. That makes it impossible to get a path from instance > model. The other way works, model > instance - when you traverse the model tree.
-
... You can get some information regarding inheritance in one of two ways:
-
If the user is actively editing a group or component, you can get the current path using: Sketchup.active_model.active_path
-
If you are traversing through the model's entities, you can keep track of where you are in the model and develop your own inheritance structure that way.
-
-
I believe g3.parent.instances[0] gives you the correct group.
-
I repeat this from somewhere else...
<span class="syntaxdefault"></span><span class="syntaxkeyword">class </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group<br /> </span><span class="syntaxcomment">### Sometimes the group.entities.parent refer to the wrong definition. <br /> ### method checks for this and locates the correct parent definition.<br /> </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">not Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">method_defined</span><span class="syntaxkeyword">?(;</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">def definition</span><span class="syntaxkeyword">()<br /> if </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">parent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instances</span><span class="syntaxkeyword">.include?(</span><span class="syntaxdefault">self</span><span class="syntaxkeyword">)<br /> return </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">parent<br /> </span><span class="syntaxkeyword">else<br /> </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">|<br /> return </span><span class="syntaxdefault">definition </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instances</span><span class="syntaxkeyword">.include?(</span><span class="syntaxdefault">self</span><span class="syntaxkeyword">)<br /> }<br /> </span><span class="syntaxdefault">end<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil </span><span class="syntaxcomment">### Should not happen.<br /> </span><span class="syntaxdefault">end<br /> end</span><span class="syntaxcomment">#if<br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#class Group </span><span class="syntaxdefault"></span>
Usage:
defn = group.definition
-
@cleverbeans said:
I believe g3.parent.instances[0] gives you the correct group.
Not if the group has instances, which it may have after copying a group and not editing it.
Advertisement