我已经处理了很长一段时间,甚至得到了帮助,但我无法解决它。以下宏根据用户和PartName的不同而重命名InstanceName或CADSelection。
问题是它不适用于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发布于 2018-04-26 16:22:15
部件“名称”实际上是部件号,并使用"PartNumber“属性进行更改。
所以试着改变
PartName.ReferenceProduct.Name= NewPName + CStr(NewPNumber)至
PartName.ReferenceProduct.PartNumber= NewPName + CStr(NewPNumber) 这不会影响文档名,除非您还没有保存您的部件。
还有什么:
1)变量命名令人困惑。您在一个地方将产品称为"InstName“,在另一个地方将"PartName”称为“PartName”。乍一看,我以为这些是字符串。使用oProduct不会让人感到困惑。
2)您似乎对用户预先选择了正确的类型很有信心。由于您在程序集中进行选择,所以可以使用Selection.item(i).LeafProduct,而不是使用Selection.Item(i).Value,它始终是所选对象的实例产品。即使用户选择了一个曲面,它也会返回包含所选曲面的实例产品。
https://stackoverflow.com/questions/50047508
复制相似问题