DXF文件 点击:3080 | 回复:7



山水

    
  • 精华:4帖
  • 求助:0帖
  • 帖子:128帖 | 2238回
  • 年度积分:0
  • 历史总积分:3675
  • 注册:2004年1月06日
发表于:2006-02-09 18:49:00
楼主
DXF文件是Autodesk公司为其AutoCAD与外部CAD/CAM系统接口所定义的一种图形交换格式文件。随着AutoCAD在业界的广泛使用,大多数的CAD/CAM系统都具备与AutoCAD接口的功能。DXF文件的文本格式易于阅读分析,图形数据按照一定的顺序存储,一直是广大CAD/CAM开发人员研究的对象。本文阐述了对AutoCAD图形、文字进行处理后,输出DXF文件数据用于数控系统的方法。 

    二、将图形、文字转化为包含路径信息的DXF文件 

    在AutoCAD内进行编辑的文字直接保存为DXF文件,不能生成用于数控加工的数据。要使AutoCAD的文字用于生成可用于数控的路径信息的DXF文件,需要作如下处理: 

    (1)将AutoCAD内的环境变量textfill的值设为0,使文字只显示边缘; 

    (2)在AutoCAD中编辑所需要的文字; 

    (3)将文件输出为*.eps文件; 

    (4)新建一个文件,将保存的eps文件插入; 

    (5)将文字炸开,得到只含有细小线段的文字轮廓; 

    (6)将文件保存为DXF格式。 

    三、DXF文件中的实体信息 

    DXF的全称为图形转换文件(Drawing Interchange File),AutoCAD软件支持ASCⅡ文本格式的DXF文件和二进制格式的DXF文件,主要用于与其他CAD系统及用户应用程序间的图形信息交换,其中ASCⅡ文本格式的DXF文件应用较多。DXF文件由多个节(SECTION)组成,每个节均由多个组(GROUP)组成,组在文件中占两行,第一行是组码(GROUPCODE),为一整数;第二行是组值(GROUPVALUE),它的数据类型由组码决定。典型的DXF文件由:标题节(HEADERS)、类节(CLASSES)、表节(TABLES)、块节(BLOCKS)、实体节(ENTITIES)、对象节(OBJECTS)和调形预览节(THUMBNAILIMAGES)组成。 

    以文本形式打开一个DXF文件,可以看到该文件的具体结构。下面是一个Line实体: 

AcDbLine 

10 

120.0 

 20 

274.56 

 30 

0.0 

 11 

120.96 

 21 

275.5199999999999 

 31 

0.0 

  0 

    其中,10、20、30下面一行分别是线段起点的x、y、z坐标,11、21、31下面一行对应的是线段终点的x、y、z坐标。对于同一版本的DXF文件,该结构和格式是固定的,因此很容易从DXF文件中提取实体(如LINE、CIRCLE、ARC等)位置信息。 




DB__ZS

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 3回
  • 年度积分:0
  • 历史总积分:3
  • 注册:2008年8月30日
发表于:2008-08-30 16:28:17
1楼

写的这段VBA代码是在COREL DRAW 12上运行的,是从COREL DRAW 页面上导出DXF格式的小程序,存在的问题是圆弧有时导出后反了。我估计是圆的开始点和结束点如何判断的问题搞不懂,为此请教。祥见代码,如果方便请帮助修改,先感谢了。

Private Sub CommandButton3_Click()
ActiveDocument.Unit = cdrMillimeter
'On Error Resume Next
If ActivePage.Shapes.Count = 0 Then Exit Sub
Application.Optimization = True
Dim sT As Shape
Dim sr As New ShapeRange
Dim z As Long
Dim i As Long

ActivePage.Shapes.All.CreateSelection
ActivePage.Shapes.All.Copy

For Each sT In ActivePage.Shapes '取消全部群组
    If sT.Type = cdrGroupShape Then sT.UngroupAll
Next

For Each sT In ActivePage.Shapes
    If sT.Type = cdrPolygonShape Then sT.ConvertToCurves
    If sT.Type = cdrRectangleShape Then sT.ConvertToCurves
    If sT.Type = cdrEllipseShape Then
        If Round(sT.Ellipse.HRadius, 2) <> Round(sT.Ellipse.VRadius, 2) Then sT.ConvertToCurves
    End If
Next

For Each sT In ActivePage.FindShapes(, cdrCurveShape)
    sT.Curve.Closed = False
    z = sT.Curve.Nodes.Count
    For i = z To 1 Step -1
        sT.Curve.Nodes(i).BreakApart
    Next i
    sT.BreakApart
Next

For Each sT In ActivePage.FindShapes(, cdrCurveShape)
    If sT.Curve.SubPaths(1).Segments(1).Type = 1 Then
        For i = 1 To 4 ''''''''''''''''
        sT.Curve.SubPaths(1).AddNodeAt (i / 5), cdrRelativeSegmentOffset ''''''''''''''''''''''
        Next
       
        sT.Curve.Closed = False
        z = sT.Curve.Nodes.Count
        For i = z To 1 Step -1
            sT.Curve.Nodes(i).BreakApart
        Next i
        sT.BreakApart
    End If
Next

WriteLineToFile
ActivePage.Shapes.All.Delete
ActiveLayer.Paste
Application.Optimization = False
Application.ActiveWindow.Refresh
End Sub

Private Function WriteLineToFile() As String
    Const ForReading = 1, ForWriting = 2
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile("c:\testfile.dxf", ForWriting, True)
    '-------------------------------------------
    Call WriteHeader(ActivePage.Shapes.All)
    Call writeTable
    Call WriteBeginEntities
    Call WriteLineText '''''''''''''''''''''''''''''
    Call WriteEndSEC
    Call WriteEof
    f.Close
    Set f = Nothing
    '-------------------------------------------
    Set f = fso.OpenTextFile("c:\testfile.dxf", ForReading)
    WriteLineToFile = f.ReadAll
    f.Close
    Set f = Nothing
    Set fso = Nothing
End Function

Private Sub WriteHeader(sr As ShapeRange)
'写DXF文件头
    WriteBeginSEC
    WriteLine "  2"
    WriteLine "HEADER"
   
    ActiveDocument.ReferencePoint = cdrBottomLeft
    WriteLine "  9"
    WriteLine "$EXTMIN"
        With sr
            WriteLine " 10"
            WriteLine .PositionX
           
            WriteLine " 20"
            WriteLine .PositionY
        End With
       
    ActiveDocument.ReferencePoint = cdrTopRight
    WriteLine "  9"
    WriteLine "$EXTMAX"
        With sr
            WriteLine " 10"
            WriteLine .PositionX
           
            WriteLine " 20"
            WriteLine .PositionY
        End With
    WriteEndSEC
End Sub

Private Sub writeTable()
'此部份为DXF文件第二部份
    WriteBeginSEC
    WriteLine "  2"
    WriteLine "TABLES"
    WriteLine "  0"
    WriteLine "TABLE"
    WriteLine "  2"
    WriteLine "LAYER"
    WriteLine " 70"
    WriteLine "  1"
    WriteLine "  0"
    WriteLine "LAYER"
    WriteLine "  2"
    WriteLine "Design"
    WriteLine " 70"
    WriteLine "64"
    WriteLine " 62"
    WriteLine "7"
    WriteLine "  6"
    WriteLine "CONTINUOUS"
    WriteLine "  0"
    WriteLine "ENDTAB"
    WriteEndSEC
End Sub

Private Sub WriteBeginBlocks()
    WriteBeginSEC
    WriteLine "  2"
    WriteLine "BLOCKS"
    WriteEndSEC
End Sub

Private Sub WriteBeginEntities()
  WriteBeginSEC
  WriteLine "  2"
  WriteLine "ENTITIES"
End Sub

Private Sub WriteLineText()
Dim x2 As Double, y2 As Double
For Each s In ActivePage.Shapes
    Select Case s.Type
        Case 3 'cdrCurveShape
            Select Case s.Curve.SubPaths(1).Segments(1).Type
                Case 0 'cdrLineSegment
                    Call WriteCdrLine(CMYKColorToIndexColor(s.Outline.Color.Name), _
                                    s.Curve.SubPaths(1).Segments(1).StartNode.PositionX, _
                                    s.Curve.SubPaths(1).Segments(1).StartNode.PositionY, _
                                    s.Curve.SubPaths(1).Segments(1).EndNode.PositionX, _
                                    s.Curve.SubPaths(1).Segments(1).EndNode.PositionY) '直线
                Case 1 'cdrCurveSegment
                s.Curve.SubPaths(1).Segments(1).GetPointPositionAt x2, y2
                    Call WriteCdrCArc(CMYKColorToIndexColor(s.Outline.Color.Name), _
                                    s.Curve.SubPaths(1).Segments(1).StartNode.PositionX, _
                                    s.Curve.SubPaths(1).Segments(1).StartNode.PositionY, _
                                    x2, _
                                    y2, _
                                    s.Curve.SubPaths(1).Segments(1).EndNode.PositionX, _
                                    s.Curve.SubPaths(1).Segments(1).EndNode.PositionY) '曲线弧
                Case 2
                    MsgBox "复合线出现!"
            End Select
        Case 2 'cdrEllipseShape
            If Round(s.Ellipse.HRadius, 2) = Round(s.Ellipse.VRadius, 2) Then
                Call WriteCdrEArc(CMYKColorToIndexColor(s.Outline.Color.Name), _
                s.Ellipse.CenterX, _
                s.Ellipse.CenterY, _
                s.Ellipse.HRadius, _
                s.Ellipse.StartAngle + s.RotationAngle + 0.01, _
                s.Ellipse.EndAngle + s.RotationAngle)
            End If
    End Select
Next
End Sub

Sub WriteCdrCArc(indexColor As Integer, x1 As Double, y1 As Double, x2 As Double, y2 As Double, x3 As Double, y3 As Double)
    Dim x0 As Double
    Dim y0 As Double
    Dim r As Double
    Dim StartAngle As Double
    Dim EndAngle As Double
   
    x0 = ((y3 - y1) * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1) + (y2 - y1) * (y1 * y1 - y3 * y3 + x1 * x1 - x3 * x3)) / (2 * (x2 - x1) * (y3 - y1) - 2 * (x3 - x1) * (y2 - y1))
    y0 = ((x3 - x1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1) + (x2 - x1) * (x1 * x1 - x3 * x3 + y1 * y1 - y3 * y3)) / (2 * (y2 - y1) * (x3 - x1) - 2 * (y3 - y1) * (x2 - x1))
    r = Sqr((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0))
    EndAngle = MyAngle(x1 - x0, y1 - y0, x1, y1, x0, y0)
    StartAngle = MyAngle(x3 - x0, y3 - y0, x3, y3, x0, y0)
   
    WriteLine "  0"
    WriteLine "ARC"
   
    WriteLine "  8"
    WriteLine "Design"
   
    WriteLine "  6"
    WriteLine "CONTINUOUS"
   
    WriteLine " 62"
    WriteLine CStr(indexColor)
   
    WriteLine " 10"
    WriteLine CStr(x0)
   
    WriteLine " 20"
    WriteLine CStr(y0)
   
    WriteLine " 40"
    WriteLine CStr(r)
   
    WriteLine " 50"
    WriteLine CStr(StartAngle)
   
    WriteLine " 51"
    WriteLine CStr(EndAngle)
End Sub


Private Sub WriteCdrLine(indexColor As Integer, StartNodeX As Double, StartNodeY As Double, EndNodeX As Double, EndNodeY As Double) '直线
    WriteLine "  0"
    WriteLine "LINE"
   
    WriteLine "  8" '层名
    WriteLine "Design"
   
    WriteLine "  6"
    WriteLine "CONTINUOUS"
   
    WriteLine " 62" '颜色
    WriteLine CStr(indexColor)
           
    WriteLine " 10"
    WriteLine CStr(StartNodeX)
   
    WriteLine " 20"
    WriteLine CStr(StartNodeY)
   
    WriteLine " 11"
    WriteLine CStr(EndNodeX)
   
    WriteLine " 21"
    WriteLine CStr(EndNodeY)
End Sub

Private Sub WriteCdrEArc(indexColor As Integer, S_CenterX As Double, S_CenterY As Double, S_HRadius As Double, StartAngelM As Double, EndAngelM As Double) '正圆和弧
    WriteLine "  0"
    WriteLine "ARC"
   
    WriteLine "  8"
    WriteLine "Design"
   
    WriteLine "  6"
    WriteLine "CONTINUOUS"
   
    WriteLine " 62"
    WriteLine CStr(indexColor)
   
    WriteLine " 10"
    WriteLine CStr(S_CenterX)
   
    WriteLine " 20"
    WriteLine CStr(S_CenterY)
   
    WriteLine " 40"
    WriteLine CStr(S_HRadius)
   
    WriteLine " 51"
    WriteLine CStr(StartAngelM)
   
    WriteLine " 50"
    WriteLine CStr(EndAngelM)
End Sub

Private Sub WriteLine(sOneRow As String)
'向DXF文件流中写入一行信息
On Error GoTo ErrorHandler
  f.WriteLine sOneRow
  Exit Sub
ErrorHandler:
  MsgBox Err.Description
End Sub

Private Sub WriteBeginSEC()
  f.WriteLine "  0"
  f.WriteLine "SECTION"
End Sub

Private Sub WriteEndSEC()
  WriteLine "  0"
  WriteLine "ENDSEC"
End Sub

Private Sub WriteEof()
  f.WriteLine "  0"
  f.WriteLine "EOF"
End Sub

Private Function MyAngle(w As Double, h As Double, xx As Double, yy As Double, x0 As Double, y0 As Double) As Double
Dim HW As Double
    HW = h / w
    MyAngle = Abs(Atn(HW) * 180 / 3.14159265358979)
   
    Select Case xx - x0
        Case Is >= 0
            Select Case yy - y0
            Case Is >= 0 '++++++   1
                MyAngle = MyAngle
            Case Is < 0 '++++++   4
                MyAngle = (90 - MyAngle) + 270
            End Select
        Case Is < 0
            Select Case yy - y0
            Case Is >= 0 '++++++   2
                MyAngle = (90 - MyAngle) + 90
            Case Is < 0 '++++++   3
                MyAngle = MyAngle + 180
            End Select
    End Select
End Function

Private Function CMYKColorToIndexColor(CMYK As String) As Integer
    Select Case CMYK
        Case "黑"
            CMYKColorToIndexColor = 7
        Case "青"
            CMYKColorToIndexColor = 4
        Case "蓝"
            CMYKColorToIndexColor = 5
        Case "红"
            CMYKColorToIndexColor = 1
        Case "紫"
            CMYKColorToIndexColor = 202
        Case "洋红"
            CMYKColorToIndexColor = 6
        Case "绿"
            CMYKColorToIndexColor = 3
        Case "黄"
            CMYKColorToIndexColor = 2
        Case Else
            CMYKColorToIndexColor = 9
   End Select
End Function

CHENNET

  • 精华:0帖
  • 求助:0帖
  • 帖子:1帖 | 105回
  • 年度积分:0
  • 历史总积分:269
  • 注册:2004年4月20日
发表于:2008-08-30 18:59:25
2楼
dxf文件中,圆弧有起始角度和终止角度两个参数,如果起始角度<终止角度,那就是逆时针方向的圆弧,反之则是顺时针方向的。

DB__ZS

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 3回
  • 年度积分:0
  • 历史总积分:3
  • 注册:2008年8月30日
发表于:2008-09-01 15:11:24
3楼

谢谢,反方向的问题解决了。不过急转弯的位置精度太差。我们用的是等分断开法,可能与之有关系吧,请教更先进的方法,以求得到得到高精度的结果。

DB__ZS

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 3回
  • 年度积分:0
  • 历史总积分:3
  • 注册:2008年8月30日
发表于:2008-09-01 16:13:43
4楼
还有一个导出直线方面的问题:用上述程序导出的DXF文件再导回CD解散群组并拆分后发现,本来的一条直线这时已经成为21条,重合在一起,两端分别还有一个点,不过这个点很特殊,长宽:X=0;Y=0,色彩属性和线一样。只可以选择、移动、删除,不可以改变其他属性。我也注意到:CAD导出的DXF也有这个点,不过只有一个,而且线正常不多。不知道如何能做的和CAD一样,只有一个点,当然这一个点也没有才更好呢

angelina

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 1回
  • 年度积分:0
  • 历史总积分:1
  • 注册:2008年12月25日
发表于:2008-12-25 15:16:57
5楼

请教:我收到一个文件是DXF格式的,我们目前在使用的是ET的软件,要怎么样打开这个DXF文件? 谢谢!

PistoN

  • 精华:0帖
  • 求助:1帖
  • 帖子:23帖 | 13107回
  • 年度积分:18
  • 历史总积分:40065
  • 注册:2009年5月22日
发表于:2008-12-25 21:31:10
6楼
一般来说CAD/CAM软件都能够识别DXF格式的文件

yjig

  • 精华:2帖
  • 求助:5帖
  • 帖子:97帖 | 5237回
  • 年度积分:0
  • 历史总积分:12514
  • 注册:2004年6月04日
发表于:2008-12-25 21:38:40
7楼
DXF格式的文件,仍然用AutoCAD软件打开。

热门招聘
相关主题

官方公众号

智造工程师