1

I'm trying to convert the Bodies from Parts to Parts in a Product in CATIA. I found a VBA script that works fine, but I have to translate it to C#.
The VBA script is the following:

Sub GreateProductsFromBodies_SelectAllBodies() 
   
    On Error Resume Next
    Set CATIA = GetObject(, "CATIA.Application")
    
    'Declare variables
    Dim oPartDoc As PartDocument
    Dim oPart As Part
    Dim oProductDoc As ProductDocument
    Dim oProduct As Product
    
    'Create a new ProductDoc and rename it's PartNumber equals to Partdoc's PartNumber
    Set oPartDoc = CATIA.ActiveDocument
    Set oProductDoc = CATIA.Documents.Add("Product")
    oProductDoc.Product.PartNumber = oPartDoc.Product.PartNumber
    
    'Arrange windows use "Title Vertically" ,then active window contain Partdoc
    CATIA.Windows.Arrange catArrangeTiledVertical
    CATIA.Windows.Item(1).Activate
    
    'Check the Body's name use "For ... Next"loop . If Body's name duplicate,then rename.
    Dim j As Integer, k As Integer
    For j = 1 To oPartDoc.Part.Bodies.Count
        For k = 1 To oPartDoc.Part.Bodies.Count
            If oPartDoc.Part.Bodies.Item(j).name = oPartDoc.Part.Bodies.Item(k).name And j <> k Then
                oPartDoc.Part.Bodies.Item(j).name = oPartDoc.Part.Bodies.Item(k).name & "_Rename_" & j
            End If
        Next
    Next
    
    'Copy Bodies from PartDocument
    Dim i As Integer, ProductPN As String, FinalProductPN As String
    For i = 1 To oPartDoc.Part.Bodies.Count
        With oPartDoc.Selection
            .Clear
            .Add oPartDoc.Part.Bodies.Item(i)
            .Copy
            .Clear
        End With
        
         'Modify the Product's PartNumber,replace "\" and "."to "_" ,then delete Space
        ProductPN = oPartDoc.Part.Bodies.Item(i).name
        If Right(ProductPN, 1) = "\" Then
            ProductPN = Left(ProductPN, Len(ProductPN) - 1)
        End If
        FinalProductPN = Replace(Replace(Replace(ProductPN, "\", "_"), ".", "_"), " ", "") 'Replace "\" and "."to "_",Delete Space
        
          'Paste Body in Product's Part as Result
        Set oProduct = oProductDoc.Product.Products.AddNewComponent("Part", FinalProductPN) 'Add Part
        With oProductDoc.Selection
            .Clear
            .Add oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part
            .PasteSpecial "CATPrtResultWithOutLink"
            .Clear
        End With
        oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part.Update
    Next
    
    'Use Msgbox to echo the complete flag
    MsgBox "All the select Bodies had been created as a PartDocument successfully !" & Chr(13) & _
    ">>> The Partdocument's Bodies's count : " & oPartDoc.Part.Bodies.Count & Chr(13) & _
    ">>> The ProductDocument's PartDocument's count : " & oProductDoc.Product.Products.Count, _
    vbOKOnly + vbInformation, "@LSY >>> CATIAVBAMacro of Part to Product >>> Run Result"      
End Sub

I translated every line, except the line:

oProductDoc.Selection.Add oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part

I found no corresponding property in C#, cause the last property Part is missing in C#.
I wrote:

oProductDoc.Selection.Add(oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.??????);

I'm very thankfull for every help!

GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

0

I solved it, if someone else is in this situation:

I had to cast the line as PartDocument, whish gives me the needed .Part Property!

Before the selection:

PartDocument partDoc = oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent as PartDocument;

And in the required line:

oProductDoc.Selection.Add(partDoc.Part);