How can I attached a component to either a parent component or parent model?

In the sketchup_c_api, there is a function SUComponentDefinitionCreate(SUComponentDefinitionRef* comp_def), I can use it to create a component, but how can i attach it to a parent component?Which interface or function can i use?
The function is in SketchUpAPI/model/component_definition.h

In general (I am not a programmer or developer) one has to consider the concepts of ‚ÄėContext‚Äô and ‚ÄėNesting‚Äô in SketchUp.

Let’s say you start with an empty Model (=level 1)
If you add Component definition (eg. Table) , you add it to the model, the table is on level 2 (You could say that the model is the Parent)

If you open the Table definition and you create a ‚Äėleg‚Äô component definition, the leg is nested in the Table component (Table is the Parent of the Leg(s))

Model (Level 1)

  • Table (level 2)
    **Leg (Level 3)

etc.

so it depends on what you create, but more important, when.

You do not attach definitions to anything. They are members of the model’s definition collection.

You attach component instances to entities collections using the function:
SUEntitiesAddInstance( parent_entities, instance, instance_name )
In this function’s parameter list … "parent_entities" can reference either the model’s top level entities collection or a component (or group) definitions’s entities collection.

See this post for how to create a definition and add an instance of it to an entities collection referenced by the identifier "entities". (In the example this is referencing the model’s top level entities collection.)
SUEntitiesFill throw the exception: Access violation reading location 0x0000000000000000 - #4 by DanRathbun

CORRECTION : You actually need to add the definition into the model’s definitions collection before you try to an an instance of it into any entities collection (be it the model’s or another defintion’s entities collection.)

SUModelAddComponentDefinitions()

Thank you very much for helping me !!
I can use this method to attach an instances to an entities,but when I add a lot of instances into an entities they are juxtaposed. How can i make nested instance?
I mean that… such as attach an entities to another entities, or attach an instance to another instance as its kid.

You add an instance of one definition to another definition’s entities collection.
However, all of the second definition’s instances will now have the nested instance.

instances do not have entities collection. Only definitions do.

thank you very much!

Thank you for your reply

Oh there is another question HAHA.
Thank you for providing me a code example of creating component, which is very helpful.
Can you provide me with an example of creating group?

Please first check examples in the SDK package.
Try to create code that creates a group using the C API functions yourself.
Refer to the C API documentation for these functions.

If you still cannot figure it out, start a new topic thread as this one has been solved.

Thank you! I solved it!

1 Like