]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/wizard/CSGBrepModelExporter.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / wizard / CSGBrepModelExporter.java
1 package org.simantics.g3d.csg.wizard;\r
2 \r
3 import java.io.IOException;\r
4 import java.lang.reflect.InvocationTargetException;\r
5 import java.util.ArrayList;\r
6 import java.util.List;\r
7 \r
8 import org.eclipse.core.runtime.IProgressMonitor;\r
9 import org.eclipse.core.runtime.SubMonitor;\r
10 import org.eclipse.jface.operation.IRunnableWithProgress;\r
11 import org.jcae.opencascade.jni.TopoDS_Shape;\r
12 import org.simantics.Simantics;\r
13 import org.simantics.db.ReadGraph;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.exception.DatabaseException;\r
16 import org.simantics.db.request.Read;\r
17 import org.simantics.g3d.csg.scenegraph2.CSGrootNode;\r
18 import org.simantics.g3d.csg.scenegraph2.ICSGnode;\r
19 import org.simantics.g3d.csg.scenegraph2.SchemaBuilder;\r
20 import org.simantics.objmap.graph.IMapping;\r
21 import org.simantics.objmap.graph.Mappings;\r
22 import org.simantics.objmap.graph.schema.IMappingSchema;\r
23 import org.simantics.opencascade.OccTriangulator;\r
24 \r
25 public class CSGBrepModelExporter implements IRunnableWithProgress {\r
26 \r
27                 CSGExportModel exportModel;\r
28                 public CSGBrepModelExporter(CSGExportModel exportModel) {\r
29                         this.exportModel = exportModel;\r
30                 }\r
31                 \r
32                 @Override\r
33                 public void run(IProgressMonitor monitor) throws InvocationTargetException,\r
34                                 InterruptedException {\r
35                          SubMonitor progress = SubMonitor.convert(monitor, 50);\r
36                          SubMonitor mon = progress.newChild(50, SubMonitor.SUPPRESS_NONE);\r
37                         try {\r
38                             exportModel(mon);\r
39                         } catch (IOException e) {\r
40                                 mon.setCanceled(true);\r
41                             throw new InvocationTargetException(e);\r
42                         } catch (DatabaseException e) {\r
43                                 mon.setCanceled(true);\r
44                             throw new InvocationTargetException(e);\r
45                         }  finally {\r
46                             monitor.done();\r
47                         }\r
48                         \r
49                 }\r
50 \r
51                 void exportModel(SubMonitor mon) throws IOException, DatabaseException {\r
52                         int taskSize = 50;\r
53                         mon.beginTask("Exporting model...", taskSize);\r
54                         mon.setTaskName("Initializing CSG model...");\r
55                         \r
56                         CSGrootNode rootNode = Simantics.getSessionContext().getSession().syncRequest(new Read<CSGrootNode>() {\r
57                                 @Override\r
58                                 public CSGrootNode perform(ReadGraph graph)\r
59                                                 throws DatabaseException {\r
60                                         IMappingSchema<Resource, Object> schema = SchemaBuilder.getSchema(graph);\r
61                                         IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);\r
62                                         CSGrootNode rootNode = (CSGrootNode) mapping.map(graph,exportModel.getModel().getResource());\r
63                                         mapping.dispose();\r
64                                         return rootNode;\r
65                                 }\r
66                         });\r
67                         mon.worked(40);\r
68                         \r
69                         mon.setTaskName("Creating solid geometry...");\r
70                         List<TopoDS_Shape> shapes = new ArrayList<TopoDS_Shape>();\r
71                         for (ICSGnode node : rootNode.getChild()) {\r
72                                 TopoDS_Shape shape = node.getGeometry();\r
73                                 if (shape != null)\r
74                                         shapes.add(shape);\r
75                         }\r
76                         if (shapes.size() == 0) {\r
77                                 mon.setTaskName("Nothing to export.");\r
78                                 mon.setCanceled(true);\r
79                                 return;\r
80                         }\r
81                         TopoDS_Shape compound = null;\r
82                         if (shapes.size() > 1) {\r
83                                 OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));\r
84                                 for (TopoDS_Shape shape : shapes)\r
85                                         shape.delete();\r
86                         } else {\r
87                                 compound = shapes.get(0);\r
88                         }\r
89                         \r
90                         mon.worked(50);\r
91                         \r
92                         mon.setTaskName("Writing file...");\r
93                         OccTriangulator.exportBREP(compound, exportModel.getExportLocation().getAbsolutePath());\r
94                         compound.delete();\r
95                         \r
96                         mon.setWorkRemaining(0);\r
97                 }\r
98 }\r