GisSurveyCadConverter.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package com.shkpr.service.alambizplugin.components;
  2. import com.aspose.cad.Color;
  3. import com.aspose.cad.Image;
  4. import com.aspose.cad.ImageOptionsBase;
  5. import com.aspose.cad.fileformats.ObserverPoint;
  6. import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
  7. import com.aspose.cad.fileformats.cad.CadImage;
  8. import com.aspose.cad.fileformats.cad.cadobjects.polylines.CadPolyline3D;
  9. import com.aspose.cad.imageoptions.*;
  10. import com.global.base.log.LogLevelFlag;
  11. import com.global.base.log.LogPrintMgr;
  12. import com.shkpr.service.alambizplugin.commproperties.GisSurveyCadConvertProperties;
  13. import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
  14. import com.shkpr.service.alambizplugin.constants.CadEnum;
  15. import com.shkpr.service.alambizplugin.constants.GisSurveConvertStatusEnum;
  16. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  17. import com.shkpr.service.alambizplugin.dto.CommAsyncResult;
  18. import org.springframework.scheduling.annotation.Async;
  19. import org.springframework.scheduling.annotation.AsyncResult;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.util.concurrent.ListenableFuture;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;
  26. import java.nio.file.Paths;
  27. import java.time.Duration;
  28. import java.time.LocalDateTime;
  29. /**
  30. * cad转换器
  31. *
  32. * @author 欧阳劲驰
  33. * @since 1.0.0
  34. */
  35. @Component
  36. public class GisSurveyCadConverter {
  37. /**
  38. * log
  39. */
  40. private final String mStrClassName;
  41. private final String mBizType;
  42. private final TempFileProperties tempFileProperties;
  43. private final GisSurveyCadConvertProperties cadConvertProperties;
  44. public GisSurveyCadConverter(GisSurveyCadConvertProperties cadConvertProperties
  45. , TempFileProperties tempFileProperties) {
  46. mStrClassName = "GisSurveyCadConverter";
  47. mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
  48. this.tempFileProperties = tempFileProperties;
  49. this.cadConvertProperties = cadConvertProperties;
  50. }
  51. /**
  52. * cad转换
  53. *
  54. * @param convertId 转换id
  55. * @param inputStream 输入流
  56. * @param cadEnum cad类型
  57. * @return 文件路径
  58. */
  59. @Async
  60. public ListenableFuture<CommAsyncResult<String>> cadConvert(String convertId, InputStream inputStream, CadEnum cadEnum) {
  61. //构建返回
  62. CommAsyncResult<String> result = CommAsyncResult.fail(convertId);
  63. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  64. , String.format(
  65. "开始执行cad转换,读取文件流;转换id:%s 输出类型:%s"
  66. , convertId
  67. , cadEnum.getSuffix()
  68. )
  69. );
  70. //读取输入流
  71. Image image = CadImage.load(inputStream);
  72. //判断是否有3D线
  73. boolean isCadPolyline3D = ((CadImage) image).getEntities().stream()
  74. .anyMatch(entity -> entity instanceof CadPolyline3D);
  75. //导出图像配置
  76. ImageOptionsBase options;
  77. switch (cadEnum) {
  78. case BMP:
  79. options = new BmpOptions();
  80. break;
  81. case CGM:
  82. options = new CgmOptions();
  83. break;
  84. case DICOM:
  85. options = new DicomOptions();
  86. break;
  87. case DWF:
  88. options = new DwfOptions();
  89. break;
  90. case DWG:
  91. options = new DwgOptions();
  92. break;
  93. case DXF:
  94. options = new DxfOptions();
  95. break;
  96. case EMF:
  97. options = new EmfOptions();
  98. break;
  99. case FBX:
  100. options = new FbxOptions();
  101. break;
  102. case GIF:
  103. options = new GifOptions();
  104. break;
  105. case GLTF:
  106. options = new GltfOptions();
  107. break;
  108. case IFC:
  109. options = new IfcOptions();
  110. break;
  111. case JP2:
  112. options = new Jpeg2000Options();
  113. break;
  114. case JPG:
  115. options = new JpegOptions();
  116. break;
  117. case OBJ:
  118. options = new ObjOptions();
  119. break;
  120. case PDF:
  121. options = new PdfOptions();
  122. break;
  123. case PNG:
  124. options = new PngOptions();
  125. break;
  126. case PSD:
  127. options = new PsdOptions();
  128. break;
  129. case STP:
  130. options = new StpOptions();
  131. break;
  132. case SVG:
  133. options = new SvgOptions();
  134. ((SvgOptions) options).setTextAsShapes(false);
  135. ((SvgOptions) options).setUseAbsoluteRescaling(false);
  136. if (!isCadPolyline3D) ((SvgOptions) options).setRescaleSubpixelLinewidths(false);
  137. break;
  138. case THREE_DS:
  139. options = new ThreeDSOptions();
  140. break;
  141. case TIFF:
  142. options = new TiffOptions();
  143. break;
  144. case U3D:
  145. options = new U3dOptions();
  146. break;
  147. case WEBP:
  148. options = new WebPOptions();
  149. break;
  150. case WMF:
  151. options = new WmfOptions();
  152. break;
  153. default:
  154. return new AsyncResult<>(result);
  155. }
  156. //栅格化选项
  157. CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
  158. //读取图像宽高
  159. rasterizationOptions.setPageWidth(Math.max(image.getWidth(), cadConvertProperties.getRasterizationMinWidth()));
  160. rasterizationOptions.setPageHeight(Math.max(image.getHeight(), cadConvertProperties.getRasterizationMinHeight()));
  161. //使用原本颜色
  162. rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
  163. //黑色背景
  164. rasterizationOptions.setBackgroundColor(Color.getBlack());
  165. //缩放配置
  166. rasterizationOptions.setNoScaling(false);
  167. rasterizationOptions.setAutomaticLayoutsScaling(true);
  168. //质量配置
  169. rasterizationOptions.getQuality().setTextThicknessNormalization(true);
  170. //调整视角
  171. if (isCadPolyline3D) rasterizationOptions.setObserverPoint(new ObserverPoint(1, 0, 0));
  172. options.setVectorRasterizationOptions(rasterizationOptions);
  173. try {
  174. //文件路径
  175. final String fileName = convertId + "." + cadEnum.getSuffix();
  176. final Path filePath = Paths.get(tempFileProperties.getResourcePath() + fileName);
  177. //如存在,则删除
  178. if (Files.exists(filePath)) Files.delete(filePath);
  179. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  180. , String.format(
  181. "读取文件流完毕,开始写入文件;转换id:%s 输出类型:%s"
  182. , convertId
  183. , cadEnum.getSuffix()
  184. )
  185. );
  186. //存至文件
  187. image.save(filePath.toString(), options);
  188. //转换完成
  189. result.setStatus(GisSurveConvertStatusEnum.SUCCESS.getCode());
  190. result.setCompleteTime(LocalDateTime.now());
  191. result.setData(fileName);
  192. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  193. , String.format(
  194. "结束执行cad转换;转换id: %s, 转换类型:%s , 用时(毫秒):%d"
  195. , convertId
  196. , cadEnum.getSuffix()
  197. , Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
  198. )
  199. );
  200. return new AsyncResult<>(result);
  201. } catch (IOException e) {
  202. //打印报错信息
  203. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  204. , String.format("转换cad异常 转换id:%s error:%s", convertId, e)
  205. );
  206. return new AsyncResult<>(result);
  207. }
  208. }
  209. }