Hi all,
I am trying to create a script for creating struts for models of geodesic domes. I do this by iterating over all edges in a model of a geodome and create boxes along the edges. I have gotten all this to work, but for convenience I would like to add all my created boxes to a new group. This fails in the second iteration of the loop, with this message:
Error; #<TypeError; reference to deleted Group>
geodome_frame.rb;58;in `entities'
geodome_frame.rb;58;in `create_frame'
geodome_frame.rb;53;in `each'
geodome_frame.rb;53;in `create_frame'
geodome_frame.rb;115;in `main'
geodome_frame.rb;84
geodome_frame.rb;57;in `call'
geodome_frame.rb;57
Here is the relevant code:
<span class="syntaxdefault"></span><span class="syntaxcomment"># Create a strut (as a box in a group)<br /># along an edge, facing a center point.<br /></span><span class="syntaxdefault">def create_strut edge</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> center</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> size<br /> entities </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> width</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> depth </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> size<br /> <br /> vec_to_center </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vector_to center<br /> plane_intersecting_with_center </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">fit_plane_to_points center</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> <br /> </span><span class="syntaxcomment"># We need a vector in the plane of the top of the strut.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># It is the same as the normal to the plane intersecting with the center<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># which is the same as the three first values in the plane we just calculated.<br /></span><span class="syntaxdefault"> width_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 plane_intersecting_with_center</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0..2</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> width_vec</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> width </span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> 2<br /> <br /> </span><span class="syntaxcomment"># We add half a strut-width to the end points<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># of the edge to get the corners of the top of the strut.<br /></span><span class="syntaxdefault"> e1 </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> width_vec<br /> e2 </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> width_vec<br /> e3 </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> width_vec<br /> e4 </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> width_vec<br /> <br /> </span><span class="syntaxcomment"># We add a group for the strut and create the strut top face<br /></span><span class="syntaxdefault"> strut_group </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br /> strut_top_rect </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">e1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> e2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> e3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> e4</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> strut_top_face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> strut_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face strut_top_rect<br /> <br /> </span><span class="syntaxcomment"># Push pull the top face to create the final strut<br /></span><span class="syntaxdefault"> strut_top_face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pushpull </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">depth<br /> <br /> return strut_group<br />end<br /><br /></span><span class="syntaxcomment"># Loop through all edges in the current selection and<br /># create struts. And add all the struts to a group.<br /></span><span class="syntaxdefault">def create_frame edges</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> center</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> size<br /> frame_group </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> 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">add_group<br /> frame_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Geodome frame'<br /></span><span class="syntaxdefault"> for edge in edges<br /> puts frame_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name<br /> puts frame_group<br /> strut </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> create_strut edge</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> center</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> size<br /> </span><span class="syntaxcomment"># strut.name = <br /></span><span class="syntaxdefault"> frame_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group strut<br /> end<br />end</span>
The entire script is here: https://bitbucket.org/tiktuk/geodome-frames-for-su/src/86808fce358a/geodome_frame.rb .
If you would like to reproduce:
- Install the plugin as usual.
- Select some geometry including edges (doesn't have to be a geodome model) for this test.
- Choose Create geodome frame from the plugin menu.
Any ideas..?