Load an object and attached it to the pointer
-
Hi all,
I got a new problem using ruby in sketchup...
I tray to create a dialog box which let user to load some objects inside the "component folder". I've successfully made it but I would like to attached the object loaded to the pointer in order to place it with the mouse like sketchup do when we drag and drop some SKP from desktop to the working window...
here is a simple example :
# First we pull in the standard API hooks. require 'sketchup' # Add a menu item to launch that one. UI.menu("PlugIns").add_item("Load Tree") { # Method calling. load_Tree } def load_Tree # Prompt parameters dialogue prompts = ["X in cm","Y in cm","Z in cm"] defaults = ["0","0","0"] list = ["","",""] input = UI.inputbox prompts, defaults, list, "Origine Point" pannelType = "MyTree.skp" model = Sketchup.active_model entities = model.active_entities path = Sketchup.find_support_file pannelType ,"Components/" definitions = model.definitions componentdefinition = definitions.load path x0= input[0].to_i / 2.54 y0= input[1].to_i / 2.54 z0= input[2].to_i / 2.54 point = Geom;;Point3d.new x0,y0,z0 transform = Geom;;Transformation.new point instance = entities.add_instance componentdefinition, transform if (instance) UI.messagebox "Tree successfully loaded" else UI.messagebox "Failure" end end
My goal is to delete the "x,y,z" arguments in the dialog box and place the object with mouse but I can't find anything inside the Google API and I turn in a round reading it...
I think it's possible and quite easy to do that...Thanks for your suggestions
-
http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#add
adds a new definition by 'name' to the model's definitions list [no instances].
http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#load
loads a definition to the model's definitions list from an external skp_path [no instances].
http://code.google.com/apis/sketchup/docs/ourdoc/entities.html#add_instance
places an instance of the definition at the given transformation.
http://code.google.com/apis/sketchup/docs/ourdoc/model.html#import
imports a file [several types] - if it's a SKP file then it also inserts an instance of the definition and attaches it to the cursor! [it is of course also 'loaded' into the model's definitions list so it is accessible]
You can also write a tool which takes the cursor position as a 'point' and dynamically changes the instance's position as you move the pointer, until you left-click and then that is the instance's location... -
TIG, thanks for the mini tut. Until this post, I didn't realize that every component in the model was a "instance". I previously thought that when you created a definition, that a component was automatically created in the model. That's wrong, right?-)
When you delete all occurrences of a component from a model, it remains in Window > Components. This is a definition, yes?
-
A model contains 'component definitions' [
model.definitions
].
These are stored in the model's data-base and may or may not have been be 'used'.
All of these show in the 'Component Browser' window's 'model' tab list.
If you use 'definitions.purge_unused
' then all component definitions that haven't been 'used' are removed from the data-base and the 'Component Browser' window's 'model' tab list.
***When a 'component definition' is 'used' [i.e. it is 'added' into the model] it becomes a 'component instance'.There are two ways of 'importing' a SKP as a new component - the first loads a component to the definitions_list -
model.definitions.load(skp_path)
.
You can then manually insert an instance of that definition into the model by picking it from the 'Component Browser' or use code, as inentities.add_instance(definition,transformation)
.
The second way ismodel.import(path.skp)
which imports the SKP as a new component and it arrives on your cursor with its insertion-point at the imported SKP's origin.
Of course after the definition is imported it is also 'loaded' so you can then use the.add_instance()
method on it later if multiple copies are needed.
You usenew_defn=model.definitions.add(skp_name)
- to make a new definition in the model's data-base [new_defn
] to which you can add new entities - [.add_...(...)
methods] e.g.new_defn.entities.add_cpoint(pt)
etc.To remove a single definition from the model [
purge_unused
removes all unused and you probably don't want that] you need to erase all of the definitions entities so that it is empty, this needs to be done inside amodel.start_operation("...")...model.commit_operation
code-block - then all instances will vanish and the entry will be removed from the 'Component Browser' window list too [Note: there are several ways to erase 'all entities' - the best is probablydefinition.entities.erase_entities(definition.entities.to_a)
].
To remove an instance and leave the definition alone useinstance.erase!
.
To change the contents of a definition [that will affect all instances] usedefinition.entities.add_...(...)
methods or [ruby:1elixfc4]entity.erase![/ruby:1elixfc4] to remove a specific thing.
If you want to change the contents of only one instance and leave the others unchanged first make the instance unique and then change its definition:
[ruby:1elixfc4]instance.make_unique
definition=instance.definition[/ruby:1elixfc4]
So an instance has a definition [instance.definition], and a definition has instances [definition.instances] - these are useful hooks... For example - to test if a definition is in use [ruby:1elixfc4]definition.instances[0][/ruby:1elixfc4] will return true [at least one] or false if none. -
Thanks, now to find the time to actually put this into practice.
-
Beware the
model.definitions
also include definitions forGroup
s andImage
s, not justComponentInstance
s. There are.group?
and.image?
test methods forComponentDefiniton
one can use to filter out the various types. -
Thanks for answering.
So I trayed that :
# First we pull in the standard API hooks. require 'sketchup' # Add a menu item to launch that one. UI.menu("PlugIns").add_item("My Models") { # Ruby Console after click Sketchup.send_action "showRubyPanel;" # Method calling. load_MyModels } def load_MyModels model = Sketchup.active_model show_summary = true status = model.import "Components/MyComponent.skp", show_summary end
But Sketchup return me that it could not find the file I've placed in the component folder ?
-
Specify a full path.
-
status = model.import("Components/MyComponent.skp", show_summary)
won't work because the path is bad.
You need to get the path to the file thus...
path=Sketchup.find_support_file("MyComponent.skp", "Components/")
it will==nil
IF it's not there...
then use
status = model.import(path, show_summary)
-
And it work !
Thank y'all for your help...
Advertisement