# How to export components of each definition file (for import Lumion as nodes)?

**URL:** <https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870>\
**Category:** Ruby API\
**Created:** [December 1, 2021, 6:49am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870 "2021-12-01T06:49:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phuongtv](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@phuongtv](https://forums.sketchup.com/u/phuongtv)\
**Post date:** [December 1, 2021, 6:49am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/1 "2021-12-01T06:49:30Z")

</div>

As title, I plan to go through a list of definition, get the components of each definition and save them to separate files.  
But I couldn’t find a way to open a new model, copy the entities and save that model.

My current solution is to select the instances of definitions, then invert the selection to delete other, save to new file and undo. But that will not work with large data, and contain too much unpurged data.

```auto
def_list = model.definitions["tree-1"]                
Sketchup.active_model.start_operation("Export to new file", true)

selection.add def_list.instances
selection.invert

selection.to_a.each { |entity|
  entity.erase!
}

```

So what is the correct way to do this?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [December 1, 2021, 7:31am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/2 "2021-12-01T07:31:54Z")

</div>

> [@phuongtv](#):
>
> get the components of each definition and save them to separate files.

[Class: Sketchup::ComponentDefinition # **save\_as** (file\_path) ⇒ Boolean— SketchUp Ruby API Documentation](https://ruby.sketchup.com/Sketchup/ComponentDefinition.html#save_as-instance_method)

---

<div class="post-metadata">

**Author:** ![phuongtv](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@phuongtv](https://forums.sketchup.com/u/phuongtv)\
**Post date:** [December 1, 2021, 8:06am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/3 "2021-12-01T08:06:12Z")

</div>

I tried that, but it only save the component definition in co-ordinate 0,0,0. Is there anything I’m missing?

What I need is to save all the components with their location, all the instances of that component definition.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [December 1, 2021, 9:14am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/4 "2021-12-01T09:14:21Z")

</div>

I see…

💡Something like this came to my mind

```ruby
def export_instances(definition)
  model = Sketchup.active_model
  entities = model.active_entities
  instances = definition.instances

  #note : All instnaces below must have a common parent
  group = entities.add_group(instances)

  def_to_export = group.definition
  success = def_to_export.save_as( "c:/export.skp" )
  #clean up:
  group.explode
end

export_instances( Sketchup.active_model.definitions["tree-1"] )

```

---

<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:** [December 1, 2021, 12:29pm UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/5 "2021-12-01T12:29:40Z")

</div>

This coding problem usually requires that the model hierarchy be walked.

> [@Ruby scene serialization](https://forums.sketchup.com/t/ruby-scene-serialization/49377/2):
>
> What tells you that there is something wrong? At the moment this code has little to do with serialization but rather with tree traversal. Do you have a specific question about traversing the SketchUp scene graph? What is your use case, do you want to export all types of entities or just specific ones (here you only consider groups)? So far I can only give some remarks: Put triple backticks ``` on the empty lines before and after your code in this forum to make the forum format it properly…

> [@Retrieve position of face vertex in global frame of reference](https://forums.sketchup.com/t/retrieve-position-of-face-vertex-in-global-frame-of-reference/41674/2):
>
> Positions obtained from vertices are always locale to their parent - not global. In order to work out the global position you need the combined transformation for all the parent instances. How you do that depend on context of how your extension work. For instance, if your extension works on the active\_entities at all time, then you can obtain the transformation via model.edit\_transform. But if you are traversing the whole model, say if you do a model export, then you need to keep track of the…

---

<div class="post-metadata">

**Author:** ![phuongtv](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@phuongtv](https://forums.sketchup.com/u/phuongtv)\
**Post date:** [December 2, 2021, 2:14am UTC](https://forums.sketchup.com/t/how-to-export-components-of-each-definition-file-for-import-lumion-as-nodes/182870/6 "2021-12-02T02:14:33Z")

</div>

Thank you all.

It’s work perfect.
