package org.simantics.g3d.csg.adapters; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import org.eclipse.core.runtime.IAdaptable; import org.jcae.opencascade.jni.TopoDS_Shape; 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.objmap.structural.IStructuralObject; import org.simantics.opencascade.SolidModelProvider; import org.simantics.ui.SimanticsUI; public class CSGSolidModelAdapter implements SolidModelProvider, IAdaptable, IStructuralObject { private Resource model; public CSGSolidModelAdapter(Resource model) { this.model = model; } @Override public Collection getModel() throws Exception { return SimanticsUI.getSession().syncRequest(new Read>() { @Override public Collection perform(ReadGraph graph) throws DatabaseException { Collection shapes = new ArrayList(); IMappingSchema schema = SchemaBuilder.getSchema(graph); IMapping mapping = Mappings.createWithoutListening(schema); CSGrootNode rootNode = (CSGrootNode)mapping.map(graph, model); for (ICSGnode node : rootNode.getNodes("child")) { TopoDS_Shape shape = node.getGeometry(); if (shape != null) shapes.add(shape); } // FIXME: clear CSG scene-graph return shapes; } }); } @SuppressWarnings("rawtypes") @Override public Object getAdapter(Class adapter) { if (Resource.class.equals(adapter)) return model; return null; } @SuppressWarnings("unchecked") @Override public List getContext() { return Collections.EMPTY_LIST; } @Override public Resource getType() { return null; } @Override public void setContext(List object) { throw new RuntimeException(); } @Override public void setType(Resource type) { throw new RuntimeException(); } }