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

Creating a Custom Line Tool

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 5 Posters 852 Views 5 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.
  • K Offline
    kwalkerman
    last edited by 4 Jun 2010, 13:05

    Hi,

    I just had an idea. I think it should be possible, but there is no official documentation:

    class Custom_Line_Tool < Sketchup::SketchTool

    def initialize
    super
    end

    def onLButtonUP(flags, x, y, view)

    do whatever special stuff you want...

    end

    end

    This would get us to a place where we can use the native sketchup tool stuff (inference locking, line tool image, use of crosshairs if that is what the user wanted, native use of the VCB...) We could then have the tool do custom things, and treat it as a custom tool. However, 'Sketchup::SketchTool' doesn't return anything useful. Any other ideas of things to try?

    We'd also need to know how native sketchup names the input point, and probably a few other things. I do have a custom line tool working, but the original is just more elegant.

    --
    Karen

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 4 Jun 2010, 13:51

      That would have been great - but unfortunately not possible. They are implemented in C and there is no documentation nor externally exposed.

      But the line tool isn't that complicated. It's mostly just the InputPoints and inference locking you need.

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

      1 Reply Last reply Reply Quote 0
      • K Offline
        kwalkerman
        last edited by 4 Jun 2010, 14:14

        Ah, too bad. Of course I want a custom move tool and rectangle tool as well. I do have a line tool working with inference locking and and the VCB activated.

        well, for future, I'll post my line tool:

        class Custom_Line

        def initialize
        @cursor = UI.create_cursor("Cursor.tiff",1,1)
        end # def initialize

        def activate
        $ip = Sketchup::InputPoint.new
        Sketchup.active_model.selection.clear
        @ip1 = nil
        end

        def onSetCursor
        UI.set_cursor(@cursor)
        end

        def onMouseMove(flags, x, y, view)
        $ip.pick(view, x, y)
        view.invalidate
        end

        def draw(view)
        $ip.draw(view)
        if(@pt1 == nil)
        view = view.draw_line $ip.position, $ip.position
        else
        Sketchup.vcb_label = @pt1.distance($ip.position).to_s
        view = view.draw_line @pt1, $ip.position #draws line from last point to current position
        view.invalidate
        end
        end

        def onKeyUp(key, repeat, flags, view)
        if(key==16)
        view.lock_inference
        end
        end

        def enableVCB?
        return true
        end

        def onUserText(text, view)
        begin
        if(@pt1 != nil)
        value = text.to_l # convert the value to a length
        point2 = [$ip.position]
        dist = @pt1.distance(point2)
        w2 = value/dist
        w1 = 1-w2
        @pt2 = Geom::Point3d.linear_combination w1, @pt1, w2, point2
        line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
        line[0].set_attribute "EPdict", "Tool", "Node/Branch"
        @pt1 = @pt2
        Sketchup.vcb_value = ''
        end
        rescue
        # Error parsing the text
        UI.beep
        value = nil
        end
        end

        def onKeyDown(key, repeat, flags, view)
        if(key==16&&@pt1!=nil)
        ip1 = Sketchup::InputPoint.new(@pt1)
        @pt2 = $ip.position
        ip2 = Sketchup::InputPoint.new(@pt2)
        view.lock_inference(ip1,ip2)
        end
        end

        def onLButtonUp(flags, x, y, view)
        if(@pt1 == nil)
        @pt1 = [$ip.position]
        else
        @pt2 = [$ip.position]
        line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
        @pt1 = @pt2
        Sketchup.active_model.active_view.invalidate
        end # if state
        end
        end

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 4 Jun 2010, 14:35

          Can you repost that in the [code] tag - preserver the formatting and much easier to read.
          I'll have a look when I get home.

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

          1 Reply Last reply Reply Quote 0
          • K Offline
            kwalkerman
            last edited by 4 Jun 2010, 14:50

            Cool. Here it is formatted. As of right now, the line is always added to Sketchup.model.entities. I will need to have it add to the active path... just haven't gotten there yet. Otherwise, it seems to work.

            --
            Karen

            class Custom_Line
            
            def initialize
             @cursor = UI.create_cursor("Cursor.tiff",1,1)
            end # def initialize
            
            def activate
             $ip = Sketchup;;InputPoint.new
             Sketchup.active_model.selection.clear
             @ip1 = nil
            end
            
            def onSetCursor
             UI.set_cursor(@cursor)
            end
            
            def onMouseMove(flags, x, y, view)
             $ip.pick(view, x, y)
             view.invalidate
            end
            
            def draw(view)
             $ip.draw(view)
             if(@pt1 == nil)
              view = view.draw_line $ip.position, $ip.position
             else 
              Sketchup.vcb_label = @pt1.distance($ip.position).to_s
              view = view.draw_line @pt1, $ip.position #draws line from last point to current position
              view.invalidate
             end
            end
            
            def onKeyUp(key, repeat, flags, view)
             if(key==16)
              view.lock_inference
             end
            end
            
            def enableVCB?
             return true
            end
            
            def onUserText(text, view)
             begin
              if(@pt1 != nil)
               value = text.to_l # convert the value to a length
               point2 = [$ip.position]
               dist = @pt1.distance(point2)
               w2 = value/dist
               w1 = 1-w2
               @pt2 = Geom;;Point3d.linear_combination w1, @pt1, w2, point2
               line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
               @pt1 = @pt2
               Sketchup.vcb_value = ''
              end
             rescue
              # Error parsing the text
              UI.beep
              value = nil
             end
            end
            
            def onKeyDown(key, repeat, flags, view)
             if(key==16&&@pt1!=nil)
              ip1 = Sketchup;;InputPoint.new(@pt1)
              @pt2 = $ip.position
              ip2 = Sketchup;;InputPoint.new(@pt2)
              view.lock_inference(ip1,ip2)
             end
            end
            
            def onLButtonUp(flags, x, y, view)
             if(@pt1 == nil)
              @pt1 = [$ip.position]
             else
              @pt2 = [$ip.position]
              line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
              @pt1 = @pt2
              Sketchup.active_model.active_view.invalidate
             end 
            end 
            end
            
            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 5 Jun 2010, 00:36

              Karen, what's different about your edition, from the Google supplied sample: Plugins/Examples/linetool.rb ??

              And Sketchup can use TIFFs as button images?? ..any advantage in doing that rather than PNG ?

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • K Offline
                kwalkerman
                last edited by 8 Jun 2010, 14:38

                Hmm... somehow my 'linetool.rb' file got put in the plugins folder and I didn't know that I had it... There are many differences (other than the obvious ones that theirs draws a construction line not an edge, and doesn't start a new line where the old one leaves off). They also work primarily with input points, where I convert earlier to point3d, which means that to do inferencing, I have to convert back.

                Also, in order to get the tool to write into the active group, $entities = Sketchup.active_model.acitve_entities (not Sketchup.active_model.entities) or (as in 'linetool.rb' view.model.entities).

                --
                Karen

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by 8 Jun 2010, 14:53

                  The built-in Line tool seems very simple until you try to recreate it. It has a lot more states (and ways to transition) than I realized at first glance.

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    Chris Fullmer
                    last edited by 8 Jun 2010, 15:16

                    I agree. I tried to use the linetool to learn how to make a tool, as the API suggests. I got so confused. It is a pretty complex tool, at least more complex than it should be if it is being pointed to as a good script for beginners to learn from.

                    Chris

                    Lately you've been tan, suspicious for the winter.
                    All my Plugins I've written

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

                    Advertisement