sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    Making a SketchUp tool - incorporating the mouse & keyboard

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    55 貼文 6 Posters 2.7k 瀏覽 6 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • thomthomT 離線
      thomthom
      最後由 編輯

      @adamb said:

      A good little learning exercise for the Tool protocol, is to see what messages SU tries to fire at it.

      Add this to your class to log whenever SU queries your Tool instance to check its capabilities. You'll be surprised how much querying goes on!

      
      > 	def respond_to?(msg)
      > 		result = super
      > 		puts "unhandled respond_to(#{msg})" unless result
      > 		result
      > 	end
      > 
      

      Undocumented feature?

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

      1 條回覆 最後回覆 回覆 引用 0
      • B 離線
        BTM
        最後由 編輯

        Other than finding out that my math is completely flawed (the larger the number, the smaller the pinch. I have to fix that...), I have also mannaged to make a script that will draw a line starting at [0, 0, 0,], and going to wherever you input into the control box. Now, how would I change it so what's input into the box is in meters?

        ` model = Sketchup.active_model
        entities = model.entities
        selection = model.selection

        prompts = ["X", "Y", "Z"]
        defaults = ["0", "0", "0"]
        list = ["", "", ""]
        results = UI.inputbox prompts, defaults, list, "Input X, Y, Z."
        pt1= Geom::Point3d.new results.to_i
        pt2= [0, 0, 0]
        edges = entities.add_edges pt1, pt2`

        1 條回覆 最後回覆 回覆 引用 0
        • Chris FullmerC 離線
          Chris Fullmer
          最後由 編輯

          SU will work in the default unit that the user has set. So you might be best not trying to force it into meters.

          And your defaults:

          defaults = ["0", "0", "0"]

          Since you have the zeros in quotes, it will treat the user input as a string. So you would need to convert each answer to a float. OR if you remove the quotes and use 0.0, 0,0, 0,0 then it will treat whatever the user inputs as a float, which is what you want to use (or an integer) to make a point3d object.

          Then what you posted works like a charm.

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

          1 條回覆 最後回覆 回覆 引用 0
          • B 離線
            BTM
            最後由 編輯

            ... Yeah, that would help; it still works as it is though, because I switch it using .to_i later on.

            ...Also, it seems to make a value of 1 to be 0.0254m, and my default is in meters. This value of 0.0254m also seems to be the value at which sketchyphysics joints like sliders use (if I type in 1 in the joint max, it's max is 0.0254m).

            1 條回覆 最後回覆 回覆 引用 0
            • Chris FullmerC 離線
              Chris Fullmer
              最後由 編輯

              It doesn't work for me the way its posted. I got an error teling me that it couldn't convert an array ( results) to an integer. For me I needed to convert each element of the array separately.

              Unit conversions are still important. Here's the trick. SU works in the users default unit. So if you are in meters and type 1 into the box, its already in meters. Or you could over-ride it and enter 1cm into the box. But now its a string. Convert it to a length with .to_l Here are som examples to run one at a time:

              distance = "2000cm".to_l.to_m
              distance = "50km".to_l.to_m
              distance = "2000'".to_l.to_m

              the .to_l converts whatever it is given into the default base unit. Then .to_m converts whatever it is given to meters. Does that make sense? Play with those and check out the entire Numeric Class:

              http://code.google.com/apis/sketchup/docs/ourdoc/numeric.html

              Hope that helps with converting,

              Chris

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

              1 條回覆 最後回覆 回覆 引用 0
              • Chris FullmerC 離線
                Chris Fullmer
                最後由 編輯

                Hmm, I see, I think that I do have something wrong about the base units. I suppose SU always works in inches and you have to always convert back to .to_l to get to the default base unit? I've clearly confused myself.

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

                1 條回覆 最後回覆 回覆 引用 0
                • B 離線
                  BTM
                  最後由 編輯

                  2 things;

                  first; really? It didn't work for you at all? 😕 Ah well, I'm mostly playing around, and with it written out like you said, it works too.

                  second; I don't think it's in the user's default length automatically, because as I said, for me it works as 0.0254m, or 1 inch. My default is set to meters.

                  UPDATE: I wrote that before You finnished writing your last post. Sorry 😆

                  1 條回覆 最後回覆 回覆 引用 0
                  • Chris FullmerC 離線
                    Chris Fullmer
                    最後由 編輯

                    Yup, if I run it in the webconsole just as you have it written, I get this error:

                    (eval):9:in ‘initialize’: undefined method ‘to_i’ for ["1", "1", "1"]:Array

                    It doesn't like converting the array to an integer. It wants each element of the array to be converted separately on my machine. SU7

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

                    1 條回覆 最後回覆 回覆 引用 0
                    • B 離線
                      BTM
                      最後由 編輯

                      ` model= Sketchup.active_model
                      @ent= model.entities

                      prompts = ["X", "Y", "Z"]
                      defaults = [ 0.0.to_l, 0.0.to_l, 0.0.to_l]
                      list = ["", "", ""]
                      results = UI.inputbox prompts, defaults, list, "Input X, Y, Z."
                      pt1= Geom::Point3d.new results
                      pt2= [ 0, 0, 0]
                      line = @ent.add_edges pt1, pt2`
                      😉

                      1 條回覆 最後回覆 回覆 引用 0
                      • B 離線
                        BTM
                        最後由 編輯

                        That's strange. .to_i worked fine on my mac. 😕

                        1 條回覆 最後回覆 回覆 引用 0
                        • Chris FullmerC 離線
                          Chris Fullmer
                          最後由 編輯

                          Hey!!!! Great job. I was really confused how on earth to get the inputbox to work in the default units! That did the trick though, setting it to 0.0.to_l. I really couldn't figure that out.

                          So there you have it, 0.0.to_l makes the inputbox work in the default units. Thanks for posting the solution before I went crazy!

                          Chris

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

                          1 條回覆 最後回覆 回覆 引用 0
                          • B 離線
                            BTM
                            最後由 編輯

                            @chris fullmer said:

                            you might need to provide a way to make and place construction points.

                            ` model = Sketchup.active_model
                            @ent = model.entities

                            prompts = ["X", "Y", "Z"]
                            defaults = [ 0.0.to_l, 0.0.to_l, 0.0.to_l]
                            list = ["", "", ""]
                            results = UI.inputbox prompts, defaults, list, "Input X, Y, Z."
                            pt1= Geom::Point3d.new results
                            @ent.add_cpoint pt1`
                            😄

                            1 條回覆 最後回覆 回覆 引用 0
                            • Chris FullmerC 離線
                              Chris Fullmer
                              最後由 編輯

                              Excellent 😄

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

                              1 條回覆 最後回覆 回覆 引用 0
                              • B 離線
                                BTM
                                最後由 編輯

                                ... Now I need to figure out where in the class to put that 😒 . I'm still not that fond of what defs do what exactly.

                                1 條回覆 最後回覆 回覆 引用 0
                                • Chris FullmerC 離線
                                  Chris Fullmer
                                  最後由 編輯

                                  I think you'll probably put it in its own method.

                                  If you want it to be the first thing run every time the user begins the script, then you can call that method at the bottom of the activate method.

                                  And then call other methods from the onReturn method probably (though onLbuttonUp is common too). So it works like this, if you want your script to advance when the user hits enter, then call all YOUR methods from the onReturn method. It only gets triggered when the user hits enter/return. So only call your method from within onReturn and it will only get run once they hit enter. Here's an example of how it could flow

                                  Your "add cpoint" method gets run from the activate method because that is run everytime the user starts the tool. So that will run your construction point right off the bat. Then the user should click on a vertex to specify that vertex as the center of operations. So you would have code in the onLbuttonUp method that yould trigger when they click the mouse. It should take the InputPoint and store it as some @centerPoint variable, assuming its a vertex they clicked on).

                                  Once the cpoint is set and the centerpoint is selected, you could pop up a UI.inputbox asking what radius to use. When they hit enter, then you run a method that does all the calculation and drawing/transforming.

                                  The way I described it there did not require the user to hit enter, so all that would be run from the onLButtonUp method then.

                                  Seriously a tool with the mouse, and with transformations. Nearly as hard as it gets, but do-able. Good luck!

                                  Chris

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

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • B 離線
                                    BTM
                                    最後由 編輯

                                    One problem i'm having is not knowing what to enter in the grayed in area. I thought that Pinchmover, the name of the class, would work, but it doesn't. What does?
                                    I put in a bunch of variables at the start, just incase I'll need them, so I won't forget what they are.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • Chris FullmerC 離線
                                      Chris Fullmer
                                      最後由 編輯

                                      Sketchup.active_model.select_tool Pinchmover.new

                                      And you will also need to move that to the bottom of the script. You can't instantiate the class until after it has been defined. Just put all your menu work at the bottom of the script.

                                      Chris

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

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • Chris FullmerC 離線
                                        Chris Fullmer
                                        最後由 編輯

                                        Just the name of the class doesn't work, because with "tools" you are making special classes that Sketchup watches. So you have to tell sketchup to start your class as a tool.

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

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • B 離線
                                          BTM
                                          最後由 編輯

                                          Okay 👍
                                          Man, I've got a LOT to learn 😆

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • B 離線
                                            BTM
                                            最後由 編輯

                                            ` require "sketchup.rb"

                                            class Pinchmover
                                            
                                            	def activate
                                            	@model = Sketchup.active_model
                                            	@ent = @model.entities
                                            	@view = @model.active_view
                                            	@slct = @model.selection
                                            	@slct.clear
                                            	end
                                            
                                            	def onLButtonUp(flags, x, y, view)
                                            	ip = view.inputpoint x,y
                                            	pt = ip.position
                                            	pt2 = Geom::Point3d.new pt
                                            	@cpoint = @ent.add_cpoint pt2
                                            	end
                                            
                                            end
                                            

                                            plugins_menu = UI.menu("Plugins")
                                            item = plugins_menu.add_item("Pinch Mover") {
                                            Sketchup.active_model.select_tool Pinchmover.new
                                            }`

                                            I like mouse interaction and inferencing better than a dumb little inputbox 💚
                                            ... Now to get it to make only ONE cont.point ...
                                            ... And THEN a way to select an endpoint for the tool to use as the centerpoint of the pinch 😒

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 3
                                            • 3 / 3
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement