Group in a group in a group... - transform point to outside?
-
If you have got a reference to a Vertex it belongs to an Edge [to which you can get a reference too].
That Edge has a Parent.
It's either the Model or a ComponentDefinition.
It's not a Group - although it might be a group that might be theComponentDefinition.instances[0]
, assuming there are no copies of the group - if there are we get into problems.
If it's a ComponentInstance we have problems finding the right instance if its definition has more that one instance.
Assuming you get a lone group, it has a transformation.
If that group is inside another 'parent' the issue repeats... and you must get each container's transformation etc in turn.
As TT explained - usetr = tr2 * tr1 * tr0
to combine several transformations.
Andtr.inverse
unravels it 'backwards', depending on how you want to use it...If your reference is not to a specific Vertex, but rather to a Point3d picked/determined in the Model, that you wish to 'transform' back into an equivalent Point3d inside a group or grouped group etc, then the inverse transformation of all of the nested groups should return that. Also as you know the outermost group from it being picked etc and that the inner nesting is only ever one group then group.entities[0] will return the nested groups in turn, rather than a definition; so this 'inward mining' approach will work, whereas 'outward mining' is fraught !
-
When you use
vertex.position.transform(group.transformation)
it will not transform the vertex, it only returns the point that it had as a new point.
The vertex remains unchanged.
Is this what's expected?
If you want to really transform a vertex or vertices you need to use a way likeverts[0].edges[0].parent.entities.transform_entities(transformation, [verts])
or.transform_by_vectors([verts], [vectors])
... -
Just thinking out loud, but he's drilling down then he's already got the right component instance because he would have found it while iterating over the current level of entities.
When he drills down into it, he'll know what instance he's in. So once he drills into it, could he find a faces parent definition, then iterate over all those definition instances to see which matches the object_id of the instance he knows he's inside of?
-
Since he's going from the current context down into sub-components it's no need to preference parent entities at all. He just picks up the transformation for each level he digs into.
-
You helped me a lot! Thanks!!!!
Now, my current (but not yet perfectly working result):
<span class="syntaxdefault">group </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> projectGroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br /><br /></span><span class="syntaxcomment"># add selected faces to group<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">tmp</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if tmp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> tmp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> getEntities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">tmp</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> nil</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> group</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> elsif tmp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> newFace </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> tmp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> nil</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br />end</span>
<span class="syntaxdefault"></span><span class="syntaxcomment"># Thanks to http://sketchucation.com/forums/viewtopic.php?f=180&t=29138&start=0<br /></span><span class="syntaxdefault">class Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face<br /> def copy</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> trans</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> vec </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">new 0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0<br /> dist </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> 0<br /><br /> ov</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault"> vec</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> dist if dist </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> 0<br /> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if dist </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> 0<br /> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">offset</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">vec</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> else<br /> if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?)<br /></span><span class="syntaxdefault"> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> else<br /> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br /> end<br /> end<br /> outer_face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face ov<br /> outer_face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"green"<br /><br /></span><span class="syntaxdefault"> inner_faces </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault"> if self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">loops</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">></span><span class="syntaxdefault"> 1<br /> il </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">loops<br /> il</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">shift<br /> il</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">loop</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> ov</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault"> loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if dist </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> 0<br /> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">offset</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">vec</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> else<br /> if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?)<br /></span><span class="syntaxdefault"> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> else<br /> ov</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br /> end<br /> end<br /> inner_face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face ov<br /> inner_faces</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">inner_face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br /> inner_faces</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">f</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault"> end<br /> end<br /> return outer_face<br /> end<br />end </span><span class="syntaxcomment"># Class Face </span><span class="syntaxdefault"></span>
<span class="syntaxdefault">def getEntities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">element</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> trans</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> group</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> entitiesArray </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Array</span><span class="syntaxkeyword">.new()<br /><br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">element</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> elementEnts </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> element</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition<br /> else<br /> elementEnts </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> element<br /> end<br /><br /> elementEnts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?)<br /></span><span class="syntaxdefault"> trans </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> trans </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /> else<br /> trans </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /> end<br /> getEntities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> trans</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> group</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> elsif entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> newFace </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> trans</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br /> end<br />end</span>
Oh my goodness - lots of stuff
So, copying all the faces into the group "group" is successful. Problem: At the end, the position of "group" is different then the original one... so, the transformation doesn't work.
Do you understand my Ruby code and, with a little bit of luck - can you find a mistake using the transformation? I'm not quite sure about
trans = trans * entity.transformation
Thanks again for all your help!!!
EDIT: Oh, why the tabs in the code are not shown? Now, the code looks not very clear...
-
Regarding your modification of the
Sketchup::Face
class: please don't release this code in a public plugin. Modifying base classes can easily conflict with other plugins. More info: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/ -
@thomthom said:
Regarding your modification of the
Sketchup::Face
class: please don't release this code in a public plugin. Modifying base classes can easily conflict with other plugins. More info: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/Good hint! Thanks a lot! I will try to write everything in a module to avoid problems with other plugins!
Thanks for pimping my code style!!
-
@thomthom said:
Regarding your modification of the
Sketchup::Face
class: please don't release this code in a public plugin.If you read the new Trimble edition of the API Terms of Use, it can be understood that modifying the API classes is a violation of the terms.
-
<span class="syntaxdefault"><br />Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">modelEnt</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if modelEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> modelEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> </span><span class="syntaxstring">"Model2GCode_project_x"<br /><br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox modelEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">origin</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> modelEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">projectEnt</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if projectEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> projectEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">.include?(</span><span class="syntaxstring">"Model2GCode_part_x_"</span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox projectEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">origin</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> projectEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">partEnt</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if partEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> partEnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">vertex</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox vertex</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> end<br /> end<br /> end<br /><br /> end<br /> end<br /><br /> end<br />end<br /></span>
I solved a lot of problems... but a last one is very tricky...
But let me explain a little bit more:
I have a group "Model2GCode_project_x". In this group, there is another group "Model2GCode_part_x_y". In this group, there are a lot of faces.
The faces inside this inner group start in the coordinates origin of Sketchup. So, at the end, I should get at least one point in (0, 0, 0). But I get very strange points with completly different positions.However, the group.transformations are identity, means the origin of these transformations are (0, 0, 0).
At first: Do you understand my problem?
And then: Do you have any idea, what's going wrong? I tested a lot, but always the same problem...Thanks a lot for your help!!
-
It is not advisable to use
UI.messagebox()
for debugging. (If the string argument is invalid, thenUI.messagebox()
either fails silently, or causes a error by itself.)Use
puts()
to output inspection strings to the Console instead. -
I tried to simulate your problem using the code you posted but I got 0,0,0 points for some of the faces as expected.
Perhaps if you posted a model just so we are working on the same problem.
-
@sdmitch said:
I tried to simulate your problem using the code you posted but I got 0,0,0 points for some of the faces as expected
Perhaps if you posted a model just so we are working on the same problem.
Oh, that's so nice! Thanks a lot for your help!
I uploaded a Sketchupfile:
http://arbeitenkannstduspaeter.eu/Sketchup/Test3.skp
When you get the right result - this means, I have to look for the problem on an other position... puhh...
-
Okay, I played a little bit with this problem...
My new findings:- when I move the group, the transformation of the outer group change in the right way
- but I have still an offset (in my case in -x direction)
Perhaps it helps a little bit to find the main problem!?
-
I think, I solved the problem - but I don't know exactly, what WAS wrong...
I saw, by copiing the selected faces into a new group, not every face has the original direction. So, I changed my Face.copy function (I know, its not good to change the Face.class, but I will solve THIS problem later) in that way:
outer_face = ents.add_face ov outer_face.material = "green" if outer_face.normal != self.normal outer_face.reverse! end
Now, all the faces have the right direction and now the problem with the offset of the points is gone.
But, to be honest, I have realy no idea, how these two problems correlate! And it doesn't help, to change the direction of these faces by hand - the problem stays! Just by copiing all the faces again, the problem of the offset is gone!
Do you have ANY idea what's going on?
I thank you sohhhhh much for all your support!!! You helped my realy a lot!
Advertisement