/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ 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.Simantics; public class CSGSolidModelAdapter implements SolidModelProvider, IAdaptable, IStructuralObject { private Resource model; public CSGSolidModelAdapter(Resource model) { this.model = model; } @Override public Collection getModel() throws Exception { return Simantics.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; } }); } @Override public C getAdapter(Class adapter) { if (adapter.isAssignableFrom(Resource.class)) return adapter.cast(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(); } }