# Error: #\<TypeError: wrong argument type (expected Sketchup::ComponentDefinition)\>

**URL:** <https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807>\
**Category:** Ruby API\
**Created:** [March 24, 2023, 11:13pm UTC](https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807 "2023-03-24T23:13:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iAmIvyMai](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/iamivymai/32/175719_2.png) [@iAmIvyMai](https://forums.sketchup.com/u/iAmIvyMai)\
**Post date:** [March 24, 2023, 11:13pm UTC](https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807/1 "2023-03-24T23:13:30Z")

</div>

Dear all,

i am a newbIE to sketchUP !  
heRE i got this problem when i employ the ruby console in order to:

load the box.rb file which stored a very simple rectangular block into my opening sketchup file in sketchup 2022

my code is as the follows:

```auto
**SKETCHUP_CONSOLE.clear**

**# Path to the .rb file that contains the model**
 **file_path = "C:/Users/hk/Desktop/box.rb"**

**# Load the .rb file and execute it**
 **load file_path**

**# Get the loaded model from the global variable**
 **model = $loaded_model**

**# Add the loaded model to the SketchUp active model**
**Sketchup.active_model.entities.add_instance(model, Geom::Transformation.new)**

```

and my box.rb content is as the follows:

```auto
**model = Sketchup.active_model**
 **entities = model.entities**
 **definitions = model.definitions**

**definition = definitions.add("box")**
 **group = entities.add_group**
 **group.name = "box"**

**group.entities.add_edges [2151.mm, 1709.mm, 1551.mm], [2151.mm, 0, 1551.mm]**
**group.entities.add_edges [0, 0, 0], [0, 0, 1551.mm]**
**group.entities.add_face [2151.mm, 1709.mm, 0], [0, 1709.mm, 0], [0, 1709.mm, 1551.mm], [2151.mm, 1709.mm, 1551.mm]**
**group.entities.add_edges [0.mm, 1709.mm, 0.mm], [2151.mm, 1709.mm, 0.mm]**
**group.entities.add_edges [0.mm, 1709.mm, 0.mm], [0.mm, 1709.mm, 1551.mm]**
**group.entities.add_edges [0.mm, 1709.mm, 1551.mm], [2151.mm, 1709.mm, 1551.mm]**
**group.entities.add_edges [2151.mm, 1709.mm, 0.mm], [2151.mm, 1709.mm, 1551.mm]**
**group.entities.add_face [2151.mm, 0.mm, 0.mm], [2151.mm, 1709.mm, 0.mm], [2151.mm, 1709.mm, 1551.mm], [2151.mm, 0.mm, 1551.mm]**
**group.entities.add_edges [2151.mm, 1709.mm, 0.mm], [2151.mm, 0.mm, 0.mm]**
**group.entities.add_edges [2151.mm, 0.mm, 0.mm], [2151.mm, 0.mm, 1551.mm]**
**group.entities.add_face [0.mm, 0.mm, 0.mm], [2151.mm, 0.mm, 0.mm], [2151.mm, 0.mm, 1551.mm], [0.mm, 0.mm, 1551.mm]**
**group.entities.add_edges [2151.mm, 0.mm, 0.mm], [0.mm, 0.mm, 0.mm]**
**group.entities.add_edges [2151.mm, 0.mm, 1551.mm], [0.mm, 0.mm, 1551.mm]**
**group.entities.add_edges [0.mm, 0.mm, 1551.mm], [0.mm, 1709.mm, 1551.mm]**
**group.entities.add_face [0.mm, 1709.mm, 0.mm], [0.mm, 0.mm, 0.mm], [0.mm, 0.mm, 1551.mm], [0.mm, 1709.mm, 1551.mm]**
**group.entities.add_edges [0.mm, 0.mm, 0.mm], [0.mm, 1709.mm, 0.mm]**
**group.entities.add_face [0.mm, 0.mm, 1551.mm], [2151.mm, 0.mm, 1551.mm], [2151.mm, 1709.mm, 1551.mm], [0.mm, 1709.mm, 1551.mm]**
**group.entities.add_face [2151.mm, 0.mm, 0.mm], [0.mm, 0.mm, 0.mm], [0.mm, 1709.mm, 0.mm], [2151.mm, 1709.mm, 0.mm]**

```

and i got the following output in my ruby console, but i can still load the box.rb into my opening .skp file and i saw my box created before

![Screenshot 2023-03-25 071200](https://global.discourse-cdn.com/sketchup/original/3X/8/4/84702bbce7895f43158989bd54b2601cfce9524f.png)

what should i do ? please help !!!

regards,  
ivYYvi

---

<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:** [March 24, 2023, 11:53pm UTC](https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807/2 "2023-03-24T23:53:25Z")

</div>

> [@iAmIvyMai](#):
>
> what should i do ?

The first thing is please learn to post code correctly in the forum …

- [[How to] Post correctly formatted and colorized code on the forum?](https://forums.sketchup.com/t/how-to-post-correctly-formatted-and-colorized-code-on-the-forum/46189)

Please edit your previous post and repost the code.

* * *

1. Do not use global variables even if you see other _naughty_ code doing it.

Your error message tells you that the 1st argument to [`entities.add_instance`](https://ruby.sketchup.com/Sketchup/Entities.html#add_instance-instance_method) needs to be a [`Sketchup::ComponentDefinition`](https://ruby.sketchup.com/Sketchup/ComponentDefinition.html) object.

So it wasn’t. What was it ? You would need to interrogate the `model` reference in the console …

```ruby
puts model.class

```

* * *

The first part of you code is not correct… you will want to add the group to the definition’s entities, not the model’s entities:

```ruby
model = Sketchup.active_model
definitions = model.definitions

definition = definitions.add("box")
entities = definition.entities

group = entities.add_group
gents = group.entitie
cpt = gents.add_cpoint(ORIGIN)
group.name = "box"

#
# add edges and faces, etc. to the group "gents" here
#

# Save the definition to storage:
file_path = "C:/Users/hk/Desktop/box.skp"
definition.save_as(file_path)

```

Then later on in another model:

```ruby
model = Sketchup.active_model
definitions = model.definitions

file_path = "C:/Users/hk/Desktop/box.skp"
definitions.load(file_path)
# Place it:
model.entities.add_instance(definition, ORIGIN)

```

---

<div class="post-metadata">

**Author:** ![ene\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ene_su/32/103091_2.png) [@ene\_su](https://forums.sketchup.com/u/ene_su)\
**Post date:** [March 27, 2023, 12:32pm UTC](https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807/3 "2023-03-27T12:32:23Z")

</div>

The problem seems to be caused at this line because the first parameter `model` is not a `ComponentDefinition`. `model` is defined from `$loaded_model` but this code doesn’t show how `$loaded_model` is defined.

> [@iAmIvyMai](#):
>
> ```auto
> Sketchup.active_model.entities.add_instance(model, Geom::Transformation.new)
> 
> ```

---

<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:** [March 27, 2023, 2:33pm UTC](https://forums.sketchup.com/t/error-typeerror-wrong-argument-type-expected-sketchup-componentdefinition/224807/4 "2023-03-27T14:33:58Z")

</div>

There’s an echo in here … in Here … In HERE … **IN HERE** …

> [@DanRathbun](#):
>
> 1. Do not use global variables even if you see other _naughty_ code doing it.
> 
> Your error message tells you that the 1st argument to [`entities.add_instance`](https://ruby.sketchup.com/Sketchup/Entities.html#add_instance-instance_method) needs to be a [`Sketchup::ComponentDefinition`](https://ruby.sketchup.com/Sketchup/ComponentDefinition.html) object.
> 
> So it wasn’t. What was it ? You would need to interrogate the `model` reference in the console …
> 
> ```auto
> puts model.class
> 
> ```
