2016-10-18 4 views
0

Я пытаюсь создать круг с помощью VB.NET. Однако, когда я запускаю код ниже, я получаю эту ошибку «Попытка прочитать или записать защищенную память. Это часто свидетельствует о том, что другая память повреждена». Я могу без проблем изменять свойства строк и точек, это только проблема когда я пытаюсь рисовать круги. Кто-нибудь есть идея, что происходит?CATIA automation в 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) 

ответ

0

Вы пытались создать ссылки на входные элементы круга? Попробуйте это:

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

и использовать ссылки, созданные

0

@Matheus Titarelli правильно о необходимости ссылки на линии и поверхности. Я бы не стал напрямую ссылаться на FindObjectByName в CreateReferenceFromObject, вместо этого я бы вместо этого установил локальную переменную как найденную строку (если она существует), а затем создала ссылку.

@A. Gharbi Я делаю всю свою разработку Catia на C#, так грустно. У меня нет рабочей версии 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; 
} 
+0

Спасибо за ответ! Я попробовал это, и это не сработало. Мне показалось, что проблема связана с привилегиями пользователя. По какой-то причине я не могу получить доступ к некоторым свойствам, если я не админ. Я не понимаю, почему ... –

Смежные вопросы