Observers that make changes to elements

Observers would seem to be an ideal method for doing something like this trivial example:

  1. Whenever user adds an instance of a particular component which is glued to a face
  2. Drop a perpendicular from its local XY center to the nearest edge of that face.
  3. Move the instance center to that point and rotate it about its local z axis so that it’s X axis aligns with the edge.
  4. Go through the same process whenever the user moves that instance.

Using observers I would need a

  1. EntitiesObserver.onElementAdded which would do 1,2, and 3
    AND add a EntityObserver.onChangeEntity for the instance.
  2. The EntityObserver.onChangeEntity would do the same steps 1,2 and 3.

Why this kind of process would crash SketchUp is not clearly explained in any of the posts I’ve read. not even sketchup-safe-observer-events/README.md at main · SketchUp/sketchup-safe-observer-events · GitHub

One obvious problem is that EntityObserver.onChangeEntity would trigger itself causing an infinite loop. So when EntityObserver.onChangeEntity is triggered I could first remove itself (Entity.remove_observer), then do the transforms, then reinstate itself.

Any advice on how best to do this? Or is it doable at all?
Do I need to EntitiesObserver.onElementRemoved to remove a deleted entity’s observer(s) or does SketchUp clean up on its own.

If observers can’t do this then I’ll need user to go through a 2 step process

  1. Add or change the instance.
  2. Click on a custom tool to do the transform.
    Not ideal.

Try using instead a DefinitionObserver subclass’ [onComponentInstanceAdded()] (http://www.sketchup.com/intl/en/developer/docs/ourdoc/definitionobserver#onComponentInstanceAdded) callback to do the initial 1, 2, 3 tasks. This obsever would be attached using a DefinitionsObserver’s onComponentAdded() callback.

For the later move, you might set a module flag in the EntityObserver, and use a tools observer to detect that the move of the instance is complete. Then start a UI.start_timer to let all the observer callbacks return before calling the “task123()” method. ?

Just ideas.