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

**URL:** <https://forums.sketchup.com/t/open-the-download-dwg-3d-file-directly-in-sketchup-from-the-htmldialog-extension/177530>\
**Category:** Ruby API\
**Tags:** sketchup, components\
**Created:** [October 6, 2021, 2:44pm UTC](https://forums.sketchup.com/t/open-the-download-dwg-3d-file-directly-in-sketchup-from-the-htmldialog-extension/177530 "2021-10-06T14:44:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Addy](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@Addy](https://forums.sketchup.com/u/Addy)\
**Post date:** [October 6, 2021, 2:44pm UTC](https://forums.sketchup.com/t/open-the-download-dwg-3d-file-directly-in-sketchup-from-the-htmldialog-extension/177530/1 "2021-10-06T14:44:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 6, 2021, 10:07pm UTC](https://forums.sketchup.com/t/open-the-download-dwg-3d-file-directly-in-sketchup-from-the-htmldialog-extension/177530/2 "2021-10-06T22:07:22Z")

</div>

> [@Addy](#):
>
> Could you please help me with a sample script to achieve this scenario?

**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()`](https://ruby.sketchup.com/Sketchup/DefinitionList.html#load_from_url-instance_method) method.

Otherwise you will need to use [`Sketchup::Http::Request`](https://ruby.sketchup.com/Sketchup/Http/Request.html) to **get** the component file as the [response](https://ruby.sketchup.com/Sketchup/Http/Response.html) body. If there were no [errors](https://ruby.sketchup.com/Sketchup/Http/Request.html#status-instance_method), then you use Ruby core [`IO::write`](https://ruby-doc.org/core-2.7.2/IO.html#method-c-write) (or [`IO::binwrite`](https://ruby-doc.org/core-2.7.2/IO.html#method-c-binwrite)) to save the response body as a file to the user’s local filesystem.

To get the path for saving **SKP** files use:

```ruby
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.

```ruby
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)

```

REF:

- [`Sketchup.find_support_file()`](https://ruby.sketchup.com/Sketchup.html#find_support_file-class_method)
- [`File`](https://ruby-doc.org/core-2.7.2/File.html) `class`
- [`Dir`](https://ruby-doc.org/core-2.7.2/Dir.html) `class`

* * *

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

```ruby
save_path = Sketchup.temp_dir

```

REF:

- [`Sketchup::temp_dir()`](https://ruby.sketchup.com/Sketchup.html#temp_dir-class_method)

* * *

> [@Addy](#):
>
> I want to handle this download file and open it directly in the SketchUp model.

Once downloaded you can check that it exists, then use [`DefinitionList.import`](https://ruby.sketchup.com/Sketchup/DefinitionList.html#import-instance_method) if it is **not** a SKP file, or [`DefinitionList.load`](https://ruby.sketchup.com/Sketchup/DefinitionList.html#load-instance_method) if it is a SKP file. …

```ruby
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)
  else
    comp_def = model.definitions.load(save_file)
  end
end

```

* * *

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`](https://ruby.sketchup.com/Sketchup/Entities.html#add_instance-instance_method)

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

```

REF:

- [`Sketchup::Entities.add_instance`](https://ruby.sketchup.com/Sketchup/Entities.html#add_instance-instance_method)
- [`Array`](https://ruby.sketchup.com/Array.html)
- [`Geom::Transformation`](https://ruby.sketchup.com/Geom/Transformation.html)

… **or** ( **b** ) you can have the user place it using [`Model.place_component`](https://ruby.sketchup.com/Sketchup/Model.html#place_component-instance_method)

```ruby
model.place_component(comp_def)

```

REF:

- [`Sketchup::Model.place_component()`](https://ruby.sketchup.com/Sketchup/Model.html#place_component-instance_method)

* * *

> [@Addy](#):
>
> I am new to Ruby …

Take some some time to learn the basics …

> [@Ruby Learning Resources \[WikiLists\]](https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861):
>
> All seven of the lists below are set as wikis. card_index INDEX [notebook PRIMERS & GUIDES](http://forums.sketchup.com/t/ruby-learning-resources/22861/2)[books BOOKS](http://forums.sketchup.com/t/ruby-learning-resources/22861/3)[mortar_board TUTORIALS](http://forums.sketchup.com/t/ruby-learning-resources/22861/4)[card_box REFERENCE](http://forums.sketchup.com/t/ruby-learning-resources/22861/5)[clipboard TEMPLATES](http://forums.sketchup.com/t/ruby-learning-resources/22861/6)[newspaper NEWS](http://forums.sketchup.com/t/ruby-learning-resources/22861/7)[bulb IDEAS](http://forums.sketchup.com/t/ruby-learning-resources/22861/8)

…

> [@Addy](#):
>
> I am new to … SketchUp.

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

- [https://learn.sketchup.com](https://learn.sketchup.com)
