我已经处理了一段时间,甚至有帮助,但我无法解决这个问题。
以下宏根据用户和CADSelection重命名PartName
或InstanceName
。
问题是它在PartName
更改中无效。
有人可以帮我完成这个宏吗?并且理想地解释我做错了什么?
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
答案 0 :(得分:0)
部分&#34;名称&#34;实际上是零件编号,并使用&#34; PartNumber&#34;进行更改。属性。
所以尝试改变
PartName.ReferenceProduct.Name= NewPName + CStr(NewPNumber)
到
PartName.ReferenceProduct.PartNumber= NewPName + CStr(NewPNumber)
除非您尚未保存零件,否则这不会影响文档名称。
还有什么:
1)您的变量命名令人困惑。你打电话给产品&#34; InstName&#34;在一个地方和&#34; PartName&#34;在另一个。乍一看,我认为那些是弦乐。使用oProduct会减少混乱。
2)您似乎确信用户已预先选择了正确的类型。由于您在程序集中进行选择,而不是使用Selection.Item(i).Value,您可以使用 Selection.item(i).LeafProduct ,它始终是所选对象的实例产品。即使用户选择表面,它也将返回包含所选表面的实例产品。