|
@@ -0,0 +1,103 @@
|
|
|
+package com.fdkankan.dxf.parse.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.dxf.fdjson.vo.FdPoints;
|
|
|
+import com.fdkankan.dxf.generate.DxfDocWriter;
|
|
|
+import com.fdkankan.dxf.generate.Vector3;
|
|
|
+import com.fdkankan.dxf.generate.enums.LineWidthEnum;
|
|
|
+import com.fdkankan.dxf.generate.model.DxfArc;
|
|
|
+import com.fdkankan.dxf.generate.model.DxfPoint;
|
|
|
+import com.fdkankan.dxf.generate.model.DxfText;
|
|
|
+import com.fdkankan.dxf.generate.model.base.Color;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+public class LaserMeterToDxfUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 场景测量数据转换dxf
|
|
|
+ * @param inPath json输入文件地址
|
|
|
+ * @param outPath dxf输出地址
|
|
|
+ */
|
|
|
+ private static void toDxf(String inPath, String outPath) throws Exception{
|
|
|
+
|
|
|
+ DxfDocWriter dxfDocWriter = new DxfDocWriter();
|
|
|
+
|
|
|
+ String msg = new String(Files.readAllBytes(Paths.get(inPath)));
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(msg);
|
|
|
+ JSONArray points = jsonObject.getJSONArray("control_points_3d"); //点
|
|
|
+ JSONArray lines = jsonObject.getJSONArray("distances"); //线
|
|
|
+
|
|
|
+ HashMap<String, Vector3> pointMap= new HashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模拟人眼视角,将三维点投影到二维平面,常用于计算机图形学。公式为:
|
|
|
+ * x1 = x/z ,y1=y/z
|
|
|
+ */
|
|
|
+ for (Object obj : points) {
|
|
|
+ JSONObject point = (JSONObject) (obj);
|
|
|
+ Vector3 vector3 = new Vector3(point.getDouble("x") /point.getDouble("z") * 100 ,point.getDouble("y")/point.getDouble("z") * 100,0);
|
|
|
+ pointMap.put(point.getString("uuid"),vector3);
|
|
|
+ }
|
|
|
+ for (Object obj : lines) {
|
|
|
+ JSONObject line = (JSONObject) (obj);
|
|
|
+ String startPoint = line.getString("uuid_start");
|
|
|
+ String endPoint = line.getString("uuid_end");
|
|
|
+ Double distance = line.getDouble("distance");
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(distance).setScale(5,BigDecimal.ROUND_UP);
|
|
|
+ FdJsonToDxfUtil.drawLinePoint(pointMap.get(startPoint),pointMap.get(endPoint),dxfDocWriter);
|
|
|
+
|
|
|
+ DxfText dxfText = new DxfText();
|
|
|
+ Vector3 vector3 = new Vector3((pointMap.get(endPoint).getX() + pointMap.get(startPoint).getX()) /2 -10,
|
|
|
+ (pointMap.get(endPoint).getY() + pointMap.get(startPoint).getY() )/2 -10,
|
|
|
+ 0);
|
|
|
+ dxfText.setStartPoint(vector3);
|
|
|
+ dxfText.setText(bigDecimal.toString()+"cm");
|
|
|
+ dxfText.setAngle(angleBetween(pointMap.get(startPoint),pointMap.get(endPoint)));
|
|
|
+ dxfDocWriter.addEntity(dxfText);
|
|
|
+ }
|
|
|
+ for (String s : pointMap.keySet()) {
|
|
|
+ DxfArc dxfArc = new DxfArc();
|
|
|
+ dxfArc.setCenter(pointMap.get(s));
|
|
|
+ dxfArc.setRadius(0.5);
|
|
|
+ dxfArc.setStartAngle(0);
|
|
|
+ dxfArc.setEndAngle(360);
|
|
|
+ dxfArc.setSolid(true);
|
|
|
+ dxfArc.setSolidColor(new Color(0, 255, 0));
|
|
|
+ dxfArc.setColor(new Color(0, 255, 0));
|
|
|
+// DxfPoint dxfPoint = new DxfPoint();
|
|
|
+// dxfPoint.setPoint(pointMap.get(s));
|
|
|
+// dxfPoint.setColor(new Color(0, 255, 0));
|
|
|
+ dxfDocWriter.addEntity(dxfArc);
|
|
|
+ }
|
|
|
+ dxfDocWriter.save(outPath, true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 计算两点之间的角度(相对于水平轴,单位为弧度)
|
|
|
+ public static double angleBetween(Vector3 point, Vector3 point2) {
|
|
|
+ //角度
|
|
|
+ Double atan = Math.atan((point2.getY()-point.getY()) / (point2.getX()-point.getX())) / 3.14 * 180;
|
|
|
+ return atan;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算两点之间的线段长度
|
|
|
+ public static double distanceBetween(Vector3 p1, Vector3 p2) {
|
|
|
+ double deltaX = p2.getX() - p1.getX();
|
|
|
+ double deltaY = p2.getY() - p1.getY();
|
|
|
+ return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
|
|
|
+ }
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+ String inPath ="D:\\cad\\work\\111\\1.json";
|
|
|
+ String outPath ="D:\\cad\\work\\111\\"+new Date().getTime()+".dxf";
|
|
|
+ LaserMeterToDxfUtil.toDxf(inPath,outPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|