Reading a Dxf file with Java

独自空忆成欢 提交于 2019-11-27 18:11:29

问题


I am trying to write/find some code in Java which reads dxf files and stores geometry from the "Entities" section into arrays so that I can later import that information into Oracle 11g in terms of tables.

Thank you in advance!


回答1:


I've used kabeja recently and haven´t had any problems, though I did quite simple tasks. If you just want to bring those geometries into an array it will do the job (in other cases I can't tell). If you already know the DXF format you will have no problems to get started. It can get as easy as follows (e.g. to extract some circle entities):

Parser parser = ParserBuilder.createDefaultParser();
parser.parse("path/file.dxf", DXFParser.DEFAULT_ENCODING);
DXFDocument doc = parser.getDocument();
DXFlayer layer = doc.getDXFLayer("layer_name");
List<DXFCircle> arcs = layer.getDXFEntities(DXFConstants.ENTITY_TYPE_CIRCLE);

The documentation is a bit incomplete, but it has decent javadocs and clean api.




回答2:


For some feedback of general interest read Reading .DXF files

There's a number of Java libraries that implement a DXF reader/writer like kabeja (Open Source) and de-caff (a free viewer, the libraries it's based on are available commercially).

A word of warning: DXF is way more complicated than it looks at first sight, so if you select a solution test it thoroughly on your data.




回答3:


I have used Kabeja in my project and I noticed that it is a very good choice because it is a free and powerful tool.

A autocad line is represented by two extremities (the start and the end point)

Each point has 3 coordinates : X, Y and Z

Below is a 2D method (working on both X and Y) that takes a dxf file as parameter, parse it and then extracts the lines inside it.

The autocad lines will be readed using kabeja functions, then we can build our own Java Lines

public class ReadAutocadFile {

    public static ArrayList<Line> getAutocadFile(String filePath) throws ParseException {
        ArrayList<Line> lines = new ArrayList<>();
        Parser parser = ParserBuilder.createDefaultParser();
        parser.parse(filePath, DXFParser.DEFAULT_ENCODING);
        DXFDocument doc = parser.getDocument();
        List<DXFLine> lst = doc.getDXFLayer("layername ... whatever").getDXFEntities(DXFConstants.ENTITY_TYPE_LINE);
        for (int index = 0; index < lst.size(); index++) {
            Bounds bounds = lst.get(index).getBounds();
            Line line = new Line(
                    new Point(new Double(bounds.getMinimumX()).intValue(),
                    new Double(bounds.getMinimumY()).intValue()),
                    new Point(new Double(bounds.getMaximumX()).intValue(),
                    new Double(bounds.getMaximumY()).intValue())
                    );
            lines.add(line);
        }
        return lines;
    }
}

Where each Line is about two Points in Java

public class Line {

    private Point p1;
    private Point p2;

    public Line (Point p1, Point p2){
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }
}

Demo:

public class ReadAutocadFileTest {

    public static void main(String[] args) {
        try {
            File file = new File("testFile.dxf");
            ArrayList<Line> lines  = ReadAutocadFile.getAutocadFile(file.getAbsolutePath());
            for(int index = 0 ; index < lines.size() ; index++){
                System.out.println("Line " + (index +1));
                System.out.print("(" + lines.get(index).getP1().getX() + "," + lines.get(index).getP1().getY()+ ")");
                System.out.print(" to (" + lines.get(index).getP2().getX() + "," + lines.get(index).getP2().getY()+ ")\n");
            }
        } catch (Exception e) {

        }
    }
}

The ArrayList of Lines contains all the lines of the autocad file, we can per example draw this arraylist on a JPanel so in this way we are migrating completely from autocad to Java.



来源:https://stackoverflow.com/questions/6740736/reading-a-dxf-file-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!