I'm an introductory Ruby who wants to make Ruby in Google Sketchup
Ruby is hard for beginners because there's not much data
I'm uploading the code (it's very short, so please watch it even if it's annoying )/)
class MakeboxTool
def self.makebox
#======================↓Normal operation↓==============================
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
faces = selection.grep(Sketchup::Face)
faces.each{|face|
group = entities.add_group(face)
group2 = group.copy
group.explode
entities2 = group2.entities
before = entities2.grep(Sketchup::Face)
before[0].material = [50, 255, 255]
before[0].pushpull( 10.mm )
new_faces = entities2.grep(Sketchup::Face) - before
upper_face = new_faces.find {|f| f.normal == before[0].normal.reverse }
unless upper_face.nil?
start_point = upper_face.bounds.center
offset_vector = upper_face.normal
offset_vector.length = 90.mm
end_point = start_point.offset(offset_vector)
end
edge = group2.entities.add_line(start_point, end_point)
layer = Sketchup.active_model.layers.add("win")
group2.layer = layer
entities2.each{|e| e.layer = layer }
if !model.active_path.nil?
instance = model.active_path.last
definition = instance.definition
group2_copy = group2.parent.parent.parent.entities.add_instance(group2.definition,group2.transformation)
group2.erase!
end
}
end # of makebox class
end
#==================↑ Normal operating part ↑=========================
if( not file_loaded?("makebox.rb") )
UI.menu("Plugins").add_item("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
dir = Sketchup.find_support_file("Plugins")
cmd = UI::Command.new("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
cmd.large_icon = cmd.small_icon = dir+"/makebox.png"
cmd.status_bar_text = cmd.tooltip = "Tool for making blue boxes"
tb = UI::Toolbar.new("makebox")
tb.add_item cmd
tb.show if tb.get_last_state == -1
end
file_loaded("makebox.rb")
It's a simple makebox tool that makes windows on the wall (face)
The part marked with a dotted line in the middle will operate normally alone.
To create an execution button with an icon
The classdef method at the top and
class MakeboxTool
def self.makebox
If you add this method at the bottom, there will be no response when you run it
if( not file_loaded?("makebox.rb") )
UI.menu("Plugins").add_item("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
dir = Sketchup.find_support_file("Plugins")
cmd = UI::Command.new("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
cmd.large_icon = cmd.small_icon = dir+"/makebox.png"
cmd.status_bar_text = cmd.tooltip = "Tool for making blue boxes"
tb = UI::Toolbar.new("makebox")
tb.add_item cmd
tb.show if tb.get_last_state == -1
end
file_loaded("makebox.rb")
Let me tell you again,
To create an execution button with an icon in a working code
When I added code to top and bottom, the execution didn't work. (No error message)
I don't understand the solution I received...
There are two major things wrong with your code.
First, your MakeboxTool class has only loose class-scope code, no methods (def’s) at all, not even initialize. So, the code is run as the file loads, but thereafter is dead. Creating an instance via MakeboxTool.new does not cause the class-scope code to run again, so your menu item and toolbar button just create dead objects.
You could convert the class into a one-shot by wrapping the loose code in def initialize…end. initialize is the method run to initialize an object created by new.
But, that takes us into the second major issue: your code does not implement any of the methods of the Tool protocol 2. It does not really qualify as a thing that should be activated using select_tool. If you want a one-shot, you could create a class method (def self.methodname) and invoke it in the command’s code block instead of selecting a Tool.
Help me!!!!!!!
ruby
Roughly translated:
There are no methods in the MakeboxTool class you just wrote. There is no initialize
and these codes are just loosely defined and executed. So once the file is loaded, it's going to run all the time, but then it doesn't do anything anymore. It would be nice to put those codes in definitialize...end
so that they run right at the initial initialization of this class.
But the additional problem is that the code you just wrote does not implement Tool Protocol 2 methods at all. It becomes something that should not be activated by using select_tool
. Instead of choosing Tool, I think you should define a class method and sing it in the command code block.
Usually, when it's like this, I get something that has an "execution button" that other people have implemented and refer to it... There's no such thing?
© 2024 OneMinuteCode. All rights reserved.