首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Catia V5宏:不完全重命名函数

Catia V5宏:不完全重命名函数
EN

Stack Overflow用户
提问于 2018-04-26 16:03:54
回答 1查看 1.5K关注 0票数 0

我已经处理了很长一段时间,甚至得到了帮助,但我无法解决它。以下宏根据用户和PartName的不同而重命名InstanceName或CADSelection。

问题是它不适用于PartName更改。有人能帮我完成这个宏吗?最理想的解释我做错了什么?

代码语言:javascript
复制
Sub CATMain()

  If CATIA.Documents.Count = 0 Then
    MsgBox "There are no CATIA documents open. Please open a CATIA document and try again.", ,msgboxtext
    Exit Sub
  End If

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
    MsgBox "The active document is not a Product. Please open a CATIA Product and try again.", ,msgboxtext

    Exit Sub
  End If

  Dim oSelection As Selection
  Set oSelection = CATIA.ActiveDocument.Selection

  If oSelection.Count < 1 then
    MsgBox "Pick some components using cad selection."
  Else
    '****** Alter Instance Name *****'
    Dim msg
    msg = MsgBox ("Click ""Yes"" to change Instance Name, ""No"" to change Part Name or ""Cancel"" to exit", _
       vbYesNoCancel, "Renaming Tool")

    if vbYes = msg then
      '****** Inputbox for Instance name alteration *****
      Dim NewIName As String
      NewIName = InputBox("Please input the desired Instance Name. Example: E","Instance name alteration","E")
      '****** Inputbox for Instance number alteration *****
      Dim NewINumber As Integer
      NewINumber = InputBox("Please input the initial number for the 1st component. Example: 1","Instance numbering alteration","1")

      Dim oIBody
      Dim InstName As Body
      For oIBody = 1 to oSelection.Count
         Set InstName = oSelection.Item(oIBody).Value
         '****** Instance name alteration *****
         InstName.Parent.Parent.ReferenceProduct.Products.Item( _
                  InstName.Name).Name= NewIName + CStr(NewINumber)
         NewINumber=NewINumber+1
      Next
    elseif vbNo = msg then
      '****** Inputbox for Part name alteration *****
      Dim NewPName As String
      NewPName = InputBox("Please input the desired Part Name. Example: E","Part Name alteration","E")

      '****** Inputbox for Part number alteration *****
      Dim NewPNumber As Integer
      NewPNumber = InputBox("Please input the initial number for the 1st Component. Example: 1","Part numbering alteration","1")

      Dim oPBody
      Dim PartName As Body

      For oPBody = 1 to oSelection.Count
        Set PartName = oSelection.Item(oPBody).Value

        '****** Part name alteration *****
        PartName.ReferenceProduct.Name= NewPName + CStr(NewPNumber)
        NewPNumber=NewPNumber+1
      Next
    End If
  End If

  oSelection.Clear
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-26 16:22:15

部件“名称”实际上是部件号,并使用"PartNumber“属性进行更改。

所以试着改变

代码语言:javascript
复制
PartName.ReferenceProduct.Name= NewPName + CStr(NewPNumber)

代码语言:javascript
复制
PartName.ReferenceProduct.PartNumber= NewPName + CStr(NewPNumber) 

这不会影响文档名,除非您还没有保存您的部件。

还有什么:

1)变量命名令人困惑。您在一个地方将产品称为"InstName“,在另一个地方将"PartName”称为“PartName”。乍一看,我以为这些是字符串。使用oProduct不会让人感到困惑。

2)您似乎对用户预先选择了正确的类型很有信心。由于您在程序集中进行选择,所以可以使用Selection.item(i).LeafProduct,而不是使用Selection.Item(i).Value,它始终是所选对象的实例产品。即使用户选择了一个曲面,它也会返回包含所选曲面的实例产品。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50047508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档