1 package org.simantics.plant3d.actions;
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseWheelEvent;
6 import java.util.HashSet;
9 import javax.vecmath.Vector3d;
11 import org.eclipse.swt.widgets.Display;
12 import org.simantics.g3d.scenegraph.NodeMap;
13 import org.simantics.g3d.scenegraph.base.INode;
14 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
15 import org.simantics.g3d.vtk.swt.vtkSwtAction;
16 import org.simantics.plant3d.Activator;
17 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
18 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
19 import org.simantics.plant3d.scenegraph.InlineComponent;
20 import org.simantics.plant3d.scenegraph.Nozzle;
21 import org.simantics.plant3d.scenegraph.P3DRootNode;
22 import org.simantics.plant3d.scenegraph.PipeRun;
23 import org.simantics.plant3d.scenegraph.PipelineComponent;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
28 import org.simantics.plant3d.utils.ComponentUtils;
29 import org.simantics.plant3d.utils.Item;
30 import org.simantics.plant3d.utils.Item.Type;
31 import org.simantics.utils.threads.ThreadUtils;
32 import org.simantics.utils.ui.ExceptionUtils;
36 public class AddComponentAction extends vtkSwtAction {
39 private P3DRootNode root;
40 private PipelineComponent component;
41 private NodeMap<vtkProp,INode> nodeMap;
43 private TerminalSelectionGizmo gizmo;
45 private Set<PositionType> allowed = new HashSet<PositionType>();
47 private Item toAdd = null;
48 private PositionType insertPosition;
49 private boolean insertAdjustable;
50 private boolean lengthAdjustable;
52 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
55 setText("Add Component");
56 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
57 nodeMap = root.getNodeMap();
58 gizmo = new TerminalSelectionGizmo(panel);
61 public void setComponent(PipelineComponent component) {
62 this.component = component;
65 if (component instanceof Nozzle) {
66 if (component.getNext() == null && component.getPrevious() == null) {
67 allowed.add(PositionType.NEXT);
70 if (component.getNext() == null) {
71 allowed.add(PositionType.NEXT);
73 if (component.getPrevious() == null) {
74 allowed.add(PositionType.PREVIOUS);
76 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
77 allowed.add(PositionType.SPLIT);
80 setEnabled(allowed.size() > 0);
83 private Double length;
85 private Double diameter;
86 private Double turnRadius;
91 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component);
92 if (dialog.open() == ComponentSelectionDialog.CANCEL)
94 toAdd = dialog.getSelected();
97 this.insertPosition = dialog.getInsertPosition();
98 this.insertAdjustable = dialog.isInsertAdjustable();
99 this.lengthAdjustable = dialog.isLenghtAdjustable();
100 this.length = dialog.getLength();
101 this.angle = dialog.getAngle();
102 this.diameter = dialog.getDiameter();
103 this.turnRadius = dialog.getTurnRadius();
104 allowed = dialog.filterAllowed();
105 gizmo.setComponent(component, allowed);
111 public boolean keyPressed(KeyEvent e) {
112 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
113 panel.useDefaultAction();
118 public void attach() {
119 if (component == null)
123 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
131 public void deattach() {
134 nodeMap.commit("Add component");
140 private void attachUI() {
141 //panel.setCursor(activeCursor);
145 private void deattachUI() {
146 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
151 public boolean mouseMoved(MouseEvent e) {
152 return getDefaultAction().mouseMoved(e);
156 public boolean mousePressed(MouseEvent e) {
157 return getDefaultAction().mousePressed(e);
161 public boolean mouseReleased(MouseEvent e) {
162 return getDefaultAction().mouseReleased(e);
166 public boolean mouseDragged(MouseEvent e) {
167 return getDefaultAction().mouseDragged(e);
170 public void doInsert(PositionType position) {
172 PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
173 PipeControlPoint newPcp = newComponent.getControlPoint();
175 PipeControlPoint toPcp = component.getControlPoint();
176 PipeRun pipeRun = toPcp.getPipeRun();
182 if (toPcp.isInline()) {
185 if (toPcp.isDualInline())
186 toPcp = toPcp.getSubPoint().get(0);
190 if (toPcp.isDualSub())
191 toPcp = toPcp.parent;
193 Vector3d start = new Vector3d();
194 Vector3d end = new Vector3d();
195 dir = new Vector3d();
196 toPcp.getInlineControlPointEnds(start, end, dir);
200 pos = new Vector3d(end);
203 pos = new Vector3d(start);
206 pos = new Vector3d(toPcp.getWorldPosition());
210 } else if (toPcp.isDirected()) {
211 dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
212 pos = new Vector3d(toPcp.getWorldPosition());
213 } else if (toPcp.isTurn() && toPcp.isFixed()) {
214 dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
215 pos = new Vector3d(toPcp.getWorldPosition());
216 if (!lengthAdjustable) {
217 Vector3d v = new Vector3d(dir);
218 v.scale(toPcp.getInlineLength());
221 if (insertPosition == PositionType.NEXT) {
222 Vector3d v = new Vector3d(dir);
223 v.scale(toPcp.getInlineLength());
225 } else if (insertPosition == PositionType.SPLIT) {
226 // scale 0.5*length so that we don't remove the length twice from the new component
227 Vector3d v = new Vector3d(dir);
228 v.scale(toPcp.getInlineLength()*0.5);
235 if (!toAdd.isSizeChange()) {
236 String name = component.getPipeRun().getUniqueName(toAdd.getName());
237 newComponent.setName(name);
239 pipeRun.addChild(newComponent);
240 if (toAdd.isVariable()) {
241 // TODO: these options are not stored into DB. Should they?!
242 if (toAdd.getType() == Type.INLINE) {
243 newPcp.setLength(length);
244 // newPcp.setFixed(true);
245 } else if (toAdd.getType() == Type.TURN) {
246 newPcp.setTurnAngle(angle);
247 // newPcp.setFixed(true);
250 newComponent.updateParameters();
252 Vector3d v = new Vector3d(dir);
253 if (insertAdjustable) {
254 if (insertPosition == PositionType.NEXT)
255 v.scale(newComponent.getControlPoint().getInlineLength());
256 else if (insertPosition == PositionType.SPLIT)
258 else if (insertPosition == PositionType.PREVIOUS)
259 v.scale(-newComponent.getControlPoint().getInlineLength());
261 v.scale(newComponent.getControlPoint().getInlineLength());
276 if (toPcp.isDualInline())
277 toPcp = toPcp.getSubPoint().get(0);
278 newPcp.insert(toPcp, Direction.NEXT);
279 newPcp.setWorldPosition(pos);
282 if (toPcp.isDualSub())
283 toPcp = toPcp.parent;
284 newPcp.insert(toPcp, Direction.PREVIOUS);
285 newPcp.setWorldPosition(pos);
288 PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
291 PipeRun other = new PipeRun();
292 String n = root.getUniqueName("PipeRun");
294 other.setPipeDiameter(diameter);
295 other.setTurnRadius(turnRadius);
296 root.addChild(other);
299 if (position == PositionType.NEXT) {
300 PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
301 } else if (position == PositionType.PREVIOUS){
302 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
304 newPcp.setWorldPosition(pos);
305 // TODO : chicken-egg problem
306 newComponent.updateParameters();
307 Vector3d v = new Vector3d(dir);
308 v.scale(newComponent.getControlPoint().getLength()*0.5);
319 newPcp.setWorldPosition(pos);
324 } catch (Exception e) {
325 ExceptionUtils.logAndShowError("Cannot add component", e);
329 public boolean mouseClicked(MouseEvent e) {
330 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
331 int type = panel.getPickType();
332 //panel.setPickType(0);
333 panel.setPickType(5);
334 vtkProp[] picked = panel.pick(e.getX(), e.getY());
335 panel.setPickType(type);
336 PositionType position = gizmo.getPickedPosition(picked);
338 if (position != null) {
340 panel.useDefaultAction();
344 return getDefaultAction().mouseClicked(e);
348 public boolean mouseWheelMoved(MouseWheelEvent e) {
349 return getDefaultAction().mouseWheelMoved(e);