Saving selection to file, and placing on the mouse pointer.
-
Is there any simple way of attaching a selection of entities, or group to the mouse pointer? The only method I have found that would do that is import model. So, my second question, is is there any simple way of making a skp of a selection, or group of entities? I looked at this too, but could only find a method to save the whole model. As a test, I even tried to delete everything except the selection, then saved it, but even with a different name, the system would not (recursive?? error) import it back into the current model.
-
You can save a component definition out to a file with this:
http://code.google.com/apis/sketchup/docs/ourdoc/componentdefinition.html#save_as
So you could take the selection, then turn it into a component, save that out, then explode the component (or don't, whichever). And I'm trying to rememeber, but it seems like when you add a component instance to the model, SU will stick it to the cursor automatically (sticking the "instertion point to the cursor).
So perhaps converting to components is the way to go in this case?
Chris
-
Yes, if you make a ComponentDefinition out of your entities, then you can use
model.place_component
.
http://code.google.com/apis/sketchup/docs/ourdoc/model.html#place_component -
Thanks, I'll give that a try tomorrow.
-
In order to get the entities in my_selection into my_definition, and make the file my_component.skp, is it necessary to do it for every entity as follows?
my_selection.each do |e| if e.is_a? Sketchup;;Entity face = [] if e.is_a? Sketchup;;Face face = e.vertices my_definition.entities.add_face face end if e.is_a? Sketchup;;ComponentInstance ci=e.definition ti=e.transformation my_definition.entities.add_instance(ci,ti) end #do if end for each entity type # end end success = my_definition.save_as "my_component"
Or, is there an easier way? How do I make a curve an arc, or circle?
-
For those who may be interested, here is the whole snippet. Problem is the selection attaches itself to the mouse pointer by the model's origin. Should have thought of that:-( Anyway, you may remove the "commented out" in the code to erase the selection from the model. Although you can recover by ^z, It needs a automatic undo, if you quit before locating the selection.
Another problem is that Su entities are faces and edges, so arcs and circles lose their geometric attributes. Although you can
add_...
them, they are notSketchup::Entities
. Actually still not sure how all that stuff works.model = Sketchup.active_model definitions = model.definitions entities = model.entities selection = model.selection definition = definitions.add "Selection" my_definition = definitions.add "My Selection" selection.each do |e| if e.is_a? Sketchup;;Entity if e.is_a? Sketchup;;Face my_definition.entities.add_face e.vertices end # if e.is_a? Sketchup;;Curve # result = my_definition.entities.add_curve e.vertices # end if e.is_a? Sketchup;;Edge my_definition.entities.add_edges e.vertices end if e.is_a? Sketchup;;ComponentInstance my_definition.entities.add_instance(e.definition,e.transformation) end if e.is_a? Sketchup;;Group group = my_definition.entities.add_group entities = e.explode entities.each do |ge| if ge.is_a? Sketchup;;Face my_definition.entities.add_face ge.vertices end if ge.is_a? Sketchup;;Edge my_definition.entities.add_edges ge.vertices end end end #e.erase! end end success = my_definition.save_as "mycomponent" show_summary = true status = model.import "mycomponent.skp", show_summary File.delete("mycomponent.skp")
-
Why not group the selected entities and convert it into a component?
-
OK, I'll give that a try.
-
How to attach a group to the mousepointer:
First, I think, you have to use the "Tool"-class, because in this class you have the onMouseMove-method.
(see also \plugins\examples\linetool.rb - it shows in detail how to use the tool-class)class My_tool def initialize # a good place to put variables. Notice the "@". It means you can see the variable # in the whole My_tool class. If no "@" it would only be visible inside the def. @mod = Sketchup.active_model @ent = @mod.entities @state = 0 @pts = [] @pts[0] = [0,0,0] @pts[1] = [10,0,0] @pts[2] = [10,20,0] @pts[3] = [0,20,0] end def activate # activates the tool end def onMouseMove(flags, x, y, view) ip = Sketchup;;InputPoint.new ip.pick view, x,y ip = ip.position if (@state == 0) # Create Wall_group @my_group = @ent.add_group my_face = @my_group.entities.add_face @pts my_face.pushpull -10 # ip is the input point from the mouse, converted to a 3d point point = Geom;;Point3d.new ip new_transform = Geom;;Transformation.new point @my_group.transformation = new_transform @state = 1 end if (@state == 1) # @my_group follow mousemove Sketchup.status_text = "@state = 1 ; Inputpoint; ", ip point = Geom;;Point3d.new ip new_transform = Geom;;Transformation.new (point) @my_group.transformation = new_transform end if (@state == 2) # onLMouseButton place @my_group in choosen position Sketchup.status_text = "@state = 2 ; Inputpoint; ", ip point = Geom;;Point3d.new ip new_transform = Geom;;Transformation.new (point) @my_group.transformation = new_transform @ip1 = ip UI.messagebox "that should do it, Honolulu ;) - Press space to exit" @state = 3 end end # onMouseMove def onLButtonDown (flags, x, y, view) if (@state == 1) @state = 2 end end # onLButtonDown end # class My_tool #------------------------------------ #------------------------------------ # Here you activate the defined tool in your program... mytool = My_tool.new Sketchup.active_model.select_tool mytool
Regards RVS
-
If you just want to place the a component you can simply SU's use native tool
model.place_component
-
RVS, thanks for help with the snippit. I have another use for it.
tt, finally got to it, shoot, that was too easy:
my_def = definitions.add "My Selection" my_inst = @master_group.to_component my_def.entities.add_instance(my_inst.definition, my_inst.transformation) my_inst.erase! repeat = false model.place_component my_def, repeat
Advertisement