[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions CsvReader.java, NONE, 1.1 FileIm
Status: Pre-Alpha
Brought to you by:
henryml
|
From: rimestad <rim...@us...> - 2007-01-30 19:51:39
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3371/src/net/sourceforge/bprocessor/gui/actions Modified Files: FileImportActionListener.java Added Files: CsvReader.java Log Message: Put the Landmålergården import in its own class CVSreader (that is removed it from Project) Fixed it in many ways, so that it now reads surfaces and spaces but there are still some major flaws --- NEW FILE: CsvReader.java --- //--------------------------------------------------------------------------------- // $Id: CsvReader.java,v 1.1 2007/01/30 19:50:45 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.actions; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import org.apache.log4j.Logger; import net.sourceforge.bprocessor.model.Classification; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Space; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; /** * A reader of the dataformat by Landmålergården * 1. Create a reader with a file * 2. Call import * 3. fetch the wanted data from the Csv Reader via getters */ public class CsvReader { private static Logger log = Logger.getLogger(CsvReader.class); private File file = null; private Space where = null; /** * Construct a CSV reader * @param lfile the file object to read from * @param where The space to load into * @throws FileNotFoundException thrown if the file does not exist */ public CsvReader(File lfile, Space where) throws FileNotFoundException { if (lfile.exists()) { file = lfile; long lines = lfile.length() / 45; this.where = where; } else { throw new FileNotFoundException(); } } /** * Make a list of edges out of a list of vertexes * @param verts the vertex list (have to be greater than 2) * @return The list of edges */ public List<Edge> makeEdgesFromVertexes(List<Vertex> verts) { if (verts.size() > 2) { List<Edge> res = new ArrayList<Edge>(verts.size() + 1); Vertex first = null; Vertex previous = null; Vertex last = null; for (Vertex v : verts) { if (first == null) { first = v; previous = first; } else { res.add(new Edge(previous, v)); previous = v; } last = previous; } res.add(new Edge(last, first)); return res; } else { return null; } } /** * Read in a floor from the file * @param chosenFloor The chosen floor * @throws Exception If the file were lost or unable to be read */ public void importFloor(int chosenFloor) throws Exception { BufferedReader bf; FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); bf = new BufferedReader(isr); String current = ""; double x = 0; double y = 0; double z = 0; int curId = 0; int prevId = 0; int spaceid = 0; int surfaceId = -1; int floor = -1; int previousSpaceId = spaceid; int classification = 0; Space currentSpace = null; Surface outerSurface = null; List<Vertex> verts = new ArrayList<Vertex>(); int line = 0; while (bf.ready()) { current = bf.readLine(); line++; StringTokenizer st = new StringTokenizer(current, ";"); if (st.countTokens() == 16) { int curSurfaceId = Integer.parseInt(st.nextToken()); // surface(with holes)id if (surfaceId == -1) { surfaceId = curSurfaceId; } st.nextToken(); // hus floor = Integer.parseInt(st.nextToken()); // etage st.nextToken(); // lejl st.nextToken(); // rum st.nextToken(); // kode y = Double.parseDouble(st.nextToken()); // y x = Double.parseDouble(st.nextToken()); // x z = Double.parseDouble(st.nextToken()); // z st.nextToken(); // tal curId = Integer.parseInt(st.nextToken()); // utal st.nextToken(); // type st.nextToken(); // material st.nextToken(); // billed spaceid = Integer.parseInt(st.nextToken()); // Object_id classification = Integer.parseInt(st.nextToken()); // Object_kode if (floor == chosenFloor) { if (curId < prevId && !verts.isEmpty()) { // Started on new surface Surface s = addToRead(verts, currentSpace, outerSurface); if (outerSurface == null) { // this is the first surface in a face outerSurface = s; } if (surfaceId != curSurfaceId) { // this were the last surface in a face outerSurface = null; surfaceId = curSurfaceId; } } if (spaceid != previousSpaceId) { currentSpace = new Space("Space with id " + spaceid, Space.CONSTRUCTION, Space.SPACE_LEVEL, true); where.add(currentSpace); currentSpace.setClassification(new Classification("Type " + classification, -1)); previousSpaceId = spaceid; } // add the just read vertex to the list of verts for the next surface verts.add(new Vertex(x, y, z)); } else if (!verts.isEmpty()) { // The previous vertex were from a wanted floor Surface s = addToRead(verts, currentSpace, outerSurface); if (outerSurface == null) { // this is the first surface in a face outerSurface = s; } if (surfaceId != curSurfaceId) { // this were the last surface in a face outerSurface = null; surfaceId = curSurfaceId; } } // last part of while loop prevId = curId; } else { throw new IOException("Problems in format at line " + line); } } //check if there is something left to read if (!verts.isEmpty()) { addToRead(verts, currentSpace, outerSurface); } } /** * @param verts the vertex list * @param outer If the given vertex list is inside a * surface pass it here otherwise pass null * @param currentSpace the added space * @return the generated surface */ private Surface addToRead(List<Vertex> verts, Space currentSpace, Surface outer) { List<Edge> es = makeEdgesFromVertexes(verts); Surface s = null; if (es != null) { es = where.insert(es); if (es.size() > 2) { s = new Surface(es); if (outer != null) { outer.addHole(s); } s.setFrontDomain(currentSpace); where.add(s); } else { log.warn("There were a edge list with fewer that 3 edges"); } verts.clear(); return s; } else { log.info("There were " + verts.size() + " vertexes but edges were null"); return s; } } } Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FileImportActionListener.java 8 Jan 2007 12:31:48 -0000 1.4 --- FileImportActionListener.java 30 Jan 2007 19:50:45 -0000 1.5 *************** *** 40,44 **** lstate == JFileChooser.APPROVE_OPTION) { try { ! Project.getInstance().importCSV(lfile); } catch (Exception ex) { log.error("Could not open file: " + lfile, ex); --- 40,46 ---- lstate == JFileChooser.APPROVE_OPTION) { try { ! CsvReader read = new CsvReader(lfile, Project.getInstance().world()); ! read.importFloor(10); ! Project.getInstance().changed(Project.getInstance()); } catch (Exception ex) { log.error("Could not open file: " + lfile, ex); |