美文网首页
cad转换成png,svg

cad转换成png,svg

作者: 架构师与哈苏 | 来源:发表于2020-06-18 15:50 被阅读0次

    1.设置仓库可以在setting.xml中设置(也可以在pom中设置,设置项即可)

    <mirror>  
          <id>AsposeJavaAPI</id>  
          <name>Aspose Java API</name>  
          <url>https://repository.aspose.com/repo/</url>          
        </mirror>  
    

    2.pom

    <!-- Aspose CAD标准dwg文件转svg或png -->
            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-cad</artifactId>
                <version>20.1</version>
            </dependency>
            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-pdf</artifactId>
                <version>20.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.xmlgraphics</groupId>
                <artifactId>batik-all</artifactId>
                <version>1.12</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
    
    
    <repositories>
            <!-- Aspose maven仓库 -->
            <repository>
                <id>AsposeJavaAPI</id>
                <name>Aspose Java API</name>
                <url>https://repository.aspose.com/repo/</url>
            </repository>
        </repositories>
    

    3.装换工具类
    CADUtils.java

    package com.meeno.chemical.common.cad.utils;
    
    import com.aspose.cad.Image;
    import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
    import com.aspose.cad.imageoptions.CadRasterizationOptions;
    import com.aspose.cad.imageoptions.PngOptions;
    import com.aspose.cad.imageoptions.SvgOptions;
    import com.aspose.cad.imageoptions.UnitType;
    
    /**
     * @description: cad转换工具类
     * @author: Wzq
     * @create: 2020-06-18 15:18
     */
    public class CADUtils {
    
        /**
         * dwg转换成png
         * @param sourcePath
         * @param targetPath
         */
        public static void dwgToPng(String sourcePath,String targetPath){
            //String sourcePath = "D:\\IDEA\\cad\\17.dwg";
            //String targetPath = "D:\\IDEA\\cad\\17.png";
    
            PngOptions pngOptions = new PngOptions();
            // 设置png的压缩等级 0 最低 9 最高
            pngOptions.setCompressionLevel(0);
            Image image = Image.load(sourcePath);
            // 原图尺寸类型
            int unitType = image.getUnitType();
            int width = image.getWidth();
            int height = image.getHeight();
            // 100万像素等比倍数,比较粗,就这样吧
            int zoom = 1008000 / width / height;
            CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
            cadRasterizationOptions.setPageHeight(width * zoom);
            cadRasterizationOptions.setPageWidth(height * zoom);
            cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
            // 这个无尺寸类型就当是像素吧
            cadRasterizationOptions.setUnitType(UnitType.Unitless);
    
            pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
            image.save(targetPath, pngOptions);
        }
    
        /**
         * dwg转换成Svg
         * @param sourcePath
         * @param targetPath
         */
        public static void dwgToSvg(String sourcePath,String targetPath){
            SvgOptions options = new SvgOptions();
            Image image = Image.load(sourcePath);
            int width = image.getWidth();
            int height = image.getHeight();
            //100万像素等比倍数
            int zoom = 1008000 / width / height;
    
            CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
            cadRasterizationOptions.setPageHeight(height * zoom);
            cadRasterizationOptions.setPageWidth(width * zoom);
            cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
    
            options.setVectorRasterizationOptions(cadRasterizationOptions);
            //options.setColorType(SvgColorMode.Grayscale);
            //options.setTextAsShapes(true);
            image.save(targetPath,options);
        }
    
        public static void main(String[] args) {
            String sourcePath = "E:\\区域位置图.dwg";
    //        String targetPath = "E:\\区域位置图.png";
    //        CADUtils.dwgToPng(sourcePath,targetPath);
            String svg = "E:\\区域位置图.svg";
            CADUtils.dwgToSvg(sourcePath,svg);
        }
    
    }
    
    

    关注公众号回复“书籍”就可以看大佬看得同款书籍了:


    image.png

    相关文章

      网友评论

          本文标题:cad转换成png,svg

          本文链接:https://www.haomeiwen.com/subject/aapuxktx.html