Dear all,
i searched but can't find a good solution to my problem - therefore my first post here. Maybe someone can point me in the right direction (if it can be done at all)?
Consider the following szenario:
In the component browser i have a couple of components for which i'd like to implement a custom component movement/placement tool.
E.g. when dragging these components from the browser for placing them in the model i'd like to check clearance when moving the components around and prevent placement if the component would end up too close to some obstacle.
My first thought was to
- tag these components by adding an attribute
and
- install a ToolsObserver watching for the ComponentTool in its onActiveToolChanged callback (i.e. detecting the native tool when the user drags a component from the browser to place it in the model).
Then, if the chosen component has the attribute set, i'd just call my custom placement tool (automatically canceling the native placement tool)
The ToolsObserver fires as expected whenever i click on a component inside the component browser window, but i can't find a way to get the component definition chosen.
I'd need this info to check for the tag and pass the definition to my own tool if appropriate.
For the materials browser there's Sketchup.active_model.materials.current to retrieve the current selection but there's nothing similar (i know of) for the components browser.
The ToolsObserver would have an additional benefit that extending it to do obstacle checking when moving tagged components already instantiated in the model should be fairly straightforward:
In the ToolsObserver additionally watch for the MoveTool and call the custom move/place tool with the definition of the currently selected component instance (if tagged)...
The only workaround alternative i can think of:
Instead of observing tools, i could monitor the model definitions using a DefinitionsObserver. It fires an onComponentAdded when the component is selected in the browser for the first time (i.e. when its definition is added to the model) and it reports the definition as needed.
But dragging the same component from the browser again doesn't add a new definition to the model and the observer doesn't fire ...
So - workaround for the workaround would be:
in the onComponentAdded callback function
- clone the chosen original component definition without the tag (to avoid endless loop)
- destroy the original definition so the original is added to the DefinitionList (and the observer fires) next time again
- and place the clone by my own tool.
Now - isn't that ugly (and a nightmare for the undostack)? You'd have to make sure these components never occur in original form at any time inside your model or otherwise the observer wouldn't work.
So i'd really prefer sticking to a ToolsObserver if possible.
Has anyone done something similar? Is there a "hack" or workaround to retrieve the selected component from the browser (Windows, script is for personal use, so what?) or does someone know a plugin/script i should take a look at?
Sorry for the long text and thanks in advance!
Michael S.