--- /dev/null
+package org.simantics.g3d.csg.wizard;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.jcae.opencascade.jni.TopoDS_Shape;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.g3d.csg.scenegraph2.CSGrootNode;\r
+import org.simantics.g3d.csg.scenegraph2.ICSGnode;\r
+import org.simantics.g3d.csg.scenegraph2.SchemaBuilder;\r
+import org.simantics.objmap.graph.IMapping;\r
+import org.simantics.objmap.graph.Mappings;\r
+import org.simantics.objmap.graph.schema.IMappingSchema;\r
+import org.simantics.opencascade.OccTriangulator;\r
+\r
+public class CSGBrepModelExporter implements IRunnableWithProgress {\r
+\r
+ CSGExportModel exportModel;\r
+ public CSGBrepModelExporter(CSGExportModel exportModel) {\r
+ this.exportModel = exportModel;\r
+ }\r
+ \r
+ @Override\r
+ public void run(IProgressMonitor monitor) throws InvocationTargetException,\r
+ InterruptedException {\r
+ SubMonitor progress = SubMonitor.convert(monitor, 50);\r
+ SubMonitor mon = progress.newChild(50, SubMonitor.SUPPRESS_NONE);\r
+ try {\r
+ exportModel(mon);\r
+ } catch (IOException e) {\r
+ mon.setCanceled(true);\r
+ throw new InvocationTargetException(e);\r
+ } catch (DatabaseException e) {\r
+ mon.setCanceled(true);\r
+ throw new InvocationTargetException(e);\r
+ } finally {\r
+ monitor.done();\r
+ }\r
+ \r
+ }\r
+\r
+ void exportModel(SubMonitor mon) throws IOException, DatabaseException {\r
+ int taskSize = 50;\r
+ mon.beginTask("Exporting model...", taskSize);\r
+ mon.setTaskName("Initializing CSG model...");\r
+ \r
+ CSGrootNode rootNode = Simantics.getSessionContext().getSession().syncRequest(new Read<CSGrootNode>() {\r
+ @Override\r
+ public CSGrootNode perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ IMappingSchema<Resource, Object> schema = SchemaBuilder.getSchema(graph);\r
+ IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);\r
+ CSGrootNode rootNode = (CSGrootNode) mapping.map(graph,exportModel.getModel().getResource());\r
+ mapping.dispose();\r
+ return rootNode;\r
+ }\r
+ });\r
+ mon.worked(40);\r
+ \r
+ mon.setTaskName("Creating solid geometry...");\r
+ List<TopoDS_Shape> shapes = new ArrayList<TopoDS_Shape>();\r
+ for (ICSGnode node : rootNode.getChild()) {\r
+ TopoDS_Shape shape = node.getGeometry();\r
+ if (shape != null)\r
+ shapes.add(shape);\r
+ }\r
+ if (shapes.size() == 0) {\r
+ mon.setTaskName("Nothing to export.");\r
+ mon.setCanceled(true);\r
+ return;\r
+ }\r
+ TopoDS_Shape compound = null;\r
+ if (shapes.size() > 1) {\r
+ OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));\r
+ for (TopoDS_Shape shape : shapes)\r
+ shape.delete();\r
+ } else {\r
+ compound = shapes.get(0);\r
+ }\r
+ \r
+ mon.worked(50);\r
+ \r
+ mon.setTaskName("Writing file...");\r
+ OccTriangulator.exportBREP(compound, exportModel.getExportLocation().getAbsolutePath());\r
+ compound.delete();\r
+ \r
+ mon.setWorkRemaining(0);\r
+ }\r
+}\r