• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Code correction

Scheduled Pinned Locked Moved Newbie Forum
sketchup
7 Posts 3 Posters 465 Views 3 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
    kmk111890
    last edited by 11 Jun 2015, 02:57

    Hello, I'm practicing making code by myself and got stuck on error. I can connect arc with a line from 0,0,0. but can't add one more line that connects between 0,0,0 and the other side of arc. I wish one icecream cone shape will show up after executing codes.

    ents = Sketchup.active_model.entities pt0 = [0,0,0] pt1 = [0, 0, -5.cm] pt2=pt1.transform(Geom::Transformation.rotation(pt0,[0,0,1],30.degrees)) arc = ents.add_arc [0,0,0], [0,0,-1],[0,-1,0], 5.cm, 0, 30.degrees g=ents.add_group() ge=g.entities ge.add_cpoint(pt0) ge.add_cpoint(pt1) ge.add_cpoint(pt2) ents.add_line pt0, pt1 ents.add_line pt0, pt2 face = ents.add_face pt0, pt1, pt2 path = ents.add_circle [0,0,-5.cm], [0,0,1], -5.cm face.followme path ents.erase_entities path

    1 Reply Last reply Reply Quote 0
    • S Offline
      slbaumgartner
      last edited by 11 Jun 2015, 11:56

      In the future, please enclose your code in

      ...
      
      • make it easier for others to copy.

      You try to rotate pt1, which is on the blue axis, using a rotation also about the blue axis. This has no effect, since pt1 has no x or y value to rotate. Hence pt2 is the same as pt1 and this causes a duplicate points error when you try to add a face using them as two corners. Try using [0,-1,0] as the vector instead of [0,0,1].

      1 Reply Last reply Reply Quote 0
      • K Offline
        kmk111890
        last edited by 16 Jun 2015, 02:42

        Thanks you, slbaumgartner. Now its working well 😄
        However, this is the first step for drawing cone. The main goal is to draw cone freely.
        I'm trying to add inputbox with the corrected code you gave, and have already made codes that can draw various shape with inputbox, but it didn't work again. In my view, there is no problem. Could you please look over it again?

        require 'sketchup.rb'
        
        class Cone
        @@Length = @@angle = nil
        
        def create_cone
        @@Length = 0.mm if not @@Length
        @@angle = 0.degrees if not @@angle
        
        prompts = ["Radius","Angle"]
        values = [@@Length, @@angle]
        results = inputbox prompts, values, "Fire sensor"
        if results 
        @@Length, @@angle = results
        model = Sketchup.active_model
        model.start_operation "Fire sensor"
        compdef = Sketchup.active_model.definition.add
        ents = compdef.entities
        
        @Length = @@Length
        @angle = (@@angle)/2
        @pt0 = [0,0,0]
        @pt1 = [0, 0, -@Length]
        
        @pt2=@pt1.transform(Geom;;Transformation.rotation(@pt0,[0,-1,0],@angle))
        arcarray = ents.add_arc [0,0,0], [0,0,-1],[0,-1,0], @Length, 0, @angle
        arc = arcarray[0]
        arccurve = arc.curve
        verts = arccurve.vertices
        
        a = ents.add_line @pt0, @pt1
        b = ents.add_line @pt0, @pt2
        face = ents.add_face @pt0, @pt1, @pt2
        path = ents.add_circle [0,0,-@Length], [0,0,1], -@Length
        face.followme path
        ents.erase_entities path
        
        face2 = ents.add_face verts
        path2 = ents.add_circle [0,0,-@Length], [0,0,1], -@Length
        face2.followme path2
        ents.erase_entities path2
        
        group=ents.add_group ents.to_a
        
        trans = Geom;;Transformation.new
        status = model.place_component(compdef)
        Sketchup.active_model.active_entities.add_instance(compdef, trans)
        
        model.commit_operation
        end #if
        Sketchup.send_action 'selectSelectionTool;'
        end #def
        end #Class
        
        if( not file_loaded?(__FILE__) )
        
          UI.add_context_menu_handler do |menu|
          
          end
          
          dir=File.dirname(__FILE__)
          	cmd_array = []		
        
           cmd = UI;;Toolbar.new "sphere"
           # Create the Command object
           cmd = UI;;Command.new("Cone") {Sketchup.active_model.select_tool Cone.new.create_cone}
           # Configure the Command's appearance
           cmd.small_icon = cmd.large_icon = dir+"/FGmanual/images/cone.png"#<--
           cmd.tooltip = "Cone"
           cmd_array.push(cmd)
           
           tb=UI;;Toolbar.new("Creating tools")
           cmd_array.each {|i| tb.add_item(i)}
           	tb.show if tb.get_last_state == -1
        end
        file_loaded(__FILE__)
        
        
        
        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 16 Jun 2015, 10:25

          ...
          @@angle = **30.0** unless @@angle
          ...
          @angle = (@@angle**.degrees**)/2
          ...
          The various parts of the API accepting angles needs them in radians.
          If the user inputs a float as 'degrees' you need to tell SketchUp it in 'radians' by using the converter .degrees
          You do NOT want the angle to be 0.0 as that does nothing so trap for wrong inputs.
          Also the length should never be 0.0 so trap for that too - perhaps with defaults of say 30.0 and 50.mm ?

          TIG

          1 Reply Last reply Reply Quote 0
          • K Offline
            kmk111890
            last edited by 16 Jun 2015, 11:38

            Thanks you for a kind comment, TIG, but it's not still working now. There is no problem until adding numbers in the inputbox. The problem is nothing shows up after clicking 'ok'.

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 16 Jun 2015, 15:59

              Sorry, but your code is full of issues...

              Can I suggest you temporarily add a few Sketchup.active_model.active_view.refresh;UI.messagebox('?') into your code, then run it with the Ruby Console open, and see what happens, when you see it fails you can see what's made at that step.

              The length/angle need fixing as I said before, but also...
              I see is that you attempt to add a group from an array of entities that are NOT in the active_entities context - this will often fail - even splat! I don't see why you do this anyway ?
              Also its model.definition**s** - you missed off the 's'.
              Also consider naming the component definition ?
              Also why place an instance and then also add one - why ?

              TIG

              1 Reply Last reply Reply Quote 0
              • K Offline
                kmk111890
                last edited by 16 Jun 2015, 22:25

                Hi, TIG. Finally, Its working as I have hoped. This is the corrected code. I needed not use 'group'. The cone shape is created after I deleted the line. Thank you!

                    require 'sketchup.rb'
                
                    class Cone
                    @@Length = @@angle =@@x=@@y=@@z = nil
                
                    def create_cone
                    @@Length = 100 if not @@Length
                    @@angle = 30.0 if not @@angle
                    @@x = 0.mm if not @@x
                    @@y = 0.mm if not @@y
                    @@z = 0.mm if not @@z
                    
                    prompts = ["Radius","Angle", "x", "y", "z"]
                    values = [@@Length, @@angle, @@x, @@y, @@z]
                    results = inputbox prompts, values, "Fire sensor"
                    if results
                    @@Length, @@angle, @@x, @@y, @@z = results
                    model = Sketchup.active_model
                    model.start_operation "Fire sensor"
                    compdef = Sketchup.active_model.definitions.add
                    ents = compdef.entities
                
                    @Length = @@Length.mm
                    @angle = (@@angle.degrees)/2
                    @pt0 = [0,0,0]
                    @pt1 = [0, 0, -@Length]
                
                    @pt2=@pt1.transform(Geom;;Transformation.rotation(@pt0,[0,-1,0],@angle))
                    arcarray = ents.add_arc [0,0,0], [0,0,-1],[0,-1,0], @Length, 0, @angle
                    arc = arcarray[0]
                    arccurve = arc.curve
                    verts = arccurve.vertices
                
                    a = ents.add_line @pt0, @pt1
                    b = ents.add_line @pt0, @pt2
                    face = ents.add_face @pt0, @pt1, @pt2
                    path = ents.add_circle [0,0,-@Length], [0,0,1], -@Length
                    face.followme path
                    ents.erase_entities path
                
                    face2 = ents.add_face verts
                    path2 = ents.add_circle [0,0,-@Length], [0,0,1], -@Length
                    face2.followme path2
                    ents.erase_entities path2
                
                    trans = Geom;;Transformation.new([@@x.to_i, @@y.to_i, @@z.to_i])
                    status=model.place_component(compdef)
                    Sketchup.active_model.active_entities.add_instance(compdef, trans)
                  
                    model.commit_operation
                    end #if
                    Sketchup.send_action 'selectSelectionTool;'
                    end #def
                    end #Class
                
                    if( not file_loaded?(__FILE__) )
                
                      UI.add_context_menu_handler do |menu|
                     
                      end
                     
                      dir=File.dirname(__FILE__)
                         cmd_array = []      
                
                       cmd = UI;;Toolbar.new "sphere"
                       # Create the Command object
                       cmd = UI;;Command.new("Cone") {Sketchup.active_model.select_tool Cone.new.create_cone}
                       # Configure the Command's appearance
                       cmd.small_icon = cmd.large_icon = dir+"/FGmanual/images/cone.png"#<--
                       cmd.tooltip = "Cone"
                       cmd_array.push(cmd)
                       
                       tb=UI;;Toolbar.new("Creating tools")
                       cmd_array.each {|i| tb.add_item(i)}
                          tb.show if tb.get_last_state == -1
                    end
                    file_loaded(__FILE__)
                
                
                
                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement