123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package com.shkpr.service.alambizplugin.components;
- import com.aspose.cad.Color;
- import com.aspose.cad.Image;
- import com.aspose.cad.ImageOptionsBase;
- import com.aspose.cad.fileformats.ObserverPoint;
- import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
- import com.aspose.cad.fileformats.cad.CadImage;
- import com.aspose.cad.fileformats.cad.cadobjects.polylines.CadPolyline3D;
- import com.aspose.cad.imageoptions.*;
- import com.global.base.log.LogLevelFlag;
- import com.global.base.log.LogPrintMgr;
- import com.shkpr.service.alambizplugin.commproperties.GisSurveyCadConvertProperties;
- import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
- import com.shkpr.service.alambizplugin.constants.CadEnum;
- import com.shkpr.service.alambizplugin.constants.GisSurveConvertStatusEnum;
- import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
- import com.shkpr.service.alambizplugin.dto.CommAsyncResult;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.scheduling.annotation.AsyncResult;
- import org.springframework.stereotype.Component;
- import org.springframework.util.concurrent.ListenableFuture;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.Duration;
- import java.time.LocalDateTime;
- /**
- * cad转换器
- *
- * @author 欧阳劲驰
- * @since 1.0.0
- */
- @Component
- public class GisSurveyCadConverter {
- /**
- * log
- */
- private final String mStrClassName;
- private final String mBizType;
- private final TempFileProperties tempFileProperties;
- private final GisSurveyCadConvertProperties cadConvertProperties;
- public GisSurveyCadConverter(GisSurveyCadConvertProperties cadConvertProperties
- , TempFileProperties tempFileProperties) {
- mStrClassName = "GisSurveyCadConverter";
- mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
- this.tempFileProperties = tempFileProperties;
- this.cadConvertProperties = cadConvertProperties;
- }
- /**
- * cad转换
- *
- * @param convertId 转换id
- * @param inputStream 输入流
- * @param cadEnum cad类型
- * @return 文件路径
- */
- @Async
- public ListenableFuture<CommAsyncResult<String>> cadConvert(String convertId, InputStream inputStream, CadEnum cadEnum) {
- //构建返回
- CommAsyncResult<String> result = CommAsyncResult.fail(convertId);
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "开始执行cad转换,读取文件流;转换id:%s 输出类型:%s"
- , convertId
- , cadEnum.getSuffix()
- )
- );
- //读取输入流
- Image image = CadImage.load(inputStream);
- //判断是否有3D线
- boolean isCadPolyline3D = ((CadImage) image).getEntities().stream()
- .anyMatch(entity -> entity instanceof CadPolyline3D);
- //导出图像配置
- ImageOptionsBase options;
- switch (cadEnum) {
- case BMP:
- options = new BmpOptions();
- break;
- case CGM:
- options = new CgmOptions();
- break;
- case DICOM:
- options = new DicomOptions();
- break;
- case DWF:
- options = new DwfOptions();
- break;
- case DWG:
- options = new DwgOptions();
- break;
- case DXF:
- options = new DxfOptions();
- break;
- case EMF:
- options = new EmfOptions();
- break;
- case FBX:
- options = new FbxOptions();
- break;
- case GIF:
- options = new GifOptions();
- break;
- case GLTF:
- options = new GltfOptions();
- break;
- case IFC:
- options = new IfcOptions();
- break;
- case JP2:
- options = new Jpeg2000Options();
- break;
- case JPG:
- options = new JpegOptions();
- break;
- case OBJ:
- options = new ObjOptions();
- break;
- case PDF:
- options = new PdfOptions();
- break;
- case PNG:
- options = new PngOptions();
- break;
- case PSD:
- options = new PsdOptions();
- break;
- case STP:
- options = new StpOptions();
- break;
- case SVG:
- options = new SvgOptions();
- ((SvgOptions) options).setTextAsShapes(false);
- ((SvgOptions) options).setUseAbsoluteRescaling(false);
- if (!isCadPolyline3D) ((SvgOptions) options).setRescaleSubpixelLinewidths(false);
- break;
- case THREE_DS:
- options = new ThreeDSOptions();
- break;
- case TIFF:
- options = new TiffOptions();
- break;
- case U3D:
- options = new U3dOptions();
- break;
- case WEBP:
- options = new WebPOptions();
- break;
- case WMF:
- options = new WmfOptions();
- break;
- default:
- return new AsyncResult<>(result);
- }
- //栅格化选项
- CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
- //读取图像宽高
- rasterizationOptions.setPageWidth(Math.max(image.getWidth(), cadConvertProperties.getRasterizationMinWidth()));
- rasterizationOptions.setPageHeight(Math.max(image.getHeight(), cadConvertProperties.getRasterizationMinHeight()));
- //使用原本颜色
- rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
- //黑色背景
- rasterizationOptions.setBackgroundColor(Color.getBlack());
- //缩放配置
- rasterizationOptions.setNoScaling(false);
- rasterizationOptions.setAutomaticLayoutsScaling(true);
- //质量配置
- rasterizationOptions.getQuality().setTextThicknessNormalization(true);
- //调整视角
- if (isCadPolyline3D) rasterizationOptions.setObserverPoint(new ObserverPoint(1, 0, 0));
- options.setVectorRasterizationOptions(rasterizationOptions);
- try {
- //文件路径
- final String fileName = convertId + "." + cadEnum.getSuffix();
- final Path filePath = Paths.get(tempFileProperties.getResourcePath() + fileName);
- //如存在,则删除
- if (Files.exists(filePath)) Files.delete(filePath);
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "读取文件流完毕,开始写入文件;转换id:%s 输出类型:%s"
- , convertId
- , cadEnum.getSuffix()
- )
- );
- //存至文件
- image.save(filePath.toString(), options);
- //转换完成
- result.setStatus(GisSurveConvertStatusEnum.SUCCESS.getCode());
- result.setCompleteTime(LocalDateTime.now());
- result.setData(fileName);
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "结束执行cad转换;转换id: %s, 转换类型:%s , 用时(毫秒):%d"
- , convertId
- , cadEnum.getSuffix()
- , Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
- )
- );
- return new AsyncResult<>(result);
- } catch (IOException e) {
- //打印报错信息
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
- , String.format("转换cad异常 转换id:%s error:%s", convertId, e)
- );
- return new AsyncResult<>(result);
- }
- }
- }
|