Search code examples
vbacatia

Catia V5 Macro: Incomplete renaming function


I've been dealing with this a while and even had help but i can't work it out. The following macro renames PartName or InstanceName depending on user and CADSelection.

Problem is it's not working in PartName alteration. Can someone help me complete this macro? and ideally explain what i did incorrectly?

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

Solution

  • The part "name" is really the Part Number and is changed using the "PartNumber" property.

    So try changing

    PartName.ReferenceProduct.Name= NewPName + CStr(NewPNumber)
    

    to

    PartName.ReferenceProduct.PartNumber= NewPName + CStr(NewPNumber) 
    

    This doesn't influence the document name unless you have not saved your part already.

    What else :

    1) Your variable naming is confusing. You call the Product "InstName" in one place and "PartName" in another. At first glance I thought those were strings. Using oProduct would be less confusing.

    2) You seem real confident that the user has pre-selected the correct types. Since you are selecting in an assembly, instead of using Selection.Item(i).Value, you can use Selection.item(i).LeafProduct which will always be the instance product of whatever object is selected. Even if the user picks a surface, it will return the instance product which contains the selected surface.