VB.NET中的CATIA自动化

时间:2016-10-18 21:54:17

标签: vb.net catia

我正在尝试使用VB.NET创建一个圆圈。但是,当我运行下面的代码时,我得到此错误“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”我可以更改行和点的属性而没有任何问题,这只是一个问题当我试图画圆圈时。有谁知道发生了什么?

Dim inCircle As HybridShapeCircleTritangent

Dim testHB As MECMOD.HybridBody

testHB = part1.FindObjectByName("Test HB")


inCircle = hsf.AddNewCircleTritangent(part1.FindObjectByName("Line.1"), part1.FindObjectByName("Line.2"), part1.FindObjectByName("Line.3"), part1.FindObjectByName("Surf"), -1, 1, -1)
inCircle.DiscriminationIndex = 1
inCircle.BeginOfCircle = 1
inCircle.TangentOrientation1 = -1
inCircle.TangentOrientation2 = 1
inCircle.TangentOrientation3 = -1

inCircle.SetLimitation(1)
testHB.AppendHybridShape(inCircle)

2 个答案:

答案 0 :(得分:0)

您是否尝试为圈子的输入项创建参考? 试试这个:

Dim reference2 As Reference
Set reference2 = part1.CreateReferenceFromObject(part1.FindObjectByName("Line.1"))

并使用创建的引用

答案 1 :(得分:0)

@Matheus Titarelli对于需要引用线条和曲面是正确的。我不会直接调用CreateReferenceFromObject中的FindObjectByName,而是首先将局部变量设置为找到的行(如果存在),然后创建引用。

@A。 Gharbi我在C#中完成了所有Catia开发,所以很遗憾我没有VB.net的工作版本,我可以与你共享代码,但我确实有一个我使用的C#版本。我确实对VB.net代码进行了粗略的翻译,但没有测试过。

VB.net代码(未经测试):

'--------------------------------------------------------------
Public Shared Function GetHybridShapeFactory(iPart As MECMOD.Part) As HybridShapeTypeLib.HybridShapeFactory
    If iPart Is Nothing Then
        Return Nothing
    End If

    Dim oHSF As HybridShapeTypeLib.HybridShapeFactory = Nothing
    Try
        oHSF = DirectCast(iPart.HybridShapeFactory, HybridShapeTypeLib.HybridShapeFactory)
    Catch ex As Exception
        Return Nothing
    End Try
    Return oHSF
End Function


'--------------------------------------------------------------
Public Shared Function AddNewCircleTritangent(ByRef iPart As MECMOD.Part, 
                                              ByRef iHB As MECMOD.HybridBody, 
                                              ByRef iLine1 As INFITF.AnyObject, 
                                              ByRef iLine2 As INFITF.AnyObject, 
                                              ByRef iLine3 As INFITF.AnyObject, 
                                              ByRef iSurface As INFITF.AnyObject, 
                                              ByVal iOri1 As Integer, 
                                              ByVal iOri2 As Integer, 
                                              ByVal iOri3 As Integer) As HybridShapeTypeLib.HybridShapeCircleTritangent

    If (iPart Is Nothing) Or (iHB Is Nothing) Or
       (iLine1 Is Nothing) Or (iLine2 Is Nothing) Or
       (iLine3 Is Nothing) Or (iSurface Is Nothing) Then
        Return Nothing
    End If

    Dim HSF As HybridShapeTypeLib.HybridShapeFactory = Nothing
    HSF = GetHybridShapeFactory(iPart)
    If HSF Is Nothing Then
        Return Nothing
    End If

    ' Get the Reference of the first line
    Dim refLine1 As INFITF.Reference = Nothing
    Try
        refLine1 = iPart.CreateReferenceFromObject(iLine1)
    Catch ex As Exception
        Return Nothing
    End Try
    If refLine1 Is Nothing Then
        Return Nothing
    End If

    ' Get the Reference of the second line
    Dim refLine2 As INFITF.Reference = Nothing
    Try
        refLine2 = iPart.CreateReferenceFromObject(iLine2)
    Catch ex As Exception
        Return Nothing
    End Try
    If refLine2 Is Nothing Then
        Return Nothing
    End If

    ' Get the Reference of the third line
    Dim refLine3 As INFITF.Reference = Nothing
    Try
        refLine3 = iPart.CreateReferenceFromObject(iLine3)
    Catch ex As Exception
        Return Nothing
    End Try
    If refLine3 Is Nothing Then
        Return Nothing
    End If

    ' Get the Reference of the support surface
    Dim refSurf As INFITF.Reference = Nothing
    Try
        refSurf = iPart.CreateReferenceFromObject(iSurface)
    Catch ex As Exception
        Return Nothing
    End Try
    If refSurf Is Nothing Then
        Return Nothing
    End If

    ' Create the object, add it the Geometric set, and update the part.
    Dim HSCTT As HybridShapeTypeLib.HybridShapeCircleTritangent = Nothing
    Try
        HSCTT = DirectCast(HSF.AddNewCircleTritangent(refLine1, refLine2, refLine3, refSurf, iOri1, iOri2, iOri3), HybridShapeTypeLib.HybridShapeCircleTritangent)
        iHB.AppendHybridShape(HSCTT)
        iPart.InWorkObject = HSCTT
        iPart.Update()
    Catch ex As Exception
        Return Nothing
    End Try

    Return HSCTT
End Function

C#代码:

//--------------------------------------------------------------
public static HybridShapeTypeLib.HybridShapeFactory GetHybridShapeFactory(MECMOD.Part iPart)
{
    if (null == iPart) 
    {
        return null; 
    }

    HybridShapeTypeLib.HybridShapeFactory oHSF = null;
    try
    {
        oHSF = (HybridShapeTypeLib.HybridShapeFactory)iPart.HybridShapeFactory;
    }
    catch (Exception)
    { 
        return null;
    }
    return oHSF;
}

//--------------------------------------------------------------
public static HybridShapeTypeLib.HybridShapeCircleTritangent AddNewCircleTritangent(MECMOD.Part iPart,
                                                                                    MECMOD.HybridBody iHB,
                                                                                    INFITF.AnyObject iLine1,
                                                                                    INFITF.AnyObject iLine2,
                                                                                    INFITF.AnyObject iLine3,
                                                                                    INFITF.AnyObject iSurface,
                                                                                    int iOri1,
                                                                                    int iOri2,
                                                                                    int iOri3)
{
    if ((null == iPart) || (null == iHB) || 
        (null == iLine1) || (null == iLine2) || 
        (null == iLine3) || (null == iSurface))
    {
        return null;
    }

    HybridShapeTypeLib.HybridShapeFactory HSF = null;
    HSF = GetHybridShapeFactory(iPart);
    if (null == HSF)
    {
        return null;
    }

    // Get the Reference of the first line
    INFITF.Reference refLine1 = null;
    try
    {
        refLine1 = iPart.CreateReferenceFromObject(iLine1);
    }
    catch (Exception)
    {
        return null;
    }
    if (null == refLine1)
    {
        return null;
    }

    // Get the Reference of the second line
    INFITF.Reference refLine2 = null;
    try
    {
        refLine2 = iPart.CreateReferenceFromObject(iLine2);
    }
    catch (Exception)
    {
        return null;
    }
    if (null == refLine2)
    {
        return null;
    }

    // Get the Reference of the third line
    INFITF.Reference refLine3 = null;
    try
    {
        refLine3 = iPart.CreateReferenceFromObject(iLine3);
    }
    catch (Exception)
    {
        return null;
    }
    if (null == refLine3)
    {
        return null;
    }

    // Get the Reference of the support surface
    INFITF.Reference refSurf = null;
    try
    {
        refSurf = iPart.CreateReferenceFromObject(iSurface);
    }
    catch (Exception)
    {
        return null;
    }
    if (null == refSurf)
    {
        return null;
    }

    // Create the object, add it the Geometric set, and update the part.
    HybridShapeTypeLib.HybridShapeCircleTritangent HSCTT = null;
    try
    {
        HSCTT = (HybridShapeTypeLib.HybridShapeCircleTritangent)HSF.AddNewCircleTritangent(refLine1, 
                                                                                           refLine2, 
                                                                                           refLine3,
                                                                                           refSurf, 
                                                                                           iOri1,
                                                                                           iOri2, 
                                                                                           iOri3);
        iHB.AppendHybridShape(HSCTT);
        iPart.InWorkObject = HSCTT;
        iPart.Update();
    }
    catch (Exception)
    {
        return null;
    }

    return HSCTT;
}