ArcMap中用VBA导出当前窗口图形到DXF文件

'输出DXF文件所用变量//
Public g_doc As IMxDocument
Public OutputFile As String
Public layer_Current As String
   


Private Sub 导出DXF_Click()

   Dim pActiveView As IActiveView
   Set g_doc = Application.Document
   Set pActiveView = g_doc.FocusMap
   
   If g_doc.FocusMap.LayerCount = 0 Then
       MsgBox "当前视图没有加载图形!", vbCritical
       Exit Sub
   End If

   If MsgBox("本工具只导出可见图层在视窗范围内的图形!", vbOKCancel, "提示") = vbCancel Then
     Exit Sub
   End If
        
    Dim OutputFile As String
    
    OutputFile = InputBox("请输入文件名", "导出DXF文件存放路径", "C:\导出.dxf")
    If Len(Trim(OutputFile)) = 0 Then
       MsgBox "请输入有效的文件名"
       OutputFile = InputBox("请输入文件名", "导出DXF文件存放路径", "c:\导出.dxf")
       Exit Sub
    End If
    
    
    'Dim pMap As IMap, pCurrentScale As Double
    'Set pMap = g_doc.FocusMap
    'pCurrentScale = pMap.MapScale

    
    If Open_DXF(OutputFile) = False Then Exit Sub
    Write_Header
    Write_Tables
    Write_Blocks
    Write_Entities
    Application.StatusBar.Message(0) = "成功将窗口内的图形导出为DXF文件!"
    Close #1
    
    'pMap.MapScale = pCurrentScale


    Set g_doc = Nothing
    
   MsgBox "导出的CAD文件保存于" & OutputFile
End Sub


Private Function Open_DXF(sFile As String) As Boolean
    On Error GoTo errhandle:
    
        Open sFile For Output As #1
        Open_DXF = True
        Exit Function
errhandle:
      Open_DXF = False
      MsgBox "写文件出错,可能同名文件已经打开!"
'      MsgBox "Open_DXF: " & Err.Description

End Function

Private Sub Write_Header()
On Error GoTo errhandle:

    Dim pEnv As IEnvelope
    Set pEnv = g_doc.ActiveView.Extent
    
    Dim min_extents As IPoint
    Set min_extents = New point
    min_extents.X = pEnv.LowerLeft.X
    min_extents.Y = pEnv.LowerLeft.Y
    min_extents.Z = 0
    
    Dim max_extents As IPoint
    Set max_extents = New point
    max_extents.X = pEnv.UpperRight.X
    max_extents.Y = pEnv.UpperRight.Y
    max_extents.Z = 0
    
    Print #1, CStr(0)
    Print #1, "SECTION"
    Print #1, CStr(2)
    Print #1, "HEADER"
    Print #1, 9
    Print #1, "$EXTMIN"
    Print #1, 10
    Print #1, CStr(min_extents.X)
    Print #1, 20
    Print #1, CStr(min_extents.Y)
    Print #1, 30
    Print #1, CStr(min_extents.Z)
    Print #1, 9
    Print #1, "$EXTMAX"
    Print #1, 10
    Print #1, CStr(max_extents.X)
    Print #1, 20
    Print #1, CStr(max_extents.Y)
    Print #1, 30
    Print #1, CStr(max_extents.Z)
    Print #1, 0
    Print #1, "ENDSEC"
    
    Exit Sub
errhandle:
MsgBox "Write_Header: " & Err.Description

    
End Sub

Private Sub Write_Tables()
On Error GoTo errhandle:

    Print #1, 0
    Print #1, "SECTION"
    Print #1, 2
    Print #1, "TABLES"
    
  'Write_VPort_Information
  'Write_Layer_Information
  
    Print #1, 0
    Print #1, "ENDSEC"
    
    Exit Sub
errhandle:
MsgBox "Write_Tables: " & Err.Description

End Sub

Private Sub Write_Blocks()
On Error GoTo errhandle:

    Print #1, 0
    Print #1, "SECTION"
    Print #1, 2
    Print #1, "BLOCKS"
    Print #1, 0
    Print #1, "ENDSEC"
    
    Exit Sub
errhandle:
   MsgBox "Write_Blocks: " & Err.Description
    
End Sub
'
Private Sub Write_Entities()
  'On Error GoTo errhandle:

    Dim pFeat As IFeature
    Print #1, 0
    Print #1, "SECTION"
    Print #1, 2
    Print #1, "ENTITIES"

    Dim pMap As IMap
    Dim pFCursor As IFeatureCursor, pFL As IFeatureLayer
    Set pMap = g_doc.FocusMap
    Dim pShape As IGeometry

    Dim pSpatialFilter As ISpatialFilter
    Dim pGeometry As IGeometry
    Set pGeometry = g_doc.ActiveView.Extent.Envelope
    Set pSpatialFilter = New SpatialFilter

    Dim i, j As Integer
    Dim layer As ILayer
    Dim pComLayer As ICompositeLayer

    Dim pStepProgressor As IStepProgressor
    Set pStepProgressor = Application.StatusBar.ProgressBar

    pStepProgressor.MinRange = 1
    pStepProgressor.MaxRange = pMap.LayerCount
    pStepProgressor.Show
    
    Dim layersCount As Integer
    layersCount = pMap.LayerCount
    
   
   For i = 0 To pMap.LayerCount - 1
     
     pStepProgressor.Message = "正在导出:" & pMap.layer(i).name

     Set layer = pMap.layer(i)
     If layer.Visible = True Then  '只导出可见图层
       If (TypeOf pMap.layer(i) Is IFeatureLayer) Then
        'If Not pMap.layer(i).Name = "Graphics" Then

          Set pFL = pMap.layer(i)
          If Not pFL.FeatureClass Is Nothing Then

            'If pFL.featureClass.featurecount(Nothing) > 0 And pFL.Visible = True Then
            If pFL.FeatureClass.FeatureCount(Nothing) > 0 Then
              layer_Current = pFL.FeatureClass.AliasName
              'Application.StatusBar.Message(0) = "正在将视窗范围内的图形导出为DXF文件(" & Str(i) & "/" & Str(pMap.LayerCount) & ")"

               '输出当前视窗内的要素
              With pSpatialFilter
                  Set .Geometry = pGeometry
                 .GeometryField = pFL.FeatureClass.ShapeFieldName
                 .SpatialRel = esriSpatialRelRelation
                 .SpatialRelDescription = "T********"  '完全被包含的和相交的
              End With
              Set pFCursor = pFL.Search(pSpatialFilter, False)
              Set pFeat = pFCursor.NextFeature
              '///
              
               
              While Not pFeat Is Nothing
                If TypeOf pFeat Is IAnnotationFeature Then   ' Handle anno
                  write_anno pFeat
                Else        ' Handle normal features
                  Set pShape = pFeat.ShapeCopy
                  Select Case pShape.GeometryType

                         Case esriGeometryPoint
                              write_point pFeat
                         Case esriGeometryMultipoint
                              write_points pFeat
                         Case esriGeometryPolyline
                              write_polyline pShape
                         Case esriGeometryPolygon
                              write_poly pFeat
                              write_label pFeat
                         Case esriGeometryLine
                              write_polyline pShape

                   End Select
                End If

                Set pFeat = pFCursor.NextFeature
                '
                Wend
            End If
          End If
'        End If
      ElseIf TypeOf pMap.layer(i) Is IGroupLayer Then

         Set pComLayer = pMap.layer(i)
         For j = 0 To pComLayer.count - 1
            Set pFL = pComLayer.layer(j)
            If Not pFL.FeatureClass Is Nothing Then

              If pFL.FeatureClass.FeatureCount(Nothing) > 0 Then

                layer_Current = pFL.FeatureClass.AliasName
                'Application.StatusBar.Message(0) = "正在将视窗范围内的图形导出为DXF文件(" & Str(i) & "/" & Str(pMap.LayerCount) & ")"

                 '输出当前视窗内的要素
                With pSpatialFilter
                    Set .Geometry = pGeometry
                   .GeometryField = pFL.FeatureClass.ShapeFieldName
                   '第一种空间查询方式
                   '.SpatialRel = esriSpatialRelContains  '完全被指定的范围所包含
                   '第二种空间查询方式
                   .SpatialRel = esriSpatialRelRelation
                   .SpatialRelDescription = "T********"  '完全被包含的和相交的
                End With
                Set pFCursor = pFL.Search(pSpatialFilter, False)
                Set pFeat = pFCursor.NextFeature

                '///
                While Not pFeat Is Nothing
                  If TypeOf pFeat Is IAnnotationFeature Then   ' Handle anno
                      write_anno pFeat
                  Else        ' Handle normal features
                      Set pShape = pFeat.ShapeCopy
                      Select Case pShape.GeometryType

                             Case esriGeometryPoint
                                  write_point pFeat
                             Case esriGeometryMultipoint
                                  write_points pFeat
                             Case esriGeometryPolyline
                                  write_polyline pShape
                             Case esriGeometryPolygon
                                  write_poly pFeat
                                  write_label pFeat
                             Case esriGeometryLine
                                  write_polyline pShape

                      End Select
                  End If
                  
                  Set pFeat = pFCursor.NextFeature
                  
                  '
               Wend
            End If
          End If
        Next j
      End If

      End If
      pStepProgressor.position = i
    Next i

    pStepProgressor.Hide

    Print #1, 0
    Print #1, "ENDSEC"
    Print #1, 0
    Print #1, "EOF"
    Exit Sub

'errhandle:
'MsgBox "写实体出错: " & Err.Description

End Sub

Private Sub Write_VPort_Information()
On Error GoTo errhandle:

    Print #1, 0
    Print #1, "TABLE"
    Print #1, 2
    Print #1, "VPORT"
    Print #1, 0
    Print #1, "VPORT"
    Print #1, 2
    Print #1, "*ACTIVE"
    Print #1, 41
    Print #1, CStr(1#)
    Print #1, 0
    Print #1, "ENDTAB"
    
    Exit Sub
errhandle:
MsgBox "Write_VPort_Information: " & Err.Description

End Sub

Private Sub Write_Layer_Information()
On Error GoTo errhandle:

    ' Write the LineTypes
    Print #1, 0
    Print #1, "TABLE"
    Print #1, 2
    Print #1, "LTYPE"
    Print #1, 5
    Print #1, 1
    Print #1, 0
    Print #1, "LTYPE"
    Print #1, 2
    Print #1, "CONTINUOUS"
    Print #1, 70
    Print #1, 0
    Print #1, 3
    Print #1, "Solid line"
    Print #1, 72
    Print #1, 65
    Print #1, 73
    Print #1, 0
    Print #1, 0
    Print #1, "ENDTAB"


    Print #1, 0
    Print #1, "TABLE"
    Print #1, 2
    Print #1, "LAYER"
    Print #1, 5
    Print #1, 2
    Print #1, 0
    Print #1, "LAYER"
    Print #1, 2
    Print #1, 0
    Print #1, 70
    Print #1, 0
    Print #1, 62
    Print #1, 7
    Print #1, 6
    Print #1, "CONTINUOUS"
    Print #1, 0
    Print #1, "ENDTAB"
    
    Exit Sub
errhandle:
MsgBox "Write_Layer_Information: " & Err.Description

End Sub


Private Sub write_polylineInPolygon(pShape As IGeometry)
'On Error GoTo errhandle:

    Dim bFirstSeg As Boolean
    bFirstSeg = True

    Dim pSegColl As ISegmentCollection
    Set pSegColl = pShape

    Dim pSeg As ISegment

    Dim i As Long
    For i = 0 To pSegColl.SegmentCount - 1

        Set pSeg = pSegColl.Segment(i)

        Select Case pSeg.GeometryType

        Case esriGeometryLine
            If bFirstSeg Then
                ' First segment of polyline, print the header info
                Print #1, CStr(0)
                Print #1, "POLYLINE"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, CStr(66)
                Print #1, CStr(1)
                Print #1, CStr(62)
                Print #1, CStr(7)
                Print #1, CStr(6)
                Print #1, "CONTINUOUS"
                ' Print the "from point"
                Print #1, CStr(0)
                Print #1, "VERTEX"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, 10
                Print #1, pSeg.FromPoint.X
                Print #1, 20
                Print #1, pSeg.FromPoint.Y
                bFirstSeg = False
            End If

            ' Now print the "to point"
            Print #1, CStr(0)
            Print #1, "VERTEX"
            Print #1, CStr(8)
            Print #1, layer_Current
            Print #1, 10
            Print #1, pSeg.ToPoint.X
            Print #1, 20
            Print #1, pSeg.ToPoint.Y


            ' Do we need to end the entity?
            If i = pSegColl.SegmentCount - 1 Then

                Print #1, CStr(0)
                Print #1, "SEQEND"
                Print #1, 8
                Print #1, layer_Current
            End If

        ' Handle curves
        Case esriGeometryCircularArc
            If Not bFirstSeg Then
                ' ArcMap allows a line and curve as one entity.  Autocad doesn't.
                ' So, in this case, this is a curve but isn't the first segment.
                ' End the polyline entity first, then create a new ARC entity.
                Print #1, CStr(0)
                Print #1, "SEQEND"
                Print #1, 8
                Print #1, layer_Current
            End If

            Dim pCA As ICircularArc
            Set pCA = pSeg

            If Not pCA.CenterPoint Is Nothing Then

                Print #1, CStr(0)
                Print #1, "ARC"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, 10
                Print #1, pCA.CenterPoint.X
                Print #1, 20
                Print #1, pCA.CenterPoint.Y
                Print #1, 40
                Print #1, pCA.radius

                If pCA.IsCounterClockwise Then
                    Print #1, 50
                    Print #1, CStr(CDbl(pCA.FromAngle * 180 / PI))
                    Print #1, 51
                    Print #1, CStr(CDbl(pCA.ToAngle * 180 / PI))
                Else
                    Print #1, 50
                    Print #1, CStr(CDbl(pCA.ToAngle * 180 / PI))
                    Print #1, 51
                    Print #1, CStr(CDbl(pCA.FromAngle * 180 / PI))
                End If

            End If

            'Debug.Print " *** Arc *** "
            Set pCA = pSeg
            'Debug.Print "Center:  " & pCA.CenterPoint.X & " , " & pCA.CenterPoint.Y
            'Debug.Print "Radius:  " & pCA.radius
            'Debug.Print "Start Angle:  " & CDbl(pCA.FromAngle * 180 / Pi)
            'Debug.Print "End Angle:  " & CDbl(pCA.ToAngle * 180 / Pi)
            bFirstSeg = True

        End Select

    Next i


'    Exit Sub
'errhandle:
'
'   MsgBox "Write_Polyline: " & Err.Description

End Sub

Private Sub write_polyline(pShape As IGeometry)
    Dim bFirstSeg As Boolean
    Dim pGeometryColl As IGeometryCollection
    Dim pGeometryCount As Integer
    Dim pPointColl As IPointCollection
    Dim pPoint As IPoint
    Dim i As Long

    Set pGeometryColl = pShape
    
    For pGeometryCount = 0 To pGeometryColl.GeometryCount - 1
       Set pPointColl = pGeometryColl.Geometry(pGeometryCount)
       bFirstSeg = True
       
       For i = 0 To pPointColl.PointCount - 1
          Set pPoint = pPointColl.point(i)
          
          If bFirstSeg Then
                ' First segment of polyline, print the header info
                Print #1, CStr(0)
                Print #1, "POLYLINE"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, CStr(66)
                Print #1, CStr(1)
                Print #1, CStr(62)
                Print #1, CStr(7)
                Print #1, CStr(6)
                Print #1, "CONTINUOUS"
                ' Print the "from point"
                Print #1, CStr(0)
                Print #1, "VERTEX"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, 10
                Print #1, pPoint.X
                Print #1, 20
                Print #1, pPoint.Y
                bFirstSeg = False
            End If

            ' Now print the "to point"
            Print #1, CStr(0)
            Print #1, "VERTEX"
            Print #1, CStr(8)
            Print #1, layer_Current
            Print #1, 10
            Print #1, pPoint.X
            Print #1, 20
            Print #1, pPoint.Y
            
            ' Do we need to end the entity?
            If i = pPointColl.PointCount - 1 Then

                Print #1, CStr(0)
                Print #1, "SEQEND"
                Print #1, 8
                Print #1, layer_Current
            End If
          
       Next i
    
    Next pGeometryCount

End Sub


Private Sub write_polyline_original(pShape As IGeometry)
On Error GoTo errhandle:

    Dim pPoints As IPointCollection
    Set pPoints = pShape
    
    Dim pPoly As IPolygon
    Dim pRing As IRing
    
    Print #1, CStr(0)
    Print #1, "POLYLINE"
    Print #1, CStr(8)
    Print #1, layer_Current
    Print #1, CStr(66)
    Print #1, CStr(1)
    Print #1, CStr(62)
    Print #1, CStr(7)
    Print #1, CStr(6)
    Print #1, "CONTINUOUS"
    
    Dim i As Long
    For i = 0 To pPoints.PointCount - 1
        
        Print #1, CStr(0)
        Print #1, "VERTEX"
        Print #1, CStr(8)
        Print #1, layer_Current
        Print #1, 10
        Print #1, pPoints.point(i).X
        Print #1, 20
        Print #1, pPoints.point(i).Y
        Dim j As Long
                    
    Next i
    
    Print #1, CStr(0)
    Print #1, "SEQEND"
    Print #1, 8
    Print #1, layer_Current
    
    Exit Sub
errhandle:
MsgBox "Write_Polyline: " & Err.Description

End Sub

Private Sub write_poly(pFeature As IFeature)
' On Error GoTo errhandle:
    Dim pShape As IGeometry
    Dim pPoly As IPolygon, lLoop As Long, lExtCount As Long, pExtRings() As IRing
    Dim pIntRings() As IRing, lLoop2 As Long, lIntCount As Long
    Set pShape = pFeature.ShapeCopy
    Set pPoly = pShape
    lExtCount = pPoly.ExteriorRingCount
    If lExtCount = 0 Then Exit Sub
    ReDim pExtRings(lExtCount - 1)
    For lLoop = 0 To lExtCount - 1
      Set pExtRings(lLoop) = New ring
    Next lLoop
    pPoly.QueryExteriorRings pExtRings(0)
    For lLoop = 0 To lExtCount - 1
      'Write out the external ring
      write_polylineInPolygon pExtRings(lLoop)
      
      lIntCount = pPoly.InteriorRingCount(pExtRings(lLoop))
      If lIntCount > 0 Then
        ReDim pIntRings(lIntCount - 1)
        For lLoop2 = 0 To lIntCount - 1
          Set pIntRings(lLoop2) = New ring
        Next lLoop2
        pPoly.QueryInteriorRings pExtRings(lLoop), pIntRings(0)
        For lLoop2 = 0 To lIntCount - 1
          'Write out the internal rings
          Dim pPoints As IPointCollection
          Set pPoints = pIntRings(lLoop2)
          
          write_polylineInPolygon pIntRings(lLoop2)
        Next lLoop2
      End If
    Next lLoop
    
'    Exit Sub
'errhandle:
'    MsgBox "Write_Poly: " & Err.Description

End Sub


'写lable注记
Private Sub write_label(pFeat As IFeature)
On Error GoTo errhandle:

    
    Dim Point1 As IPoint
    Dim pDlbm As String
    Dim pTbbh As String
    Dim pLable As String
    
    If IsNull(pFeat.value(pFeat.Fields.FindField("DLBM"))) Then
        Exit Sub
    Else
        pDlbm = Trim(pFeat.value(pFeat.Fields.FindField("DLBM")))
    End If

    
    If IsNull(pFeat.value(pFeat.Fields.FindField("TBBH"))) Then
        pTbbh = ""
    Else
        pTbbh = Trim(pFeat.value(pFeat.Fields.FindField("TBBH")))
    End If
    
    
    If pTbbh = "" Then
       pLable = pDlbm
    Else
       pLable = pTbbh & "/" & pDlbm
    End If
       
'    Dim pGeometryColl As IGeometryCollection
'    Set pGeometryColl = pFeat.Shape
    
    Dim pArea As IArea
    Set pArea = pFeat.ShapeCopy
    Set Point1 = pArea.LabelPoint

    Print #1, CStr(0)
    Print #1, "TEXT"
    Print #1, CStr(8)
    Print #1, "图斑注记"
    Print #1, CStr(1)
    Print #1, pLable
    Print #1, 72
    Print #1, CStr(0)
    Print #1, 73
    Print #1, CStr(0)
    
    Print #1, 50
    Print #1, CStr(0)
    
    Print #1, 40
    Print #1, CStr(1)

    Print #1, "10"
    Print #1, Point1.X
    Print #1, "20"
    Print #1, Point1.Y
    Print #1, "11"
    Print #1, Point1.X + 30
    Print #1, "21"
    Print #1, Point1.Y + 30
    
    
    Exit Sub
errhandle:

   Exit Sub

End Sub

'写lable注记
Private Sub write_label22(pFeat As IFeature)
On Error GoTo errhandle:

    Dim pShape As IGeometry
    Set pShape = pFeat.ShapeCopy
    
    Dim pArea As IArea
    Dim Point1 As IPoint
    Dim pDlbm As String
    Dim pTbbh As String
    Dim pLable As String
    
    If IsNull(pFeat.value(pFeat.Fields.FindField("DLBM"))) Then
        Exit Sub
    Else
        pDlbm = Trim(pFeat.value(pFeat.Fields.FindField("DLBM")))
    End If

    
    If IsNull(pFeat.value(pFeat.Fields.FindField("TBBH"))) Then
        pTbbh = ""
    Else
        pTbbh = Trim(pFeat.value(pFeat.Fields.FindField("TBBH")))
    End If
    
    
    If pTbbh = "" Then
       pLable = pDlbm
    Else
       pLable = pTbbh & "/" & pDlbm
    End If
       
   
    Dim pPoly As IPolygon, lLoop As Long, lExtCount As Long, pExtRings() As IRing
    Set pPoly = pShape
    lExtCount = pPoly.ExteriorRingCount
    
    
    If lExtCount = 0 Then Exit Sub
    ReDim pExtRings(lExtCount - 1)
    For lLoop = 0 To lExtCount - 1
      Set pExtRings(lLoop) = New ring
    Next lLoop
    pPoly.QueryExteriorRings pExtRings(0)
    For lLoop = 0 To lExtCount - 1
     
        Set pArea = pExtRings(lLoop)
        Set Point1 = pArea.LabelPoint
        
        Print #1, CStr(0)
        Print #1, "TEXT"
        Print #1, CStr(8)
        Print #1, "图斑注记"
        Print #1, CStr(1)
        Print #1, pLable
        Print #1, 72
        Print #1, CStr(0)
        Print #1, 73
        Print #1, CStr(0)
        
        Print #1, 50
        Print #1, CStr(0)
        
        Print #1, 40
        Print #1, CStr(1)
    
        Print #1, "10"
        Print #1, Point1.X
        Print #1, "20"
        Print #1, Point1.Y
        Print #1, "11"
        Print #1, Point1.X + 30
        Print #1, "21"
        Print #1, Point1.Y + 30

    Next lLoop
    
    
    
    Exit Sub
errhandle:

   Exit Sub

End Sub


Private Sub write_anno(pFeat As IFeature)
On Error GoTo errhandle:

    Dim pAnno As IAnnotationFeature
    Set pAnno = pFeat
    
    Dim pShape As IGeometry
    Set pShape = pFeat.ShapeCopy
    
    Dim pPoly As IPolygon
    Set pPoly = pShape
    
    Dim pEnv As IEnvelope
    Set pEnv = pPoly.Envelope
    
    Dim pElem As IElement
    Set pElem = pAnno.Annotation
    Dim pTextEl As ITextElement
    '处理地类注记中间的短线
    If TypeOf pElem Is ITextElement Then
       Set pTextEl = pElem
    ElseIf TypeOf pElem Is ILineElement Then
       write_middleLine pElem.Geometry  '调用专门处理分子分母线的函数
    End If
    
    'Determine the size to use for the annotation in map units
    Dim dSize As Double, pAnnoClass As IAnnoClass
    Set pAnnoClass = pFeat.Class.Extension
    Dim pMap As IMap, pCurrentScale As Double, pDisp As IScreenDisplay
    Set pMap = g_doc.FocusMap
    pCurrentScale = pMap.MapScale
    pMap.MapScale = pAnnoClass.ReferenceScale
    Dim pActive As IActiveView
    Set pActive = pMap
    Set pDisp = pActive.ScreenDisplay
    dSize = pDisp.DisplayTransformation.FromPoints(pTextEl.Symbol.Size) / 1.333
    
    Dim pMP As IMultipoint
    Set pMP = New Multipoint
    
    Dim pPoints As IPointCollection
    '修改后的
    Dim Point1 As IPoint
    Dim Point2 As IPoint
    Set Point1 = New point
    Set Point2 = New point
    
    Set Point1 = pEnv.LowerLeft
    Set Point2 = pEnv.LowerRight
    
    
    Set pPoints = pMP
    pPoints.AddPoint Point1
    pPoints.AddPoint Point2

    '直接获取注记的角度
    Dim dAngle As Double
    dAngle = pTextEl.Symbol.Angle
    
    '处理多行文本,如果为多行文本,只取第一行
    Dim pText As String
    pText = Trim(pTextEl.Text)
    
    If InStr(pText, Chr(13)) > 0 Then
       pText = Left(pText, InStr(pText, Chr(13)) - 1)
    End If
    
    Print #1, CStr(0)
    Print #1, "TEXT"
    Print #1, CStr(8)
    Print #1, layer_Current
    Print #1, CStr(1)
    Print #1, pText
    Print #1, 72
    Print #1, CStr(0)
    Print #1, 73
    Print #1, CStr(0)
    
    Print #1, 50
    Print #1, CStr(dAngle)
    
    Print #1, 40
    Print #1, CStr(dSize)       ' TODO: if we're in meters, divide by 3.  Otherwise, don't.

    
    'Write out the first and last points (DXF only allows two points for defining the  position)
    Print #1, "10"
    Print #1, pPoints.point(0).X
    Print #1, "20"
    Print #1, pPoints.point(0).Y
    Print #1, "11"
    Print #1, pPoints.point(pPoints.PointCount - 1).X
    Print #1, "21"
    Print #1, pPoints.point(pPoints.PointCount - 1).Y
    
    Exit Sub
errhandle:
   'MsgBox "写注记出错: " & Err.Description

   Exit Sub

End Sub


'用于输出分数线
Private Sub write_middleLine(pShape As IGeometry)
On Error GoTo errhandle:

    Dim bFirstSeg As Boolean
    bFirstSeg = True
    
    Dim pSegColl As ISegmentCollection
    Set pSegColl = pShape
    
    Dim pSeg As ISegment
    
    Dim i As Long
    For i = 0 To pSegColl.SegmentCount - 1
    
        Set pSeg = pSegColl.Segment(i)
        
        Select Case pSeg.GeometryType
        
        Case esriGeometryLine
            If bFirstSeg Then
                ' First segment of polyline, print the header info
                Print #1, CStr(0)
                Print #1, "POLYLINE"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, CStr(66)
                Print #1, CStr(1)
                Print #1, CStr(62)
                Print #1, CStr(7)
                Print #1, CStr(6)
                Print #1, "CONTINUOUS"
                ' Print the "from point"
                Print #1, CStr(0)
                Print #1, "VERTEX"
                Print #1, CStr(8)
                Print #1, layer_Current
                Print #1, 10
                Print #1, pSeg.FromPoint.X
                Print #1, 20
                Print #1, pSeg.FromPoint.Y - 0.2
                bFirstSeg = False
            End If
            
            ' Now print the "to point"
            Print #1, CStr(0)
            Print #1, "VERTEX"
            Print #1, CStr(8)
            Print #1, layer_Current
            Print #1, 10
            Print #1, pSeg.ToPoint.X
            Print #1, 20
            Print #1, pSeg.ToPoint.Y - 0.2
            
            ' Do we need to end the entity?
            If i = pSegColl.SegmentCount - 1 Then
                
                Print #1, CStr(0)
                Print #1, "SEQEND"
                Print #1, 8
                Print #1, layer_Current
            End If
        End Select
    
    Next i
    
    Exit Sub
errhandle:
MsgBox "Write_Polyline: " & Err.Description

End Sub

'输出单点
'private Sub write_point(pShape As IGeometry)
Private Sub write_point(pFeature As IFeature)
On Error GoTo errhandle:

    Dim pPoint As IPoint
    Set pPoint = pFeature.Shape
    
    Dim index As Integer
    index = pFeature.Fields.FindField("YSDM")
    
        Print #1, CStr(0)
        Print #1, "POINT"
        Print #1, CStr(8)
        Print #1, layer_Current
        
        Print #1, 10
        Print #1, pPoint.X
        Print #1, 20
        Print #1, pPoint.Y
                        
        Print #1, 39            ' Thickness
        Print #1, CStr(3)
    
    Exit Sub
errhandle:
    MsgBox "Write_Point: " & Err.Description

End Sub

'输出多点

Private Sub write_points(pFeature As IFeature)
On Error GoTo errhandle:

    Dim pPoints As IPointCollection
    Set pPoints = pFeature.Shape
    
    Dim index As Integer
    index = pFeature.Fields.FindField("YSDM")
    
    Dim i As Long
    For i = 0 To pPoints.PointCount - 1
        Print #1, CStr(0)
        Print #1, "POINT"
        Print #1, CStr(8)
        Print #1, layer_Current
        
        Print #1, 10
        Print #1, pPoints.point(i).X
        Print #1, 20
        Print #1, pPoints.point(i).Y
        
        Print #1, 39            ' Thickness
        Print #1, CStr(3)
'        If index <> -1 Then
'           Print #1, Trim(pFeature.Value(index))
'        Else
'           Print #1, CStr(3)
'        End If
        
    Next i
    
    Exit Sub
errhandle:
MsgBox "Write_Points: " & Err.Description

End Sub

内容概要:本文详细介绍了使用COMSOL进行三相电力变压器温度场与流体场耦合计算的具体步骤和技术要点。首先讨论了变压器温升问题的重要性和现有仿真与实测数据之间的偏差,接着阐述了电磁-热-流三场耦合建模的难点及其解决方法。文中提供了关键的材料属性设置代码,如变压器油的密度和粘度随温度变化的关系表达式,并强调了网格划分、求解器配置以及后处理阶段需要注意的技术细节。此外,还分享了一些实用的经验和技巧,例如采用分离式步进求解策略、优化网格划分方式等,确保模型能够顺利收敛并获得精确的结果。最后,作者推荐了几种常用的湍流模型,并给出了具体的参数设置建议。 适用人群:从事电力系统设计、变压器制造及相关领域的工程师和技术人员,特别是那些希望深入了解COMSOL软件在复杂多物理场耦合计算方面应用的人群。 使用场景及目标:适用于需要对变压器内部温度分布和油流情况进行精确模拟的研究或工程项目。主要目的是提高仿真精度,使仿真结果更加贴近实际情况,从而指导产品设计和优化运行参数。 其他说明:文中不仅包含了详细的理论解释和技术指导,还提供了一些实际案例供读者参考。对于初学者来说,可以从简单的单相变压器开始练习,逐步掌握复杂的三相变压器建模技能。同时,作者提醒读者要注意单位的一致性和材料属性的准确性,这是避免许多常见错误的关键所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值