Entities count is different in C API and Ruby API

I am trying to retrieve the number of component definitions from a given SketchUp model. The values I am getting are not same in SketchUp C SDK and the Ruby API.

model = Sketchup.active_model
definitions = model.definitions
definitions.count

In the above Ruby code definitions.count is giving a value “101”.

SUResult res = SUModelCreateFromFile(&model, skp_file_path);
if (res != SU_ERROR_NONE) {
    std::cout << "Unable to open SketchUp file!" << std::endl;
    return -1;
}		
//Component Definitions
size_t componentDefinitionsCount = 0;
size_t retrived = 0;
SUModelGetNumComponentDefinitions(model,&componentDefinitionsCount);
SUComponentDefinitionRef *definitions = new SUComponentDefinitionRef[componentDefinitionsCount];
SUModelGetComponentDefinitions(model, componentDefinitionsCount,definitions,&retrived);
std::cout << "Number of component definitions: " << retrived<< std::endl;

Above C code prints the count as “23”.

I am seeing the same issue when counting the total number of entities in both Ruby and C.

Am I doing something fundamentally wrong ?

The Ruby gives direct access to the definition list in the model. However, under the hood Group and Image entities also have definitions - they are technically a type of component even though the UI somewhat hides this.

The C API, being newer, tries to mimic more what the UI concept is. And therefore SUModelGetNumComponentDefinitions return the number of definitions excluding groups and images.

In SU2016 we also added SUModelGetNumGroupDefinitions as we saw use cases for this as well.

For more details on how definitions work in the Ruby API: Definitions and Instances in SketchUp | Procrastinators Revolt!

1 Like

…so to filter the count using the newer Ruby 2.x block form of the count method:

model = Sketchup.active_model
definitions = model.definitions
num = definitions.count {|d| !d.group? && !d.image? }

In older Ruby 1.8.x (SketchUp prior to v14) use:

model = Sketchup.active_model
definitions = model.definitions
num = definitions.select {|d| !d.group? && !d.image? }.size