1 package org.simantics.plant3d.editor;
3 import java.util.ArrayList;
4 import java.util.HashSet;
8 import org.eclipse.jface.action.IMenuListener;
9 import org.eclipse.jface.action.IMenuManager;
10 import org.eclipse.jface.action.MenuManager;
11 import org.eclipse.jface.layout.GridDataFactory;
12 import org.eclipse.jface.layout.GridLayoutFactory;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.ISelectionProvider;
15 import org.eclipse.jface.viewers.SelectionChangedEvent;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.g3d.scenegraph.NodeMap;
29 import org.simantics.g3d.scenegraph.RenderListener;
30 import org.simantics.g3d.scenegraph.base.INode;
31 import org.simantics.g3d.toolbar.ToolComposite;
32 import org.simantics.g3d.vtk.action.RemoveAction;
33 import org.simantics.g3d.vtk.common.HoverHighlighter;
34 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
35 import org.simantics.g3d.vtk.common.SelectionHighlighter;
36 import org.simantics.g3d.vtk.common.VTKSelectionItem;
37 import org.simantics.g3d.vtk.common.VtkView;
38 import org.simantics.g3d.vtk.shape.vtkShape;
39 import org.simantics.g3d.vtk.swt.ContextMenuListener;
40 import org.simantics.g3d.vtk.swt.FocusAction;
41 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
42 import org.simantics.g3d.vtk.swt.RotateAction;
43 import org.simantics.g3d.vtk.swt.TranslateAction;
44 import org.simantics.g3d.vtk.swt.vtkCameraAndSelectorAction;
45 import org.simantics.g3d.vtk.utils.AxesDisplay;
46 import org.simantics.objmap.graph.IMapping;
47 import org.simantics.objmap.graph.Mappings;
48 import org.simantics.objmap.graph.schema.IMappingSchema;
49 import org.simantics.plant3d.actions.AddComponentAction;
50 import org.simantics.plant3d.actions.AddEquipmentAction;
51 import org.simantics.plant3d.actions.AddNozzleAction;
52 import org.simantics.plant3d.actions.RemoveAndSplitAction;
53 import org.simantics.plant3d.actions.ReversePipeRunAction;
54 import org.simantics.plant3d.actions.RoutePipeAction;
55 import org.simantics.plant3d.actions.TranslateFreeVariableLengthAction;
56 import org.simantics.plant3d.actions.TranslateInlineAction;
57 import org.simantics.plant3d.ontology.Plant3D;
58 import org.simantics.plant3d.scenegraph.EndComponent;
59 import org.simantics.plant3d.scenegraph.Equipment;
60 import org.simantics.plant3d.scenegraph.IP3DNode;
61 import org.simantics.plant3d.scenegraph.InlineComponent;
62 import org.simantics.plant3d.scenegraph.Nozzle;
63 import org.simantics.plant3d.scenegraph.P3DRootNode;
64 import org.simantics.plant3d.scenegraph.PipeRun;
65 import org.simantics.plant3d.scenegraph.PipelineComponent;
66 import org.simantics.plant3d.scenegraph.SchemaBuilder;
67 import org.simantics.plant3d.scenegraph.TurnComponent;
68 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
69 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
70 import org.simantics.plant3d.utils.ComponentUtils;
71 import org.simantics.plant3d.utils.Item;
72 import org.simantics.plant3d.utils.P3DUtil;
73 import org.simantics.selectionview.StandardPropertyPage;
74 import org.simantics.ui.workbench.IPropertyPage;
75 import org.simantics.ui.workbench.IResourceEditorInput;
76 import org.simantics.ui.workbench.ResourceEditorPart;
77 import org.simantics.utils.threads.ThreadUtils;
78 import org.simantics.utils.ui.ExceptionUtils;
81 import vtk.vtkCameraPass;
82 import vtk.vtkDefaultPass;
83 import vtk.vtkGaussianBlurPass;
84 import vtk.vtkLightsPass;
86 import vtk.vtkRenderPassCollection;
87 import vtk.vtkRenderer;
88 import vtk.vtkSSAAPass;
89 import vtk.vtkSequencePass;
90 import vtk.vtkSimpleMotionBlurPass;
93 public class Plant3DEditor extends ResourceEditorPart {
95 private Composite parent;
96 protected ToolComposite toolComposite;
97 private Resource input;
98 // private InteractiveVtkPanel panel;
99 // private SWTAWTComponent component;
100 private InteractiveVtkComposite panel;
103 private P3DRootNode rootNode;
104 private IMapping<Resource,INode> mapping;
106 protected NodeSelectionProvider2<Resource, INode> selectionProvider;
109 protected vtkCameraAndSelectorAction cameraAction;
110 protected FocusAction focusAction;
111 protected TranslateAction translateAction;
112 protected TranslateInlineAction translateInlineAction;
113 protected TranslateFreeVariableLengthAction translateFreeVariableLengthAction;
114 protected RotateAction rotateAction;
115 protected RemoveAction removeAction;
116 protected RemoveAndSplitAction removeSplitAction;
117 protected RoutePipeAction routePipeAction;
118 protected AddComponentAction addComponentAction;
119 protected ReversePipeRunAction reversePipeRunAction;
121 private P3DNodeMap nodeMap;
124 public void createPartControl(Composite parent) {
125 this.parent = parent;
126 //parent.setLayout (new FillLayout ());
129 // component = new SWTAWTComponent(parent,SWT.NONE) {
132 // protected Component createSwingComponent() {
133 // if (panel == null) {
134 // panel = new InteractiveVtkPanel();
135 // vtkPanelUtil.registerPanel(panel);
142 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
143 input = rei.getResource();
145 toolComposite = new ToolComposite(parent, SWT.BORDER);
146 toolComposite.setVisible(true);
148 panel = new InteractiveVtkComposite(parent);
150 GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).applyTo(parent);
151 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(getPanel().getComponent());
154 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(toolComposite);
156 //IActionBars actionBars = getEditorSite().getActionBars();
162 new ContextMenuListener(panel, contextMenu);
164 cameraAction = createCameraAction();
165 panel.setDefaultAction(cameraAction);
166 panel.useDefaultAction();
167 panel.setPickType(4);
170 ControlPointFactory.preloadCache(getLibraryUri());
171 ComponentUtils.preloadCache();
172 } catch (Exception e) {
173 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
178 getSession().syncRequest(new ReadRequest() {
181 public void run(ReadGraph graph) throws DatabaseException {
182 System.out.println("START PLANT3D LOAD");
183 PipingRules.setEnabled(false);
184 IMappingSchema<Resource,INode> schema = getSchema(graph);
185 mapping = Mappings.createWithListening(schema);
186 rootNode = (P3DRootNode)mapping.map(graph, input);
187 // update control points.
188 // TODO : this should be optimized.
191 P3DUtil.finalizeDBLoad(rootNode);
192 nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
194 } catch (Exception e) {
195 throw new DatabaseException(e);
198 System.out.println("END PLANT3D LOAD");
202 if (rootNode == null)
203 throw new RuntimeException("Scenegraph loading failed.");
206 selectionProvider = createSelectionProvider();
208 cameraAction.addSelectionChangedListener(selectionProvider);
210 cameraAction.addHoverChangedListener(createHoverHighlhighter());
211 selectionProvider.addSelectionChangedListener(createSelectionHighlighter());
213 getSite().setSelectionProvider(selectionProvider);
214 getSite().getPage().addPostSelectionListener(selectionProvider);
216 //outlinePage = new ScenegraphOutlinePage(rootNode);
219 parent.addDisposeListener(new DisposeListener() {
222 public void widgetDisposed(DisposeEvent e) {
223 getSite().getPage().removePostSelectionListener(selectionProvider);
225 PipingRules.setEnabled(false);
227 PipingRules.setEnabled(true);
229 // component.dispose();
230 //panel.getComponent().dispose();
234 } catch (DatabaseException e1) {
235 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
243 protected vtkCameraAndSelectorAction createCameraAction() {
244 return new vtkCameraAndSelectorAction(panel);
247 protected NodeSelectionProvider2<Resource,INode> createSelectionProvider() {
248 return new NodeSelectionProvider2<Resource,INode>(this,mapping,nodeMap);
251 protected HoverHighlighter<Resource> createHoverHighlhighter() {
252 return new HoverHighlighter<>(panel,nodeMap);
255 protected SelectionHighlighter<Resource> createSelectionHighlighter() {
256 return new SelectionHighlighter<Resource>(panel,nodeMap);
259 protected String getLibraryUri() {
260 return Plant3D.URIs.Builtin;
263 protected void createActions() {
264 focusAction = new FocusAction(panel, cameraAction);
265 translateAction = new TranslateAction(panel,nodeMap,toolComposite);
266 translateInlineAction = new TranslateInlineAction(panel, nodeMap,toolComposite);
267 translateFreeVariableLengthAction = new TranslateFreeVariableLengthAction(panel, getRootNode(), toolComposite);
268 rotateAction = new RotateAction(panel,nodeMap,toolComposite);
269 removeAction = new RemoveAction(nodeMap);
270 removeSplitAction = new RemoveAndSplitAction(nodeMap);
271 routePipeAction = new RoutePipeAction(panel,rootNode, toolComposite);
272 addComponentAction = new AddComponentAction(panel, rootNode, getLibraryUri());
273 reversePipeRunAction = new ReversePipeRunAction(nodeMap);
276 public void populate() {
277 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
282 panel.addListener(new RenderListener() {
285 public void preRender() {
290 public void postRender() {
292 P3DUtil.finalizeDBLoad2(rootNode);
293 if (nodeMap.isRangeModified());
294 nodeMap.commit("Load sync");
295 } catch (Exception e) {
296 //throw new DatabaseException(e);
298 panel.removeListener(this);
308 protected IMappingSchema<Resource, INode> getSchema(ReadGraph graph) throws DatabaseException {
309 IMappingSchema<Resource,INode> schema = SchemaBuilder.getSchema(graph);
313 protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, INode> mapping, VtkView panel, P3DRootNode rootNode) {
314 return new P3DNodeMap(session, mapping, panel,rootNode);
318 public void setFocus() {
319 //component.setFocus();
320 panel.getComponent().setFocus();
323 private void createScene() {
324 vtkRenderer ren1 = panel.getRenderer();
326 boolean multiPass = false;
327 boolean blur = false;
328 boolean ssaa = false;
329 //boolean sobel = true;
330 boolean mblur = false;
333 vtkLightsPass lightsPass = new vtkLightsPass();
334 vtkDefaultPass defaultPass = new vtkDefaultPass();
337 vtkRenderPassCollection passes = new vtkRenderPassCollection();
338 passes.AddItem(lightsPass);
339 passes.AddItem(defaultPass);
341 vtkSequencePass seq = new vtkSequencePass();
342 seq.SetPasses(passes);
344 vtkCameraPass cameraPass = new vtkCameraPass();
345 cameraPass.SetDelegatePass(seq);
348 vtkGaussianBlurPass blurPass = new vtkGaussianBlurPass();
349 blurPass.SetDelegatePass(cameraPass);
350 ren1.SetPass(blurPass);
352 vtkSSAAPass ssaaPass = new vtkSSAAPass();
353 ssaaPass.SetDelegatePass(cameraPass);
354 ren1.SetPass(ssaaPass);
356 vtkSimpleMotionBlurPass mBlurPass = new vtkSimpleMotionBlurPass();
357 mBlurPass.SetDelegatePass(cameraPass);
358 ren1.SetPass(mBlurPass);
359 // } else if (sobel) {
360 // vtkSobelGradientMagnitudePass sobelPass = new vtkSobelGradientMagnitudePass();
361 // sobelPass.SetDelegatePass(sobelPass);
362 // ren1.SetPass(sobelPass);
364 ren1.SetPass(cameraPass);
368 // ren1.GetRenderWindow().LineSmoothingOn();
369 // ren1.GetRenderWindow().PointSmoothingOn();
370 // ren1.GetRenderWindow().PolygonSmoothingOn();
371 // ren1.GetRenderWindow().SetMultiSamples(2);
375 ren1.SetBackground2(1,1,1); // background color white
376 ren1.SetBackground(0.9,0.9,0.9);
377 ren1.SetGradientBackground(true);
379 // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
380 vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
383 panel.addDeletable(grid);
385 AxesDisplay axesDisplay = new AxesDisplay(panel);
390 protected Menu contextMenu;
392 protected void hookContextMenu() {
393 MenuManager menuMgr = new MenuManager("#PopupMenu");
394 menuMgr.setRemoveAllWhenShown(true);
395 menuMgr.addMenuListener(new IMenuListener() {
396 public void menuAboutToShow(IMenuManager manager) {
397 createContextMenu(manager);
401 contextMenu = menuMgr.createContextMenu(parent);
404 protected void createContextMenu(IMenuManager m) {
405 List<INode> selected = selectionProvider.getSelectedNodes();
406 List<VTKSelectionItem<Resource>> selectedItems = selectionProvider.getSelectedItems();
407 if (selectedItems.size() > 0) {
408 List<vtkProp> props = new ArrayList<>();
409 for (VTKSelectionItem<Resource> i : selectedItems) {
410 vtkProp p = (vtkProp)i.getAdapter(vtkProp.class);
414 if (props.size() > 0) {
415 focusAction.setProps(props);
420 if (selected.size() == 0) {
421 m.add(new AddEquipmentAction(rootNode, getLibraryUri()));
422 // for (Item eq : P3DUtil.getEquipments(getLibraryUri())) {
423 // m.add(new AddEquipmentAction(rootNode, eq));
425 } else if (selected.size() == 1) {
426 IP3DNode node = (IP3DNode)selected.get(0);
427 if (node instanceof Equipment) {
428 m.add(translateAction);
430 for (Item eq : P3DUtil.getNozzles(getLibraryUri())) {
431 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
432 add.setEquipment((Equipment)node);
436 } else if (node instanceof Nozzle) {
437 Nozzle nozzle = (Nozzle)node;
438 if (!nozzle.isFixed()) {
439 m.add(translateAction);
442 m.add(routePipeAction);
443 routePipeAction.setComponent(nozzle);
444 routePipeAction.setEnabled(nozzle.getNext() == null && nozzle.getPrevious() == null);
445 m.add(addComponentAction);
446 addComponentAction.setComponent(nozzle);
448 } else if (node instanceof TurnComponent) {
449 m.add(translateAction);
450 TurnComponent component = (TurnComponent)node;
451 m.add(routePipeAction);
452 routePipeAction.setComponent(component);
453 routePipeAction.setEnabled(component.getNext() == null || component.getPrevious() == null);
454 m.add(addComponentAction);
455 addComponentAction.setComponent(component);
457 m.add(removeSplitAction);
458 removeSplitAction.setNode(node);
459 } else if (node instanceof EndComponent) {
460 m.add(translateAction);
461 m.add(addComponentAction);
462 addComponentAction.setComponent((PipelineComponent)node);
464 } else if (node instanceof InlineComponent) {
465 //m.add(translateInlineAction);
466 InlineComponent component = (InlineComponent)node;
467 if (component.isVariableLength())
468 m.add(translateFreeVariableLengthAction);
470 m.add(translateInlineAction);
471 m.add(routePipeAction);
472 routePipeAction.setComponent(component);
473 m.add(addComponentAction);
474 addComponentAction.setComponent(component);
476 m.add(removeSplitAction);
477 removeSplitAction.setNode(node);
478 } else if (node instanceof PipeRun) {
479 m.add(reversePipeRunAction);
481 reversePipeRunAction.setNode(node);
487 translateAction.setNode(node);
488 translateInlineAction.setNode(node);
489 translateFreeVariableLengthAction.setNode(node);
490 rotateAction.setNode(node);
491 removeAction.setNode(node);
494 } catch (DatabaseException e) {
495 ExceptionUtils.logAndShowError(e);
499 private IContentOutlinePage createOutline() {
500 if (rootNode == null || selectionProvider == null)
502 //IContentOutlinePage outlinePage = new VTKContentOutlinePage<Resource,Object>(rootNode, selectionProvider);
503 IContentOutlinePage outlinePage = new P3DContentOutlinePage(rootNode, selectionProvider) {
504 protected void createContextMenu(IMenuManager manager) {
505 Plant3DEditor.this.createContextMenu(manager);
508 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
511 public void selectionChanged(SelectionChangedEvent event) {
512 selectionProvider.selectionChanged(event);
519 public <T> T getAdapter(Class<T> adapter) {
520 if (adapter.isAssignableFrom(IPropertyPage.class))
521 return adapter.cast(new StandardPropertyPage(getSite(),getPropertyContexts()));
522 if (adapter.isAssignableFrom(ISelectionProvider.class))
523 return adapter.cast(selectionProvider);
524 if (adapter.isAssignableFrom(IContentOutlinePage.class)) {
525 return adapter.cast(createOutline());
527 if (adapter.isAssignableFrom(NodeMap.class)) {
528 return adapter.cast(nodeMap);
530 if (adapter.isAssignableFrom(INode.class)) {
531 return adapter.cast(rootNode);
533 if (adapter.isAssignableFrom(IMapping.class)) {
534 return adapter.cast(mapping);
536 // if (adapter.isAssignableFrom(InteractiveVtkPanel.class)) {
537 // return adapter.cast(panel);
539 if (adapter.isAssignableFrom(VtkView.class))
540 return adapter.cast(panel);
541 return super.getAdapter(adapter);
544 public Set<String> getPropertyContexts() {
545 Set<String> result = new HashSet<String>();
546 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
550 public InteractiveVtkComposite getPanel() {
554 public P3DRootNode getRootNode() {
558 public IMapping<Resource, INode> getMapping() {
562 public P3DNodeMap getNodeMap() {