1

我正在尝试为 3D CAD 程序 Solid Edge 制作一个 Visual Basic 控制台应用程序,在这个控制台应用程序中,我希望 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
4

1 回答 1

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块,你只会对自己隐藏任何错误。

于 2012-11-15T16:42:50.210 回答