#----------------------------------------------------------------------------- # Name : slot.2.rb # Description : This file creates a 2D slot. # Parameters : The slot width and length. # Menu Item : Draw -> Slot # Context Menu: NONE # Usage : N/A # Date : 8/14/2006 # Author : Doug Herrmann #----------------------------------------------------------------------------- require 'sketchup.rb' #----------------------------------------------------------------------------- # Slot Class: Made up of 2 semi-circular arcs and 2 edges. # Extruded surface has 4 internal edges. #----------------------------------------------------------------------------- class Slot # default values @@width = 4.inch @@length = 7.inch # Use Class Method to prompt for parameters def Slot.create # Inputbox prompts = ["Slot Width: ", "Slot Length: "] values = [@@width, @@length] results = inputbox prompts, values, $exStrings.GetString("Slot Parameters") # This means that the user cancelled the operation return if not results # otherwise, set new default values @@width, @@length = results @@width = 0.01.inch if (@@width <= 0.0.inch) @@length = 0.01.inch if (@@length <= 0.0.inch) #create the slot Slot.new end def initialize(width=@@width, length=@@length) @width = width @length = length draw end private def draw # Bracket everything for a single UNDO operation. model = Sketchup.active_model model.start_operation "Create Slot" entities = model.active_entities # Unit vectors along the X-axis and Z-axis i = Geom::Vector3d.new 1,0,0; i.normalize! k = Geom::Vector3d.new 0,0,1; k.normalize! # Draw top arc c = [0, @length/2.0, 0] arccurve1 = entities.add_arc c, i, k, @width/2.0, 0, Math::PI, 24 # Draw bottom arc c = [0, -@length/2.0, 0] arccurve2 = entities.add_arc c, i, k, @width/2.0, Math::PI, 2*Math::PI, 24 # Draw sides p1 = [-@width/2.0, -@length/2.0, 0] p2 = [-@width/2.0, +@length/2.0, 0] edges1 = entities.add_edges p2, p1 p1 = [@width/2.0, -@length/2.0, 0] p2 = [@width/2.0, +@length/2.0, 0] edges2 = entities.add_edges p1, p2 # Add a face to slot face = entities.add_face arccurve1 + edges1 + arccurve2 + edges2 model.commit_operation end end #----------------------------------------------------------------------------- # User Interface Stuff #----------------------------------------------------------------------------- if( not file_loaded?("slot.2.rb") ) # This will add a separator to the menu, but only once add_separator_to_menu($exStrings.GetString("Draw")) # To add an item to a menu UI.menu($exStrings.GetString("Draw")).add_item($exStrings.GetString("Slot")) { Slot.create } end #----------------------------------------------------------------------------- file_loaded("slot.2.rb")