Change tool onElementAdded
-
Hello,
Are there any known issues with changing the current tool from an EntitiesObserver in the onElementedAdded callback? As currently when I try to do this it just crashes SU, I know the EntitiesObserver has plenty of issues, but I was hoping this wasn't going to be one of them.
Basically I want to be able to change the current tool when a specific kind of component is added, checking if the component is the right one that I want is easy.
I tried changing the tool in a Thread but the sleep functionality doesn't seem to work as expected, as it doesn't wake up again until the onElementAdded function is called again which is bizarre in itself.
I also tried a onPlaceComponent ModelObserver which doesn't crash but it also doesn't do anything either.
This is what the code boils down to.
class MyObserver < Sketchup;;EntitiesObserver def onElementAdded(entities, entity) if (check_entity(entity)) Sketchup.active_model.select_tool $my_tool end end end Sketchup.active_model.entities.add_observer(MyObserver.new())
I tried searching on the forum for anything about changing the current tool from an observer and I didn't find anything.
If this is a known issue/limitation then it's not the end of the world.
Thanks
-
I've not noted any issues with that method: http://www.thomthom.net/software/sketchup/observers/#EntitiesObserver
( Though if you can come up with a reproducible case I'd like to know. )It could be that your
check_entity(entity)
is referring to invalid geometry - or data not ready or expired. Can you post a bare bone sample script that reproduce the issue? (Often one even find the cause of the issue when doing do.)Have you tried with the
DefinitionObserver
?
http://www.thomthom.net/software/sketchup/observers/#DefinitionObserver(Btw, I see you use a global for
$my_tool
- I recommend you refactor your code to not use globals and ensure everything is wrapped in it's own module (namespace). Remember that the SketchUp Ruby API environment is a shared environment. Further reading: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/ ) -
Thanks, I didn't notice the DefinitionObservers, however they still had the same problems.
This is the simplest example I have using both an EntitiesObserver and DefinitionObserver (with one commented out at the moment) and both result in a crash.The UI context menu at the bottom is just to show that the tool can be selected without crashing when done outside of an observer. Going by the fact that it crashes with even this, I'm going to assume that it's a bug and just live with it. The only other thing I could think of was some way of changing the tool after the observer had finished, but the threading wasn't playing nice.
module Tool_test class TestTool @@tool = TestTool.new def self.getTool() return @@tool end def activate() puts "Tool activated" end end class MyDefObserver < Sketchup;;DefinitionObserver def onComponentInstanceAdded(definition, instance) Sketchup.active_model.select_tool TestTool.getTool() end end class MyDefsObserver < Sketchup;;DefinitionsObserver def onComponentAdded(definitions, definition) definition.add_observer(MyDefObserver.new()) end end Sketchup.active_model.definitions.add_observer(MyDefsObserver.new) class MyEntitiesObserver < Sketchup;;EntitiesObserver def onElementAdded(entities, entity) Sketchup.active_model.select_tool TestTool.getTool() end end #Sketchup.active_model.entities.add_observer(MyEntitiesObserver.new) UI.add_context_menu_handler do |menu| sub_menu = menu.add_item('Test Tool') { Sketchup.active_model.select_tool TestTool.getTool() } end end
-
I saw the bug with
DefinitionObserver
- it crashed.I then tried with
EntitiesObserver
- though with this mod:<span class="syntaxdefault"><br /> def onElementAdded</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">)<br /> if </span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?( </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">puts </span><span class="syntaxstring">'onElementAdded'<br /> </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool TestTool</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">getTool</span><span class="syntaxkeyword">()<br /> </span><span class="syntaxdefault">end<br /> end<br /></span>
What I noticed was that when I did a normal paste of an instance the tool would be deactivated immediately.
If I did a Paste In Place the tool would activate. -
Yea it's still crashing for me, and it's strange that I can put a
puts 'after select_tool'
after theselect_tool
method call and see it output before Sketchup crashes.But this was only for in-house really, so we're just going to leave it for now. Thanks for you're help though thomthom!
-
What version of SketchUp did you use?
Note that the M3 fixed even more observer related crashes...
Advertisement