An observer activated before opening an instance?

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?

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

1 Like

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

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

1 Like

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

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

1 Like

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 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.)

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 callback could cache some properties or data for the instance in preparation if it is opened for editing. Then within the #onOpen callback you’d use this “pre-edit” state data.

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!

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.)

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!

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