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

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:

**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:

**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

what should i do ? please help !!!

regards,
ivYYvi

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

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 needs to be a Sketchup::ComponentDefinition object.

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

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:

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:

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)
1 Like

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.

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