How can I get width and depth of component instance in Api C++?

Hello guys,

I have instances and I need to know its LenX and LenY in c++ Api Sketchup.

:blush: I have no idea how to write code in C ++ but I would look for it e.g. here …
https://extensions.sketchup.com/developers/sketchup_c_api/sketchup/struct_s_u_dynamic_component_attribute_ref.html

Thank you @dezmo,
but I don’t know how to use it because the documentation doesn’t have examples .

Then, You should wait for someone else… sorry I have no experience with SDK.

1 Like

There are examples in the SDK “samples” folder.

Also search this category for “dynamic”:

https://forums.sketchup.com/search?q=dynamic%20%23developers%3Asketchup-sdk

1 Like

Thank you @DanRathbun for your answers.

My code is :

SUResult result = SU_ERROR_NONE;
SUAttributeDictionaryRef dictionary = SU_INVALID;
res = SUEntityGetAttributeDictionary(entity_ref, “dynamic_attributes”, &dictionary);
if (res != SU_ERROR_NULL_POINTER_OUTPUT) {

    SUTypedValueRef val_x = SU_INVALID;
    result = SUTypedValueCreate(&val_x);
    if (result == SU_ERROR_NONE) {
        SUAttributeDictionaryGetValue(dictionary, "lenx", &val_x);
        SUTypedValueType tvt;
        printf("val_x is : %s \n", val_x);     
        SUTypedValueGetType(val_x, &tvt);      
        if (tvt == SUTypedValueType_Float || tvt == SUTypedValueType_Double) {
           
            // Do something with the X value ...
        }
        SUTypedValueRelease(&val_x);
    }
}

And when i run, i get this result :
val_x is : :spades:

Please post code correctly for the forum. See:
[How to] Post correctly formatted and colorized code on the forum?

And is your code working ? I see a spades symbol …

Perhaps standard C printf() does not know how to convert a SUTypedValueRef to a String as you are using a %s replacement parameter ?

I think you need to put the printf() statements inside the type checking clauses of the if statements.
Something like this (although I’m not that good with C language):

        if(tvt == SUTypedValueType_Float || tvt == SUTypedValueType_Double) {
            double x;
            SUTypedValueGetFloat(val_x, &x);
            printf("val_x is : %f \n", x);
            // Do something with the X value ...
        } else if(tvt == SUTypedValueType_String)  {
            char x[100]; // make big enough for value
            SUTypedValueGetString(val_x, &x);
            printf("val_x is : %s \n", x);
            // Do something with the X value ...
        } else if(tvt == SUTypedValueType_Empty) {
            print("val_x is empty);
        } else {
            print("val_x is unknown typed value);
        }


You can also get String values by using SUComponentInstanceCreateDCInfo() to get the DC instance’s SUDynamicComponentInfoRef struct, from which you can get the attribute array via SUDynamicComponentInfoGetDCAttributes().

Then you can iterate the array of SUDynamicComponentAttributeRef structs and get the String value via SUDynamicComponentAttributeGetDisplayValue().

Lastly convert the String to a double using val_x = strtod(s,NULL).

Thank you @DanRathbun for your explanation, please can you help me again.

SUDynamicComponentInfoRef dc_info = SU_INVALID;
      SUComponentInstanceCreateDCInfo(instances1[i], &dc_info);
      SUResult re = SU_ERROR_NONE;
      size_t 	len = 100;
      size_t count;
      SUDynamicComponentAttributeRef attributes[] = SU_INVALID;
      re = SUDynamicComponentInfoGetDCAttributes(dc_info,
          len,
          attributes,
          &count
      );
      if (re == SU_ERROR_NONE) {

          for (size_t i = 0; i <  sizeof(attributes); i++)
          {
              SUStringRef display_value;
              SUStringCreate(&display_value);
              re = SUDynamicComponentAttributeGetDisplayValue(attributes[i],
                  &display_value
              );
              size_t name_length = 0;
              SUStringGetUTF8Length(display_value, &name_length);
              char* name_utf8 = new char[name_length + 1];
              SUStringGetUTF8(display_value, name_length + 1, name_utf8, &name_length);
              double d = atof(name_utf8);
              printf("val is : %f \n", d);
         

          }
      }

Now I get these values :
image


But I need these values :
image

It looks like they are the same same values.

Are you saying that the function only returns the 3 instance specific values ?

@DanRathbun Yes just 3, and not all components have these values, but it has LenX, LenY, LenZ

Hmmm … this is not at parity with the DC extension’s internal Ruby get_attribute method.

@tt_su, wouldn’t we expect (for the C API interface) that the definition’s dynamic attributes also be returned (accessible) as they are in the Ruby method? (Ie, the method first checks the instance for the named property, and if it’s not a group and is not in the instance’s dictionary, then the method checks the definition’s dictionary.)

It looks like there is no DC info getter function for SUComponentDefinitionRef. This further reinforces my belief that the instance’s SUComponentInstanceCreateDCInfo function should be doing all the “leg work” and populating an object that has all of the attributes that apply to the instance from both the instance and it’s defintion.

So @AmmarKayali it looks like, for a workaround, if the property you want is not in the instance’s dictionary (or it’s DC info array) then you’ll need to directly check it’s definition’s attribute dictionary.

1 Like

Ok, Thank you very much @DanRathbun.