【发布时间】:2016-03-28 19:31:11
【问题描述】:
已解决 - 它发生在名称分配(见下面的新代码)
原因 - 试图重命名为相同的名称,因此它会自动为我区分
解决方法——使用 IF 语句查看名称是否相同
感谢 Jim Foltz 的解决方案
新代码
if from != newname
name = Sketchup.active_model.selection[0].definition.name=newname
end
插件用于木工 - 假设每个组件都是单独的板。 选择一个组件 - 运行插件。 在“实体信息”窗口中附加组件“定义”字段,包括零件厚度、宽度和长度(英寸)
示例:
运行前的组件名称 = [中心面板]
运行后的组件名称 = [中心面板:3/4" x 11 7/8" x 75 1/4"]
*********** 问题***********
后续运行后的组件名称 = [中心面板:3/4" x 11 7/8" x 75 1/4"#1]
(将 #1 附加到字符串)
第一次运行良好。如果我在同一个组件上再次运行插件,它会在组件名称后附加“#1”。每次后续运行都会在字符串中切换“#1”
如果存在“并且”与组件具有相同的名称,插件还将更新关联的 TEXT LABEL。文本标签字符串没有这个问题。
前半部分代码为原创 后半部分是新代码(如第 62 行注释)
require 'sketchup.rb'
module JF
module GetDimensions
module_function
def get_dimensions
model = Sketchup.active_model
selection = model.selection
### show VCB and status info...
Sketchup::set_status_text(("DIMENSIONS..." ), SB_PROMPT)
Sketchup::set_status_text(" ", SB_VCB_LABEL)
Sketchup::set_status_text(" ", SB_VCB_VALUE)
### Get Selected Entities.
return unless selection.length == 1
e = selection[0]
return unless e.respond_to?(:transformation)
scale_x = ((Geom::Vector3d.new 1,0,0).transform! e.transformation).length
scale_y = ((Geom::Vector3d.new 0,1,0).transform! e.transformation).length
scale_z = ((Geom::Vector3d.new 0,0,1).transform! e.transformation).length
bb = nil
if e.is_a? Sketchup::Group
bb = Geom::BoundingBox.new
e.entities.each {|en| bb.add(en.bounds) }
elsif e.is_a? Sketchup::ComponentInstance
bb = e.definition.bounds
end
if bb
dims = [
width = bb.width * scale_x,
height = bb.height * scale_y,
depth = bb.depth * scale_z
]
# Original messagebox - commented out by kp
# UI.messagebox("Largeur:\t#{dims[0].to_l}\nHauteur:\t#{dims[1].to_l}\nProfondeur:\t#{dims[2].to_l}")
end
# Newly added code follows
# UI.messagebox("Thickness:\t#{dims.sort[0].to_l}\nWidth:\t#{dims.sort[1].to_l}\nHeight:\t#{dims.sort[2].to_l}")
# Added by kp - translated and sorted dims smallest to largest
# Retrieved Component Name for processing
# Checked to see if This component has already been modified
# If already modified then strip off previous modification to prepare for current modification
# Modify component name
# New code - added by kp
name = Sketchup.active_model.selection[0].definition.name
from = name
vpos = name.index(":")
# The following "IF" code could be improved but
# it worked around an 'array vs string' error
# I was getting when I tried a different method
if vpos.nil?
vpos = 0
elsif vpos>0
tempname = name.partition(':').first
name = tempname
end
newname = name + ": #{dims.sort[0].to_l} x #{dims.sort[1].to_l} x #{dims.sort[2].to_l}"
name = Sketchup.active_model.selection[0].definition.name=newname
to = newname
# Update the TEXT LABEL
model.entities.grep(Sketchup::Text) { |text|
self.replace(text, from, to)
}
# End of new code - by kp
end
def self.replace(text_entity, from, to)
text_entity.text = text_entity.text.gsub(from, to)
end
end
end
### do menu
if( not file_loaded?("kp_ get_dimensions.rb") )
add_separator_to_menu("Plugins")
# menu_name = "[jf] Get Dimensions"
menu_name = "[kp] Get Dimensions"
UI.menu("Plugins").add_item(menu_name) { JF::GetDimensions.get_dimensions }
end#if
file_loaded("kp_ get_dimensions.rb")
【问题讨论】:
-
这绝对是奇怪的行为。顺便说一句,你看起来像个不错的程序员,不要卖空自己。一般来说,我建议使用像
pry这样的调试器并单步执行您的代码,这将为您节省大量时间。 -
请将问题作为答案而不是问题的一部分来回答。如果合理,您可以自行回答。