DXF格式是一种开源的CAD文件格式,如何实现Java代码对齐的解析,在网上找了很久,也没有找到非常成熟的库。很奇怪,开源的格式,正常应该会有很多的库来支持。只找到了一个kabeja库,最新版本还是2008年出的0.4版本,而且官网还是提供jar和exe运行文件的支持方式。在研究它的过程中遇到了一些问题,这里记录下来,希望大家少踩一些坑吧。

下载依赖

  1. https://kabeja.sourceforge.net/index.html下载对应的包就可以
  2. 下载完成后,解压缩,找到lib文件夹下的kabeja-0.4.jar,它是核心包,如果需要其他模块,也可以引入其他的包在这里插入图片描述
  3. 将这个jar包打入本地Maven仓库
 mvn install:install-file -Dfile=E:\temp\lib\kabeja-0.4.jar -DgroupId=org.kabeja -DartifactId=kabeja -Dversion=0.4 -Dpackaging=jar
  1. 在pom文件中引入
        <dependency>
            <groupId>org.kabeja</groupId>
            <artifactId>kabeja</artifactId>
            <version>0.4</version>
        </dependency>

代码测试

package com.example.gdal;

import org.kabeja.dxf.*;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;

public class ReadDxf {
    public static void main(String[] args) throws FileNotFoundException, ParseException {
        Parser dxfParser = ParserBuilder.createDefaultParser();
        dxfParser.parse(new FileInputStream("C://Users/hanbo/Desktop/cad/color.dxf"), "UTF-8");//需要转换的dxf
        DXFDocument doc = dxfParser.getDocument();

//        System.out.println(doc.);
        //遍历图层
        Iterator iter= doc.getDXFLayerIterator();

        while (iter.hasNext()){
            DXFLayer dxfLayer = (DXFLayer)iter.next();
            String layerName=dxfLayer.getName();
//            System.out.println("读取图层"+layerName);
            readEntity(dxfLayer);
        }

        readBlock(doc);
    }

    public static void readEntity(DXFLayer layer){
        Iterator iter= layer.getDXFEntityTypeIterator();
        while (iter.hasNext()){
            String s=(String) iter.next();
//            System.out.println(s);
            getEntity(layer,s);
        }
    }
    //读取block
    public static void readBlock(DXFDocument doc){
        Iterator iter= doc.getDXFBlockIterator();
        while (iter.hasNext()){
            DXFBlock s=(DXFBlock) iter.next();

//            System.out.println(s.getName());
        }
    }
    //读取图层下的实体
    public static void getEntity(DXFLayer layer,String type){
        List<DXFEntity>  entities= layer.getDXFEntities(type);
        for (int i = 0; i < entities.size(); i++) {
            System.out.println(entities.get(i).getColor());
            String color= DXFColor.getRGBString(entities.get(i).getColor());

            System.out.println(color);
//            System.out.println(entities.get(i).getID());
        }
    }
}

注意事项

解析出来的实体的颜色都是一个整数,不是rgb格式的,查询资料发现可以参考下表中数据

索引号RGB
125500
22552550
302550
40255255
500255
62550255
7255255255
8128128128
9192192192
1025500
11255127127
1220400
13204102102
1415300
151537676
1612700
171276363
187600
19763838
20255630
21255159127
22204510
23204127102
24153380
251539576
26127310
271277963
2876190
29764738
302551270
31255191127
322041020
33204153102
34153760
3515311476
36127630
371279563
3876380
39765738
402551910
41255223127
422041530
43204178102
441531140
4515313376
46127950
4712711163
4876570
49766638
502552550
51255255127
522042040
53204204102
541531530
5515315376
561271270
5712712763
5876760
59767638
601912550
61223255127
621532040
63178204102
641141530
6513315376
66951270
6711112763
6857760
69667638
701272550
71191255127
721022040
73153204102
74761530
7511415376
76631270
779512763
7838760
79577638
80632550
81159255127
82512040
83127204102
84381530
859515376
86311270
877912763
8819760
89477638
9002550
91127255127
9202040
93102204102
9401530
957615376
9601270
976312763
980760
99387638
100025563
101127255159
102020451
103102204127
104015338
1057615395
106012731
1076312779
10807619
109387647
1100255127
111127255191
1120204102
113102204153
114015376
11576153114
116012763
1176312795
11807638
119387657
1200255191
121127255223
1220204153
123102204178
1240153114
12576153133
126012795
12763127111
12807657
129387666
1300255255
131127255255
1320204204
133102204204
1340153153
13576153153
1360127127
13763127127
13807676
139387676
1400191255
141127223255
1420153204
143102178204
1440114153
14576133153
146095127
14763111127
14805776
149386676
1500127255
151127191255
1520102204
153102153204
154076153
15576114153
156063127
1576395127
15803876
159385776
160063255
161127159255
162051204
163102127204
164038153
1657695153
166031127
1676379127
16801976
169384776
17000255
171127127255
17200204
173102102204
17400153
1757676153
17600127
1776363127
1780076
179383876
180630255
181159127255
182510204
183127102204
184380153
1859576153
186310127
1877963127
18819076
189473876
1901270255
191191127255
1921020204
193153102204
194760153
19511476153
196630127
1979563127
19838076
199573876
2001910255
201223127255
2021530204
203178102204
2041140153
20513376153
206950127
20711163127
20857076
209663876
2102550255
211255127255
2122040204
213204102204
2141530153
21515376153
2161270127
21712763127
21876076
219763876
2202550191
221255127223
2222040153
223204102178
2241530114
22515376133
226127095
22712763111
22876057
229763866
2302550127
231255127191
2322040102
233204102153
234153076
23515376114
236127063
2371276395
23876038
239763857
240255063
241255127159
242204051
243204102127
244153038
2451537695
246127031
2471276379
24876019
249763847
250515151
251919191
252132132132
253173173173
254214214214
255255255255
255255255255
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐