1 package org.simantics.plant3d.editor;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.Session;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.g3d.ontology.G3D;
15 import org.simantics.g3d.scenegraph.base.INode;
16 import org.simantics.g3d.scenegraph.base.ParentNode;
17 import org.simantics.g3d.vtk.common.AbstractVTKNodeMap;
18 import org.simantics.g3d.vtk.common.VtkView;
19 import org.simantics.objmap.graph.IMapping;
20 import org.simantics.plant3d.ontology.Plant3D;
21 import org.simantics.plant3d.scenegraph.IP3DNode;
22 import org.simantics.plant3d.scenegraph.IP3DVisualNode;
23 import org.simantics.plant3d.scenegraph.P3DParentNode;
24 import org.simantics.plant3d.scenegraph.P3DRootNode;
25 import org.simantics.plant3d.scenegraph.ParameterizedNode;
26 import org.simantics.plant3d.scenegraph.PipeRun;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
28 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
33 public class P3DNodeMap extends AbstractVTKNodeMap<Resource,INode> {
35 private static final boolean DEBUG = false;
37 public P3DNodeMap(Session session, IMapping<Resource,INode> mapping, VtkView panel, P3DRootNode rootNode) {
38 super(session, mapping, panel, rootNode);
39 rootNode.setNodeMap(this);
42 protected void updateActor(INode n, Set<String> ids) {
43 if (DEBUG) System.out.println("P3DNodeMap update " + debugString(n));
44 if (!(n instanceof IP3DVisualNode)) {
45 if (n instanceof PipeControlPoint) {
46 n = ((PipeControlPoint)n).getPipelineComponent();
54 IP3DVisualNode node = (IP3DVisualNode)n;
57 System.out.print("P3DNodeMap update " + debugString(node));
59 System.out.print(" " + s);
63 if (ids.contains(Plant3D.URIs.hasGeometry)) {
65 updateRenderObjectsFor(node);
66 updateTransform(node);
68 if (n instanceof ParameterizedNode) {
69 ParameterizedNode geom = (ParameterizedNode)n;
70 for (String id : geom.getParameterMap().keySet()) {
71 if (ids.contains(id)) {
73 updateRenderObjectsFor(node);
74 //updateTransform(node);
78 } else if (n instanceof PipeRun) {
79 // FIXME: may require rule based update!
80 PipeRun run = (PipeRun)n;
81 Set<String> ids2 = new HashSet<String>();
82 ids2.add(Plant3D.URIs.hasGeometry);
83 for (PipeControlPoint pcp : run.getControlPoints()) {
84 updateActor(pcp, ids2);
88 if (ids.contains(G3D.URIs.hasPosition) ||
89 ids.contains(G3D.URIs.hasOrientation) ||
90 ids.contains(G3D.URIs.hasWorldPosition) ||
91 ids.contains(G3D.URIs.hasWorldOrientation)) {
92 updateTransform(node);
94 if (ids.contains(Plant3D.URIs.HasTurnRadiusIndex)) {
96 updateRenderObjectsFor(node);
100 private void updateTransform(IP3DNode node) {
101 if (DEBUG) System.out.println("P3DNodeMap update Transform " + debugString(node));
103 node.update(view.getRenderer());
105 if (node instanceof ParentNode<?>) {
106 @SuppressWarnings("unchecked")
107 ParentNode<IP3DNode> p = (ParentNode<IP3DNode>)node;
108 for (IP3DNode n : p.getNodes())
114 protected Collection<vtkProp> getActors(INode n) {
115 List<vtkProp> props = new ArrayList<vtkProp>();
116 if (!(n instanceof IP3DVisualNode))
118 IP3DVisualNode node = (IP3DVisualNode)n;
119 for (vtkProp3D p : ((IP3DVisualNode)node).getActors())
126 protected void removeActor(INode n) {
127 if (DEBUG) System.out.println("P3DNodeMap.removeActor " + debugString(n));
128 if (!(n instanceof IP3DVisualNode))
130 IP3DVisualNode node = (IP3DVisualNode)n;
133 if (node instanceof P3DParentNode<?>) {
134 for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
135 if (n2 instanceof IP3DVisualNode)
136 removeActor((IP3DVisualNode)n2);
141 protected void addActor(INode n) {
142 if (DEBUG) System.out.println("P3DNodeMap.addActor " + debugString(n));
143 if (!(n instanceof IP3DVisualNode))
145 IP3DVisualNode node = (IP3DVisualNode)n;
147 if (hasActor(node)) {
148 if (DEBUG) System.out.println("Node already has an actor");
151 if (Thread.currentThread() != view.getThreadQueue().getThread())
152 throw new RuntimeException("Illegal thread.");
156 node.visualize(view);
158 map(node, node.getActors());
160 if (DEBUG) System.out.println("Added " + node.getActors().size() + " actors");
162 if (node instanceof P3DParentNode<?>) {
163 for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
164 if (n2 instanceof IP3DVisualNode)
165 addActor((IP3DVisualNode)n2);
168 updateTransform(node);
176 private boolean hasActor(IP3DVisualNode node) {
177 Collection<vtkProp> list = getRenderObjects(node);
178 if (list == null || list.size() == 0)
183 private void remActor(IP3DVisualNode node) {
184 if (Thread.currentThread() != view.getThreadQueue().getThread())
185 throw new RuntimeException("Illegal thread.");
187 Collection<vtkProp> list = getRenderObjects(node);
188 if (list.size() > 0) {
191 node.stopVisualize();
197 protected void update(ReadGraph graph) throws DatabaseException {
199 // System.out.println("Graph updates");
205 public void commit(String commitMessage) {
207 // System.out.println("Graph commit");
208 super.commit(commitMessage);
213 protected void commit(WriteGraph graph) throws DatabaseException {
215 validateGraph(graph);
218 protected void doCommit() {
219 // System.out.println("Do commit");
224 private void validate() {
225 for (INode node : rootNode.getNodes()) {
226 if (node instanceof PipeRun)
227 PipingRules.validate((PipeRun)node);
232 public synchronized void preRender() {
233 // System.out.println("P3DNodeMap preRender");
234 // super.preRender();
236 // boolean b = false;
237 // synchronized (syncMutex) {
238 // b = PipingRules.update();
241 // super.preRender();
245 b = PipingRules.update();
247 } catch (Exception e) {
253 protected void validateGraph(ReadGraph graph) throws DatabaseException {
254 Plant3D P3D = Plant3D.getInstance(graph);
255 Resource root = (Resource)mapping.inverseGet(rootNode);
256 if (!graph.isInstanceOf(root, P3D.Plant))
257 throw new DatabaseException("Root is not a Plant");
258 Set<Resource> equipment = new HashSet<>();
259 Set<Resource> pipeRuns = new HashSet<>();
260 for (Resource r : graph.getObjects(root, P3D.children)) {
261 if (graph.isInstanceOf(r, P3D.Equipment)) {
263 } else if (graph.isInstanceOf(r, P3D.PipeRun)) {
266 throw new DatabaseException("Unknown resource " + r+ " " + mapping.get(r));
269 Set<Resource> nozzles = new HashSet<>();
270 for (Resource e : equipment) {
271 for (Resource n : graph.getObjects(e, P3D.HasNozzle)) {
272 if (graph.isInstanceOf(n, P3D.Nozzle)) {
275 throw new DatabaseException("Unknown nozzle resource " + n+ " " + mapping.get(n));
280 Set<Resource> components = new HashSet<>();
281 for (Resource run : pipeRuns) {
282 for (Resource c : graph.getObjects(run, P3D.children)) {
283 if (graph.isInstanceOf(c, P3D.PipelineComponent)) {
286 throw new DatabaseException("Unknown component resource " + c + " " + mapping.get(c));
291 for (Resource c : components) {
292 for (Resource connected : graph.getObjects(c, P3D.Connects)) {
293 if (!components.contains(connected) && !nozzles.contains(connected)) {
294 throw new DatabaseException("Unbrowsable component resource " + connected + " " + mapping.get(connected) +" connected to " + c + " " + mapping.get(c));
296 Collection<Resource> connectedConnected = graph.getObjects(connected, P3D.Connects);
297 if (!connectedConnected.contains(c)) {
298 throw new DatabaseException("Component resource " + c + " " + mapping.get(c) + " is connected to " + connected + " " + mapping.get(connected) +", but its has no connection back.");