X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.csg%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fcsg%2Fwizard%2FCSGBrepModelExporter.java;fp=org.simantics.g3d.csg%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fcsg%2Fwizard%2FCSGBrepModelExporter.java;h=8e00a3de23773023a4f5025d9ae885a5b70a4b57;hb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;hp=0000000000000000000000000000000000000000;hpb=1f0bcd66274375f2278d2e6c486cb28257a5f7b2;p=simantics%2F3d.git diff --git a/org.simantics.g3d.csg/src/org/simantics/g3d/csg/wizard/CSGBrepModelExporter.java b/org.simantics.g3d.csg/src/org/simantics/g3d/csg/wizard/CSGBrepModelExporter.java new file mode 100644 index 00000000..8e00a3de --- /dev/null +++ b/org.simantics.g3d.csg/src/org/simantics/g3d/csg/wizard/CSGBrepModelExporter.java @@ -0,0 +1,98 @@ +package org.simantics.g3d.csg.wizard; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.jcae.opencascade.jni.TopoDS_Shape; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.g3d.csg.scenegraph2.CSGrootNode; +import org.simantics.g3d.csg.scenegraph2.ICSGnode; +import org.simantics.g3d.csg.scenegraph2.SchemaBuilder; +import org.simantics.objmap.graph.IMapping; +import org.simantics.objmap.graph.Mappings; +import org.simantics.objmap.graph.schema.IMappingSchema; +import org.simantics.opencascade.OccTriangulator; + +public class CSGBrepModelExporter implements IRunnableWithProgress { + + CSGExportModel exportModel; + public CSGBrepModelExporter(CSGExportModel exportModel) { + this.exportModel = exportModel; + } + + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException { + SubMonitor progress = SubMonitor.convert(monitor, 50); + SubMonitor mon = progress.newChild(50, SubMonitor.SUPPRESS_NONE); + try { + exportModel(mon); + } catch (IOException e) { + mon.setCanceled(true); + throw new InvocationTargetException(e); + } catch (DatabaseException e) { + mon.setCanceled(true); + throw new InvocationTargetException(e); + } finally { + monitor.done(); + } + + } + + void exportModel(SubMonitor mon) throws IOException, DatabaseException { + int taskSize = 50; + mon.beginTask("Exporting model...", taskSize); + mon.setTaskName("Initializing CSG model..."); + + CSGrootNode rootNode = Simantics.getSessionContext().getSession().syncRequest(new Read() { + @Override + public CSGrootNode perform(ReadGraph graph) + throws DatabaseException { + IMappingSchema schema = SchemaBuilder.getSchema(graph); + IMapping mapping = Mappings.createWithoutListening(schema); + CSGrootNode rootNode = (CSGrootNode) mapping.map(graph,exportModel.getModel().getResource()); + mapping.dispose(); + return rootNode; + } + }); + mon.worked(40); + + mon.setTaskName("Creating solid geometry..."); + List shapes = new ArrayList(); + for (ICSGnode node : rootNode.getChild()) { + TopoDS_Shape shape = node.getGeometry(); + if (shape != null) + shapes.add(shape); + } + if (shapes.size() == 0) { + mon.setTaskName("Nothing to export."); + mon.setCanceled(true); + return; + } + TopoDS_Shape compound = null; + if (shapes.size() > 1) { + OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()])); + for (TopoDS_Shape shape : shapes) + shape.delete(); + } else { + compound = shapes.get(0); + } + + mon.worked(50); + + mon.setTaskName("Writing file..."); + OccTriangulator.exportBREP(compound, exportModel.getExportLocation().getAbsolutePath()); + compound.delete(); + + mon.setWorkRemaining(0); + } +}