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 に答える