字符串更新结果意外地在两个值之间切换

时间:2016-03-28 19:31:11

标签: ruby string sketchup

已解决 - 它发生在名称分配上(见下面的新代码)

原因 - 尝试重命名为同名,以便自动为我区分

解决方案 - 使用IF语句查看名称是否相同

感谢Jim Foltz的解决方案

新代码

if from != newname
name = Sketchup.active_model.selection[0].definition.name=newname
end

插件用于木工 - 假设每个组件都是单独的板。 选择一个组件 - 运行插件。 在“实体信息”窗口中添加COMPONENT“定义”字段,其中包含零件厚度,宽度和长度(英寸)

实施例

运行前的组件名称= [中心面板]

运行后

组件名称= [中心面板: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")

1 个答案:

答案 0 :(得分:0)

我很抱歉没有发布解决方案iaw论坛标准。弹出建议让我感到困惑(很容易做到)。

如果不清楚 - 我编辑了原帖,以表明问题的解决方案。为清楚起见,我将在下面显示它。

当组件的新名称与旧名称相同时,系统(Ruby API,我猜?)会通过附加#1后缀来区分我的名称。

这是原始代码行问题

name = Sketchup.active_model.selection[0].definition.name=newname

这是解决方案 - 验证名称不一样。

if from != newname
name = Sketchup.active_model.selection[0].definition.name=newname
end

我无法通过个人电子邮件向我发送解决方案投票。