]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/wizard/CSGBrepModelExporter.java
White space clean-up
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / wizard / CSGBrepModelExporter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.csg.wizard;\r
13 \r
14 import java.io.IOException;\r
15 import java.lang.reflect.InvocationTargetException;\r
16 import java.util.ArrayList;\r
17 import java.util.List;\r
18 \r
19 import org.eclipse.core.runtime.IProgressMonitor;\r
20 import org.eclipse.core.runtime.SubMonitor;\r
21 import org.eclipse.jface.operation.IRunnableWithProgress;\r
22 import org.jcae.opencascade.jni.TopoDS_Shape;\r
23 import org.simantics.Simantics;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.request.Read;\r
28 import org.simantics.g3d.csg.scenegraph2.CSGrootNode;\r
29 import org.simantics.g3d.csg.scenegraph2.ICSGnode;\r
30 import org.simantics.g3d.csg.scenegraph2.SchemaBuilder;\r
31 import org.simantics.objmap.graph.IMapping;\r
32 import org.simantics.objmap.graph.Mappings;\r
33 import org.simantics.objmap.graph.schema.IMappingSchema;\r
34 import org.simantics.opencascade.OccTriangulator;\r
35 \r
36 public class CSGBrepModelExporter implements IRunnableWithProgress {\r
37 \r
38                 CSGExportModel exportModel;\r
39                 public CSGBrepModelExporter(CSGExportModel exportModel) {\r
40                         this.exportModel = exportModel;\r
41                 }\r
42                 \r
43                 @Override\r
44                 public void run(IProgressMonitor monitor) throws InvocationTargetException,\r
45                                 InterruptedException {\r
46                         SubMonitor progress = SubMonitor.convert(monitor, 50);\r
47                         SubMonitor mon = progress.newChild(50, SubMonitor.SUPPRESS_NONE);\r
48                         try {\r
49                                 exportModel(mon);\r
50                         } catch (IOException e) {\r
51                                 mon.setCanceled(true);\r
52                                 throw new InvocationTargetException(e);\r
53                         } catch (DatabaseException e) {\r
54                                 mon.setCanceled(true);\r
55                                 throw new InvocationTargetException(e);\r
56                         }  finally {\r
57                                 monitor.done();\r
58                         }\r
59                         \r
60                 }\r
61 \r
62                 void exportModel(SubMonitor mon) throws IOException, DatabaseException {\r
63                         int taskSize = 50;\r
64                         mon.beginTask("Exporting model...", taskSize);\r
65                         mon.setTaskName("Initializing CSG model...");\r
66                         \r
67                         CSGrootNode rootNode = Simantics.getSessionContext().getSession().syncRequest(new Read<CSGrootNode>() {\r
68                                 @Override\r
69                                 public CSGrootNode perform(ReadGraph graph)\r
70                                                 throws DatabaseException {\r
71                                         IMappingSchema<Resource, Object> schema = SchemaBuilder.getSchema(graph);\r
72                                         IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);\r
73                                         CSGrootNode rootNode = (CSGrootNode) mapping.map(graph,exportModel.getModel().getResource());\r
74                                         mapping.dispose();\r
75                                         return rootNode;\r
76                                 }\r
77                         });\r
78                         mon.worked(40);\r
79                         \r
80                         mon.setTaskName("Creating solid geometry...");\r
81                         List<TopoDS_Shape> shapes = new ArrayList<TopoDS_Shape>();\r
82                         for (ICSGnode node : rootNode.getChild()) {\r
83                                 TopoDS_Shape shape = node.getGeometry();\r
84                                 if (shape != null)\r
85                                         shapes.add(shape);\r
86                         }\r
87                         if (shapes.size() == 0) {\r
88                                 mon.setTaskName("Nothing to export.");\r
89                                 mon.setCanceled(true);\r
90                                 return;\r
91                         }\r
92                         TopoDS_Shape compound = null;\r
93                         if (shapes.size() > 1) {\r
94                                 OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));\r
95                                 for (TopoDS_Shape shape : shapes)\r
96                                         shape.delete();\r
97                         } else {\r
98                                 compound = shapes.get(0);\r
99                         }\r
100                         \r
101                         mon.worked(50);\r
102                         \r
103                         mon.setTaskName("Writing file...");\r
104                         OccTriangulator.exportBREP(compound, exportModel.getExportLocation().getAbsolutePath());\r
105                         compound.delete();\r
106                         \r
107                         mon.setWorkRemaining(0);\r
108                 }\r
109 }\r