11package org .bimserver .serializers ;
22
3- import java .io .IOException ;
43import java .io .OutputStream ;
54import java .io .PrintWriter ;
65import java .util .ArrayList ;
2625import org .bimserver .ifcengine .jvm .IfcEngine .InstanceVisualisationProperties ;
2726import org .bimserver .models .ifc2x3 .IfcColumn ;
2827import org .bimserver .models .ifc2x3 .IfcDoor ;
29- import org .bimserver .models .ifc2x3 .IfcRailing ;
30- import org .bimserver .models .ifc2x3 .IfcRamp ;
31- import org .bimserver .models .ifc2x3 .IfcRoof ;
3228import org .bimserver .models .ifc2x3 .IfcRoot ;
3329import org .bimserver .models .ifc2x3 .IfcSlab ;
34- import org .bimserver .models .ifc2x3 .IfcStair ;
35- import org .bimserver .models .ifc2x3 .IfcStairFlight ;
3630import org .bimserver .models .ifc2x3 .IfcWall ;
3731import org .bimserver .models .ifc2x3 .IfcWallStandardCase ;
3832import org .bimserver .models .ifc2x3 .IfcWindow ;
@@ -53,9 +47,10 @@ public class JSONModelFormat2Serializer extends BimModelSerializer {
5347 private Project project ;
5448 private User user ;
5549 private SchemaDefinition schemaDefinition ;
56- private FailSafeIfcEngine ifcEngine ;
57-
58- public void init (Project project , User user , String fileName , IfcModel model , FieldIgnoreMap fieldIgnoreMap , SchemaDefinition schemaDefinition , IfcEngineFactory ifcEngineFactory ) throws SerializerException {
50+ private FailSafeIfcEngine ifcEngine ;
51+ private PrintWriter out ;
52+
53+ public void init (Project project , User user , String fileName , IfcModel model , FieldIgnoreMap fieldIgnoreMap , SchemaDefinition schemaDefinition , IfcEngineFactory ifcEngineFactory ) throws SerializerException {
5954 super .init (fileName , model , fieldIgnoreMap );
6055 this .project = project ;
6156 this .user = user ;
@@ -74,41 +69,32 @@ protected void reset() {
7469
7570 @ Override
7671 public boolean write (OutputStream outputStream ) throws SerializerException {
77- //System.out.println("...");
72+
7873 if (getMode () == Mode .BODY ) {
79- List <SetGeometryResult > geometries = getGeometries ();
80-
81- PrintWriter out = new PrintWriter (outputStream );
74+ out = new PrintWriter (outputStream );
8275 out .println ("var model = [" );
83-
84- for (int i =0 ; i <geometries .size (); i ++) {
85- SetGeometryResult geometry = geometries .get (i );
86- out .println (i == 0 ? "{" : ",{" );
87- writeGeometry (out , geometry );
88- out .println ("}" );
89- }
90-
91- out .println ("" );
76+ writeGeometries ();
77+ out .println ();
9278 out .println ("];" );
93- out .println ("" );
94- out .println (" postMessage( model );" );
79+ out .println ();
80+ out .println ("postMessage( model );" );
9581 out .flush ();
9682
9783 setMode (Mode .FINISHED );
9884 return true ;
99- } else if ( getMode () == Mode . FINISHED ) {
85+ } else {
10086 return false ;
10187 }
102- return false ;
10388 }
10489
105- private void writeGeometry (PrintWriter out , SetGeometryResult geometry ) {
106- out .println ("id : '" + geometry .getGuid () + "', " );
107- out .println ("type : '" + geometry .getEntityType () + "', " );
108- out .println ("geometry : {" );
109- out .println (" 'version' : 2, " );
110- out .println (" 'materials': []," );
111- out .print (" 'vertices': [ " );
90+ private void writeGeometry (SetGeometryResult geometry , IfcRoot ifcRoot ) {
91+
92+ out .println (" 'id' : '" + ifcRoot .getGlobalId ().getWrappedValue () + "', " );
93+ out .println (" 'type' : '" + ifcRoot .eClass ().getName ().toUpperCase () + "', " );
94+ out .println (" 'geometry' : {" );
95+ out .println (" 'version' : 2, " );
96+ out .println (" 'materials': []," );
97+ out .print (" 'vertices': [ " );
11298
11399 List <Float > vertices = geometry .getBinaryVertexBuffer ().getVertices ();
114100 if (vertices != null && vertices .size () > 0 ) {
@@ -119,8 +105,8 @@ private void writeGeometry(PrintWriter out, SetGeometryResult geometry) {
119105 }
120106 }
121107
122- out .println (" ], " );
123- out .print (" 'normals': [" );
108+ out .println (" ], " );
109+ out .print (" 'normals': [" );
124110
125111 List <Float > normals = geometry .getBinaryVertexBuffer ().getNormals ();
126112 if (normals != null && normals .size () > 0 ) {
@@ -131,10 +117,10 @@ private void writeGeometry(PrintWriter out, SetGeometryResult geometry) {
131117 }
132118 }
133119
134- out .println (" ]," );
135- out .println (" 'colors': [ ]," );
136- out .println (" 'uvs': [ ]," );
137- out .print (" 'faces': [ " );
120+ out .println (" ]," );
121+ out .println (" 'colors': [ ]," );
122+ out .println (" 'uvs': [ ]," );
123+ out .print (" 'faces': [ " );
138124
139125 List <Integer > indices = geometry .getBinaryIndexBuffer ().getIndices ();
140126 if (indices != null && indices .size () > 0 ) {
@@ -147,85 +133,36 @@ private void writeGeometry(PrintWriter out, SetGeometryResult geometry) {
147133 }
148134
149135
150- out .println (" ]" );
151- out .println (" }" );
152- out .println ("" );
136+ out .println (" ]" );
137+ out .println (" }" );
138+ out .println ();
153139 }
154-
155-
156- // private void writeFaces(PrintWriter out, List<SetGeometryResult> geometries) {
157- // int offset = 0;
158- // for (int i = 0; i < geometries.size(); i++) {
159- // SetGeometryResult geometry = geometries.get(i);
160- // if (geometry == null) { continue; }
161- // List<Integer> indices = geometry.getBinaryIndexBuffer().getIndices();
162- // if (indices == null || indices.size() == 0) { continue; }
163- //
164- // for (int j = 0; j < indices.size(); j+=3) {
165- // out.print((i==0 && j==0) ? "" : ",");
166- // out.print(" 32, ");
167- // out.print((offset + indices.get(j)) + "," + (offset + indices.get(j+1)) + "," + (offset + indices.get(j+2)) + ",");
168- // out.print((offset + indices.get(j)) + "," + (offset + indices.get(j+1)) + "," + (offset + indices.get(j+2)));
169- // }
170- // offset += (geometry.getBinaryVertexBuffer().getVertices().size() / 3);
171- // System.out.println("Indices: " + indices.size());
172- // }
173- // }
174140
175- // private void writeVertices(PrintWriter out, List<SetGeometryResult> geometries) {
176- // for (int i = 0; i < geometries.size(); i++) {
177- // SetGeometryResult geometry = geometries.get(i);
178- // if (geometry == null) { continue; }
179- // List<Float> vertices = geometry.getBinaryVertexBuffer().getVertices();
180- // if (vertices == null || vertices.size() == 0) { continue; }
181- //
182- // for (int j = 0; j < vertices.size(); j++) {
183- // out.print((i==0 && j==0) ? "" : ",");
184- // out.print(j % 3 == 0 ? " " : "");
185- // out.print(vertices.get(j));
186- // }
187- // System.out.println("Vertices: " + vertices.size());
188- //
189- // }
190- // }
191-
192- // private void writeNormals(PrintWriter out, List<SetGeometryResult> geometries) {
193- // for (int i = 0; i < geometries.size(); i++) {
194- // SetGeometryResult geometry = geometries.get(i);
195- // if (geometry == null) { continue; }
196- // List<Float> normals = geometry.getBinaryVertexBuffer().getNormals();
197- // if (normals == null || normals.size() == 0) { continue; }
198- //
199- // for (int j = 0; j < normals.size(); j++) {
200- // out.print((i==0 && j==0) ? "" : ",");
201- // out.print(j % 3 == 0 ? " " : "");
202- // out.print(normals.get(j));
203- // }
204- // System.out.println("Normals: " + normals.size());
205- // }
206- //
207- // }
208-
209- private List <SetGeometryResult > getGeometries () {
141+ private void writeGeometries () {
210142 List <SetGeometryResult > geometryList = new ArrayList <SetGeometryResult >();
211143 //Class[] eClasses = new Class[] { IfcSlab.class, IfcRoof.class, IfcWall.class, IfcWallStandardCase.class, IfcWindow.class, IfcDoor.class, IfcColumn.class, IfcRamp.class,
212144 // IfcStair.class, IfcStairFlight.class, IfcRailing.class };
213145 Class [] eClasses = new Class [] { IfcWallStandardCase .class , IfcWall .class , IfcWindow .class , IfcDoor .class , IfcSlab .class , IfcColumn .class };
214146
215147 try {
148+ boolean first = true ;
216149 for (Class <? extends EObject > eClass : eClasses ) {
217150 for (Object object : model .getAll (eClass )) {
218151 IfcRoot ifcRoot = (IfcRoot ) object ;
219152 SetGeometryResult geometry = getGeometry (ifcRoot );
220- if (geometry != null ) { geometryList .add (geometry ); }
153+ if (geometry != null ) {
154+ out .println (first ? " {" : " ,{" );
155+ first = false ;
156+ writeGeometry (geometry , ifcRoot );
157+ out .print (" }" );
158+ }
221159 }
222160 }
223161 } catch (SerializerException e ) {
224162 LOGGER .error ("" , e );
225163 } catch (IfcEngineException e ) {
226164 LOGGER .error ("" , e );
227165 }
228- return geometryList ;
229166 }
230167
231168
@@ -261,9 +198,7 @@ private SetGeometryResult getGeometry(IdEObject ifcRootObject) throws Serializer
261198 binaryVertexBuffer .addNormal (geometry .getNormal (i + 1 ));
262199 binaryVertexBuffer .addNormal (geometry .getNormal (i + 2 ));
263200 }
264- String guid = ((IfcRoot )ifcRootObject ).getGlobalId ().getWrappedValue (); // "TODO" + Long.toString(ifcRootObject.getOid()); // TODO
265- String entityType = ifcRootObject .eClass ().getName ().toUpperCase ();
266- return new SetGeometryResult (entityType , guid , nrIndices * 3 , geometry .getNrVertices (), binaryIndexBuffer , binaryVertexBuffer );
201+ return new SetGeometryResult (nrIndices * 3 , geometry .getNrVertices (), binaryIndexBuffer , binaryVertexBuffer );
267202 }
268203 } finally {
269204 model .close ();
0 commit comments