数据交换文件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



   



  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
PLC是可编程逻辑控制器(Programmable Logic Controller)的缩写,是一种在工业自动化常用的控制设备。PLC通常用于监测和控制设备,以实现自动化控制和生产过程。 DXF文件是一种用于存储和交换计算机辅助设计(CAD)绘图数据的格式。DXF文件可以包含有关图形对象的几何形状、层级、颜色、线型等信息。 PLC解析DXF文件数据的过程是将DXF文件的图形对象数据提取出来,并转换为PLC可以读取和处理的格式。这样PLC就能够根据图形对象的信息来进行相应的控制操作。 为了实现PLC解析DXF文件数据,需要进行以下步骤: 1. 文件读取:PLC应该具备能够读取DXF文件的能力,可以通过专门的软件工具或自行编写程序来实现。通过文件读取操作,PLC可以获取DXF文件的原始数据。 2. 数据解析:PLC需要对DXF文件的数据进行解析,将其的图形对象数据提取出来。这需要对DXF文件的文件结构和数据格式有一定的理解。 3. 数据转换:解析出的图形对象数据可能并不是PLC能直接读取和处理的格式。因此,需要将这些数据转换为PLC可识别的格式,例如二进制或特定的数据格式。 4. 控制操作:将转换后的数据传输到PLC,并根据图形对象的信息进行相应的控制操作。例如,根据图形对象的位置信息控制机械装置的移动,根据图形对象的颜色信息控制灯光的开关等。 通过PLC解析DXF文件数据,可以实现工业自动化控制与CAD绘图数据的结合,为自动化生产提供更高的灵活性和精度。同时,PLC解析DXF文件数据也有助于提高工作效率和降低人为错误的风险。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NewTech精选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值