|
|
@@ -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<>();
|
|
|
@@ -61,8 +78,8 @@ public class LaserMeterToDxfUtil {
|
|
|
if(type.contains("AREA") || type.contains("RECTANGLE")){
|
|
|
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);
|
|
|
+ JSONObject point2 = (JSONObject) points.get(i+1 >= points.size()? 0 :i+1);
|
|
|
+ drawLine(point1,point2,pointSet,dxfDocWriter,viewpoint);
|
|
|
}
|
|
|
}
|
|
|
if(type.contains("SERIES") ){
|
|
|
@@ -71,31 +88,29 @@ public class LaserMeterToDxfUtil {
|
|
|
continue;
|
|
|
}
|
|
|
JSONObject point1 = (JSONObject) points.get(i);
|
|
|
- JSONObject point2 = (JSONObject) points.get( i+1 >= points.size()? 0 :i+1 );
|
|
|
- drawLine(point1,point2,pointSet,dxfDocWriter);
|
|
|
+ JSONObject point2 = (JSONObject) points.get( i+1 >= points.size()? 0 :i+1);
|
|
|
+ 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) {
|
|
|
- DxfArc dxfArc = new DxfArc();
|
|
|
- dxfArc.setCenter(vector3);
|
|
|
- dxfArc.setRadius(0.5);
|
|
|
- dxfArc.setStartAngle(0);
|
|
|
- dxfArc.setEndAngle(360);
|
|
|
- dxfArc.setSolid(true);
|
|
|
- dxfArc.setSolidColor(new Color(255,255,255));
|
|
|
- dxfArc.setColor(new Color(255,255,255));
|
|
|
- dxfDocWriter.addEntity(dxfArc);
|
|
|
- }
|
|
|
+// 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(255,255,255));
|
|
|
+// dxfArc.setColor(new Color(255,255,255));
|
|
|
+// //dxfDocWriter.addEntity(dxfArc);
|
|
|
+// }
|
|
|
dxfDocWriter.save(outPath, true);
|
|
|
|
|
|
}
|
|
|
@@ -107,32 +122,51 @@ 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,(point1.getDouble("z")) * 100);
|
|
|
+// endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,(point2.getDouble("z")) * 100);
|
|
|
+// }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);
|
|
|
+// }
|
|
|
+ startPoint = new Vector3((point1.getDouble("x") ) * 100 ,(point1.getDouble("y")) * 100,(point1.getDouble("z")) * 100);
|
|
|
+ endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,(point2.getDouble("z")) * 100);
|
|
|
pointSet.add(startPoint);
|
|
|
pointSet.add(endPoint);
|
|
|
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();
|
|
|
+ BigDecimal bigDecimal = BigDecimal.valueOf(distanceTo(point3d1, point3d2) ).setScale(4, RoundingMode.UP);
|
|
|
DxfText dxfText = new DxfText();
|
|
|
- Vector3 vector3 = new Vector3((endPoint.getX() + startPoint.getX()) /2 ,
|
|
|
- (endPoint.getY() + startPoint.getY() )/2 ,
|
|
|
- 0);
|
|
|
+
|
|
|
+ double px = startPoint.getX() + getPointAngle(endPoint.getX() , startPoint.getX());
|
|
|
+ double py = startPoint.getY() + getPointAngle(endPoint.getY() , startPoint.getY());
|
|
|
+ double pz = startPoint.getZ() + getPointAngle(endPoint.getZ() , startPoint.getZ());
|
|
|
+
|
|
|
+ Vector3 vector3 = new Vector3(px , py , pz);
|
|
|
dxfText.setStartPoint(vector3);
|
|
|
- dxfText.setText(bigDecimal.toString()+"cm");
|
|
|
- dxfText.setAngle(angleBetween(startPoint,endPoint));
|
|
|
- dxfText.setHigh(d *2);
|
|
|
+ dxfText.setText(bigDecimal.toString()+"m");
|
|
|
+ //dxfText.setAngle(angleBetweenVectorsRad(startPoint,endPoint));
|
|
|
+ //dxfText.setInclination(angleBetweenVectorsDeg(startPoint,endPoint));
|
|
|
+ dxfText.setHigh(bigDecimal.doubleValue() * 2);
|
|
|
dxfText.setColor(new Color(255,255,255));
|
|
|
dxfDocWriter.addEntity(dxfText);
|
|
|
}
|
|
|
|
|
|
+ public static double getPointAngle(double x ,double y){
|
|
|
+ return (2.0/5.0) * (x - y);
|
|
|
+ }
|
|
|
+
|
|
|
public static void drawLinePoint(Vector3 point,Vector3 point2,DxfDocWriter dxfDocWriter){
|
|
|
DxfLine dxfLine = new DxfLine();
|
|
|
- dxfLine.setStartPoint(new Vector3(point.getX(), point.getY(), 0));
|
|
|
- dxfLine.setEndPoint(new Vector3(point2.getX(), point2.getY(), 0));
|
|
|
+ dxfLine.setStartPoint(new Vector3(point.getX(), point.getY(), point.getZ()));
|
|
|
+ dxfLine.setEndPoint(new Vector3(point2.getX(), point2.getY(), point2.getZ()));
|
|
|
dxfLine.setColor(new Color(255,255,255));
|
|
|
dxfDocWriter.addEntity(dxfLine);
|
|
|
}
|
|
|
@@ -145,6 +179,26 @@ public class LaserMeterToDxfUtil {
|
|
|
return atan;
|
|
|
}
|
|
|
|
|
|
+ public static double angleBetweenVectorsRad(Vector3 point, Vector3 point2) {
|
|
|
+ // 计算点积
|
|
|
+ double dotProduct = point.getX() * point2.getX() + point.getY() * point2.getY() + point.getZ() * point2.getZ();
|
|
|
+
|
|
|
+ // 计算向量的模
|
|
|
+ double magA = Math.sqrt(point.getX() * point.getX() + point.getY() * point.getY() + point.getZ() * point.getZ());
|
|
|
+ double magB = Math.sqrt(point2.getX() * point2.getX() + point2.getY() * point2.getY() + point2.getZ() * point2.getZ());
|
|
|
+
|
|
|
+ // 计算余弦值,并处理可能的浮点误差
|
|
|
+ double cosTheta = dotProduct / (magA * magB);
|
|
|
+ cosTheta = Math.max(Math.min(cosTheta, 1.0), -1.0);
|
|
|
+
|
|
|
+ return Math.acos(cosTheta);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double angleBetweenVectorsDeg(Vector3 point, Vector3 point2) {
|
|
|
+ double radians = angleBetweenVectorsRad(point, point2);
|
|
|
+ return Math.toDegrees(radians);
|
|
|
+ }
|
|
|
+
|
|
|
// 计算两点之间的线段长度
|
|
|
public static double distanceBetween(Vector3 p1, Vector3 p2) {
|
|
|
double deltaX = p2.getX() - p1.getX();
|
|
|
@@ -163,7 +217,7 @@ 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); //俯视
|
|
|
}
|
|
|
|
|
|
|