Java解析dwg文件(Java处理CAD)

2,998 阅读1分钟

版本

C.png

该方法只能解析2010版本 2010版本以后没有测试

1、导入包

<dependency>  
<groupId>com.1spatial</groupId>  
<artifactId>dwg-lib</artifactId>  
<version>0.8</version>  
</dependency>

2、解析代码

@SneakyThrows
    public static void main(String[] args){
        File file = new File("D:\\admin\\Desktop\\Drawing1-2010.dwg");
        Reader reader = new Reader(file);
        System.out.println("秀儿");
        BlockControlObj blockControl = reader.getBlockControlObject();
        BlockHeader ms = (BlockHeader)blockControl.getModelSpace();
        if (ms != null) {
            for (CadObject cb : ms.getOwnedObjects()) {
                EntityObject entity=(EntityObject) cb;
                Layer layer = entity.getLayer();
                String entryName = layer.entryName;
                System.out.println(entryName);

                if (entity instanceof Point) {
                    Point point = (Point)entity;
                    System.out.println("Point: " + point.point);
                } else if (entity instanceof Line) {
                    Line line = (Line)entity;
                    System.out.println("Line: " + line.start + " to " + line.end);
                } else if (entity instanceof LwPolyline) {
                    LwPolyline polyline = (LwPolyline)entity;

                    System.out.print("LwPolyline: ");
                    for (LwPolyline.VertexOfLwPolyline point : polyline.points) {
                        System.out.print(" " + point.vertex);
                    }
                    System.out.println("");
                } else if (entity instanceof Arc) {
                    Arc arc = (Arc)entity;
                    System.out.println("Arc: " + arc.center + " with radius " + arc.radius + ", angle " + arc.startAngle + " to " + arc.endAngle);
                } else if (entity instanceof Circle) {
                    Circle arc = (Circle)entity;
                    System.out.println("Circle: " + arc.center + " with radius " + arc.radius);
                } else if (entity instanceof TwoDPolyline) {
                    TwoDPolyline polyline = (TwoDPolyline)entity;
                    System.out.println("2D Polyline: " + polyline.getOwnedObjects().size() + " vertexes");
                } else if (entity instanceof Text) {
                    Text textObject = (Text)entity;
                    System.out.println("Text: " + textObject.textValue);
                } else if (entity instanceof Insert) {
                    Insert insert = (Insert)entity;
                    BlockHeader block = insert.getBlockHeader();
                    System.out.println("Insert block " + block.entryName + ": scale is (" + insert.xScaleFactor + ", " + insert.yScaleFactor + "), rotation is " + insert.rotation);

                    for (CadObject child : block.getOwnedObjects()) {

                    }
                } else {
                    System.out.println(entity.getClass().getSimpleName());
                }
            }
        }

    }

解析结果

秀儿
0
LwPolyline:  (407.64616708246496, 620.8876698599311) (383.6564890915325, 436.9668052627819) (624.7835088978283, 401.2898482506259) (661.0755858584697, 622.7330297053874)

Process finished with exit code 0

文件图片 image.png

3、低版本测试

Exception in thread "main" com.onespatial.dwglib.UnsupportedFileVersionException: This file was produced by AutoCAD 2007, 2008, or 2009.  Only files produced by AutoCAD 2010 or later are supported.

此文件由 AutoCAD 2007、2008 或 2009 生成。 仅支持 AutoCAD 2010 或更高版本生成的文件。

4、获取版本

File file = new File("Drawing1-2010.dwg");  
Reader reader = new Reader(file);  
String version = reader.getVersion();  
System.out.println(version);