CATIA VBA 宏错误 - 无效的过程调用或参数

问题描述 投票:0回答:1

我正在 CATIA 中编写一个宏,提示用户选择圆柱面。该宏的目的是从选定的面创建一条轴线。但是,我在执行过程中遇到以下错误:

Set hybridShapeAxisLine1 = hybridShapeFactory1.AddNewAxisLine(reference1)
这行代码引发“无效的过程调用或参数”错误。我怀疑这个问题可能与reference1变量没有持有有效的引用有关。

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

尝试:

  1. 我尝试过选择不同的圆柱面,但问题仍然存在。
  2. 我添加了一个消息框来检查引用1的类型,但它无效。

帮助请求:

  1. CreateReferenceFromObject 的正确使用方法是什么? 2.是否有其他方法可以从圆柱面创建轴线?
vba surface cad catia
1个回答
0
投票

问题是,使用

CreateReferenceFromObject
创建参考仅对树中的特征有效。您的选择将返回 BREP(边界表示)。 (检查
cylindricalFace.Name
的值)

要从 BREP 创建参考,您必须使用

CreateReferenceFromBRepName
。必须修改您从选择中获得的字符串才能创建引用。

这可以通过以下辅助函数来完成:

'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

创建参考的示例:

Dim strBrep as String
strBrep = GetBrep(cylindricalFace.Name)
Set reference1 = part.CreateReferenceFromBRepName(strBrep ,cylindricalFace.parent)
© www.soinside.com 2019 - 2024. All rights reserved.