Open the download(dwg/3d) file directly in SketchUP from the HtmlDialog extension

Hi Developers.

I am creating an extension in which I am passing my website having components in HtmlDialog. In the HtmlDialog after few clicks, the component( DWG/3G) file gets downloaded. I want to handle this download file and open it directly in the SketchUp model. I know similar kinds of extensions have been developed in past. Could you please help me with a sample script to achieve this scenario? I am new to Ruby and SketchUp. How can I use action_callback to open the file in SketchUp.

We ask that you at the very least, make an attempt yourself at solving your challenges.

If your component files are SKP models, then your extension can download directly into the model’s component collection using the Sketchup::DefinitionList#load_from_url() method.

Otherwise you will need to use Sketchup::Http::Request to get the component file as the response body. If there were no errors, then you use Ruby core IO::write (or IO::binwrite) to save the response body as a file to the user’s local filesystem.

To get the path for saving SKP files use:

comp_folder = Sketchup.find_support_file("Components")

All SKP component files should be in a subfolder of the above folder so that they can be browsed using SketchUp’s Component Inspector panel.

save_path = File.join(comp_path, "AddySingh")
Dir.mkdir(save_path) if !File.exist?(save_path)
save_file = File.join(save_path, "some_file.skp")
IO.binwrite(save_file, response.body)


Non-SKP files should likely be saved in the TEMP folder and then deleted later.

save_path = Sketchup.temp_dir


Once downloaded you can check that it exists, then use DefinitionList.import if it is not a SKP file, or DefinitionList.load if it is a SKP file. …

if File.exist?(save_file)
  # load the component into the model's definitions collection:
  if File.extname(save_file) !~ /skp/i
    comp_def = model.definitions.import(save_file)
    comp_def = model.definitions.load(save_file)

Once in the model’s definitions collection either the user can use it manually from the Component inspector panel (ie, it will appear in the “In Model” collection via the house button) or:

(a) you can insert it at a particular 3D point using Entities.add_instance

# Transform can be a point array or a transformation object:
model.active_entities.add_instance(comp_def, transform)


or (b) you can have the user place it using Model.place_component



Take some some time to learn the basics …

It is advisable to know how the application works before extending it.