简体   繁体   中英

Java: parse dxf file using Ycad / Kabeja or any other simmilar library

I'm pretty new to programming so any help would be much appreciated.

I'm trying to parse a .dxf file in order to get the coordinates of the entities and plot them to a JPanel. Basically I would need a graphical presentation of the dxf file.

So far I've only found some examples on how to use Ycad or Kabeja library but it's still not clear to me how to get the entities or even how the libraries work. It also seems like that the libraries aren't complete because some classes are missing and practically every example code I used had some problems with missing classes.

Also old questions on SO don't give me many answers. If anybody has any experience with the libraries mentioned above or any other method that would help me to resolve my problem, it would be greatly appreciated.

Use kabeja library, it converts DXF to PDF/SVG/JPEG Working example :

    private static void parseFile(String sourceFile, String index)
        throws FileNotFoundException, ParseException, SAXException {

    InputStream in = new FileInputStream("C:\\Users\\z003kebe\\Downloads\\DWGAndDxf\\dwg\\"+sourceFile);
    // Parser dxfParser = DXFParserBuilder.createDefaultParser();
    Parser dxfParser = ParserBuilder.createDefaultParser();
    dxfParser.parse(new FileInputStream("C:\\Users\\z003kebe\\Downloads\\DWGAndDxf\\dwg\\"+sourceFile), "UTF-8");
    DXFDocument doc = dxfParser.getDocument();

    SAXGenerator generator = new SVGGenerator();

    // generate into outputstream

    // output the SVG
    SAXSerializer out = new SAXPDFSerializer();
    // or you can use also pdf
    // org.kabeja.xml.SAXSerialzer out =
    // org.kabeja.batik.tools.SAXPDFSerializer();
    // tiff
    // org.kabeja.xml.SAXSerialzer out =
    // org.kabeja.batik.tools.SAXTIFFSerializer();
    // png
    // org.kabeja.xml.SAXSerialzer out =
    // org.kabeja.batik.tools.SAXPNGSerializer();
    // jpg
    // org.kabeja.xml.SAXSerialzer out =
    // org.kabeja.batik.tools.SAXJEPGSerializer();

    OutputStream fileo = new FileOutputStream(outputFile+index+".PDF");
    // out.setOutputStream(response.getOutputStream()) //write direct to
    // ServletResponse
    out.setOutput(fileo);
    // generate
    generator.generate(doc, out, new HashMap());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM