# Force Dynamic Components to Redraw

**URL:** <https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387>\
**Category:** Ruby API\
**Created:** [July 22, 2019, 6:36am UTC](https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387 "2019-07-22T06:36:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milmandre](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/milmandre/32/76145_2.png) [@milmandre](https://forums.sketchup.com/u/milmandre)\
**Post date:** [July 22, 2019, 6:36am UTC](https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387/1 "2019-07-22T06:36:36Z")

</div>

Hi all,

I’m actually writing a wall tool that wrap a dynamic component placement, orientation and scaling. Everything works fine except that I’m unable to force the dynamic component to be refreshed/redrawn correctly

I found @DanRathbun post regarding the way to force a dynamic component to be redrawn.

> [@Method to ask a dynamic component instance to recalc itself?](https://forums.sketchup.com/t/method-to-ask-a-dynamic-component-instance-to-recalc-itself/13905/2):
>
> Yes, provided that the global reference is always there. (It shouldn’t really be a global and could change in the future to a class method that returns a reference to the singleton instance.) def recalc\_dc(inst) return nil unless defined?($dc\_observers) if inst.is\_a?(Sketchup::ComponentInstance) && inst.attribute\_dictionary('dynamic\_attributes') $dc\_observers.get\_latest\_class.redraw\_with\_undo(inst) end nil # (there is no particular return value) end

That works perfectly for a single usage but doesn’t for subsequent use. When I add an another instance of my dynamic component that use the ScaleTool behaviour with my drawing tool I get the dynamic component instance redrawn using both previous and current scale factors.

[https://forums.sketchup.com/uploads/short-url/bMmj3r96hhvrS4F7DXgCSehc70i.mp4](https://forums.sketchup.com/uploads/short-url/bMmj3r96hhvrS4F7DXgCSehc70i.mp4)

Any idea of what append cause I’m unable to understand that code snippet and there’s no documentation regarding the dynamic components  
:  
`$dc_observers.get_latest_class.redraw_with_undo(inst)`

Thanks all for your help as usual 😉

---

<div class="post-metadata">

**Author:** ![milmandre](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/milmandre/32/76145_2.png) [@milmandre](https://forums.sketchup.com/u/milmandre)\
**Post date:** [July 22, 2019, 8:59am UTC](https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387/3 "2019-07-22T08:59:52Z")

</div>

Ok my mistake, I didn’t well understood how the dynamic component worked. I was using them like standard component, ie :

1. First iteration

- Sourcing the component definitions in the model
- Inserting a instance with its transform (with scale implying the definition to be updated).
- Force a redraw.

1. Second iteration

- The component was already sourced into the model so I was directly inserting an instance into my model with its transform with again a scale factor applied on a modified definition and leads to a new definition generated.
- Force a redraw update to something unwanted.

Manual exemple below :

[https://forums.sketchup.com/uploads/short-url/b0RFTppYOV3cAqiWL4n1cFdaplN.mp4](https://forums.sketchup.com/uploads/short-url/b0RFTppYOV3cAqiWL4n1cFdaplN.mp4)

---

<div class="post-metadata">

**Author:** ![pcmoor](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/pcmoor/32/3427_2.png) [@pcmoor](https://forums.sketchup.com/u/pcmoor)\
**Post date:** [July 22, 2019, 9:05am UTC](https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387/4 "2019-07-22T09:05:41Z")

</div>

Have a look at [http://sdmitch.blogspot.com/](http://sdmitch.blogspot.com/) and the scale\_align.rb

 ![Capture](https://global.discourse-cdn.com/sketchup/original/3X/8/6/86547f376ae550e8aee0df96d180755f7f0a43ce.jpeg)

 ![Capture%202](https://global.discourse-cdn.com/sketchup/original/3X/4/9/493b3aaef643fb814400fa2dbda506aafdb204a0.jpeg)

could be you have not allowed for the scale between the original component and the new length

guess this not the problem, all sorted!

---

<div class="post-metadata">

**Author:** ![milmandre](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/milmandre/32/76145_2.png) [@milmandre](https://forums.sketchup.com/u/milmandre)\
**Post date:** [July 22, 2019, 9:14am UTC](https://forums.sketchup.com/t/force-dynamic-components-to-redraw/99387/5 "2019-07-22T09:14:07Z")

</div>

Thanks @pcmoor but the problem was that the second time I was computing a scale factor based on a definition that have been modified at the first iteration (as I didn’t noticed that scaling a dynamic component leads to updating the definition), so I wasn’t computing the right scale factor.

I’m now reinserting my base dynamic component at each use unless the requested length already have been drawn (then reuse the existing one)
