Making submenu
-
I am a beginner in learning Sketchup with Ruby. Now, I am practicing coding from existing .rb files. Below code is one of examples. You will be able to place the figures in .skp on cartesian space if you run this script(you need .skp files, images for making icons).
I would like to run this script in another way, submenu. I hope all figures are placed wherever i want after selection in input box, not clicking icons. It will be my pleasure if you give advices.
` require 'sketchup.rb'
class FGsystemdef FGsystem::reset_global_variables
@status_prefix=""
end
def FGsystem::set_none_params(e,dist)
FGsystem.set_default_(e,dist) dict_name="FGsystem" dict_key_attenuation = e.get_attribute(dict_name, " ")
end
def FGsystem::set_30m90d_params(e,dist)
FGsystem.set_default_30m90d(e,dist) dict_name="FGsystem" dict_key_attenuation = " " if dict_key_attenuation==nil dict_key_radius =@spothot if@spothot!=nil dict_key_falloff = @spotoff if @spotoff!=nil
end
def FGsystem::set_50m60d_params(e,dist)
FGsystem.set_default_50m60d(e,dist) dict_name="FGsystem" dict_key_attenuation = " " if dict_key_attenuation==nil dict_key_radius =@spothot if@spothot!=nil dict_key_falloff = @spotoff if @spotoff!=nil
end
def FGsystem::set_default_none(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::set_default_30m90d(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::set_default_50m60d(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::status_bar(stat_text)
statbar = Sketchup.set_status_text(@status_prefix + stat_text)
enddef FGsystem::instert_none
Sketchup.active_model.select_tool FGdrawing.new("none.skp")
enddef FGsystem::insert_30m90d
Sketchup.active_model.select_tool FGdrawing.new("30m_90degree.skp")
enddef FGsystem::insert_50m60d
Sketchup.active_model.select_tool FGdrawing.new("50m_60degree.skp")
end
endclass FGdrawing
def initialize(comp)
FGsystem.reset_global_variables
@ip1 = nil
@ip1 = nil
@drawn=false
@compname=comp
model=Sketchup.active_model
status=model.start_operation(" ")
model.commit_operation
enddef activate
@ip = Sketchup::InputPoint.new
@ip1 = Sketchup::InputPoint.new
@ip2 = Sketchup::InputPoint.new
FGsystem::status_bar(" ")
self.reset(nil)
enddef deactivate(view)
view.invalidate
enddef onMouseMove(flags, x, y, view)
if @state==0
@ip.pick view, x, y
if( @ip != @ip1 )
view.invalidate if( @ip.display? or @ip1.display? )
@ip1.copy! @ip
view.tooltip = @ip1.tooltip if( @ip1.valid? )
end
else
@ip2.pick view, x, y, @ip1
view.tooltip = @ip2.tooltip if( @ip2.valid? )
view.invalidate
end
enddef onLButtonDown(flags, x, y, view)
if (@state==0)
@ip1.pick view, x, y
if( @ip1.valid? )
Sketchup::set_status_text "Select none's range", SB_PROMPT if (@compname=="none.skp")
Sketchup::set_status_text "Select 30m90d's target", SB_PROMPT if (@compname=="30m_90degree.skp")
Sketchup::set_status_text "Select 50m60d's target", SB_PROMPT if (@compname=="50m_60degree.skp")
@state=1
end
elsif (@state==1)
if(@ip2.valid?)
self.create_light(view)
self.reset(view)
@state=0
end
end
enddef draw(view)
if( @ip1.valid? ) if( @ip1.display? ) @ip1.draw(view) @drawn = true end end if( @ip2.valid? ) @ip2.draw(view) if( @ip2.display?) length = (@ip1.position.distance(@ip2.position)).to_m self.draw_geometry(@ip1.position, @ip2.position, view) @drawn = true end
end
def onCancel(flag, view)
mod=Sketchup.active_model
mod.select_tool nil if @state==0
self.reset(view)
enddef reset(view)
@state = 0
FGsystem::status_bar(" ")
@ip.clear
@ip1.clear
@ip2.clear
if( view )
view.tooltip = nil
view.invalidate
end
@drawn = false
enddef draw_geometry(pt1,pt2,view)
view.set_color_from_line(pt1,pt2)
view.line_stipple="-.-"
view.line_width = 1
view.draw(GL_LINE_STRIP, pt1, pt2)
enddef deactivate(view)
view.invalidate if @drawn
enddef create_light(view)
pt1=@ip1.position
pt2=@ip2.position
model=Sketchup.active_model
definitions=model.definitions
path=Sketchup.find_support_file(@compname,"Plugins/FGsystem")
definition=definitions.load(path.to_s)
model.start_operation("Insert Light")
light=model.active_entities.add_instance(definition, @ip1.position)
vector=pt1 - pt2
vector2=vector.clone
vector2.length=1
pt1=pt1-vector2
vector2=vector
vector2=Geom::Vector3d.new(0,0,1) if @compname=="none.skp"
trans=Geom::Transformation.new(pt1,vector2)
light.transformation=trans
dist=vector.length.to_m
FGsystem.set_none_params(light,dist) if @compname=="none.skp"
FGsystem.set_30m90d_params(light,dist) if @compname=="30m_90degree.skp"
FGsystem.set_50m60d_params(light,dist) if @compname=="50m_60degree.skp"
model.commit_operationend
endif( not file_loaded?(FILE) )
UI.add_context_menu_handler do |menu| end dir= File.dirname(__FILE__) cmd_array = [] cmd = UI::Command.new(" ") {FGsystem.insert_30m90d} cmd.large_icon = cmd.small_icon = dir+"/FGsystem/kt_spot.png" cmd.status_bar_text = cmd.tooltip = "30m90d" cmd_array.push(cmd) cmd = UI::Command.new(" ") {FGsystem.insert_50m60d} cmd.large_icon = cmd.small_icon = dir+"/FGsystem/kt_scene.png" cmd.status_bar_text = cmd.tooltip = "50m60d" cmd_array.push(cmd) tb = UI::Toolbar.new("FGDetector") cmd_array.each {|i| tb.add_item(i)} tb.show if tb.get_last_state == -1
end
file_loaded(FILE)` -
@kmk111890 said:
I would like to run this script in another way, submenu. I hope all figures are placed wherever i want after selection in input box, not clicking icons. It will be my pleasure if you give advices.
if( not file_loaded?(__FILE__) ) UI.add_context_menu_handler do |menu| sub=menu.add_submenu("FGsystem Insert") sub.add_item("30m90d") {FGsystem.insert_30m90d} sub.add_item("50m60d") {FGsystem.insert_50m60d) end#do end#if
-
(1) Never define classes at the toplevel (within the global
ObjectSpace
.)(2) Since you will likely be writing more than 1 (one) plugin, wrap each plugin in a sub-module.
Example how to wrap your classes & plugins like so:
module KMK # toplevel author/company namespace module module FG # plugin namespace module class Drawing end class System end unless file_loaded?(__FILE__) # code to run only during load ... end # unless loaded file_loaded(__FILE__) end # module plugin FG end # module toplevel KMK
(3) Each of you plugin folders should begin with your toplevel module name, such as "KMK_", or whatever you choose. (This is required by the Extension Warehouse using the
SketchupExtension
class.)(4) use
self::method_name()
to define class methods and module functions, so you do not need to rename them all, if you decide to (or must) change the name of the class or module.
Here's an example for the run once block:
unless file_loaded?(__FILE__) dir= File.dirname(__FILE__) cmd_array = [] menu_text = "FGDetector" item_text = "30m90d" cmd = UI;;Command.new(item_text) { System.insert_30m90d } cmd.large_icon = cmd.small_icon = "#{dir}/KMK_FGsystem/kt_spot.png" cmd.status_bar_text = cmd.tooltip = item_text cmd.set_validation_proc { MF_ENABLED } cmd_array.push(cmd) item_text = "50m60d" cmd = UI;;Command.new(item_text) { System.insert_50m60d } cmd.large_icon = cmd.small_icon = "#{dir}/KMK_FGsystem/kt_scene.png" cmd.status_bar_text = cmd.tooltip = item_text cmd.set_validation_proc { MF_ENABLED } cmd_array.push(cmd) tb = UI;;Toolbar.new(menu_text) submenu = UI.menu("Plugins").add_submenu(menu_text) # [a] The context menu is built EACH time the user clicks the right-mouse button. UI.add_context_menu_handler do |pop| # You can wrap the following lines within # a conditional expression, to control # if your popup context menu is shown. subpop = pop.add_submenu(menu_text) cmd_array.each {|i| subpop.add_item(i) } end # [b] So the normal menu and toolbars must be registered separately! cmd_array.each do |i| tb.add_item(i) submenu.add_item(i) end tb.show if tb.get_last_state == -1 end # unless loaded file_loaded(__FILE__)
-
Use
String
interpolation operator#{}
to stuff variable data into strings.
Like:"#{dir}/KMK_FGsystem/kt_spot.png"
(The above creates 1 string object.)This:
dir + "/KMK_FGsystem/kt_spot.png"
creates an extra new string object, which is slower.
(See the doc for the String class +() instance method.) -
I tried to fix some lines, but It still didn't work except that submenu is created. Nothing shown up on space once I clicked the submenu. Is it right the name of folder in plugins has to be 'KMK'?
` require 'sketchup.rb'
module KMK
module FGclass FGdrawing
def initialize(comp)
FGsystem.reset_global_variables
@ip1 = nil
@ip1 = nil
@drawn=false
@compname=comp
model=Sketchup.active_model
status=model.start_operation(" ")
model.commit_operation
enddef activate
@ip = Sketchup::InputPoint.new
@ip1 = Sketchup::InputPoint.new
@ip2 = Sketchup::InputPoint.new
FGsystem::status_bar(" ")
self.reset(nil)
enddef deactivate(view)
view.invalidate
enddef onMouseMove(flags, x, y, view)
if @state==0
@ip.pick view, x, y
if( @ip != @ip1 )
view.invalidate if( @ip.display? or @ip1.display? )
@ip1.copy! @ip
view.tooltip = @ip1.tooltip if( @ip1.valid? )
end
else
@ip2.pick view, x, y, @ip1
view.tooltip = @ip2.tooltip if( @ip2.valid? )
view.invalidate
end
enddef onLButtonDown(flags, x, y, view)
if (@state==0)
@ip1.pick view, x, y
if( @ip1.valid? )
Sketchup::set_status_text "Select none's range", SB_PROMPT if (@compname=="none.skp")
Sketchup::set_status_text "Select 30m90d's target", SB_PROMPT if (@compname=="30m_90degree.skp")
Sketchup::set_status_text "Select 50m60d's target", SB_PROMPT if (@compname=="50m_60degree.skp")
@state=1
end
elsif (@state==1)
if(@ip2.valid?)
self.create_light(view)
self.reset(view)
@state=0
end
end
enddef draw(view)
if( @ip1.valid? ) if( @ip1.display? ) @ip1.draw(view) @drawn = true end end if( @ip2.valid? ) @ip2.draw(view) if( @ip2.display?) length = (@ip1.position.distance(@ip2.position)).to_m self.draw_geometry(@ip1.position, @ip2.position, view) @drawn = true end
end
def onCancel(flag, view)
mod=Sketchup.active_model
mod.select_tool nil if @state==0
self.reset(view)
enddef reset(view)
@state = 0
FGsystem::status_bar(" ")
@ip.clear
@ip1.clear
@ip2.clear
if( view )
view.tooltip = nil
view.invalidate
end
@drawn = false
enddef draw_geometry(pt1,pt2,view)
view.set_color_from_line(pt1,pt2)
view.line_stipple="-.-"
view.line_width = 1
view.draw(GL_LINE_STRIP, pt1, pt2)
enddef deactivate(view)
view.invalidate if @drawn
enddef create_light(view)
pt1=@ip1.position
pt2=@ip2.position
model=Sketchup.active_model
definitions=model.definitions
path=Sketchup.find_support_file(@compname,"Plugins/KMK/FG")
definition=definitions.load(path.to_s)
model.start_operation("Insert Light")
light=model.active_entities.add_instance(definition, @ip1.position)
vector=pt1 - pt2
vector2=vector.clone
vector2.length=1
pt1=pt1-vector2
vector2=vector
vector2=Geom::Vector3d.new(0,0,1) if @compname=="none.skp"
trans=Geom::Transformation.new(pt1,vector2)
light.transformation=trans
dist=vector.length.to_m
FGsystem.set_none_params(light,dist) if @compname=="none.skp"
FGsystem.set_30m90d_params(light,dist) if @compname=="30m_90degree.skp"
FGsystem.set_50m60d_params(light,dist) if @compname=="50m_60degree.skp"
model.commit_operationend
end
class FGsystemdef FGsystem::reset_global_variables
@status_prefix=""
end
def FGsystem::set_none_params(e,dist)
FGsystem.set_default_(e,dist) dict_name="FGsystem" dict_key_attenuation = e.get_attribute(dict_name, " ")
end
def FGsystem::set_30m90d_params(e,dist)
FGsystem.set_default_30m90d(e,dist) dict_name="FGsystem" dict_key_attenuation = " " if dict_key_attenuation==nil dict_key_radius =@spothot if@spothot!=nil dict_key_falloff = @spotoff if @spotoff!=nil
end
def FGsystem::set_50m60d_params(e,dist)
FGsystem.set_default_50m60d(e,dist) dict_name="FGsystem" dict_key_attenuation = " " if dict_key_attenuation==nil dict_key_radius =@spothot if@spothot!=nil dict_key_falloff = @spotoff if @spotoff!=nil
end
def FGsystem::set_default_none(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::set_default_30m90d(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::set_default_50m60d(e,dist)
dict_name="FGsystem" if !e.attribute_dictionary dict_name dict_key_animate = " " dict_key_attenuation = " " end
end
def FGsystem::status_bar(stat_text)
statbar = Sketchup.set_status_text(@status_prefix + stat_text)
enddef FGsystem::instert_none
Sketchup.active_model.select_tool FGdrawing.new("none.skp")
enddef FGsystem::insert_30m90d
Sketchup.active_model.select_tool FGdrawing.new("30m_90degree.skp")
enddef FGsystem::insert_50m60d
Sketchup.active_model.select_tool FGdrawing.new("50m_60degree.skp")
end
endunless file_loaded?(__FILE__) dir= File.dirname(__FILE__) cmd_array = [] menu_text = "FGDetector" item_text = "30m90d" cmd = UI::Command.new(item_text) { FGSystem.insert_30m90d } cmd.large_icon = cmd.small_icon = dir + "/KMK/FG/kt_spot.png" cmd.status_bar_text = cmd.tooltip = item_text cmd.set_validation_proc { MF_ENABLED } cmd_array.push(cmd) item_text = "50m60d" cmd = UI::Command.new(item_text) { FGSystem.insert_50m60d } cmd.large_icon = cmd.small_icon = dir + "/KMK/FG/kt_scene.png" cmd.status_bar_text = cmd.tooltip = item_text cmd.set_validation_proc { MF_ENABLED } cmd_array.push(cmd) tb = UI::Toolbar.new(menu_text) submenu = UI.menu("Plugins").add_submenu(menu_text) # [a] The context menu is built EACH time the user clicks the right-mouse button. UI.add_context_menu_handler do |pop| # You can wrap the following lines within # a conditional expression, to control # if your popup context menu is shown. subpop = pop.add_submenu(menu_text) cmd_array.each {|i| subpop.add_item(i) } end # [b] So the normal menu and toolbars must be registered separately! cmd_array.each do |i| tb.add_item(i) submenu.add_item(i) end tb.show if tb.get_last_state == -1 end # unless loaded file_loaded(__FILE__)
end # module plugin FG
end # module toplevel KMK`
-
The folder should begin with "KMK_", so would be "KMK_FG"
The extension registration script in the "Plugins" folder would be "KMK_FG.rb"
See the API page forSketchupExtension
class.Please use [ code ] [ /code ] blocks around your code like I did.
And properly indent code. -
Ok, Thanks, Dan
I will be able to fix it by myself )
Thank you!!
Advertisement