Przeglądaj źródła

jp测量数据导出Dxf

lyhzzz 5 miesięcy temu
rodzic
commit
f20e28f20c

+ 88 - 54
4dkankan-utils-dxf/src/main/java/com/fdkankan/dxf/parse/utils/LaserMeterToDxfUtil.java

@@ -1,6 +1,7 @@
 package com.fdkankan.dxf.parse.utils;
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.dxf.fdjson.vo.FdPoints;
@@ -12,71 +13,95 @@ import com.fdkankan.dxf.generate.model.DxfPoint;
 import com.fdkankan.dxf.generate.model.DxfText;
 import com.fdkankan.dxf.generate.model.base.Color;
 
+import java.io.File;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 
 public class LaserMeterToDxfUtil {
 
     /**
      * 场景测量数据转换dxf
-     * @param inPath  json输入文件地址
+     * @param inFile  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);
+
+    private static void toDxf(File inFile, String outPath) throws Exception{
+        String msg = FileUtil.readString(inFile, StandardCharsets.UTF_8);
+        JSONArray jsonObject = JSONArray.parseArray(msg);
+        toDxf(jsonObject,outPath);
+    }
+    private static void toDxf(String inPath, String outPath) throws Exception {
+        JSONArray jsonObject = JSONArray.parseArray(inPath);
+        toDxf(jsonObject,outPath);
+    }
+
+    /**
+     *
+     * @param jsonArray  线数组
+     * @param outPath 输出路径
+     * @throws Exception
+     */
+
+    private static void toDxf(JSONArray jsonArray, String outPath) throws Exception{
+
+        DxfDocWriter dxfDocWriter = new DxfDocWriter();
+        HashSet<Vector3> pointSet= new HashSet<>();
+
+
+        /**
+         * 透视投影 模拟人眼视角,将三维点投影到二维平面,常用于计算机图形学。公式为:
+         * x1 = x/z ,y1=y/z
+         *
+         * 正交投影 忽略z
+         */
+        for (Object obj : jsonArray) {
+            JSONObject jsonObject = (JSONObject) (obj);
+            JSONArray points = jsonObject.getJSONArray("dataset_points");
+            JSONObject point1 = (JSONObject) (points.get(0));
+            JSONObject point2 = (JSONObject) (points.get(1));
+            Vector3 point3d1 = new Vector3(point1.getDouble("x"),point1.getDouble("y"),point1.getDouble("z"));
+            Vector3 point3d2 = new Vector3(point2.getDouble("x"),point2.getDouble("y"),point2.getDouble("z"));
+
+            //Vector3 startPoint = new Vector3((point1.getDouble("x") /point1.getDouble("z")) * 100 ,(point1.getDouble("y")/point1.getDouble("z")) * 100,0);
+            //Vector3 endPoint = new Vector3((point2.getDouble("x") /point2.getDouble("z")) * 100 ,(point2.getDouble("y")/point2.getDouble("z")) * 100,0);
+            Vector3 startPoint = new Vector3((point1.getDouble("x") ) * 100 ,(point1.getDouble("y")) * 100,0);
+            Vector3 endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,0);
+            pointSet.add(startPoint);
+            pointSet.add(endPoint);
+            FdJsonToDxfUtil.drawLinePoint(startPoint,endPoint,dxfDocWriter);
+            BigDecimal bigDecimal = BigDecimal.valueOf(distanceTo(point3d1, point3d2) * 100).setScale(5, RoundingMode.UP);
+            int d = bigDecimal.divide(new BigDecimal(100),2, RoundingMode.UP).intValue();
+            DxfText dxfText = new DxfText();
+            Vector3 vector3 = new Vector3((endPoint.getX() + startPoint.getX()) /2 ,
+                    (endPoint.getY() + startPoint.getY() )/2 ,
+                    0);
+            dxfText.setStartPoint(vector3);
+            dxfText.setText(bigDecimal.toString()+"cm");
+            dxfText.setAngle(angleBetween(startPoint,endPoint));
+            dxfText.setHigh(d *2);
+            dxfDocWriter.addEntity(dxfText);
+
+        }
+
+        for (Vector3 vector3 : pointSet) {
+            DxfArc dxfArc = new DxfArc();
+            dxfArc.setCenter(vector3);
+            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));
+            dxfDocWriter.addEntity(dxfArc);
+        }
+        dxfDocWriter.save(outPath, true);
+
     }
 
 
@@ -93,10 +118,19 @@ public class LaserMeterToDxfUtil {
         double deltaY = p2.getY() - p1.getY();
         return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
     }
+
+    // 计算两点之间的距离
+    public static double distanceTo(Vector3 p1, Vector3 p2) {
+        double dx = p1.getX() - p2.getX();
+        double dy = p1.getY() - p2.getY();
+        double dz = p1.getZ() - p2.getZ();
+        return Math.sqrt(dx * dx + dy * dy + dz * dz);
+    }
+
     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);
+        LaserMeterToDxfUtil.toDxf(new File(inPath),outPath);
     }