DXF是AutoCAD与其他应用程序交换数据时使用的文件,有多种格式,这里所说的是指ASCII DXF 格式文件.
这里主要实现java程序对DXF文件的解析,将几何体进行分类存储,便于各个程序间进行调用.下面进行文件的解析过程介绍:
1.既然是解析文件,一定要先定义实体类,这里以LwPolyline为例,还有解析成json的实体类
public class LwPolyLine extends DxfObject implements IPolyline{
private static final EntityType TYPE = EntityType.LightWeightPolyline;
@JsonProperty(value = "Vertexes")
private List vertexes;
@JsonProperty(value = "IsClosed")
private boolean isClosed;
@JsonProperty(value = "Flags")
private String flags;
@JsonProperty(value = "Layer")
private Layer layer;
@JsonProperty(value = "Color")
private AciColor color;
@JsonProperty(value = "LineType")
private LineType lineType;
@JsonProperty(value = "Normal")
private Vector3f normal;
@JsonProperty(value = "Elevation")
private float elevation;
@JsonProperty(value = "Thickness")
private float thickness;
@JsonProperty(value = "CodeName")
private String codeName;
@JsonProperty(value = "Type")
public EntityType type;
@Override
public String getType() {
// TODO Auto-generated method stub
return this.TYPE.value();
}
@Override
public AciColor getColor() {
// TODO Auto-generated method stub
return this.color;
}
@Override
public void setColor(AciColor color) {
// TODO Auto-generated method stub
this.color=color;
}
@Override
public Layer getLayer() {
// TODO Auto-generated method stub
return this.layer;
}
@Override
public void setLayer(Layer layer) {
// TODO Auto-generated method stub
this.layer=layer;
}
@Override
public LineType getLineType() {
// TODO Auto-generated method stub
return this.lineType;
}
@Override
public void setLineType(LineType lineType) {
// TODO Auto-generated method stub
this.lineType=lineType;
}
@Override
public String getFlags() {
// TODO Auto-generated method stub
return PolylineTypeFlags.getFlags(Byte.valueOf(flags)).getNum();
}
public LwPolyLine()
{
this.setCodeName(DxfObjectCode.LightWeightPolyline);
this.vertexes = new ArrayList();
this.isClosed = false;
this.layer = new Layer("0");
this.color = new AciColor((short)256);
this.lineType = new LineType("ByLayer");
this.normal = new Vector3f(0, 0, 1);
this.elevation = 0.0f;
this.flags = PolylineTypeFlags.OpenPolyline.getNum();
}
public List getVertexes() {
return vertexes;
}
public void setVertexes(List vertexes) {
this.vertexes = vertexes;
}
public boolean getIsClosed() {
return isClosed;
}
public void setIsClosed(boolean isClosed) {
this.isClosed = isClosed;
}
public Vector3f getNormal() {
return normal;
}
public void setNormal(Vector3f normal) {
this.normal = normal;
}
public float getElevation() {
return elevation;
}
public void setElevation(float elevation) {
this.elevation = elevation;
}
public float getThickness() {
return thickness;
}
public void setThickness(float thickness) {
this.thickness = thickness;
}
public void setFlags(PolylineTypeFlags flags) {
this.flags = flags.getNum();
}
public Polyline toPolyline() {
List polyVertexes = new ArrayList();
for (PolylineVertex polylineVertex : polyVertexes) {
polyVertexes.add(new PolylineVertex(polylineVertex.getLocation()).setBeginThickness(polylineVertex.getBeginThickness())
.setBulge(polylineVertex.getBulge()).setEndThickness(polylineVertex.getBulge())
);
}
return new Polyline(polyVertexes, this.isClosed);
}
@Override
public String toString() {
return "LwPolyLine [vertexes=" + vertexes + ", isClosed=" + isClosed + ", flags=" + flags + ", layer=" + layer
+ ", color=" + color + ", lineType=" + lineType + ", normal=" + normal + ", elevation=" + elevation
+ ", thickness=" + thickness + "]";
}
public String getCodeName() {
return codeName;
}
public DxfObject setCodeName(String codeName) {
this.codeName = codeName;
return this;
}
}
2.加载文件,并进行读取.这里声明一点就是要以面向对象的思想来解析Dxf文件,这里定义一个 DxfDocument的DXF文档类.
File f = new File(file);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
BufferedReader br = new BufferedReader(isr);
public void Read() {
try {
String temp = br.readLine();
while (temp != null) {
String code = temp;
String codedata = br.readLine();
str = new String[] { code.trim(), codedata.trim() };
if (str[1].equals("HEADER"))
ReadHeader();
if (str[1].equals("ENTITIES")) {
ReadEntities();
}
temp = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void ReadEntities() {
try {
while (!str[1].equals("ENDSEC")) {
if (str[1].equals("LINE")) {
ReadLine();
} else if (str[1].equals("ARC"))
ReadArc();
else if (str[1].equals("LWPOLYLINE")) {
ReadLwpolyline();
} else if (str[1].equals("TEXT")) {
ReadText();
} else if (str[1].equals("MTEXT")) {
ReadMText();
} else
str = ReadPair();
}
} catch (Exception e) {
e.printStackTrace();
}
}
4.进行LwPolyline解析
仅仅对部分组码进行了解析,应该按项目需要进行解析
public List ReadLwpolyline() throws Exception {
LwPolyLine newlw = new LwPolyLine();
LwPolylineVertex v = new LwPolylineVertex();
float constantWidth = 0.0f;
double vX = 0.0;
ArrayList list = new ArrayList();
while (!str[1].equals("ENDSEC")) {
str = ReadPair();
if (str[0].equals("5")) {
newlw.handle = str[1];
}
if (str[0].equals("8")) {
newlw.setLayer(newlw.getLayer().setName(new String(str[1].getBytes("utf-8"), "utf-8")));
}
if (str[0].equals("370"))
newlw.setThickness(Float.valueOf(str[1]));
if (str[0].equals("62"))
newlw.setColor(new AciColor(Short.valueOf(str[1].trim())));
if (str[0].equals("70")) {
if (Integer.parseInt(str[1].trim()) == 1) {
newlw.setIsClosed(true);
} else if (Integer.parseInt(str[1].trim()) == 0) {
newlw.setIsClosed(false);
}
}
if (str[0].equals("43")) {
constantWidth = Float.valueOf(str[1]);
}
if (str[0].equals("210")) {
newlw.setNormal(newlw.getNormal().setX(Double.valueOf(str[1])));
}
if (str[0].equals("220")) {
newlw.setNormal(newlw.getNormal().setY(Double.valueOf(str[1])));
}
if (str[0].equals("230")) {
newlw.setNormal(newlw.getNormal().setZ(Double.valueOf(str[1])));
}
while (str[0].equals("10") || (str[0].equals("20"))) {
if (str[0].equals("10")) {
v = new LwPolylineVertex();
v.setBeginThickness(constantWidth);
v.setEndThickness(constantWidth);
vX = Double.valueOf(str[1]);
str = ReadPair();
}
if (str[0].equals("20")) {
double vY = Float.valueOf(str[1]);
v.setLocation(new Vector2f(vX, vY));
if (v.getLocation().getX() > Xmax)
Xmax = v.getLocation().getX();
if (v.getLocation().getX() < Xmin)
Xmin = v.getLocation().getX();
if (v.getLocation().getY() > Ymax)
Ymax = v.getLocation().getY();
if (v.getLocation().getY() < Ymin) {
Ymin = v.getLocation().getY();
}
list = (ArrayList) newlw.getVertexes();
list.add(v);
str = ReadPair();
newlw.setVertexes(list);
}
}
if (str[0].equals("0")) {
polylines.add((IPolyline) newlw);
break;
}
}
return polylines;
}
5.至此解析完成.已经实现了ASCII DXF文件中描述的对象文本信息向Java对象的解析.在使用Json工具可以转为Json