數據交換文件DXF簡單java解析

     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<LwPolylineVertex> 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<LwPolylineVertex>();
    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<LwPolylineVertex> getVertexes() {
		return vertexes;
	}

	public void setVertexes(List<LwPolylineVertex> 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<PolylineVertex> polyVertexes = new ArrayList<PolylineVertex>();
         
         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文檔類.

  1. 以流的方式加載文件
     File f = new File(file);
                FileInputStream fis = new FileInputStream(f);
                InputStreamReader isr = new InputStreamReader(fis, "utf-8");
                BufferedReader br = new BufferedReader(isr);

  2. 解析文件
     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();
            }
    
        }

  3. 解析具體的entity段
      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<IPolyline> ReadLwpolyline() throws Exception {
        LwPolyLine newlw = new LwPolyLine();
        LwPolylineVertex v = new LwPolylineVertex();
        float constantWidth = 0.0f;
        double vX = 0.0;
        ArrayList<LwPolylineVertex> list = new ArrayList<LwPolylineVertex>();
        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<LwPolylineVertex>) 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



   



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章