[code] intersect_with example
-
Hi,
I have tried to use two intersected cubes to demonstrate how to use the intersect_with command. This could be a example for the ruby doc.
<span class="syntaxdefault"><br /> model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> entities </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /><br /> </span><span class="syntaxcomment">#Create a group to collect all entities for base cube<br /> </span><span class="syntaxdefault">basegroup</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br /> basegroupentities</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">basegroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> p1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p2 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">2000</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p3 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">2000</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">150</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p4 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">150</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p5 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">points </span><span class="syntaxkeyword">= [</span><span class="syntaxdefault">p1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p4</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p5</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxcomment">#Add a face entity into base group<br /> </span><span class="syntaxdefault">base </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">basegroupentities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face points<br /> normal </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normal<br /> </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">normal</span><span class="syntaxkeyword">==[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">base </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">reverse</span><span class="syntaxkeyword">! <br /> </span><span class="syntaxdefault">end<br /> base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pushpull 1000<br /> base_trans</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">basegroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /><br /> </span><span class="syntaxcomment">#Create a group to collect all entities used for cutting cube<br /> </span><span class="syntaxdefault">cutgroup</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br /> cutgroupentities</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">cutgroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> p1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">500</span><span class="syntaxkeyword">, -</span><span class="syntaxdefault">50</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">500</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p2 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1500</span><span class="syntaxkeyword">, -</span><span class="syntaxdefault">50</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">500</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p3 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1500</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">200</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">500</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p4 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">500</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">200</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">500</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">p5 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">500</span><span class="syntaxkeyword">, -</span><span class="syntaxdefault">50</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">500</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">points </span><span class="syntaxkeyword">= [</span><span class="syntaxdefault">p1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p4</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">p5</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxcomment">#Add a face entity into cut group<br /> </span><span class="syntaxdefault">base </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">cutgroupentities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face points<br /> normal </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normal<br /> </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">normal</span><span class="syntaxkeyword">==[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">base </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">reverse</span><span class="syntaxkeyword">! <br /> </span><span class="syntaxdefault">end<br /> base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pushpull 1000<br /> cut_trans</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">cutgroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /> <br /> </span><span class="syntaxcomment">#Use all entities in cut group to cut through all entities in base group<br /> #The middle basegroup variable is to specify that the intersection lines are drawn on base group entities.<br /> </span><span class="syntaxdefault">cutgroupentities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">intersect_with false</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">cut_trans</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">basegroup</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">base_trans </span><span class="syntaxkeyword">, </span><span class="syntaxdefault">true</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">basegroup <br /> <br /> status </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">basegroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">explode<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zoom_extents<br /> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Click to delete cut group."</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">cutgroup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!<br /> </span><span class="syntaxdefault"></span>
-
Thanks, that was really helpful, but I'm having problems with a simple variation on this.
The basic concept is a model with only groups at the top level. The user selects a group and then runs the script. The script should intersect that group with the model; the new geometry is created inside of that group. My next step after this is working is to let the user multi-select and iterate through their selection. That should be easy if I can just get the basic code working first.
This is what I want for the intersect_with line, but I just can't get it to work. I guess I need to get the name of the group that is in selection[0]? and then use that similar to your code.
selection[0].intersect_with true, selection[0].transformation, selection[0], [0,0,0], true, entities
-
Your
selection[0]
will be anentity
BUT theintersect_with
method is forentities
- that is the 'container-entities' holding that individual entity... see
http://code.google.com/apis/sketchup/docs/ourdoc/entities.html#intersect_with
Recast it something like...selection[0].parent.entities.intersect_with(true, selection[0].transformation, selection[0].parent.entities, selection[0].transformation, true, entities)
***
***I'm not sure what your final argument 'entities
' is - it should either be an 'entities object' or an 'array of entities'... it could beselection[0].parent.entities.to_a
so all entities in the same context are used in the intersection -
Someone please..
Codebox the script example, parenthesize the
intersect_with
call (it wraps,) and rename the topic title:
[ code ] Entities.intersect_with example
.. so that Jim's sticky code index script can find it. -
TIG, thanks for you help. That got me over the hump. Below is what I was looking for. If you select one or more top level groups and run this script, it will iterate through the groups adding necessary intersecting geometry under each group.
model = Sketchup.active_model entities = model.active_entities selection = model.selection # loop through selection of top level groups to create new intersecting geomtry under each group. selection.each { |entity| entity.entities.intersect_with(true, entity.transformation, entity.entities.parent, entity.transformation, true, entity.parent.entities.to_a) }
-
I suggest you trap for non-groups
selection.each { |entity| ### next if not entity.class==Sketchup;;Group ### entity.entities.intersect_with(true, entity.transformation, entity.entities.parent, entity.transformation, true, entity.parent.entities.to_a) }
Also shouldn't
entity.entities.parent
beentity.entities.parent.entities
Advertisement