Dxf文件格式以及使用kabeja解析Dxf文件(1)

Dxf文件格式
http://en.wikipedia.org/wiki/Dxf
http://www.autodesk.com/techpubs/autocad/acadr14/dxf/dxf_group_codes.htm

没想到dxf是文本格式
其结构如下

  • HEADER section – General information about the drawing.
    Each parameter has a variable name and an associated value.
  • CLASSES section – Holds the information for
    application-defined classes whose instances appear in the BLOCKS,
    ENTITIES, and OBJECTS sections of the database. Generally does not
    provide sufficient information to allow interoperability with other
    programs.
  • TABLES section – This section contains definitions of
    named items.
Application ID (APPID) table
Block Record (BLOCK_RECORD) table
Dimension Style (DIMSTYPE) table
Layer (LAYER) table
Linetype (LTYPE) table
Text style (STYLE) table
User Coordinate System (UCS) table
View (VIEW) table
Viewport configuration (VPORT) table
  • BLOCKS section – This section contains Block Definition
    entities describing the entities comprising each Block in the drawing.
  • ENTITIES section – This section contains the drawing
    entities, including any Block References.
  • OBJECTS section – Contains the data that apply to
    nongraphical objects, used by AutoLISP and ObjectARX applications.
  • THUMBNAILIMAGE section – Contains the preview image for
    the DXF file.
  • END OF FILE

其中
header中定义了起始坐标和坐标范围
entity表示基本图形,如点线面…
block表示元件,如一把椅子,block由一堆entity组成
Object包含非图形属性,用于二次开发 ???

用kabeja来查看dxf文件
http://kabeja.sourceforge.net/

Kabeja is a Java library for parsing,
processing and converting
Autodesk’s
DXF format. You can use Kabeja from the CommandLine or embed into your
application. All parsed data are accessible with the DOM-like API.

The processing system allows you to process, filter
(e.g. XSLT) and convert DXF drafts to different output formats (SVG,
JPEG, PNG, TIFF, PDF, XML).

用下面的代码读取dxf文件,得到DXFDocument,可以操作DXFBlock,DXFLayer,DXFEntity …
Parser p = ParserBuilder.createDefaultParser();

p.parse(ResourceAgent.class.getResource(“ESSEMBLE.DXF”).getFile());
DXFDocument doc = p.getDocument();

System.out.println(“DXFBlock:”);
output(doc.getDXFBlockIterator());

System.out.println(“Objects:”);
output(doc.getObjects());

System.out.println(“Layers:”);
output(doc.getDXFLayerIterator());

System.out.println(“Entity”);
DXFEntity
entity=(DXFEntity)((DXFBlock)doc.getDXFBlockIterator().next()).getDXFEntitiesIterator().next();
output(entity);

kabeja有一个svg的客户端,使用了batik项目相关库,kabeja-svg-0.4.jar没有提供源代码,可以反编辑查看细节

package kabeja;

import java.util.Iterator;
import java.util.Map;

import org.kabeja.dxf.DXFBlock;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.DXFEntity;
import org.kabeja.dxf.DXFLayer;
import org.kabeja.dxf.DXFLine;
import org.kabeja.dxf.DXFPolyline;
import org.kabeja.dxf.DXFVertex;
import org.kabeja.dxf.objects.DXFDictionary;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;

import resource.ResourceAgent;

public class DXFParserTest {
public static void main(String[] args) throws ParseException {
Parser p = ParserBuilder.createDefaultParser();

p.parse(ResourceAgent.class.getResource(“ESSEMBLE.DXF”).getFile());
DXFDocument doc = p.getDocument();

System.out.println(“DXFBlock:”);
output(doc.getDXFBlockIterator());

System.out.println(“Objects:”);
output(doc.getObjects());

System.out.println(“Layers:”);
output(doc.getDXFLayerIterator());

System.out.println(“Entity”);
DXFEntity
entity=(DXFEntity)((DXFBlock)doc.getDXFBlockIterator().next()).getDXFEntitiesIterator().next();
output(entity);
}

private static void output(Object o){
if(o==null){
throw new IllegalArgumentException(“params can not be
null”);
}
if(o instanceof Map){
for(Object key:((Map) o).keySet()){
System.out.println(key);
output(((Map)o).get(key));
}
}else if(o instanceof Iterator){
Iterator it=(Iterator)o;
while(it.hasNext()){
output(it.next());
}
}else if(o instanceof DXFDictionary){
DXFDictionary d=(DXFDictionary)o;
System.out.println(d.getID()+”:”+d.getObjectType());
if(d.getDXFObjectIterator()!=null){
output(d.getDXFObjectIterator());
}
}else if(o instanceof DXFBlock){
DXFBlock d=(DXFBlock)o;

System.out.println(d.getName()+”:”+d.getBounds().getWidth()+”,”+d.getBounds().getHeight());
}else if(o instanceof DXFLayer){
DXFLayer d=(DXFLayer)o;
System.out.println(d.getName());
}else if(o instanceof DXFEntity){
DXFEntity d=(DXFEntity)o;
System.out.println(“ID:”+d.getID()+” Type:”+d.getType()+”
Color:”+d.getColor());
if(d instanceof DXFLine){
System.out.println(“startPoint:”);
output(((DXFLine)d).getStartPoint());
System.out.println(“endPoint:”);
output(((DXFLine)d).getEndPoint());
}else if(d instanceof DXFPolyline){
DXFPolyline dpl=(DXFPolyline)d;
output(dpl.getVertexIterator());
}
}else if(o instanceof DXFVertex){
DXFVertex d=(DXFVertex)o;
System.out.println(“type:”+d.getType());
System.out.println(“point:”);
output(d.getPoint());
}else if(o instanceof org.kabeja.dxf.helpers.Point){
org.kabeja.dxf.helpers.Point
p=(org.kabeja.dxf.helpers.Point)o;

System.out.println(“["+p.getX()+","+p.getY()+","+p.getZ()+"]“);
}else{
System.out.println(o.toString());
}
}
}

输出:
DXFBlock:
*A27:21.272479599999997,10.127198000000021
*A26:22.968773199999987,8.288786999999985
CARTOUCH:180.5,32.5
*A25:13.305412200000006,9.02374949999998
*A24:12.500476600000013,9.183685999999994
*A23:10.3298032,14.429505000000006
*A22:10.523896000000008,12.423013499999968
*A21:10.973129,10.269785500000012
*G9:10.0,5.630691999999982
*A20:14.26597959999998,5.345429000000024
*G5:5.0,22.0
*G4:5.0,22.0
*H8:25.0,25.402900999999986
*H18:20.753310999999997,5.009576999999979
*H7:6.6294979999999555,35.0
*H17:65.437083,5.019422999999961
*H6:6.468677000000014,35.0
*H16:13.189459,5.017488000000014
*H15:2.5,5.0
Bloc_2:188.432070756676,31.874719164807452
*H14:3.75,21.0
Bloc_1:29.25,2.8125
*A19:10.345729500000033,12.911685749999947
*H13:3.75,20.879391
*H12:3.75,20.23961699999998
*H11:3.75,21.002515000000017
*H10:16.663907999999992,22.028070000000014
A4-V:190.0,277.0
Objects:
Layers:
0
Entity
ID: Type:POLYLINE Color:1


− 1 = 三