# Changing dynamic component parameters from importer doesn't always work

**URL:** <https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487>\
**Category:** SketchUp SDK\
**Tags:** dynamiccomponents, importer\
**Created:** [August 7, 2019, 1:14pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487 "2019-08-07T13:14:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 7, 2019, 1:14pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/1 "2019-08-07T13:14:46Z")

</div>

Hi,  
I’m attempting to write an importer for SketchUp, but I cannot change some parameters on some dynamic components, while I can change these on other dynamic components.

I’ve asked this question before in the following thread [Calling dynamic components redraw from C importer](https://forums.sketchup.com/t/changing-parameters-of-dynamic-components-problems/100401) but @tt_su asked me to start a new thread for each of the issues I described there.

The issue is the following:

> [@Calling dynamic components redraw from C importer](https://forums.sketchup.com/t/calling-dynamic-components-redraw-from-c-importer/100401/1):
>
> I can set the height of the following component: [https://3dwarehouse.sketchup.com/warehouse/v1.0/publiccontent/d68c504c-a4cc-4640-b5ea-5b3a6cff2a6e](https://3dwarehouse.sketchup.com/warehouse/v1.0/publiccontent/d68c504c-a4cc-4640-b5ea-5b3a6cff2a6e)  
> But I cannot set the height for this component: [https://3dwarehouse.sketchup.com/warehouse/v1.0/publiccontent/cff9cf2a-8a69-4bca-a9a5-157c69fc8f78](https://3dwarehouse.sketchup.com/warehouse/v1.0/publiccontent/cff9cf2a-8a69-4bca-a9a5-157c69fc8f78)

A small sample project (for windows) illustrating this problem can be downloaded here:  
[ExampleImporter.zip](https://forums.sketchup.com/uploads/short-url/7UxpYQFpwHcCseB7JtNEqQx8vES.zip) (19.5 KB)  
In order to build this project, a directory containing the SDK should be added in a binaries directory in the same directory where the ExampleImporter directory resides.

All of the relevant code is located in the common directory of the project. The importing process starts from the Convert function in exampleImporter.cpp.

A release build of this project can be downloaded here:  
[exampleImporter.zip](https://forums.sketchup.com/uploads/short-url/xJwvU0zCH5CEisaVXrDGJV4MObc.zip) (13.5 KB)  
This dll file should be placed in the Importers directory which is located in the installation directory of SketchUp.

In order to test the exporter any .txt file should be opened. This will trigger the importer, which will in turn download both components mentioned above and will try to change the lenz parameter of both. If successful the imported components will scale when redrawn from the dynamic components menu. You will notice that only the big frame will scale, but not the small cover.

Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 7, 2019, 3:20pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/2 "2019-08-07T15:20:36Z")

</div>

What part of the code is where it doesn’t behave as expected?

Also, have you checked the return values of the function calls to the SketchUp C API?

I often create a helper macro to assist in this for debugging:

```cpp
#define SU(api_function_call) {\
SUResult su_api_result = api_function_call;\
assert(SU_ERROR_NONE == su_api_result);\
}\

```

Then I use that to wrap function calls so I get asserts in debug builds when a function returns with an error:

```cpp
std::string GetString(const SUStringRef& string) {
  size_t length = 0;
  SU(SUStringGetUTF8Length(string, &length));
  std::vector<char> buffer(length + 1);
  size_t out_length = 0;
  SU(SUStringGetUTF8(string, length + 1, buffer.data(), &out_length));
  assert(out_length == length);
  return std::string(begin(buffer), end(buffer));
}

```

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 7, 2019, 3:23pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/3 "2019-08-07T15:23:15Z")

</div>

Side note, as I’m looking at the code getting familiar:

This:

```auto
    SUTypedValueRef typedValue;
    SUSetInvalid(typedValue);

```

can be simplified to:

```auto
    SUTypedValueRef typedValue = SU_INVALID;

```

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 8, 2019, 6:59am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/4 "2019-08-08T06:59:19Z")

</div>

Thank you for your reply!

> [@tt\_su](#):
>
> What part of the code is where it doesn’t behave as expected?

The misbehaving code is called in the Convert function of the CExampleImporter class. This class can be found in Common/exampleImporter. I’ve created a function that executes exactly the same code for both situations, which is situated in the same class: LoadDefinitionInstantiateAndSetParameter. This function downloads a model from an URL, loads it as a definition, instatiates the definition and sets the height parameter on this definition.

When running the code, you can see that everything works fine, except for setting the parameter on the small cover. I think that the problem lies in the SetDoubleAttribute function of the HelperFunctions class (Common/HelperFunctions).

> [@tt\_su](#):
>
> I often create a helper macro to assist in this for debugging:
> 
> ```auto
> #define SU(api_function_call) {\
> SUResult su_api_result = api_function_call;\
> assert(SU_ERROR_NONE == su_api_result);\
> }\
> 
> ```

I’ve tried adding the helper macro to all function calls in the SetDoubleAttribute function. The importer still completes successfully, so I can conclude that all of these function calls succeed.

I also noticed that I forgot to add the common/utils.h file from the root directory. This file contains the SU\_CALL macro which has the same functionality as the macro you’ve sent me. I’ve created a new zip containing the common/utils.h file and with the helper macro you provided to the SetDoubleAttribute function.  
[SketchupPlugin.zip](https://forums.sketchup.com/uploads/short-url/2TO2h7suBfghWqJHKWzcIO6BMz.zip) (17.6 KB)

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 9, 2019, 9:51am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/5 "2019-08-09T09:51:59Z")

</div>

> [@kevin.ae](#):
>
> SU\_CALL macro which has the same functionality as the macro you’ve sent me.

I think that `SU_CALL` throws an error. Which isn’t something I’d recommend in production as you’d crash the application. Instead it’s better to handle the error gracefully. The macro I used simply triggers an assert in debug build. (Also not ideal for production as it doesn’t handle any errors in any way - but useful for examples like this.)

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 9, 2019, 10:52am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/6 "2019-08-09T10:52:06Z")

</div>

Inspecting the model after running the importer I can confirm that the attribute “lenz” is being set:

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/c/c/cc253f0ac2fd3af95541a0ea6b049775220752b7.png)

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/7/8/78fad896b4c54d88d492706dad93201c8b7179d0.png)

You can verify that visually with the Attribute Helper:  
[https://extensions.sketchup.com/en/content/attribute-helper](https://extensions.sketchup.com/en/content/attribute-helper)

But I do see that it appear differently in the Dynamic Component dialog:

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/5/f/5f463fc31f8c83e48e9a0e452eb7e74ee241e799.png)

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/6/f/6f9bd917e729312e252303d9a19d5110d1e6bd13.png)

At this point I’m not sure what is going on. Setting DC attributes via code is poking into internal logic and I’m not familiar with exactly how DC works.

But I’m guessing it relates to some of the other attributes that is set on the definition.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 9, 2019, 10:55am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/7 "2019-08-09T10:55:06Z")

</div>

Actually, it appear that these extensions have fixed set of options for LenZ, and only one of them list `114.7"` as a valid value:

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/2/6/2620b9e8ddee78426f2343006c3a04ce4ca5fbd8.png)

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/6/5/6561515d78f41abc11875cc25787497b39365b31.png)

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 9, 2019, 1:33pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/8 "2019-08-09T13:33:12Z")

</div>

> [@tt\_su](#):
>
> You can verify that visually with the Attribute Helper:  
> [Extension | SketchUp Extension Warehouse](https://extensions.sketchup.com/en/content/attribute-helper)

This will be very helpful for debugging these issues, thanks for the tip!

> [@tt\_su](#):
>
> Actually, it appear that these extensions have fixed set of options for LenZ, and only one of them list `114.7"` as a valid value

Doesn’t the auto option automatically map other values to the expected values?

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 9, 2019, 1:35pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/9 "2019-08-09T13:35:05Z")

</div>

I’m still in the prototyping phase. When I’m moving up to production, I’ll handle errors and exceptions more gracefully.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 12, 2019, 10:54am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/10 "2019-08-12T10:54:30Z")

</div>

> [@kevin.ae](#):
>
> Doesn’t the auto option automatically map other values to the expected values?

I’m afraid that I don’t know much about the exact details of Dynamic Components.  
But DC is made with the intent of the components being edited by the user. When setting attribute data directly from a third party extension there might be a number of things that are not updated in the same manner as the UI would.

---

<div class="post-metadata">

**Author:** ![kengey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/kengey/32/234545_2.png) [@kengey](https://forums.sketchup.com/u/kengey)\
**Post date:** [August 12, 2019, 12:17pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/11 "2019-08-12T12:17:27Z")

</div>

I see you are writing an importer for aluvision components, with which I happen to have some experience.

Why not, instead of changing the lenz attribute, actually change the scale of the component in z direction? That is the workflow a user would follow when doing it manually.

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 12, 2019, 1:18pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/12 "2019-08-12T13:18:34Z")

</div>

Hi,

I will give this a shot, thanks for your feedback!

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 12, 2019, 1:45pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/13 "2019-08-12T13:45:49Z")

</div>

> [@kengey](#):
>
> Why not, instead of changing the lenz attribute, actually change the scale of the component in z direction? That is the workflow a user would follow when doing it manually.

That won’t work from an importer - as it’s being done on a temporary model and Dynamic Components won’t have any knowledge of its existence.

Since this appear to rely so heavily on DC it might be better to have a Ruby extension importer.

> **[Class: Sketchup::Importer](https://ruby.sketchup.com/Sketchup/Importer.html)**
>
> The Importer interface lets you build your own importers for SketchUp.

You can either make a [Ruby C extension](https://github.com/SketchUp/ruby-c-extension-examples) to build your importer in C/C++ - but use the Ruby `Importer` interface instead of the SDK interface.

Alternatively you can use [Fiddle](https://ruby-doc.org/stdlib-2.5.3/libdoc/fiddle/rdoc/Fiddle.html) to interact with dynamic libraries directly if you want to avoid Ruby C extensions. (Ruby C extensions will need a separate version per Ruby version you target, which differ from SU version to version.)

But if you go this route you can probably do some post-import cleanups in Ruby land which can trigger DC to do its thing.

However, I want to take a step back as ask what you are using Dynamic Components for? I’ve seem people try to do some really complication interactions with it when it turned out they didn’t really need to use DCs at all. I just want to make sure there isn’t a simpler and more stable solution at hand.

---

<div class="post-metadata">

**Author:** ![kengey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/kengey/32/234545_2.png) [@kengey](https://forums.sketchup.com/u/kengey)\
**Post date:** [August 12, 2019, 1:46pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/14 "2019-08-12T13:46:41Z")

</div>

I was hoping dc would do a redraw upon file load.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [August 12, 2019, 3:06pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/15 "2019-08-12T15:06:57Z")

</div>

I think it only will do this upon interacting directly with each instance.

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 13, 2019, 12:57pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/16 "2019-08-13T12:57:00Z")

</div>

I’ll give the Ruby Importer a try.

I’m building a tool that imports a configuration of Aluvision models and applies settings to them in order to integrate SketchUp with a custom external tool. I don’t see a way around using the DCs for solving this problem.

---

<div class="post-metadata">

**Author:** ![kengey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/kengey/32/234545_2.png) [@kengey](https://forums.sketchup.com/u/kengey)\
**Post date:** [August 13, 2019, 9:24pm UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/17 "2019-08-13T21:24:33Z")

</div>

Aluvision happens to have a sketchup extension. Perhaps this could be integrated in that extension?

---

<div class="post-metadata">

**Author:** ![kevin.ae](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@kevin.ae](https://forums.sketchup.com/u/kevin.ae)\
**Post date:** [August 14, 2019, 7:08am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/18 "2019-08-14T07:08:32Z")

</div>

I cannot use the Aluvision extension to aid me, because this only spawns a dynamic component with default parameters. Then the end user can change some parameters of this dynamic component. I’ve not found a way to spawn a DC with non default values using this extension, which is required for my extension.

Or are you asking that my custom importer will be integrated into the existing sketchup extension? If so, I will have to find the developer of that extension.

---

<div class="post-metadata">

**Author:** ![kengey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/kengey/32/234545_2.png) [@kengey](https://forums.sketchup.com/u/kengey)\
**Post date:** [August 14, 2019, 7:27am UTC](https://forums.sketchup.com/t/changing-dynamic-component-parameters-from-importer-doesnt-always-work/100487/19 "2019-08-14T07:27:31Z")

</div>

> [@kevin.ae](#):
>
> Or are you asking that my custom importer will be integrated into the existing sketchup extension? If so, I will have to find the developer of that extension.

Well, it does seem plausible that if you are creating a tool that is very Aluvision-specific, that it gets integrated in their extension.
