How to import SKP to a 3dpoint? Why is it stuck on my mouse?

Hello All,

Sorry for the daft questions!

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

Thanks Ed

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.

:confused:

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!

Thanks for the input!

We already have a preferred way to do this:

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:

mod.definitions.purge_unused()

see: DefinitionList#load
and: Entities#add_instance

3 Likes

I stand edified! Didn’t think of going at it that way.

Is there an equivalent way to do this for .3ds or .dae files (i.e., non-skp files)? Seems the

cdef = mod.definitions.load(skp)

line expects a skp file.

I was looking for the same over the weekend, but came to the conclusion that no it is not possible without some external conversion to .skp

Technically to could script a new instance of sketchup to import a dae and save it as a skp then import that into the original sketchup instance.

It can be done, on MS Windows by sending an ESC key to SketchUp to cancel the placing of the imported 3DS, etc.

Then get the last definition in the collection, and add an instance as above.

See my example:
[Example] Import a 3DS using a specific transform (Cross-Platform)

FYI, I was able to make the example cross-platform, with the help of the Apple Developer website and John Boundy.