sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Change tool onElementAdded

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 118 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      astrellon
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

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

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • A Offline
          astrellon
          last edited by

          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
          
          
          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            I saw the bug with DefinitionObserver - it crashed.

            I then tried with EntitiesObserver - though with this mod:

            <span class="syntaxdefault"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;onElementAdded</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance&nbsp;</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">puts&nbsp;</span><span class="syntaxstring">'onElementAdded'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;TestTool</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">getTool</span><span class="syntaxkeyword">()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • A Offline
              astrellon
              last edited by

              Yea it's still crashing for me, and it's strange that I can put a puts 'after select_tool' after the select_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!

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                What version of SketchUp did you use?

                Note that the M3 fixed even more observer related crashes...

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement