How to import several models to designated positions
-
@kwalkerman said:
Take a look at entities.add_instance:
https://developers.google.com/sketchup/docs/ourdoc/entities#add_instance
and the DefinitionList, especially DefinitionList.load and DefinitionList.load_from_url:
https://developers.google.com/sketchup/docs/ourdoc/definitionlist
--
KarenI got it. Thanks very much!
-
@thomthom said:
If the models are not SketchUp models, look at:
Model.import
https://developers.google.com/sketchup/docs/ourdoc/model#importMy original models are .3ds. Thanks for your tip!
-
@thomthom said:
I had a look at
model.import
and found that it annoyingly didn't return the definition to the imported model. Instead it activates a tool for the user to place it.I created this little snippet that worked around that and placed the imported model at a given coordinate:
<span class="syntaxdefault"><br />module Test<br /><br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">import_3ds</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> file </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'c;/cube.3ds'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> position </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ORIGIN </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> </span><span class="syntaxcomment"># Cache camera position because model#import will change it.<br /></span><span class="syntaxdefault"> camera </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">camera<br /> eye </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">eye<br /> target </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">target<br /> up </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">up<br /> </span><span class="syntaxcomment"># Begin import operation.<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Import 3DS Model'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Cache existing list of definitions.<br /></span><span class="syntaxdefault"> cache </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br /> </span><span class="syntaxcomment"># Import model, this will attach it to the cursor.<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">import</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> file </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> nil </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Deactivate Place Model tool.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Find root definition because the import creates multiple definitions.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># The sub-components are maked as #internal? - so we look for the one<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># without the mark.<br /></span><span class="syntaxdefault"> imported </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> cache<br /> definition </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> imported</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">internal</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Place instance in current context.<br /></span><span class="syntaxdefault"> tr </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault"> position </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_instance</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> definition</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> tr </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Restore camera.<br /></span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> eye</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> target</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> up </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Finished! ;)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /> rescue<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">abort_operation<br /> raise<br /> end<br /><br />end </span><span class="syntaxcomment"># module<br /></span><span class="syntaxdefault"> </span>
I test the code, it works well. Thanks!
But now I find a problem about the given coordinate which is designated by me. After importing the model, the position of the model may not be the one I designated. This is because of the file I want to import. Use the Bed.skp file as an example. Suppose I have two Bed models as these in the attachment, if I import them into sketchup with the same given coordinate, after importing, however, the positions of them are not same.
I want to import model with its center at given coordinate in sketchup, no matter where the model is in the import file. Is there any way to implement it?
-
Ah, then you would find the centre of the imported model based on its bounds - and work out where its origin is relative to this centre.
-
Why not get the selected component instance BEFORE you deactivate the MoveTool ???
Then you can get the instance's definition without all that search part.
AND... There IS a standard Ruby module
Test
(and the API added a few methods in there,) that should really not be modified.Something like this... wrap inside a module
Temp
, or at the very least, in a submodule ofTest
, such as...Test::Import
-
-
@unknownuser said:
@dan rathbun said:
There IS a standard Ruby module
Test
There is a
Test
module part of the Ruby that ships with SketchUp?YES. And.. John H & Co. added test methods to it:
Test.methods(false).sort
%(#000000)[>>] %(#008000)[["AutoClickTime", "AutoClickTime=", "AutoDragDistance", "AutoDragDistance=", "CorruptModel", "FindFile", "eat_mem", "face_to_mesh", "flip_images_for_origami", "project_textures_from_photo", "show_fps", "time_display", "time_pick", "use_sg_off_screen", "use_sg_on_screen"]]
This is the same module that gets defined when you use the Standard Library Extension for unit testing. However.. in this case the
Test
module is only used as a namespace. (That is not to say that other library extensions, might add methods to moduleTest
.) -
flip_images_for_origam
-
I had a look at
model.import
and found that it annoyingly didn't return the definition to the imported model. Instead it activates a tool for the user to place it.I created this little snippet that worked around that and placed the imported model at a given coordinate:
<span class="syntaxdefault"><br />module Testbed<br /><br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">import_3ds</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> file </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'c;/cube.3ds'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> position </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ORIGIN </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> </span><span class="syntaxcomment"># Cache camera position because model#import will change it.<br /></span><span class="syntaxdefault"> camera </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">camera<br /> eye </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">eye<br /> target </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">target<br /> up </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">up<br /> </span><span class="syntaxcomment"># Begin import operation.<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Import 3DS Model'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Cache existing list of definitions.<br /></span><span class="syntaxdefault"> cache </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br /> </span><span class="syntaxcomment"># Import model, this will attach it to the cursor.<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">import</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> file </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">instance </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">definition </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">instance</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> nil </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Deactivate Place Model tool.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Place instance in current context.<br /></span><span class="syntaxdefault"> tr </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault"> position </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_instance</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> definition</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> tr </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Restore camera.<br /></span><span class="syntaxdefault"> camera</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> eye</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> target</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> up </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Finished! ;)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /> rescue<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">abort_operation<br /> raise<br /> end<br /><br />end </span><span class="syntaxcomment"># module<br /></span><span class="syntaxdefault"> </span>
-
When I first listed the methods, I thot for a moment (until I looked twice,) that the seventh method was named
eat_me!
...
Advertisement