Застрял в программировании этого консольного приложения для Solid Edge

Я пытаюсь создать визуальное базовое консольное приложение для 3D-программы САПР Solid Edge, в этом консольном приложении я хочу, чтобы Visual Basic делал в нем кривые. Я хочу сделать эти кривые с помощью уравнения 4-й степени. Данные, используемые для уравнения, хранятся в текстовом файле с разделителями-пробелами. В текстовом файле есть несколько строк с разными данными, каждая строка используется для составления уравнения 4-й степени. Теперь я попытался сделать программу, но она застряла на создании кривых линий, консоль просто остается черной и не закрывается. Программа подключается к программе и открывает среду 3D-моделирования.

Кто-нибудь знает, что я делаю неправильно в своем коде или что мне делать вместо этого? Я использую стандарт Visual Basic 2003 года. Я не программист, но я пытаюсь автоматизировать работу программы 3D CAD.

Вот мой код:

Imports System.IO
Imports System.Runtime.InteropServices
Module Module1
    Sub Main()

        Dim strFileName As String = "U:\pompen.prn"
        Dim objFS As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
        Dim objSR As New StreamReader(objFS)

        Dim objApp As SolidEdgeFramework.Application = Nothing
        Dim objDocuments As SolidEdgeFramework.Documents = Nothing
        Dim objPart As SolidEdgePart.PartDocument = Nothing
        Dim objProfileSets As SolidEdgePart.ProfileSets = Nothing
        Dim objProfileSet As SolidEdgePart.ProfileSet = Nothing
        Dim objProfiles As SolidEdgePart.Profiles = Nothing
        Dim objProfile As SolidEdgePart.Profile = Nothing
        Dim objRefplanes As SolidEdgePart.RefPlanes = Nothing
        Dim objdraft2d As SolidEdgeDraft.CoordinateSystem2d = Nothing
        Dim objBSpline As SolidEdgeFrameworkSupport.BSplineCurve2d = Nothing
        Dim objBSplines As SolidEdgeFrameworkSupport.BSplineCurves2d = Nothing
        Dim objLines2d As SolidEdgeFrameworkSupport.Lines2d = Nothing
        Dim objLine2d As SolidEdgeFrameworkSupport.Line2d = Nothing
        Dim objsketch As SolidEdgePart.Sketch = Nothing

        Dim strEmpRecord As String
        Dim type As String
        Dim N1 As Double
        Dim QT As Double
        Dim DW1 As Decimal
        Dim EQ As Decimal
        Dim EH As Decimal
        Dim DWL As Decimal
        Dim DHL As Decimal
        Dim C1 As Decimal
        Dim C2 As Decimal
        Dim C3 As Decimal
        Dim C4 As Decimal
        Dim C5 As Decimal
        Dim QFACTOR As Decimal


        Do While objSR.Peek <> -1
            ' read the current record (line) into the variable strEmpRecord
            strEmpRecord = objSR.ReadLine
            Try
                ' break up the record into separate variables
                type = strEmpRecord.Substring(0, 20)
                N1 = CDbl(strEmpRecord.Substring(20, 10))
                QT = CDbl(strEmpRecord.Substring(30, 10))
                DW1 = CDec(strEmpRecord.Substring(40, 10))
                EQ = CDec(strEmpRecord.Substring(50, 10))
                EH = CDec(strEmpRecord.Substring(60, 10))
                DHL = CDec(strEmpRecord.Substring(70, 10))
                DWL = CDec(strEmpRecord.Substring(80, 10))
                C1 = CDec(strEmpRecord.Substring(90, 20))
                C2 = CDec(strEmpRecord.Substring(110, 20))
                C3 = CDec(strEmpRecord.Substring(130, 20))
                C4 = CDec(strEmpRecord.Substring(150, 20))
                C5 = CDec(strEmpRecord.Substring(170, 20))
                QFACTOR = CDec(strEmpRecord.Substring(190, 10))
            Catch ex As System.InvalidCastException
            End Try

            'connect to a Solid Edge file.
            ' Connect to a running instance of Solid Edge 
            objApp = Marshal.GetActiveObject("SolidEdge.Application")
            ' Get a reference to the documents collection 
            objDocuments = objApp.Documents
            ' Create a new part document 
            objPart = objDocuments.Add("SolidEdge.PartDocument")
            ' Get a reference to the profile sets collection 
            objProfileSets = objPart.ProfileSets
            ' Add a new profile set 
            objProfileSet = objProfileSets.Add()
            ' Get a reference to the profiles collection 
            objProfiles = objProfileSet.Profiles
            ' Get a reference to the ref planes collection 
            objRefplanes = objPart.RefPlanes
            'open a new sketch
            objsketch = objPart.Sketches.Add
            ' Add a new profile 
            objProfile = objProfiles.Add(objRefplanes.Item(3))
            'Gat a reference to spline curve collection
            objBSplines = objProfile.BSplineCurves2d
            ' Get a reference to the lines2d collection 
            objLines2d = objProfile.Lines2d

            Try
                'Draw the curves with 4th degree equation, DMax
                Dim q As Decimal = (QT * 0.2)
                Dim h As Decimal = (((q ^ 4) * C1) + ((q ^ 3) * C2) + ((q ^ 2) * C3) + (q * C4) + C5)
                Do
                    q = (q + 0.5)
                Loop Until q <= (QT * QFACTOR)
                objBSpline = objBSplines.objsr.AddByPoints(q, h)

                'Draw the curves with 4th degree equation, DMin
                Dim qx As Decimal = ((DWL / DW1) ^ EQ)
                Dim hx As Decimal = ((DWL / DW1) ^ EH)
                Dim qa As Decimal = (qx * q)
                Dim ha As Decimal = (hx * (((q ^ 4) * C1) + ((q ^ 3) * C2) + ((q ^ 2) * C3) + (q * C4) + C5))
                Do
                    qa = (qa + 0.5)
                Loop Until qa = (qx * (QT * QFACTOR))
                objBSpline = objBSplines.objsr.addbypoints(qa, ha)

                'make connection lines
                objLine2d = objLines2d.AddBy2Points((qx * (QT * QFACTOR)), (QFACTOR * (hx * (((q ^ 4) * C1) + ((q ^ 3) * C2) + ((q ^ 2) * C3) + (q * C4) + C5))), (QT * QFACTOR), (QFACTOR * (((q ^ 4) * C1) + ((q ^ 3) * C2) + ((q ^ 2) * C3) + (q * C4) + C5)))

                ' Close the profile 
                objProfile.End( _
                    SolidEdgePart.ProfileValidationType.igProfileClosed)

            Catch ex As Exception

            Finally
                If Not (objLine2d Is Nothing) Then
                    Marshal.ReleaseComObject(objLine2d)
                    objLine2d = Nothing
                End If
                If Not (objLines2d Is Nothing) Then
                    Marshal.ReleaseComObject(objLines2d)
                    objLines2d = Nothing
                End If
                If Not (objBSpline Is Nothing) Then
                    Marshal.ReleaseComObject(objBSpline)
                    objBSpline = Nothing
                End If
                If Not (objRefplanes Is Nothing) Then
                    Marshal.ReleaseComObject(objRefplanes)
                    objRefplanes = Nothing
                End If
                If Not (objProfile Is Nothing) Then
                    Marshal.ReleaseComObject(objProfile)
                    objProfile = Nothing
                End If
                If Not (objProfiles Is Nothing) Then
                    Marshal.ReleaseComObject(objProfiles)
                    objProfiles = Nothing
                End If
                If Not (objProfileSet Is Nothing) Then
                    Marshal.ReleaseComObject(objProfileSet)
                    objProfileSet = Nothing
                End If
                If Not (objProfileSets Is Nothing) Then
                    Marshal.ReleaseComObject(objProfileSets)
                    objProfileSets = Nothing
                End If
                If Not (objPart Is Nothing) Then
                    Marshal.ReleaseComObject(objPart)
                    objPart = Nothing
                End If
                If Not (objDocuments Is Nothing) Then
                    Marshal.ReleaseComObject(objDocuments)
                    objDocuments = Nothing
                End If
                If Not (objsketch Is Nothing) Then
                    Marshal.ReleaseComObject(objsketch)
                    objsketch = Nothing
                End If
                If Not (objApp Is Nothing) Then
                    Marshal.ReleaseComObject(objApp)
                    objApp = Nothing
                End If
            End Try
        Loop

        objSR.Close()
        Console.WriteLine("")
        Console.WriteLine("Press Enter to close this window.")
        Console.ReadLine()

    End Sub
End Module

person Community    schedule 15.11.2012    source источник


Ответы (1)


Я предполагаю, что вы должны переместить этот блок кода за пределы вашего цикла, т.е. перед Do While objSR.Peek <> -1. В цикле он будет делать все эти вещи для каждой строки в вашем файле. Я бы подумал, что вы захотите сделать это только один раз.

      'connect to a Solid Edge file.
        ' Connect to a running instance of Solid Edge 
        objApp = Marshal.GetActiveObject("SolidEdge.Application")
        ' Get a reference to the documents collection 
        objDocuments = objApp.Documents
        ' Create a new part document 
        objPart = objDocuments.Add("SolidEdge.PartDocument")
        ' Get a reference to the profile sets collection 
        objProfileSets = objPart.ProfileSets
        ' Add a new profile set 
        objProfileSet = objProfileSets.Add()
        ' Get a reference to the profiles collection 
        objProfiles = objProfileSet.Profiles
        ' Get a reference to the ref planes collection 
        objRefplanes = objPart.RefPlanes
        'open a new sketch
        objsketch = objPart.Sketches.Add
        ' Add a new profile 
        objProfile = objProfiles.Add(objRefplanes.Item(3))
        'Gat a reference to spline curve collection
        objBSplines = objProfile.BSplineCurves2d
        ' Get a reference to the lines2d collection 
        objLines2d = objProfile.Lines2d

Тогда вам, вероятно, придется переместить это после вашего цикла,

            ' Close the profile 
            objProfile.End( _
                SolidEdgePart.ProfileValidationType.igProfileClosed)

Просто совет, вы должны удалить эту строку

  Catch ex As Exception

Если у вас есть пустой блок catch, вы просто скроете от себя все ошибки.

person Kratz    schedule 15.11.2012
comment
Эй, спасибо за авзер! Это решило проблему, которая у меня была, но все еще не делало кривые. Я думаю, что это должно быть в той части, в которой я хочу сделать их с уравнением 4-й степени. Чтобы сделать линию с помощью метода AddByPoints, мне нужно отказаться от массива. Любые идеи, как мои расчетные данные с уравнением 4-й степени могут быть установлены в массив? - person ; 16.11.2012