11

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!

Jayan
  • 18,003
  • 15
  • 89
  • 143
Spyros
  • 111
  • 1
  • 1
  • 3

3 Answers3

10

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.

aacotroneo
  • 2,170
  • 16
  • 22
7

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.

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
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.

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
  • @Samane : I don't understand, you mean add a point in the autocad file? – Jad Chahine Jun 02 '16 at 18:32
  • @Samane : The line is represented by two points (the starting and the ending point), to draw a line in autocad you just have to use the draw line command by pressing the L button and then draw the line. – Jad Chahine Jun 04 '16 at 05:07
  • No, I mean is that I want to add points to AutoCAD file similar code using `kabeja` library in java. – Samane Jul 09 '16 at 12:13
  • @Samane : Kabeja is a Java library for parsing dxf files so we can extract lines and shapes from it only, you cannot draw lines in dxf file using java. – Jad Chahine Jul 09 '16 at 13:42
  • Oh, Is there another way? – Samane Jul 09 '16 at 14:15