[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions ImportFileReader.java, 1.17, 1.
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2008-04-03 14:01:16
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv17807/src/net/sourceforge/bprocessor/gui/actions Modified Files: ImportFileReader.java FileImportActionListener.java Log Message: very basic design file import Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** FileImportActionListener.java 1 Apr 2008 13:37:15 -0000 1.20 --- FileImportActionListener.java 3 Apr 2008 14:01:18 -0000 1.21 *************** *** 47,50 **** --- 47,52 ---- private FileFilter csvFilter; + private FileFilter dgnFilter; + /** The xy axis is the basis plane */ public static final int XY = 0; *************** *** 83,88 **** --- 85,101 ---- } }; + dgnFilter = new FileFilter() { + @Override + public boolean accept(File f) { + return f.isDirectory() || f.getName().endsWith(".dgn"); + } + @Override + public String getDescription() { + return "dgn - Microstation format"; + } + }; loadChooser.addChoosableFileFilter(csvFilter); loadChooser.addChoosableFileFilter(objFilter); + loadChooser.addChoosableFileFilter(dgnFilter); loadChooser.setFileFilter(csvFilter); } *************** *** 120,123 **** --- 133,145 ---- log.error("Could not open file: " + lfile, ex); } + } else if (lfile.getName().endsWith(".dgn")) { + try { + Container result = ImportFileReader.importDgnFormat(lfile); + Project.getInstance().world().add(result); + Project.getInstance().changed(Project.getInstance()); + Project.getInstance().checkpoint(); + } catch (Exception ex) { + log.error("Could not open file: " + lfile, ex); + } } loadChooser.revalidate(); Index: ImportFileReader.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/ImportFileReader.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** ImportFileReader.java 13 Dec 2007 12:00:55 -0000 1.17 --- ImportFileReader.java 3 Apr 2008 14:01:18 -0000 1.18 *************** *** 12,15 **** --- 12,16 ---- import java.io.FileNotFoundException; import java.io.IOException; + import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; *************** *** 530,532 **** --- 531,623 ---- } } + + + private static class DgnReader { + private InputStream input; + private Container output; + public DgnReader(InputStream input) { + this.input = input; + } + private int read() throws IOException { + return input.read(); + } + + private int readShort() throws IOException { + int b0 = read(); + int b1 = read(); + return (b1 << 8) | b0; + } + + private int readLong() throws IOException { + int result = 0; + int b0 = read(); + int b1 = read(); + int b2 = read(); + int b3 = read(); + result = (b1 << 24) | (b0 << 16) | b3 << 8 | b2; + return result; + } + + private void skip(int size) throws IOException { + for (int i = 0; i < size; i++) { + read(); + } + } + + private void readElement() throws IOException { + int token = read(); + int type = read(); + int size = readShort(); + System.out.println("type " + type); + switch (type) { + case 3: { + int xlow = readLong(); + int ylow = readLong(); + int zlow = readLong(); + int xhigh = readLong(); + int yhigh = readLong(); + int zhigh = readLong(); + skip(8); + int x0 = readLong(); + int y0 = readLong(); + int x1 = readLong(); + int y1 = readLong(); + Vertex v0 = new Vertex(x0 / 1000.0, y0 / 1000.0, 0); + Vertex v1 = new Vertex(x1 / 1000.0, y1 / 1000.0, 0); + output.add(v0); + output.add(v1); + output.add(new Edge(v0, v1)); + skip(size * 2 - (12 * 4)); + } + break; + default: + skip(size * 2); + } + } + + /** + * + * @return Container + * @throws IOException + */ + public Container readFile() throws IOException { + output = new Container("Design", Container.CONSTRUCTION, true); + while (input.available() > 0) { + readElement(); + } + return output; + } + } + /** + * + * @param file File + * @return Container + * @throws IOException IOException + */ + public static Container importDgnFormat(File file) throws IOException { + InputStream stream = new FileInputStream(file); + DgnReader reader = new DgnReader(stream); + Container result = reader.readFile(); + return result; + } } |