vbasurfacecadcatia

CATIA VBA Macro Error - Invalid Procedure Call or Argument


I am writing a macro in CATIA that prompts the user to select a cylindrical face. The purpose of this macro is to create an axis line from the selected face. However, I am encountering the following error during execution:

Set hybridShapeAxisLine1 = hybridShapeFactory1.AddNewAxisLine(reference1) This line of code raises an "Invalid procedure call or argument" error. I suspect that the issue may be related to the reference1 variable not holding a valid reference.

Sub SelectCylindricalFaceAndCreateAxis()
    Dim objSel As selection
    Dim objSelLB As Object
    Dim strReturn As String
    Dim varFilter(0) As Variant
    Dim cylindricalFace As Face
    Dim partDocument As Document
    Dim part As part
    Dim hybridBody1 As HybridBody
    Dim hybridShapeFactory1 As hybridShapeFactory
    Dim hybridShapeAxisLine1 As HybridShapeAxisLine
    Dim reference1 As reference

    ' Aktif belgeyi al
    Set objSel = CATIA.ActiveDocument.selection
    Set objSelLB = objSel
    Set partDocument = CATIA.ActiveDocument
    Set part = partDocument.part

    ' Kullanıcıya talimat veren mesaj kutusu
    MsgBox "Select the first cylindrical face of the tube. Then press the finish button in the tools palette toolbar."

    ' Seçim işlemi için filtreyi belirle
    varFilter(0) = "CylindricalFace"
    objSel.Clear

    ' Seçim işlemi
    strReturn = objSelLB.SelectElement3(varFilter, "Select a cylindrical face...", False, CATMultiSelTriggWhenUserValidatesSelection, False)

    ' Seçilen silindirik yüzeyi kontrol et
    If strReturn <> "Cancel" Then
        Set cylindricalFace = objSel.Item(1).Value ' Seçilen silindirik yüzey
        
        ' Yüzeyin türünü kontrol et
        MsgBox "Seçilen yüzeyin türü: " & TypeName(cylindricalFace)
        
        ' Yeni bir geometrik set oluştur
        Set hybridBody1 = part.hybridBodies.Add()
        hybridBody1.Name = "CenterLine"

        ' Seçilen silindirik yüzeyden referans oluştur
        On Error Resume Next ' Hata oluşursa devam et
        Set reference1 = part.CreateReferenceFromObject(cylindricalFace)
        On Error GoTo 0 ' Hata kontrolünü kapat

        ' Referansın geçerli olup olmadığını kontrol et
        If reference1 Is Nothing Then
            MsgBox "Referans oluşturulamadı. Lütfen silindirik yüzeyi kontrol edin."
            Exit Sub
        End If

        ' Seçilen silindirik yüzeyden eksen çizgisi oluştur
        Set hybridShapeFactory1 = part.hybridShapeFactory

        ' Hata ayıklama: referans türünü kontrol et
        MsgBox "Referans türü: " & TypeName(reference1)

        On Error Resume Next
        Set hybridShapeAxisLine1 = hybridShapeFactory1.AddNewAxisLine(reference1)

        ' Hata kontrolü
        If Err.Number <> 0 Then
            MsgBox "Hata: " & Err.Description
            On Error GoTo 0
            Exit Sub
        End If
        On Error GoTo 0

        hybridShapeAxisLine1.AxisLineType = 1 ' Eksen çizgisinin türünü ayarla

        ' Eksen çizgisini geometrik sete ekle
        hybridBody1.AppendHybridShape hybridShapeAxisLine1

        part.InWorkObject = hybridShapeAxisLine1 ' Parça için geçerli nesneyi ayarla
        part.Update ' Parçayı güncelle
    Else
        MsgBox "Seçim iptal edildi."
    End If
End Sub

Attempts:

  1. I have tried selecting different cylindrical faces, but the issue persists.
  2. I added a message box to check the type of reference1, but it is not valid.

Help Request:

  1. What is the correct way to use CreateReferenceFromObject? 2.Is there an alternative method to create an axis line from a cylindrical face?

Solution

  • The problem is, creating a reference using CreateReferenceFromObject is only valid for features in the tree. Your selection returns a BREP (boundary representation). (check the value of cylindricalFace.Name)

    For creating a reference from a BREP you have to use CreateReferenceFromBRepName. The string you got from selecting has to be modified for creating a reference.

    This can be don with following helper function:

    'credits to: https://www.grozeaion.com/catia-v5/catia-v5-programming/catia-v5-macro-useful-functions
    Public Function GetBrep(MyBRepName As String) As String
        MyBRepName = Replace(MyBRepName, "Selection_", "")
        MyBRepName = Left(MyBRepName, InStrRev(MyBRepName, "));"))
        MyBRepName = MyBRepName & ");WithPermanentBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)"
        '");WithTemporaryBody;WithoutBuildError;WithInitialFeatureSupport;MonoFond;MFBRepVersion _CXR14)"
        GetBrep = MyBRepName
    End Function
    

    Example for creating the reference:

    Dim strBrep as String
    strBrep = GetBrep(cylindricalFace.Name)
    Set reference1 = part.CreateReferenceFromBRepName(strBrep ,cylindricalFace.parent)