I’m trying to create a script to read a csv and import SKPs and place them by their origin to a given point.
When the skp is imported it is ‘stuck’ to the mouse pointer.
How do I import to a point?
here is my code
def ak_PandP
# Vars for model
mod = Sketchup.active_model
ents = mod.active_entities
pcbview = Sketchup.active_model.active_view
# generate an open file dialog and store location in 'dxfLocation'
csvLocation=UI.openpanel("Import Placement CSV", "", "CSV|*.csv||")
if csvLocation == nil
puts "cancelled at CSV in!"
else
#Generate user input box to get pcb thickness store in pcbInput array
#akPrompts store name for imputbox array
akPrompts = ["PCB Thickness"]
#akDefaults store default value array .mm to give dimension to the array element
akDefaults = ['1.6.mm'.to_l]
pcbInput = UI.inputbox(akPrompts,akDefaults,"PCB Thickness")
#Take array element and store in var 'pcbThickness'
if pcbInput == false
puts "cancelled at Thickness!"
else
pcbThickness = pcbInput.at(0)
IO.foreach(csvLocation){|block|
lineArray=block.split(",")
point = [10,10,0]#i will add point from csv later
puts lineArray
puts block
mod.import "C:/Users/me/Desktop/DemoPCB/SupportingDocs/" + lineArray.at(1)
importSkp=mod.selection
importSkp.move! point
UI.messagebox("pause")
}
pcbview.zoom_extents
puts "Done, you can now close the Ruby Console "
end
end
end
The API docs don’t make it clear, but Model#import and Model#place_component both activate interactive tools that require the user to click a point where the new component will be placed. If there’s a non-tortuous workaround, I’d also like to hear about it. Otherwise, this seems like a reasonable feature request: an option argument to these methods (or new methods) that specifies where to place the import.
I will request the feature and I will try and find a workaround.
In some cases I need to import 100s of models to known positions and rotations. I can looses days doing this by hand!
skp = UI.openpanel("Import Model","","SketchUp Model (SKP)|*.skp||"))
return if skp.nil? # user cancelled
mod = Sketchup::active_model
cdef = mod.definitions.load(skp)
return unless cdef # good practice
# could be "next unless cdef" inside a loop
point = Geom::Point3d::new( 10, 10, 0 ) # loaded from external file
cinst = mod.active_entities.add_instance(
cdef,
Geom::Transformation::new( point )
)
If no errors occur, the instance can be exploded:
cinst.explode if cinst && cinst.respond_to?(:explode)
Once done inserting components, if all are exploded, purge the definitions: