SketchUpAPI DLL (Understanding Component Structure)

Hi again,

I have been playing with the C API and i think that i do not quite comprehend the structure, maybe because i am new in SketchUp. So I have the ‘Evergreen Pine Tree’ (EPT) component, which downloaded from ‘3D Warehouse’, in my skp file and I want to create a JSON structure from that file.

I can see that the EPT is a component composed of 12 components, and this 12 components at the same time are composed of other components and this are composed of other and this of other… and go on.
In the C API I am using the functions of SUComponentDefinition to get some information of the component.

Now, i was thinking that the function ‘SUModelGetComponentDefinitions’ retrieved to me the following structure:

(Just for illustration)

  • Component1 : {
    — SubComponent1 : {
    ------ SubSubComponent1 : {
    --------- SubSubSubComponent1 : {}
    --------- }
    ------ }
    — }

But the function retrive the following:

(Just for illustration)
-Component1 : {},
-SubComponent1 : {},
-SubSubComponent1 : {},
-SubSubSubComponent1 : {}

How can i know which sub component belong to main component?
How do i relate the components with their sub components?

Followed these questions because the main component have a transformation that affects to their sub components and so on. So I have to know which component belong to which component to apply the trasnfromation of its main component.

If i do not been clear, please tell me!
I appreciate your help!
Thanks!
Cheers!

First you need to understand manually drawing components and sub-components in the application.

Second, read and understand the Ruby API exposure of the underlying C++ structure.

Then you can understand the C wrappers.


I can tell you have not yet done so, because of your incorrect assumptions.

(1) Definitions:

Every model has a component definitions collection.
'SUModelGetComponentDefinitions()' in the C API, is the complement of
Sketchup::Model#definitions() in the Ruby API.

The difference is that Ruby is OOP, and returns a Sketchup::DefinitionList collection object, whereas C is not OOP, and would return an Array.
(An array is a flat list.)

(2) Instances and Entities:

Each definition has it’s own instances collection, containing the references to each of it’s component instances.

Component instances do not “own” entities. Only component definitions have an entities collection. This collection may contain references to component instances of other component definitions. (This is component nesting.)

(3) Transforms:

Component Definitions do not have transformations. Component Instances do.


Lastly, read the examples in the SDK package. The XML output example would likely be the best one for you.

Thank you Dan.

Yeah I understand this components creation and its difference between groups; and I created a couple of components examples. For this topic, I do not have problem with that.

By another way, I did not know this of the Ruby API. I will read and take a look to Ruby API Documentation.
From the scratch of my inmersion to Sketchup API I didn’t turn to look the Ruby API, but now i have to review it. That’s good to know it!

For the rest of three points (Thanks for great explanation) I will bear in mind.

Any way, I have been taking many ideas from skp2xml exporter to get the entities and textures from skp files, Just with the components is where I had some problems because I do not understand how can I relate components with their sub components, but I hope that I will can understand better this topics with the Ruby API Documentation.

Thank you alot for the time.
I will keep you informed.

1 Like

In the Ruby API, it might look something like this:

require "pp"

def compose(ents, data)
   ents.each {|ent|
      if ent.is_a?(Sketchup::Group)
         if data[ent].nil?
            data[ent] ||= {}
            data[ent]['name'] = ent.name
            data[ent]['id'] = ent.persistent_id
            data[ent]['transformation'] = ent.transformation
            compose(ent.entities, data[ent])
         end
      end
      if ent.is_a?(Sketchup::ComponentInstance)
         if data[ent].nil?
            data[ent] ||= {}
            data[ent]['name'] = ent.definition.name
            data[ent]['id'] = ent.persistent_id
            data[ent]['transformation'] = ent.transformation
            compose(ent.definition.entities, data[ent])
         end
      end
   }
end

data = {}
compose(Sketchup.active_model.entities, data)
pp data


1 Like

Or if you wanted to store the combined transformation directly:

require "pp"

def compose(ents, data, trans)
   ents.each {|ent|
      if ent.is_a?(Sketchup::Group)
         if data[ent].nil?
            data[ent] ||= {}
            data[ent]['name'] = ent.name
            data[ent]['id'] = ent.persistent_id
            data[ent]['transformation'] = trans * ent.transformation
            compose(ent.entities, data[ent], ent.transformation)
         end
      end
      if ent.is_a?(Sketchup::ComponentInstance)
         if data[ent].nil?
            data[ent] ||= {}
            data[ent]['name'] = ent.definition.name
            data[ent]['id'] = ent.persistent_id
            data[ent]['transformation'] = trans * ent.transformation
            compose(ent.definition.entities, data[ent], ent.transformation)
         end
      end
   }
end

data = {}
compose(Sketchup.active_model.entities, data, IDENTITY)
pp data

1 Like

Hi, is there a way no know if the component is visible in the model or not?

Not exacltly visible (or “in view to the user”,) as visible means “not hidden” (which is a property switch.)


You can easily get the number of used instances for a component definition:

See: Sketchup::ComponentDefinition.html#count_used_instances()

used_in_model = comp_inst.definition.count_used_instances()
1 Like

I have posted my attempt at listing a model’s entities here.

[1] sketchup-c-api-sandbox/iterate_entities at master · jimfoltz/sketchup-c-api-sandbox · GitHub

1 Like

It may depend on what you mean by “visible”, but there exists this function:

SUDrawingElementGetHidden(SUDrawingElementRef elem, bool *hide_flag); 	

Yeah, I did not explain myselft well, sorry!

When I get the components used on the model, the SUModelGetComponentDefinitions() retriebes all components on the model but I do not exactly know which of this components are ‘in the view to the user’, I do not sure if ‘visible’ is the correct word for this concept. [quote=“jim_foltz, post:9, topic:39401”]
It may depend on what you mean by “visible”, but there exists this function:

SUDrawingElementGetHidden(SUDrawingElementRef elem, bool *hide_flag);
[/quote]

This function is got to know if the element has been checked in the hidden checkbox, don’t you?

So, if there’s a way please let me know!

Thanks a both for guide me in this magnificent world of sketchup

I keep forgetting this thread is about the C API use,
so the equivalent C function to the above Ruby method is:


A SketchUp file, does NOT have a View object, therefore nothing can be viewed by the user, and this is why there is no view interface in the C API.

Besides, this view would be dependent upon the user’s display size and aspect ratio and how they have sized the model viewport in SketchUp. It is not easy to determine, even in the “live” Ruby API when SketchUp is running.
(It might be done in a “live” model inside SketchUp, by testing the corners of bounding boxes for screen coordinates.)

1 Like

Don’t worry about that, it’s okay!

Thanks a lot!