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

    Having trouble ending a simple script

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 170 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.
    • B Offline
      Bombarde16
      last edited by

      Hello all,

      I'm trying to get from the tutorials into my first ruby plugin and am tantalizingly close but still getting a syntax error at the very end. Here's the code:

      require 'sketchup'
      Sketchup.send_action "showRubyPanel;"
      
      UI.menu("PlugIns").add_item("Square Organ Pipe 2D")
      
      prompts = [$exStrings.GetString("Internal Width"),
      $exStrings.GetString("Internal Depth"),
      $exStrings.GetString("Thickness")]
      values = [0.inches, 0.inches, 0.inches]
      results = inputbox prompts, values, $exStrings.GetString("Pipe internal dimensions and thickness")
      internal_width, internal_depth, thickness = results
      
      model = Sketchup.active_model
      entities = model.entities
      
      outer_x = (thickness * 2) + internal_width
      outer_y = (thickness * 2) + internal_depth
      
      inner_x1 = thickness
      inner_x2 = thickness + internal_width
      inner_y1 = thickness
      inner_y2 = thickness + internal_depth
      
      center_x = thickness + (internal_width * 0.5)
      center_y = thickness + (internal_depth * 0.5)
      
      outer_1 = [0,0,0]
      outer_2 = [outer_x,0,0]
      outer_3 = [outer_x,outer_y,0]
      outer_4 = [0,outer_y,0]
      
      inner_1 = [inner_x1, inner_y1, 0]
      inner_2 = [inner_x2, inner_y1, 0]
      inner_3 = [inner_x2, inner_y2, 0]
      inner_4 = [inner_x1, inner_y2, 0]
      
      center = [center_x, center_y, 0]
      
      new_face = entities.add_face outer_1, outer_2, outer_3, outer_4
      new_face = entities.add_face inner_1, inner_2, inner_3, inner_4
      line = entities.add_line inner_1, center
      line = entities.add_line inner_2, center
      
      end
      

      I'm sure I've made some silly beginner's mistake in here. Anyone see it?

      FWIW, this is to produce some 2D boxes to assist in laying out the internal design of a pipe organ. They look like this:

      http://lh3.ggpht.com/_CFXfoZ7L-vI/Sonv734qxhI/AAAAAAAASH8/PyvNcdYv4x4/s576/basic pipe plan.jpg

      ...and I need to make nearly three hundred of them in sizes differing down to a thousandth of an inch. Whence the need to automate part of the process.

      Thanks,
      Rob

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        Hi Bombarde, I looked at it very quickly. There are indeed some errors, but its all fixable.

        First I wrapped your entire script in a module, and then put all the script into a method that I named "main". So now your script and variables will not be able to conflict with other scripts.

        Second, your menu system was not implemented properly. Check out how I did it. Line 45 is the only line that actually is needed to load it into the menu.

        Third, the 0.inches did not work on my system. I changed them to 0.to_l which will change it to a measurement unit.

        And I also simplified your inputbox strings. I couldn't see why you needed to access the language handler, so I just removed all the language handler stuff, which is not needed.

        And thats about all it took. Here it is, just save it as any file name in your plugins folder.

        require 'Sketchup.rb'
        
        module Robert_Horton_box
        	def Robert_Horton_box.main
        		prompts = "Internal Width", "Internal Depth", "Thickness"
        		values = [0.to_l, 0.to_l, 0.to_l]
        		results = inputbox prompts, values, "Pipe internal dimensions and thickness"
        		internal_width, internal_depth, thickness = results
        
        		model = Sketchup.active_model
        		entities = model.entities
        
        		outer_x = (thickness * 2) + internal_width
        		outer_y = (thickness * 2) + internal_depth
        
        		inner_x1 = thickness
        		inner_x2 = thickness + internal_width
        		inner_y1 = thickness
        		inner_y2 = thickness + internal_depth
        
        		center_x = thickness + (internal_width * 0.5)
        		center_y = thickness + (internal_depth * 0.5)
        
        		outer_1 = [0,0,0]
        		outer_2 = [outer_x,0,0]
        		outer_3 = [outer_x,outer_y,0]
        		outer_4 = [0,outer_y,0]
        
        		inner_1 = [inner_x1, inner_y1, 0]
        		inner_2 = [inner_x2, inner_y1, 0]
        		inner_3 = [inner_x2, inner_y2, 0]
        		inner_4 = [inner_x1, inner_y2, 0]
        
        		center = [center_x, center_y, 0]
        
        		new_face = entities.add_face outer_1, outer_2, outer_3, outer_4
        		new_face = entities.add_face inner_1, inner_2, inner_3, inner_4
        		line = entities.add_line inner_1, center
        		line = entities.add_line inner_2, center
        
        	end # Robert_Horton_box.main
        end # Module
        
        if !file_loaded?(__FILE__)
          UI.menu("Plugins").add_item("Rob's Box") { Robert_Horton_box.main }
        end
        
        file_loaded(__FILE__)
        

        Chris

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

        1 Reply Last reply Reply Quote 0
        • B Offline
          Bombarde16
          last edited by

          Chris,

          Thanks so much for the help, the revised script works beautifully. Proof positive that I was and still am in way over my head.

          Rob

          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