# An observer activated before opening an instance?

**URL:** <https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301>\
**Category:** Developers\
**Created:** [April 12, 2018, 8:58am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301 "2018-04-12T08:58:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkoL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/markol/32/47710_2.png) [@MarkoL](https://forums.sketchup.com/u/MarkoL)\
**Post date:** [April 12, 2018, 8:58am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/1 "2018-04-12T08:58:09Z")

</div>

When you want to open an instance that is not unique, if other instances with the same definitions are locked, the sketchup asks you to make other instances unique before this instance opens, etc. I have tried a lot of observers, but they all activate after the instance opens. Which observer should i use that is activated before opening an instance, whether the user clicks 2 times on it, or opens the instance with the edit option?

---

<div class="post-metadata">

**Author:** ![Box](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/box/32/235164_2.png) [@Box](https://forums.sketchup.com/u/Box)\
**Post date:** [April 12, 2018, 9:16am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/3 "2018-04-12T09:16:08Z")

</div>

@simoncbevans this is the developer forum, unless you are a familiar with such things it is best avoided.

---

<div class="post-metadata">

**Author:** ![MarkoL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/markol/32/47710_2.png) [@MarkoL](https://forums.sketchup.com/u/MarkoL)\
**Post date:** [April 12, 2018, 9:17am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/4 "2018-04-12T09:17:19Z")

</div>

I know where I am and I need the answer! Thank you!  
Sorry @simoncbevans! I thought you were addressing me 🙂

---

<div class="post-metadata">

**Author:** ![Box](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/box/32/235164_2.png) [@Box](https://forums.sketchup.com/u/Box)\
**Post date:** [April 12, 2018, 9:25am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/5 "2018-04-12T09:25:16Z")

</div>

Simon Bevans was, it was me that was addressing Simon.

---

<div class="post-metadata">

**Author:** ![simoncbevans](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/simoncbevans/32/15904_2.png) [@simoncbevans](https://forums.sketchup.com/u/simoncbevans)\
**Post date:** [April 12, 2018, 9:26am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/6 "2018-04-12T09:26:05Z")

</div>

I was but I had not clocked the forum. So I withdrew my comments after @Box pointed it out.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [April 12, 2018, 11:54am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/8 "2018-04-12T11:54:31Z")

</div>

If you have tried a lot of observers to no avail it appears there no such observer.

You can make a feature request for it in the the API issue tracker: [Issues · SketchUp/api-issue-tracker · GitHub](https://github.com/SketchUp/api-issue-tracker/issues)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 12, 2018, 3:59pm UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/9 "2018-04-12T15:59:47Z")

</div>

> [@MarkoL](#):
>
> Which observer should i use that is activated before _[action]_ …

**Generally speaking** , unless the callback method explicitly is named “`onPre...`” or “`onBefore...`”,  
the callbacks are called **after** the event fires.

The developers asked for several “before” callbacks but only those bracketing some save tasks (model and component) were implemented.

A few callbacks like [`#onShadowInfoChanged`](http://ruby.sketchup.com/Sketchup/ShadowInfoObserver.html#onShadowInfoChanged-instance_method) will tell what changed, but if you want to know what the previous value was, then you would need to cache previous values **yourself** in your observer. (The initial values when you instantiate your observer, and then current states as a last task in your callbacks after using the previous state for comparison, etc.)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 12, 2018, 4:15pm UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/10 "2018-04-12T16:15:55Z")

</div>

> [@MarkoL](#):
>
> Which observer should i use that is activated before opening an instance, whether the user clicks 2 times on it, or opens the instance with the edit option?

Okay, take note that before either of these two actions are taken by the user (double-click or right-click+Edit,) the model’s selection changes to a single entity which would be the instance. At this point your observer’s [`#onSelectionBulkChange`](http://ruby.sketchup.com/Sketchup/SelectionObserver.html#onSelectionBulkChange-instance_method) callback could cache some properties or data for the instance in preparation if it is opened for editing. Then within the [`#onOpen`](http://ruby.sketchup.com/Sketchup/InstanceObserver.html#onOpen-instance_method) callback you’d use this “pre-edit” state data.

---

<div class="post-metadata">

**Author:** ![MarkoL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/markol/32/47710_2.png) [@MarkoL](https://forums.sketchup.com/u/MarkoL)\
**Post date:** [April 12, 2018, 5:03pm UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/11 "2018-04-12T17:03:18Z")

</div>

Thank you! I should make the component unique. Is there then a way to make a component unique when it is in editing mode without disturbing everything? The point is, I would like to automate the extension as much as possible and make the user less noticeable, except where it is needed. If I do an operation, before the user starts his own, I can interrupt ‘redo’. It is difficult to explain. To undo, I want to collect all the information about what the user is doing, because I can not change the model on undo. The problem is when a user walks between redo, undo, redo… Therefore, so that I do not break the ‘redo’, I have to make the component unique once the user has started his own operation.  
Anyway, the best thing that can happen to me now is that there is a safe way to make the component unique in editing mode. Not just for this, but for the rest of my script. That would solve all my problems!

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 12, 2018, 6:54pm UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/12 "2018-04-12T18:54:54Z")

</div>

> [@MarkoL](#):
>
> Anyway, the best thing that can happen to me now is that there is a safe way to make the component unique in editing mode.

It seems to work in SU2016, but may not in SU2017+ as a result of changes to the API.

IF you can get it to work, you’ll need to adjust the transformation. Perhaps saving it before editmode begins, then after making unique, correct the insertion point of the new definition.

(Seems that using groups might be easier as this functionality is built-in.)

---

<div class="post-metadata">

**Author:** ![MarkoL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/markol/32/47710_2.png) [@MarkoL](https://forums.sketchup.com/u/MarkoL)\
**Post date:** [April 13, 2018, 8:57am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/13 "2018-04-13T08:57:22Z")

</div>

Thank you Dan! That’s great! I thought it would be a lot more complicated!  
But … for me, even is this difficult to understand.  
I can not make corrections. SU RUBY API has a lot of transformations and insertion points. But I can not find any application for this case.  
If the “obj” is component that the user intends to open.  
I take its transformations:

obj\_transf = obj.transformation

After the user opens the component:

obj.make\_unique

From this moment I managed everything except to make the correction of the insertion point. I constantly move the component, but I can not make corrections.  
I suppose this is a routine and a single-row code. If you can help me, I would be very grateful to you!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [July 14, 2018, 12:57am UTC](https://forums.sketchup.com/t/an-observer-activated-before-opening-an-instance/66301/14 "2018-07-14T00:57:23Z")

</div>

Closed on July 14, 2018, 12:57am UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
