LaserMeterToDxfUtil.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.fdkankan.dxf.parse.utils;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fdkankan.dxf.fdjson.vo.FdPoints;
  7. import com.fdkankan.dxf.generate.DxfDocWriter;
  8. import com.fdkankan.dxf.generate.Vector3;
  9. import com.fdkankan.dxf.generate.enums.LineWidthEnum;
  10. import com.fdkankan.dxf.generate.model.DxfArc;
  11. import com.fdkankan.dxf.generate.model.DxfPoint;
  12. import com.fdkankan.dxf.generate.model.DxfText;
  13. import com.fdkankan.dxf.generate.model.base.Color;
  14. import java.io.File;
  15. import java.math.BigDecimal;
  16. import java.math.RoundingMode;
  17. import java.nio.charset.StandardCharsets;
  18. import java.nio.file.Files;
  19. import java.nio.file.Paths;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. public class LaserMeterToDxfUtil {
  24. /**
  25. * 场景测量数据转换dxf
  26. * @param inFile json输入文件
  27. * @param outPath dxf输出地址
  28. */
  29. private static void toDxf(File inFile, String outPath) throws Exception{
  30. String msg = FileUtil.readString(inFile, StandardCharsets.UTF_8);
  31. JSONArray jsonObject = JSONArray.parseArray(msg);
  32. toDxf(jsonObject,outPath);
  33. }
  34. private static void toDxf(String inPath, String outPath) throws Exception {
  35. JSONArray jsonObject = JSONArray.parseArray(inPath);
  36. toDxf(jsonObject,outPath);
  37. }
  38. /**
  39. *
  40. * @param jsonArray 线数组
  41. * @param outPath 输出路径
  42. * @throws Exception
  43. */
  44. private static void toDxf(JSONArray jsonArray, String outPath) throws Exception{
  45. DxfDocWriter dxfDocWriter = new DxfDocWriter();
  46. HashSet<Vector3> pointSet= new HashSet<>();
  47. /**
  48. * 透视投影 模拟人眼视角,将三维点投影到二维平面,常用于计算机图形学。公式为:
  49. * x1 = x/z ,y1=y/z
  50. *
  51. * 正交投影 忽略z
  52. */
  53. for (Object obj : jsonArray) {
  54. JSONObject jsonObject = (JSONObject) (obj);
  55. JSONArray points = jsonObject.getJSONArray("points");
  56. JSONObject point1 = (JSONObject) (points.get(0));
  57. JSONObject point2 = (JSONObject) (points.get(1));
  58. Vector3 point3d1 = new Vector3(point1.getDouble("x"),point1.getDouble("y"),point1.getDouble("z"));
  59. Vector3 point3d2 = new Vector3(point2.getDouble("x"),point2.getDouble("y"),point2.getDouble("z"));
  60. //Vector3 startPoint = new Vector3((point1.getDouble("x") /point1.getDouble("z")) * 100 ,(point1.getDouble("y")/point1.getDouble("z")) * 100,0);
  61. //Vector3 endPoint = new Vector3((point2.getDouble("x") /point2.getDouble("z")) * 100 ,(point2.getDouble("y")/point2.getDouble("z")) * 100,0);
  62. Vector3 startPoint = new Vector3((point1.getDouble("x") ) * 100 ,(point1.getDouble("y")) * 100,0);
  63. Vector3 endPoint = new Vector3((point2.getDouble("x") ) * 100 ,(point2.getDouble("y")) * 100,0);
  64. pointSet.add(startPoint);
  65. pointSet.add(endPoint);
  66. FdJsonToDxfUtil.drawLinePoint(startPoint,endPoint,dxfDocWriter);
  67. BigDecimal bigDecimal = BigDecimal.valueOf(distanceTo(point3d1, point3d2) * 100).setScale(5, RoundingMode.UP);
  68. int d = bigDecimal.divide(new BigDecimal(100),2, RoundingMode.UP).intValue();
  69. DxfText dxfText = new DxfText();
  70. Vector3 vector3 = new Vector3((endPoint.getX() + startPoint.getX()) /2 ,
  71. (endPoint.getY() + startPoint.getY() )/2 ,
  72. 0);
  73. dxfText.setStartPoint(vector3);
  74. dxfText.setText(bigDecimal.toString()+"cm");
  75. dxfText.setAngle(angleBetween(startPoint,endPoint));
  76. dxfText.setHigh(d *2);
  77. dxfDocWriter.addEntity(dxfText);
  78. }
  79. for (Vector3 vector3 : pointSet) {
  80. DxfArc dxfArc = new DxfArc();
  81. dxfArc.setCenter(vector3);
  82. dxfArc.setRadius(0.5);
  83. dxfArc.setStartAngle(0);
  84. dxfArc.setEndAngle(360);
  85. dxfArc.setSolid(true);
  86. dxfArc.setSolidColor(new Color(0, 255, 0));
  87. dxfArc.setColor(new Color(0, 255, 0));
  88. dxfDocWriter.addEntity(dxfArc);
  89. }
  90. dxfDocWriter.save(outPath, true);
  91. }
  92. // 计算两点之间的角度(相对于水平轴,单位为弧度)
  93. public static double angleBetween(Vector3 point, Vector3 point2) {
  94. //角度
  95. Double atan = Math.atan((point2.getY()-point.getY()) / (point2.getX()-point.getX())) / 3.14 * 180;
  96. return atan;
  97. }
  98. // 计算两点之间的线段长度
  99. public static double distanceBetween(Vector3 p1, Vector3 p2) {
  100. double deltaX = p2.getX() - p1.getX();
  101. double deltaY = p2.getY() - p1.getY();
  102. return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
  103. }
  104. // 计算两点之间的距离
  105. public static double distanceTo(Vector3 p1, Vector3 p2) {
  106. double dx = p1.getX() - p2.getX();
  107. double dy = p1.getY() - p2.getY();
  108. double dz = p1.getZ() - p2.getZ();
  109. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  110. }
  111. public static void main(String[] args) throws Exception{
  112. String inPath ="D:\\cad\\work\\111\\2.json";
  113. String outPath ="D:\\cad\\work\\111\\"+new Date().getTime()+".dxf";
  114. LaserMeterToDxfUtil.toDxf(new File(inPath),outPath);
  115. }
  116. }