11package org .bimserver .serializers .json ;
22
3+ import java .io .OutputStream ;
4+ import java .io .PrintWriter ;
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
38import org .bimserver .emf .IdEObject ;
4- import org .bimserver .ifc .IfcModel ;
5- import org .bimserver .models .ifc2x3 .*;
9+ import org .bimserver .models .ifc2x3 .IfcColumn ;
10+ import org .bimserver .models .ifc2x3 .IfcDoor ;
11+ import org .bimserver .models .ifc2x3 .IfcOpeningElement ;
12+ import org .bimserver .models .ifc2x3 .IfcRoot ;
13+ import org .bimserver .models .ifc2x3 .IfcSlab ;
14+ import org .bimserver .models .ifc2x3 .IfcWall ;
15+ import org .bimserver .models .ifc2x3 .IfcWallStandardCase ;
16+ import org .bimserver .models .ifc2x3 .IfcWindow ;
617import org .bimserver .plugins .PluginException ;
718import org .bimserver .plugins .PluginManager ;
8- import org .bimserver .plugins .ifcengine .*;
9- import org .bimserver .plugins .serializers .*;
19+ import org .bimserver .plugins .ifcengine .IfcEngine ;
20+ import org .bimserver .plugins .ifcengine .IfcEngineException ;
21+ import org .bimserver .plugins .ifcengine .IfcEngineGeometry ;
22+ import org .bimserver .plugins .ifcengine .IfcEngineInstance ;
23+ import org .bimserver .plugins .ifcengine .IfcEngineInstanceVisualisationProperties ;
24+ import org .bimserver .plugins .ifcengine .IfcEngineModel ;
25+ import org .bimserver .plugins .serializers .EmfSerializer ;
26+ import org .bimserver .plugins .serializers .IfcModelInterface ;
27+ import org .bimserver .plugins .serializers .ProjectInfo ;
28+ import org .bimserver .plugins .serializers .SerializerException ;
1029import org .eclipse .emf .ecore .EObject ;
1130import org .slf4j .Logger ;
1231import org .slf4j .LoggerFactory ;
1332
14- import java .io .OutputStream ;
15- import java .io .PrintWriter ;
16- import java .util .ArrayList ;
17- import java .util .HashMap ;
18- import java .util .List ;
19-
20- public class JSONModelFormat2Serializer extends BimModelSerializer {
33+ public class JSONModelFormat2Serializer extends EmfSerializer {
2134
2235 private static final Logger LOGGER = LoggerFactory .getLogger (JSONModelFormat2Serializer .class );
23- private IfcEngine ifcEngine ;
2436 private PrintWriter out ;
37+ private IfcEngineModel ifcEngineModel ;
38+ private IfcEngineGeometry geometry ;
2539
26- public void init (IfcModelInterface model , ProjectInfo projectInfo , PluginManager pluginManager ) throws SerializerException {
27- super .init (model , projectInfo , pluginManager );
40+ public void init (IfcModelInterface model , ProjectInfo projectInfo , PluginManager pluginManager , IfcEngine ifcEngine ) throws SerializerException {
41+ super .init (model , projectInfo , pluginManager , ifcEngine );
42+ try {
43+ EmfSerializer serializer = getPluginManager ().requireIfcStepSerializer ();
44+ serializer .init (model , getProjectInfo (), getPluginManager (), ifcEngine );
45+ ifcEngineModel = ifcEngine .openModel (serializer .getBytes ());
46+ ifcEngineModel .setPostProcessing (true );
47+ geometry = ifcEngineModel .finalizeModelling (ifcEngineModel .initializeModelling ());
48+ } catch (Exception e ) {
49+ throw new SerializerException (e );
50+ }
2851 }
2952
3053 @ Override
@@ -35,11 +58,6 @@ protected void reset() {
3558 @ Override
3659 public boolean write (OutputStream outputStream ) throws SerializerException {
3760 if (getMode () == Mode .BODY ) {
38- try {
39- ifcEngine = getPluginManager ().requireIfcEngine ().createIfcEngine ();
40- } catch (PluginException e ) {
41- throw new SerializerException (e );
42- }
4361 out = new PrintWriter (outputStream );
4462 out .println ("var model = [" );
4563 writeGeometries ();
@@ -55,7 +73,7 @@ public boolean write(OutputStream outputStream) throws SerializerException {
5573 return false ;
5674 }
5775 }
58-
76+
5977 private String colorFromClass (Class ifcClass ) {
6078 if (ifcClass == IfcWallStandardCase .class ) {
6179 return "0xFF4400" ;
@@ -122,9 +140,11 @@ private void writeGeometry(SetGeometryResult geometry, IfcRoot ifcRoot) {
122140
123141 private void writeGeometries () {
124142 List <SetGeometryResult > geometryList = new ArrayList <SetGeometryResult >();
125- //Class[] eClasses = new Class[] { IfcSlab.class, IfcRoof.class, IfcWall.class, IfcWallStandardCase.class, IfcWindow.class, IfcDoor.class, IfcColumn.class, IfcRamp.class,
126- // IfcStair.class, IfcStairFlight.class, IfcRailing.class };
127- Class [] eClasses = new Class []{IfcWallStandardCase .class , IfcWall .class , IfcWindow .class , IfcDoor .class , IfcSlab .class , IfcColumn .class };
143+ // Class[] eClasses = new Class[] { IfcSlab.class, IfcRoof.class,
144+ // IfcWall.class, IfcWallStandardCase.class, IfcWindow.class,
145+ // IfcDoor.class, IfcColumn.class, IfcRamp.class,
146+ // IfcStair.class, IfcStairFlight.class, IfcRailing.class };
147+ Class [] eClasses = new Class [] { IfcWallStandardCase .class , IfcWall .class , IfcWindow .class , IfcDoor .class , IfcSlab .class , IfcColumn .class };
128148
129149 try {
130150 boolean first = true ;
@@ -147,47 +167,28 @@ private void writeGeometries() {
147167 }
148168 }
149169
150-
151170 private SetGeometryResult getGeometry (IdEObject ifcRootObject ) throws SerializerException , IfcEngineException {
152- IfcModel ifcModel = new IfcModel ();
153- convertToSubset (ifcRootObject .eClass (), ifcRootObject , ifcModel , new HashMap <EObject , EObject >());
154- EmfSerializer serializer = getPluginManager ().requireIfcStepSerializer ();
155- serializer .init (ifcModel , null , getPluginManager ());
156171 BinaryIndexBuffer binaryIndexBuffer = new BinaryIndexBuffer ();
157172 BinaryVertexBuffer binaryVertexBuffer = new BinaryVertexBuffer ();
158- IfcEngineModel model = ifcEngine .openModel (serializer .getBytes ());
159- try {
160- IfcEngineSurfaceProperties sp = model .initializeModelling ();
161- model .setPostProcessing (true );
162- IfcEngineGeometry geometry = model .finalizeModelling (sp );
163- int nrIndices = 0 ;
164- if (geometry != null ) {
165- for (IfcEngineInstance instance : model .getInstances (ifcRootObject .eClass ().getName ().toUpperCase ())) {
166- IfcEngineInstanceVisualisationProperties instanceInModelling = instance .getVisualisationProperties ();
167- for (int i = instanceInModelling .getStartIndex (); i < instanceInModelling .getPrimitiveCount () * 3 + instanceInModelling .getStartIndex (); i += 3 ) {
168- binaryIndexBuffer .addIndex (geometry .getIndex (i ));
169- binaryIndexBuffer .addIndex (geometry .getIndex (i + 2 ));
170- binaryIndexBuffer .addIndex (geometry .getIndex (i + 1 ));
171- nrIndices ++;
172- }
173- }
174- for (int i = 0 ; i < geometry .getNrVertices (); i += 3 ) {
175- binaryVertexBuffer .addVertex (geometry .getVertex (i ));
176- binaryVertexBuffer .addVertex (geometry .getVertex (i + 1 ));
177- binaryVertexBuffer .addVertex (geometry .getVertex (i + 2 ));
178- }
179- for (int i = 0 ; i < geometry .getNrNormals (); i += 3 ) {
180- binaryVertexBuffer .addNormal (geometry .getNormal (i ));
181- binaryVertexBuffer .addNormal (geometry .getNormal (i + 1 ));
182- binaryVertexBuffer .addNormal (geometry .getNormal (i + 2 ));
183- }
184- return new SetGeometryResult (nrIndices * 3 , geometry .getNrVertices (), binaryIndexBuffer , binaryVertexBuffer );
185- }
186- } finally {
187- model .close ();
173+ int nrIndices = 0 ;
174+ IfcEngineInstance instance = ifcEngineModel .getInstanceFromExpressId ((int )ifcRootObject .getOid ());
175+ IfcEngineInstanceVisualisationProperties visualisationProperties = instance .getVisualisationProperties ();
176+ for (int i = visualisationProperties .getStartIndex (); i < visualisationProperties .getPrimitiveCount () * 3 + visualisationProperties .getStartIndex (); i += 3 ) {
177+ binaryIndexBuffer .addIndex (geometry .getIndex (i ));
178+ binaryIndexBuffer .addIndex (geometry .getIndex (i + 2 ));
179+ binaryIndexBuffer .addIndex (geometry .getIndex (i + 1 ));
180+ nrIndices ++;
181+ }
182+ for (int i = 0 ; i < geometry .getNrVertices (); i += 3 ) {
183+ binaryVertexBuffer .addVertex (geometry .getVertex (i ));
184+ binaryVertexBuffer .addVertex (geometry .getVertex (i + 1 ));
185+ binaryVertexBuffer .addVertex (geometry .getVertex (i + 2 ));
188186 }
189- return null ;
187+ for (int i = 0 ; i < geometry .getNrNormals (); i += 3 ) {
188+ binaryVertexBuffer .addNormal (geometry .getNormal (i ));
189+ binaryVertexBuffer .addNormal (geometry .getNormal (i + 1 ));
190+ binaryVertexBuffer .addNormal (geometry .getNormal (i + 2 ));
191+ }
192+ return new SetGeometryResult (nrIndices * 3 , geometry .getNrVertices (), binaryIndexBuffer , binaryVertexBuffer );
190193 }
191-
192-
193- }
194+ }
0 commit comments