使用Java读取Dxf文件

发布于 2024-11-25 02:13:54 字数 109 浏览 1 评论 0原文

我正在尝试在 Java 中编写/查找一些代码,这些代码读取 dxf 文件并将几何图形从“实体”部分存储到数组中,以便稍后可以将该信息以表的形式导入到 Oracle 11g 中。

先感谢您!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北陌 2024-12-02 02:13:54

我最近使用过 kabeja 并且没有遇到任何问题,尽管我做了非常简单的任务。如果您只想将这些几何图形放入数组中,它就可以完成这项工作(在其他情况下我无法告诉)。
如果您已经了解 DXF 格式,那么上手就不会有任何问题。它可以像下面这样简单(例如提取一些圆形实体):

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);

文档有点不完整,但它有不错的 javadocs 和干净的 api。

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.

溺深海 2024-12-02 02:13:54

对于一些普遍感兴趣的反馈,请阅读读取 .DXF 文件

有许多实现 DXF 阅读器的 Java 库/writer 像 kabeja (开源)和 de-caff(免费查看器,它所基于的库可在商业上获得)。

警告:DXF 比乍一看要复杂得多,因此,如果您选择一个解决方案,请在您的数据上对其进行彻底测试。

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.

倦话 2024-12-02 02:13:54

我在我的项目中使用了 Kabeja ,我注意到它是一个非常好的选择,因为它是一个免费且功能强大的工具。

AutoCAD 线由两个末端(起点和终点)表示,

每个点有 3 个坐标:XYZ

下面是2D 方法(适用于 XY),将 dxf 文件作为参数,对其进行解析,然后提取其中的行。

AutoCAD 行将使用 kabeja 函数读取,然后我们可以构建自己的 Java Line

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;
    }
}

其中每个 Line 大约是两个 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;
    }
}

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) {

        }
    }
}

Line 的 ArrayList 包含所有autocad 文件的行,我们可以示例在 JPanel 上绘制这个数组列表,这样我们就可以从 autocad 完全迁移到 Java。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文