suwoodblender/ruby/ruby/SUWUnitContTool.rb

137 lines
4.1 KiB
Ruby

module SUWood
class SUWUnitContTool
def self.set_type(cont_type)
if cont_type == VSUnitCont_Zone
select = SUWimpl.selected_zone
if select.nil? || select.deleted?
Sketchup::set_status_text('请选择区域'); return
end
uid = select.get_attribute("sw", "uid")
oid = select.get_attribute("sw", "zid")
cp = -1
else
select = SUWimpl.selected_part
if select.nil? || select.deleted?
Sketchup::set_status_text('请选择部件'); return
end
uid = select.get_attribute("sw", "uid")
oid = select.get_attribute("sw", "pid")
cp = select.get_attribute("sw", "cp")
end
Sketchup.active_model.select_tool(new(cont_type, select, uid, oid, cp))
end
def initialize(cont_type, select, uid, oid, cp = -1)
@cont_type, @uid, @oid, @cp = cont_type, uid, oid, cp
@select = select
if cont_type == VSUnitCont_Zone
@tooltip = '请选择区域的面, 并指定对应的轮廓'
else#VSUnitCont_Work
@tooltip = '请选择板件的面, 并指定对应的轮廓'
end
end
def activate
Sketchup.set_status_text(@tooltip)
end
def onMouseMove(flags, x, y, view)
@ref_face = nil
@face_segs = nil
ref_face = nil
xypicker = view.pick_helper
xypicker.do_pick(x, y)
xypicker.count.times do |i|
path = xypicker.path_at(i)
if path[-1].class == Sketchup::Face && !@select.entities.include?(path[-1])
ref_face = path[-1]
end
end
if ref_face
face_pts = ref_face.outer_loop.vertices.map(&:position)
@ref_face = ref_face
@face_segs = face_pts.zip(face_pts.rotate)
view.invalidate
end
Sketchup.set_status_text(@tooltip)
view.invalidate
end
def onLButtonDown(flags, x, y, view)
if @ref_face.nil?
UI.messagebox('请选择轮廓')
return
end
myself = false
depth = 0
arced = true
case @cont_type
when VSUnitCont_Zone then
return if UI.messagebox('是否确定创建区域轮廓?', MB_YESNO) == IDNO
when VSUnitCont_Part
return if UI.messagebox('是否确定创建部件轮廓?', MB_YESNO) == IDNO
when VSUnitCont_Work
arcs = @ref_face.edges.select{|edge| edge.curve.is_a?(Sketchup::ArcCurve)}
if arcs.empty?
prompts = ["表面", "深度"]
values = ["当前", 0]
options = ["当前|反面", ""]
inputs = UI.inputbox(prompts, values, options, '挖洞轮廓')
return if inputs == false
myself = inputs[0] == "当前"
depth = inputs[1] if inputs[1] > 0
else
prompts = ["表面", "深度", "圆弧"]
values = ["当前", 0, "圆弧"]
options = ["当前|反面", "", "圆弧|多段线"]
inputs = UI.inputbox(prompts, values, options, '挖洞轮廓')
return if inputs == false
myself = inputs[0] == "当前"
depth = inputs[1] if inputs[1] > 0
arced = inputs[2] == "圆弧"
end
end
params = {}
params.store("method", SUUnitContour)
params.store("type", @cont_type)
params.store("uid", @uid)
params.store("oid", @oid)
params.store("cp", @cp)
params.store("face", @ref_face.to_json(nil, 1, arced))
params.store("self", myself)
params.store("depth", depth)
set_cmd("r00", params)
edges = []
@ref_face.edges.each {|edge|
if edge.faces.length == 1
edges << edge
end
}
@ref_face.erase!
@ref_face = nil
edges.each {|edge|
if edge.valid?
edge.erase!
end
}
@face_segs = nil
view.invalidate
Sketchup.active_model.selection.clear
Sketchup.active_model.select_tool(nil)
end
def draw(view)
if @face_segs
view.drawing_color = Sketchup::Color.new(0, 255, 255)
view.line_width = 3
view.draw2d(GL_LINES, @face_segs.flat_map{|seg| seg.map{|pt| view.screen_coords(pt)}})
end
end
end
end