1 package org.simantics.g3d.csg.wizard;
\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
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
25 public class CSGBrepModelExporter implements IRunnableWithProgress {
\r
27 CSGExportModel exportModel;
\r
28 public CSGBrepModelExporter(CSGExportModel exportModel) {
\r
29 this.exportModel = exportModel;
\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
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
51 void exportModel(SubMonitor mon) throws IOException, DatabaseException {
\r
53 mon.beginTask("Exporting model...", taskSize);
\r
54 mon.setTaskName("Initializing CSG model...");
\r
56 CSGrootNode rootNode = Simantics.getSessionContext().getSession().syncRequest(new Read<CSGrootNode>() {
\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
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
76 if (shapes.size() == 0) {
\r
77 mon.setTaskName("Nothing to export.");
\r
78 mon.setCanceled(true);
\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
87 compound = shapes.get(0);
\r
92 mon.setTaskName("Writing file...");
\r
93 OccTriangulator.exportBREP(compound, exportModel.getExportLocation().getAbsolutePath());
\r
96 mon.setWorkRemaining(0);
\r