lyhzzz 1 month ago
parent
commit
a77ce175e8

+ 45 - 17
4dkankan-utils-dxf/src/main/java/com/fdkankan/dxf/parse/utils/LaserMeterToDxfUtil.java

@@ -2,6 +2,7 @@ package com.fdkankan.dxf.parse.utils;
 
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.thread.ThreadUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.dxf.fdjson.vo.FdPoints;
@@ -32,14 +33,30 @@ public class LaserMeterToDxfUtil {
      * @param outPath dxf输出地址
      */
 
-    public static void toDxf(File inFile, String outPath) throws Exception{
+    public 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);
+        toDxf(jsonObject,outPath,0);
     }
     public static void toDxf(String inPath, String outPath) throws Exception {
         JSONArray jsonObject = JSONArray.parseArray(inPath);
-        toDxf(jsonObject,outPath);
+        toDxf(jsonObject,outPath,0);
+    }
+    public static void toDxf(JSONArray jsonArray, String outPath) throws Exception{
+        work(jsonArray,outPath,0);
+    }
+
+    public static void toDxf(File inFile, String outPath,Integer viewpoint) throws Exception{
+        String msg = FileUtil.readString(inFile, StandardCharsets.UTF_8);
+        JSONArray jsonObject = JSONArray.parseArray(msg);
+        toDxf(jsonObject,outPath,viewpoint);
+    }
+    public static void toDxf(String inPath, String outPath,Integer viewpoint) throws Exception {
+        JSONArray jsonObject = JSONArray.parseArray(inPath);
+        toDxf(jsonObject,outPath,viewpoint);
+    }
+    public static void toDxf(JSONArray jsonArray, String outPath,Integer viewpoint) throws Exception{
+        work(jsonArray,outPath,viewpoint);
     }
 
     /**
@@ -49,7 +66,7 @@ public class LaserMeterToDxfUtil {
      * @throws Exception
      */
 
-    public static void toDxf(JSONArray jsonArray, String outPath) throws Exception{
+    private static void work(JSONArray jsonArray, String outPath,Integer viewpoint) throws Exception{
 
         DxfDocWriter dxfDocWriter = new DxfDocWriter();
         HashSet<Vector3> pointSet= new HashSet<>();
@@ -62,7 +79,7 @@ public class LaserMeterToDxfUtil {
                 for (int i = 0 ;i < points.size();i ++){
                     JSONObject point1 = (JSONObject) points.get(i);
                     JSONObject point2 = (JSONObject) points.get(  i+1 >= points.size()? 0 :i+1 );
-                    drawLine(point1,point2,pointSet,dxfDocWriter);
+                    drawLine(point1,point2,pointSet,dxfDocWriter,viewpoint);
                 }
             }
             if(type.contains("SERIES") ){
@@ -72,17 +89,15 @@ public class LaserMeterToDxfUtil {
                     }
                     JSONObject point1 = (JSONObject) points.get(i);
                     JSONObject point2 = (JSONObject) points.get(  i+1 >= points.size()? 0 :i+1 );
-                    drawLine(point1,point2,pointSet,dxfDocWriter);
+                    drawLine(point1,point2,pointSet,dxfDocWriter,viewpoint);
                 }
             }
             if(type.contains("LINE") ){
                 JSONObject point1 = (JSONObject) (points.get(0));
                 JSONObject point2 = (JSONObject) (points.get(1));
-                drawLine(point1,point2,pointSet,dxfDocWriter);
+                drawLine(point1,point2,pointSet,dxfDocWriter,viewpoint);
             }
 
-
-
         }
 
         for (Vector3 vector3 : pointSet) {
@@ -107,21 +122,28 @@ public class LaserMeterToDxfUtil {
      *
      * 正交投影 忽略z
      */
-    public static void drawLine(JSONObject point1, JSONObject point2, HashSet<Vector3> pointSet,DxfDocWriter dxfDocWriter){
+    public static void drawLine(JSONObject point1, JSONObject point2, HashSet<Vector3> pointSet,DxfDocWriter dxfDocWriter,Integer viewpoint){
         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") ) * 100 ,(point1.getDouble("y")) * 100,0);
-        Vector3 endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,0);
+        Vector3 startPoint;
+        Vector3 endPoint ;
+        if(viewpoint == 0){ //俯视角
+            startPoint = new Vector3((point1.getDouble("x") ) * 100 ,(point1.getDouble("y")) * 100,0);
+            endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,0);
+        }else if(viewpoint ==1) {
+            startPoint = new Vector3((point1.getDouble("x") ) * 100 ,(point1.getDouble("z")) * 100,0);
+            endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("z")) * 100,0);
+        }else {
+            startPoint = new Vector3((point1.getDouble("y") ) * 100 ,(point1.getDouble("z")) * 100,0);
+            endPoint = new Vector3((point2.getDouble("y") ) * 100 ,(point2.getDouble("z")) * 100,0);
+        }
+
         pointSet.add(startPoint);
         pointSet.add(endPoint);
         drawLinePoint(startPoint,endPoint,dxfDocWriter);
         BigDecimal bigDecimal = BigDecimal.valueOf(distanceTo(point3d1, point3d2) ).setScale(4, RoundingMode.UP);
         DxfText dxfText = new DxfText();
 
-//        Double x = (endPoint.getX() + startPoint.getX()) / 2  ;
-//        Double y = (endPoint.getY() + startPoint.getY()) / 2;
-//        double px = (2 * startPoint.getX() + endPoint.getX()) / 3;
-//        double py = (2 * startPoint.getY() + endPoint.getY()) / 3;
 
         double px = startPoint.getX() + (2.0/5.0) * (endPoint.getX() - startPoint.getX());
         double py = startPoint.getY() + (2.0/5.0) * (endPoint.getY() - startPoint.getY());
@@ -169,7 +191,13 @@ public class LaserMeterToDxfUtil {
     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(new File(inPath),outPath);
+        LaserMeterToDxfUtil.toDxf(new File(inPath),outPath, 0);  //俯视
+        Thread.sleep(1000L);
+        String outPath2 ="D:\\cad\\work\\111\\"+new Date().getTime()+".dxf";
+        LaserMeterToDxfUtil.toDxf(new File(inPath),outPath2, 1); //正视
+        Thread.sleep(1000L);
+        String outPath3 ="D:\\cad\\work\\111\\"+new Date().getTime()+".dxf";
+        LaserMeterToDxfUtil.toDxf(new File(inPath),outPath3, 2); //侧视
     }