The function model.import works partially


#1

I wrote a ruby script with the following commands:

model = Sketchup.active_model
show_summary = true
status = model.import “emptywithwall”, show_summary


#2

The returned status is OK. But the model is not uploaded.
The file emptywithwall.skp exists in the same folder as the ruby script

Doe anyone knows how to solve it?
Thanks.


#3

How does SU know that if you don’t tell it?

needs a path relative to the script so you can use

 emptywithwall = File.join(File.dirname(__FILE__),'emptywithwall.skp' )
 status = model.import(emptywithwall, show_summary)

or you can hard code it for a test…
how are you loading your ruby file?
john


#4

[quote=“HSharronn, post:1, topic:10951”]```ruby
status = model.import “emptywithwall”, show_summary

[/quote]

>[url=http://www.sketchup.com/intl/en/developer/docs/ourdoc/model.php#import]SketchUp API documentation on `Model#import()`[/url], says:
>
>**Model.import**
>   
>
> - The import method is used to load a file [u]**by recognizing the file extension**[/u] and **_calling appropriate importer_**.

So, .. it is necessary to include the ".skp" or ".obj" or ".dwg", or whatever the model file extension is, so that SketchUp can call the proper sub-routine in it's import engine.

#5

As John says, you need to tell SketchUp Ruby where the file is.

You can never assume what the working directory will be when it comes time for YOUR file handling statement to execute. The starting working directory has varied over the versions, but recently from v2014+, SketchUp has set it to the user’s HOME directory.

Even so,… other scripts that ran before yours might have changed it, but not changed it back.

It is considered best etiquette, to return the working directory to what it was before your code ran.
This is actually rather easy with the block form of the Dir::chdir module method. Ex:

status = false
show_summary = true
my_script_dir = File::dirname(__FILE__)
Dir::chdir( my_script_dir ) {
  status  = model.import( "emptywithwall.skp", show_summary )
}

Using the block form, Dir::chdir will change it only whilst the block runs, and then change it back to whatever it was, (and you don’t even need to care what it was beforehand.)


#6

Off Topic:

In Ruby 2 we have the global method __dir__ for File.dirname(__FILE__)


#7

Thanks a lot for your answers.
However, I executed your code and it behaves the same. I do not get any error message, but the skp model is not uploaded.

I am sending again my code:
require 'sketchup.rb’
SKETCHUP_CONSOLE.show

Upload an empty room

model = Sketchup.active_model
status = false
show_summary = true
emptywithwall = File.join(File.dirname(FILE),‘emptywithwall.skp’ )
status = model.import(emptywithwall, show_summary)


It is executed from the ruby console by using the load command with the full path of the ruby script

Thanks again for your help


#8

to investigate what is going wrong you can add a ‘puts’ to any script…
at the top of you script add

puts "my load path is good"

do you see the message?
then after the emptywithwall line add

 puts emptywithwall

and use the ‘up arrow’ and ‘enter’ in RC to reload, which should return the full load path to the skp…
what do you get?


#9

Thanks a lot!!!

Thanks to your advice I found the problem.
I had 2 other versions of the ruby script in the “home” folder. So they were executed sequentially. That was the problem.

Thanks so much!!!


#10

While experimenting with Ruby, never save your scripts into the SketchUp plugins directory! That is a sure road to confusion because all of them will load every time SketchUp starts! It is much better to put them into a subdirectory and then load them manually using the Ruby Console.