From: Marko Luukkainen Date: Fri, 1 Nov 2019 15:49:41 +0000 (+0200) Subject: Focus action X-Git-Tag: v1.43.0~153 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F41%2F3441%2F1;p=simantics%2F3d.git Focus action gitlab #45 Change-Id: Ic8439e3582c9759534a224d1aba6563707b2988b --- diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java new file mode 100644 index 00000000..97d5abf0 --- /dev/null +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java @@ -0,0 +1,57 @@ +package org.simantics.g3d.vtk.swt; + +import java.util.Collection; + +import org.eclipse.jface.action.Action; +import org.simantics.g3d.vtk.Activator; +import org.simantics.g3d.vtk.common.VtkView; +import org.simantics.utils.threads.AWTThread; + +import vtk.vtkProp; +import vtk.vtkProp3D; + +public class FocusAction extends Action { + + VtkView view; + vtkCameraAndSelectorAction cameraAndSelectorAction; + Collection props; + + public FocusAction(VtkView view, vtkCameraAndSelectorAction cameraAndSelectorAction) { + super("Focus"); + setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/eye.png")); + this.view = view; + this.cameraAndSelectorAction = cameraAndSelectorAction; + } + + public void setProps(Collection props) { + this.props = props; + } + + @Override + public void run() { + view.getThreadQueue().asyncExec(new Runnable() { + + @Override + public void run() { + int count = 0; + double loc[] = new double[] {0,0,0}; + for (vtkProp prop : props) { + if (prop instanceof vtkProp3D) { + vtkProp3D prop3d = (vtkProp3D)prop; + double l[] = prop3d.GetCenter(); + for (int i = 0; i < 3; i++) + loc[i] = loc[i] + l[i]; + count++; + } + } + if (count > 0) { + for (int i = 0; i < 3; i++) + loc[i] = loc[i] / count; + cameraAndSelectorAction.focus(loc[0],loc[1],loc[2]); + view.refresh(); + } + + } + }); + } +} \ No newline at end of file diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java index 5775035a..44e65292 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java @@ -1,5 +1,6 @@ package org.simantics.plant3d.editor; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -29,9 +30,11 @@ import org.simantics.g3d.vtk.action.RemoveAction; import org.simantics.g3d.vtk.common.HoverHighlighter; import org.simantics.g3d.vtk.common.NodeSelectionProvider2; import org.simantics.g3d.vtk.common.SelectionHighlighter; +import org.simantics.g3d.vtk.common.VTKSelectionItem; import org.simantics.g3d.vtk.common.VtkView; import org.simantics.g3d.vtk.shape.vtkShape; import org.simantics.g3d.vtk.swt.ContextMenuListener; +import org.simantics.g3d.vtk.swt.FocusAction; import org.simantics.g3d.vtk.swt.InteractiveVtkComposite; import org.simantics.g3d.vtk.swt.RotateAction; import org.simantics.g3d.vtk.swt.TranslateAction; @@ -73,6 +76,7 @@ import vtk.vtkActor; import vtk.vtkCameraPass; import vtk.vtkDefaultPass; import vtk.vtkLightsPass; +import vtk.vtkProp; import vtk.vtkRenderPassCollection; import vtk.vtkRenderer; import vtk.vtkSequencePass; @@ -91,7 +95,9 @@ public class Plant3DEditor extends ResourceEditorPart { protected NodeSelectionProvider2 selectionProvider; + protected vtkCameraAndSelectorAction cameraAction; + protected FocusAction focusAction; protected TranslateAction translateAction; protected TranslateInlineAction translateInlineAction; protected TranslateFreeVariableLengthAction translateFreeVariableLengthAction; @@ -235,6 +241,7 @@ public class Plant3DEditor extends ResourceEditorPart { } protected void createActions() { + focusAction = new FocusAction(panel, cameraAction); translateAction = new TranslateAction(panel,nodeMap); translateInlineAction = new TranslateInlineAction(panel, nodeMap); translateFreeVariableLengthAction = new TranslateFreeVariableLengthAction(panel, getRootNode()); @@ -358,6 +365,19 @@ public class Plant3DEditor extends ResourceEditorPart { protected void createContextMenu(IMenuManager m) { List selected = selectionProvider.getSelectedNodes(); + List> selectedItems = selectionProvider.getSelectedItems(); + if (selectedItems.size() > 0) { + List props = new ArrayList<>(); + for (VTKSelectionItem i : selectedItems) { + vtkProp p = (vtkProp)i.getAdapter(vtkProp.class); + if (p != null) + props.add(p); + } + if (props.size() > 0) { + focusAction.setProps(props); + m.add(focusAction); + } + } try { if (selected.size() == 0) { for (Item eq : P3DUtil.getEquipments(getLibraryUri())) {