Correction of codes
-
Hello, I have a problem in making box. I would like to add box on space by adding an icon, but it didn't work well. is there anybody who can fix the code?
` Class Box
def create_box1@lenght1 = 0.feet if not @lenght1 # along Green Y axis @width1 = 0.feet if not @width1 # along Red X axis @height1 = 0.feet if not @height1 # along Blue Z axis @R = 0 if not @R @G = 0 if not @G @B = 0 if not @B prompts = ["Lenght", "Width", "Height", "R(0~255)", "G(0~255)", "B(0~255)"] values = [@lenght1, @width1, @height1, @R, @G, @B] results = inputbox prompts, values, "Box Red" return if not results @lenght1, @width1, @height1, @R, @G, @B = results model = Sketchup.active_model model.start_operation "Create Box"
#-----------------------------------------------------------------------------
new_comp_def = Sketchup.active_model.definitions.add@pt0 = [0, 0, 0] @pt1 = [0, @lenght1, 0] @pt2 = [0, @lenght1, @height1] @pt3 = [0, 0, @height1] newface = new_comp_def.entities.add_face(@pt0, @pt1, @pt2, @pt3) newface.material=Sketchup::Color.new(@R, @G, @B) #color red newface.material.alpha = 0.4 newface.pushpull @width1 status=model.place_component(new_comp_def) Sketchup.active_model.active_entities.add_instance(new_comp_def, trans)
model.commit_operation
end
endCreate the Command object
box_cmd = UI::Command.new("Box") {
Sketchup.active_model.select_tool Box.new
}Configure the Command's appearance
box_cmd.small_icon = box_cmd.large_icon = dir+"/box.png"
box_cmd.tooltip = "Box"Create and configure the Toolbar
box_toolbar = UI::Toolbar.new "box"
box_toolbar.add_item box_cmd
box_toolbar.show` -
A few additional comments:
You should always wrap your class code inside a module so that what you define does not go into the global namespace where it can collide with other code that used the same names by coincidence. This will probably necessitate some name scoping (using double-colon) in the code that creates the command.
You probably don't want to invoke both model.place_component and active_entities.add_instance. The former creates a new instance and starts an interactive operation in which the user can place the instance as desired. The latter programmatically places an instance at a specified location and rotation without user interaction. Doing both is quite confusing.
-
Always place code in a code block.
class Box @@lenght1=@@width1=@@height1=@@R=@@G=@@B=nil#<--class variables def create_box1 @@lenght1 = 0.feet if not @@lenght1 # along Green Y axis @@width1 = 0.feet if not @@width1 # along Red X axis @@height1 = 0.feet if not @@height1 # along Blue Z axis @@R = 0 if not @@R @@G = 0 if not @@G @@B = 0 if not @@B prompts = ["Lenght", "Width", "Height", "R(0~255)", "G(0~255)", "B(0~255)"] values = [@@lenght1, @@width1, @@height1, @@R, @@G, @@B] results = inputbox prompts, values, "Box Red" if results @@lenght1, @@width1, @@height1, @@R, @@G, @@B = results model = Sketchup.active_model model.start_operation "Create Box" #----------------------------------------------------------------------------- new_comp_def = Sketchup.active_model.definitions.add @pt0 = [0, 0, 0] @pt1 = [0, @@lenght1, 0] @pt2 = [0, @@lenght1, @@height1] @pt3 = [0, 0, @@height1] newface = new_comp_def.entities.add_face(@pt0, @pt1, @pt2, @pt3) newface.material=Sketchup;;Color.new(@@R, @@G, @@B) #color red newface.material.alpha = 0.4 newface.pushpull @@width1 trans = Geom;;Transformation.new() #<---identity transformation status=model.place_component(new_comp_def) Sketchup.active_model.active_entities.add_instance(new_comp_def, trans) model.commit_operation end Sketchup.send_action 'selectSelectionTool;'#<-- end#def end unless file_loaded?(__FILE__) # Create and configure the Toolbar box_toolbar = UI;;Toolbar.new "box" # Create the Command object box_cmd = UI;;Command.new("Box") {Sketchup.active_model.select_tool Box.new.create_box1} # Configure the Command's appearance box_cmd.small_icon = box_cmd.large_icon = Dir.pwd + "/box.png"#<-- box_cmd.tooltip = "Box" box_toolbar.add_item box_cmd box_toolbar.show file_loaded(__FILE__) end
This code will execute but is far from a finished plugin.
-
Thanks, sdmitch and slbaumgartner. Now It works well
However, I'm planning to create some shapes more with this way and actually made few shapes. I'm worrying about increase of icons in Sketchup, and am making ideas how to simplify tools.
-
Making dialog box
I saw some free tools which 'inputbox' is added together. If the shapes can be individually selected in inputbox, I need not add icons anymore, but don't know how to make it. I know basic codes for creating inputbox, and would like to apply the basic code to what I want(choosing each shapes individually in inputbox). Additionally, Is it possible to add shapes which are saved in .skp file(I know how to load shapes saved in .skp file by using 'Class' method and how to place it on Sketchup space clicking) into inputbox too? -
Using webdialog
If i create webdialog box and add all icons into the webdialog by making tables(categorization), This way may look good as well. I'm trying to use
dlg.add_action_callback("") {|d, p|Sketchup.send_action ""}
Can i utilize this command for making icons in webdialog? I mean, the command looked like only basic tools(Undo, Redo, New, Cut, PushPull) can be used with the command. For example
dlg.add_action_callback("Undo") {|d, p|Sketchup.send_action "editUndo:"}
dlg.add_action_callback("Redo") {|d, p|Sketchup.send_action "editRedo:"}
dlg.add_action_callback("New") {|d, p|Sketchup.send_action 57600}
dlg.add_action_callback("Open") {|d, p|Sketchup.send_action 57601}
dlg.add_action_callback("Save") {|d, p|Sketchup.send_action 57603}As you know, these actions are used in HTML and '<scrip> </script>' connects between ruby codes and HTML codes for indicating icons in webdialog. How can I make my own Sketchup.send_action? If it is possible, I will add the icons into webdialog.
-
Advertisement