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); } }