1 package org.simantics.plant3d.editor;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.action.IAction;
11 import org.eclipse.jface.action.IMenuListener;
12 import org.eclipse.jface.action.IMenuManager;
13 import org.eclipse.jface.action.MenuManager;
14 import org.eclipse.jface.action.Separator;
15 import org.eclipse.jface.layout.GridDataFactory;
16 import org.eclipse.jface.layout.GridLayoutFactory;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
26 import org.simantics.Simantics;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.Session;
30 import org.simantics.db.common.request.ParametrizedRead;
31 import org.simantics.db.common.request.ReadRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.request.combinations.Combinators;
34 import org.simantics.g3d.scenegraph.NodeMap;
35 import org.simantics.g3d.scenegraph.RenderListener;
36 import org.simantics.g3d.scenegraph.base.INode;
37 import org.simantics.g3d.scenegraph.base.ParentNode;
38 import org.simantics.g3d.toolbar.ToolComposite;
39 import org.simantics.g3d.vtk.action.RemoveAction;
40 import org.simantics.g3d.vtk.common.HoverHighlighter;
41 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
42 import org.simantics.g3d.vtk.common.SelectionHighlighter;
43 import org.simantics.g3d.vtk.common.VtkView;
44 import org.simantics.g3d.vtk.shape.vtkShape;
45 import org.simantics.g3d.vtk.swt.ContextMenuListener;
46 import org.simantics.g3d.vtk.swt.FocusAction;
47 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
48 import org.simantics.g3d.vtk.swt.RotateAction;
49 import org.simantics.g3d.vtk.swt.TranslateAction;
50 import org.simantics.g3d.vtk.swt.vtkCameraAndSelectorAction;
51 import org.simantics.g3d.vtk.utils.AxesDisplay;
52 import org.simantics.objmap.graph.IMapping;
53 import org.simantics.objmap.graph.Mappings;
54 import org.simantics.objmap.graph.schema.IMappingSchema;
55 import org.simantics.plant3d.actions.AddComponentAction;
56 import org.simantics.plant3d.actions.AddEquipmentAction;
57 import org.simantics.plant3d.actions.AddNozzleAction;
58 import org.simantics.plant3d.actions.RemoveAndSplitAction;
59 import org.simantics.plant3d.actions.ReversePipeRunAction;
60 import org.simantics.plant3d.actions.RoutePipeAction;
61 import org.simantics.plant3d.actions.TranslateFreeVariableLengthAction;
62 import org.simantics.plant3d.actions.TranslateInlineAction;
63 import org.simantics.plant3d.ontology.Plant3D;
64 import org.simantics.plant3d.scenegraph.EndComponent;
65 import org.simantics.plant3d.scenegraph.Equipment;
66 import org.simantics.plant3d.scenegraph.IP3DNode;
67 import org.simantics.plant3d.scenegraph.IP3DVisualNode;
68 import org.simantics.plant3d.scenegraph.InlineComponent;
69 import org.simantics.plant3d.scenegraph.Nozzle;
70 import org.simantics.plant3d.scenegraph.P3DRootNode;
71 import org.simantics.plant3d.scenegraph.PipeRun;
72 import org.simantics.plant3d.scenegraph.PipelineComponent;
73 import org.simantics.plant3d.scenegraph.SchemaBuilder;
74 import org.simantics.plant3d.scenegraph.TurnComponent;
75 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
76 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
77 import org.simantics.plant3d.utils.ComponentUtils;
78 import org.simantics.plant3d.utils.Item;
79 import org.simantics.plant3d.utils.P3DUtil;
80 import org.simantics.selectionview.StandardPropertyPage;
81 import org.simantics.ui.workbench.IPropertyPage;
82 import org.simantics.ui.workbench.IResourceEditorInput;
83 import org.simantics.ui.workbench.ResourceEditorPart;
84 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;
85 import org.simantics.utils.threads.ThreadUtils;
86 import org.simantics.utils.ui.ExceptionUtils;
89 import vtk.vtkCameraPass;
90 import vtk.vtkDefaultPass;
91 import vtk.vtkGaussianBlurPass;
92 import vtk.vtkLightsPass;
94 import vtk.vtkRenderPassCollection;
95 import vtk.vtkRenderer;
96 import vtk.vtkSSAAPass;
97 import vtk.vtkSequencePass;
98 import vtk.vtkSimpleMotionBlurPass;
101 public class Plant3DEditor extends ResourceEditorPart {
103 private Composite parent;
104 protected ToolComposite toolComposite;
105 private Resource input;
106 // private InteractiveVtkPanel panel;
107 // private SWTAWTComponent component;
108 private InteractiveVtkComposite panel;
111 private P3DRootNode rootNode;
112 private IMapping<Resource,INode> mapping;
114 protected NodeSelectionProvider2<Resource, INode> selectionProvider;
117 protected vtkCameraAndSelectorAction cameraAction;
118 protected FocusAction focusAction;
119 protected TranslateAction translateAction;
120 protected TranslateInlineAction translateInlineAction;
121 protected TranslateFreeVariableLengthAction translateFreeVariableLengthAction;
122 protected RotateAction rotateAction;
123 protected RemoveAction removeAction;
124 protected RemoveAndSplitAction removeSplitAction;
125 protected RoutePipeAction routePipeAction;
126 protected AddComponentAction addComponentAction;
127 protected ReversePipeRunAction reversePipeRunAction;
129 private P3DNodeMap nodeMap;
131 /** Constants for selecting the up-direction */
132 public static final int X = 0, Y = 1, Z = 2;
134 protected int upDirection = 1;
136 ParametrizedRead<IResourceEditorInput, Boolean> INPUT_VALIDATOR =
138 InputValidationCombinators.hasURI(),
139 InputValidationCombinators.extractInputResource()
143 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
144 return INPUT_VALIDATOR;
148 public void createPartControl(Composite parent) {
149 this.parent = parent;
151 activateValidation();
153 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
154 input = rei.getResource();
156 toolComposite = new ToolComposite(parent, SWT.BORDER);
157 toolComposite.setVisible(true);
159 panel = new InteractiveVtkComposite(parent);
161 GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).applyTo(parent);
162 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(getPanel().getComponent());
165 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(toolComposite);
167 //IActionBars actionBars = getEditorSite().getActionBars();
173 new ContextMenuListener(panel, contextMenu);
175 cameraAction = createCameraAction();
176 switch (upDirection) {
178 cameraAction.setUpDirection(new double[] { 1, 0, 0 });
181 cameraAction.setUpDirection(new double[] { 0, 1, 0 });
184 cameraAction.setUpDirection(new double[] { 0, 0, 1 });
188 panel.setDefaultAction(cameraAction);
189 panel.useDefaultAction();
190 panel.setPickType(4);
193 ControlPointFactory.preloadCache(Simantics.getSession(), getLibraryUri());
194 ComponentUtils.preloadCache(Simantics.getSession());
195 } catch (Exception e) {
196 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
201 getSession().syncRequest(new ReadRequest() {
204 public void run(ReadGraph graph) throws DatabaseException {
205 //System.out.println("START PLANT3D LOAD");
206 PipingRules.setEnabled(false);
207 IMappingSchema<Resource,INode> schema = getSchema(graph);
208 mapping = Mappings.createWithListening(schema);
209 rootNode = (P3DRootNode)mapping.map(graph, input);
210 // update control points.
211 // TODO : this should be optimized.
214 P3DUtil.finalizeDBLoad(rootNode);
215 nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
217 } catch (Exception e) {
218 throw new DatabaseException(e);
221 //System.out.println("END PLANT3D LOAD");
225 if (rootNode == null)
226 throw new RuntimeException("Scenegraph loading failed.");
229 selectionProvider = createSelectionProvider();
231 cameraAction.addSelectionChangedListener(selectionProvider);
233 cameraAction.addHoverChangedListener(createHoverHighlhighter());
234 selectionProvider.addSelectionChangedListener(createSelectionHighlighter());
236 getSite().setSelectionProvider(selectionProvider);
237 getSite().getPage().addPostSelectionListener(selectionProvider);
239 //outlinePage = new ScenegraphOutlinePage(rootNode);
242 parent.addDisposeListener(new DisposeListener() {
245 public void widgetDisposed(DisposeEvent e) {
246 getSite().getPage().removePostSelectionListener(selectionProvider);
248 PipingRules.setEnabled(false);
250 PipingRules.setEnabled(true);
252 // component.dispose();
253 //panel.getComponent().dispose();
257 } catch (DatabaseException e1) {
258 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
265 public void setUpDirection(int upDirection) {
266 this.upDirection = upDirection;
269 protected vtkCameraAndSelectorAction createCameraAction() {
270 return new vtkCameraAndSelectorAction(panel);
273 protected NodeSelectionProvider2<Resource,INode> createSelectionProvider() {
274 return new NodeSelectionProvider2<Resource,INode>(this,mapping,nodeMap);
277 protected HoverHighlighter<Resource> createHoverHighlhighter() {
278 return new HoverHighlighter<>(panel,nodeMap);
281 protected SelectionHighlighter<Resource> createSelectionHighlighter() {
282 return new SelectionHighlighter<Resource>(panel,nodeMap);
285 protected String getLibraryUri() {
286 return Plant3D.URIs.Builtin;
289 protected void createActions() {
290 focusAction = new FocusAction(panel, cameraAction);
291 translateAction = new TranslateAction(panel,nodeMap,toolComposite);
292 translateInlineAction = new TranslateInlineAction(panel, nodeMap,toolComposite);
293 translateFreeVariableLengthAction = new TranslateFreeVariableLengthAction(panel, getRootNode(), toolComposite);
294 rotateAction = new RotateAction(panel,nodeMap,toolComposite);
295 removeAction = new RemoveAction(nodeMap);
296 removeSplitAction = new RemoveAndSplitAction(nodeMap);
297 routePipeAction = new RoutePipeAction(panel,rootNode, toolComposite);
298 addComponentAction = new AddComponentAction(panel, rootNode, getLibraryUri());
299 reversePipeRunAction = new ReversePipeRunAction(nodeMap);
302 public void populate() {
303 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
307 nodeMap.setChangeTracking(false);
309 nodeMap.setChangeTracking(true);
310 panel.addListener(new RenderListener() {
313 public void preRender() {
318 public void postRender() {
319 panel.removeListener(this);
322 P3DUtil.finalizeDBLoad2(rootNode);
323 if (nodeMap.getMapping().isRangeModified())
324 nodeMap.commit("Load sync");
325 } catch (Exception e) {
326 ExceptionUtils.logAndShowError("Failed to load model correctly", e);
329 onEditorInitializationComplete();
331 List<vtkProp3D> props = new ArrayList<>();
332 collectProps(rootNode, props);
342 * This can be overridden by clients to perform extra initialization tasks
344 protected void onEditorInitializationComplete() {
347 protected IMappingSchema<Resource, INode> getSchema(ReadGraph graph) throws DatabaseException {
348 IMappingSchema<Resource,INode> schema = SchemaBuilder.getSchema(graph);
352 protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, INode> mapping, VtkView panel, P3DRootNode rootNode) {
353 return new P3DNodeMap(session, mapping, panel,rootNode);
357 public void setFocus() {
358 //component.setFocus();
359 panel.getComponent().setFocus();
362 protected void createScene() {
363 vtkRenderer ren1 = panel.getRenderer();
365 boolean multiPass = false;
366 boolean blur = false;
367 boolean ssaa = false;
368 //boolean sobel = true;
369 boolean mblur = false;
372 vtkLightsPass lightsPass = new vtkLightsPass();
373 vtkDefaultPass defaultPass = new vtkDefaultPass();
376 vtkRenderPassCollection passes = new vtkRenderPassCollection();
377 passes.AddItem(lightsPass);
378 passes.AddItem(defaultPass);
380 vtkSequencePass seq = new vtkSequencePass();
381 seq.SetPasses(passes);
383 vtkCameraPass cameraPass = new vtkCameraPass();
384 cameraPass.SetDelegatePass(seq);
387 vtkGaussianBlurPass blurPass = new vtkGaussianBlurPass();
388 blurPass.SetDelegatePass(cameraPass);
389 ren1.SetPass(blurPass);
391 vtkSSAAPass ssaaPass = new vtkSSAAPass();
392 ssaaPass.SetDelegatePass(cameraPass);
393 ren1.SetPass(ssaaPass);
395 vtkSimpleMotionBlurPass mBlurPass = new vtkSimpleMotionBlurPass();
396 mBlurPass.SetDelegatePass(cameraPass);
397 ren1.SetPass(mBlurPass);
398 // } else if (sobel) {
399 // vtkSobelGradientMagnitudePass sobelPass = new vtkSobelGradientMagnitudePass();
400 // sobelPass.SetDelegatePass(sobelPass);
401 // ren1.SetPass(sobelPass);
403 ren1.SetPass(cameraPass);
407 // ren1.GetRenderWindow().LineSmoothingOn();
408 // ren1.GetRenderWindow().PointSmoothingOn();
409 // ren1.GetRenderWindow().PolygonSmoothingOn();
410 // ren1.GetRenderWindow().SetMultiSamples(2);
414 ren1.SetBackground2(1,1,1); // background color white
415 ren1.SetBackground(0.9,0.9,0.9);
416 ren1.SetGradientBackground(true);
418 // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
419 int dir = 1 << upDirection;
420 vtkActor grid = vtkShape.createGridActor(8, 1.0, dir);
423 panel.addDeletable(grid);
425 AxesDisplay axesDisplay = new AxesDisplay(panel);
429 protected Menu contextMenu;
431 protected void hookContextMenu() {
432 MenuManager menuMgr = new MenuManager("#PopupMenu");
433 menuMgr.setRemoveAllWhenShown(true);
434 menuMgr.addMenuListener(new IMenuListener() {
435 public void menuAboutToShow(IMenuManager manager) {
436 createContextMenu(manager);
440 contextMenu = menuMgr.createContextMenu(parent);
443 protected void createContextMenu(IMenuManager m) {
444 List<INode> selected = selectionProvider.getSelectedNodes();
446 createFocusMenu(m, selected);
448 m.add(new Separator());
451 if (selected.size() == 0) {
452 m.add(new AddEquipmentAction(rootNode, getLibraryUri()));
453 // for (Item eq : P3DUtil.getEquipments(getLibraryUri())) {
454 // m.add(new AddEquipmentAction(rootNode, eq));
456 } else if (selected.size() == 1) {
457 IP3DNode node = (IP3DNode)selected.get(0);
458 if (node instanceof Equipment) {
459 m.add(translateAction);
461 for (Item eq : P3DUtil.getNozzles(Simantics.getSession(), getLibraryUri())) {
462 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
463 add.setEquipment((Equipment)node);
467 } else if (node instanceof Nozzle) {
468 Nozzle nozzle = (Nozzle)node;
469 if (!nozzle.isFixed()) {
470 m.add(translateAction);
473 m.add(routePipeAction);
474 routePipeAction.setComponent(nozzle);
475 routePipeAction.setEnabled(nozzle.getNext() == null && nozzle.getPrevious() == null);
476 m.add(addComponentAction);
477 addComponentAction.setComponent(nozzle);
479 } else if (node instanceof TurnComponent) {
480 m.add(translateAction);
481 TurnComponent component = (TurnComponent)node;
482 m.add(routePipeAction);
483 routePipeAction.setComponent(component);
484 routePipeAction.setEnabled(component.getNext() == null || component.getPrevious() == null);
485 m.add(addComponentAction);
486 addComponentAction.setComponent(component);
488 m.add(removeSplitAction);
489 removeSplitAction.setNode(node);
490 } else if (node instanceof EndComponent) {
491 m.add(translateAction);
492 m.add(addComponentAction);
493 addComponentAction.setComponent((PipelineComponent)node);
495 } else if (node instanceof InlineComponent) {
496 //m.add(translateInlineAction);
497 InlineComponent component = (InlineComponent)node;
498 if (component.isVariableLength())
499 m.add(translateFreeVariableLengthAction);
501 m.add(translateInlineAction);
502 m.add(routePipeAction);
503 routePipeAction.setComponent(component);
504 m.add(addComponentAction);
505 addComponentAction.setComponent(component);
507 m.add(removeSplitAction);
508 removeSplitAction.setNode(node);
509 } else if (node instanceof PipeRun) {
510 m.add(reversePipeRunAction);
512 reversePipeRunAction.setNode(node);
518 translateAction.setNode(node);
519 translateInlineAction.setNode(node);
520 translateFreeVariableLengthAction.setNode(node);
521 rotateAction.setNode(node);
522 removeAction.setNode(node);
525 } catch (DatabaseException e) {
526 ExceptionUtils.logAndShowError(e);
530 protected class FitToWindow extends Action {
531 private List<INode> selected;
532 public FitToWindow(List<INode> selected) {
533 super("Fit to Window");
534 this.selected = selected;
535 //setAccelerator('1');
539 List<vtkProp3D> props = new ArrayList<>();
540 final Collection<INode> collection = !selected.isEmpty() ? selected : getRootNode().getChild();
541 for (INode n : collection)
542 collectProps(n, props);
545 getPanel().refresh();
549 protected void createFocusMenu(IMenuManager m, List<INode> selected) {
550 m.add(createFitToWindowAction(selected));
552 if (!selected.isEmpty()) {
553 List<vtkProp3D> actors = new ArrayList<>();
554 for (INode n : selected)
555 collectProps(n, actors);
556 if (actors.size() > 0) {
557 focusAction.setProps(new ArrayList<>(actors));
563 protected IAction createFitToWindowAction(List<INode> selected) {
564 return new FitToWindow(selected);
567 private IContentOutlinePage createOutline() {
568 if (rootNode == null || selectionProvider == null)
570 //IContentOutlinePage outlinePage = new VTKContentOutlinePage<Resource,Object>(rootNode, selectionProvider);
571 IContentOutlinePage outlinePage = new P3DContentOutlinePage(rootNode, selectionProvider) {
572 protected void createContextMenu(IMenuManager manager) {
573 Plant3DEditor.this.createContextMenu(manager);
576 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
579 public void selectionChanged(SelectionChangedEvent event) {
580 selectionProvider.selectionChanged(event);
587 public <T> T getAdapter(Class<T> adapter) {
588 if (adapter.isAssignableFrom(IPropertyPage.class))
589 return adapter.cast(new StandardPropertyPage(getSite(),getPropertyContexts()));
590 if (adapter.isAssignableFrom(ISelectionProvider.class))
591 return adapter.cast(selectionProvider);
592 if (adapter.isAssignableFrom(IContentOutlinePage.class)) {
593 return adapter.cast(createOutline());
595 if (adapter.isAssignableFrom(NodeMap.class)) {
596 return adapter.cast(nodeMap);
598 if (adapter.isAssignableFrom(INode.class)) {
599 return adapter.cast(rootNode);
601 if (adapter.isAssignableFrom(IMapping.class)) {
602 return adapter.cast(mapping);
604 // if (adapter.isAssignableFrom(InteractiveVtkPanel.class)) {
605 // return adapter.cast(panel);
607 if (adapter.isAssignableFrom(VtkView.class))
608 return adapter.cast(panel);
609 return super.getAdapter(adapter);
612 public Set<String> getPropertyContexts() {
613 Set<String> result = new HashSet<String>();
614 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
618 public InteractiveVtkComposite getPanel() {
622 public P3DRootNode getRootNode() {
626 public IMapping<Resource, INode> getMapping() {
630 public P3DNodeMap getNodeMap() {
634 public void fitToWindow(Collection<vtkProp3D> props) {
635 cameraAction.fitToView(props);
638 protected static void collectProps(INode node, List<vtkProp3D> props) {
639 if (node instanceof IP3DVisualNode)
640 props.addAll(((IP3DVisualNode) node).getActors());
642 if (node instanceof ParentNode)
643 for (INode n : ((ParentNode<?>) node).getNodes()) {
644 collectProps(n, props);