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.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
27 import org.simantics.plant3d.utils.ComponentUtils;
28 import org.simantics.plant3d.utils.Item;
29 import org.simantics.plant3d.utils.Item.Type;
30 import org.simantics.utils.threads.ThreadUtils;
31 import org.simantics.utils.ui.ExceptionUtils;
35 public class AddComponentAction extends vtkSwtAction {
38 private P3DRootNode root;
39 private PipelineComponent component;
40 private NodeMap<vtkProp,INode> nodeMap;
42 private TerminalSelectionGizmo gizmo;
44 private Set<PositionType> allowed = new HashSet<PositionType>();
46 private Item toAdd = null;
48 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
51 setText("Add Component");
52 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
53 nodeMap = root.getNodeMap();
54 gizmo = new TerminalSelectionGizmo(panel);
57 public void setComponent(PipelineComponent component) {
58 this.component = component;
61 if (component.getNext() == null) {
62 allowed.add(PositionType.NEXT);
64 if (component.getPrevious() == null) {
65 allowed.add(PositionType.PREVIOUS);
67 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
68 allowed.add(PositionType.SPLIT);
70 setEnabled(allowed.size() > 0);
73 private Double length;
75 private Double diameter;
76 private Double turnRadius;
80 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed);
81 if (dialog.open() == ComponentSelectionDialog.CANCEL)
83 toAdd = dialog.getSelected();
86 this.length = dialog.getLength();
87 this.angle = dialog.getAngle();
88 this.diameter = dialog.getDiameter();
89 this.turnRadius = dialog.getTurnRadius();
90 allowed = dialog.filterAllowed();
91 gizmo.setComponent(component, allowed);
97 public boolean keyPressed(KeyEvent e) {
98 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
99 panel.useDefaultAction();
104 public void attach() {
105 if (component == null)
109 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
117 public void deattach() {
126 private void attachUI() {
127 //panel.setCursor(activeCursor);
131 private void deattachUI() {
132 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
137 public boolean mouseMoved(MouseEvent e) {
138 return getDefaultAction().mouseMoved(e);
142 public boolean mousePressed(MouseEvent e) {
143 return getDefaultAction().mousePressed(e);
147 public boolean mouseReleased(MouseEvent e) {
148 return getDefaultAction().mouseReleased(e);
152 public boolean mouseDragged(MouseEvent e) {
153 return getDefaultAction().mouseDragged(e);
156 public void doInsert(PositionType position) {
158 PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
159 PipeControlPoint newPcp = newComponent.getControlPoint();
161 PipeControlPoint toPcp = component.getControlPoint();
162 PipeRun pipeRun = toPcp.getPipeRun();
168 if (toPcp.isInline()) {
171 if (toPcp.isDualInline())
172 toPcp = toPcp.getSubPoint().get(0);
176 if (toPcp.isDualSub())
177 toPcp = toPcp.parent;
179 Vector3d start = new Vector3d();
180 Vector3d end = new Vector3d();
181 dir = new Vector3d();
182 toPcp.getInlineControlPointEnds(start, end, dir);
186 pos = new Vector3d(end);
189 pos = new Vector3d(start);
195 } else if (toPcp.isDirected()) {
196 dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
197 pos = new Vector3d(toPcp.getWorldPosition());
198 } else if (toPcp.isTurn() && toPcp.isFixed()) {
199 dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
200 pos = new Vector3d(toPcp.getWorldPosition());
201 Vector3d v = new Vector3d(dir);
202 v.scale(toPcp.getInlineLength());
207 if (!toAdd.isSizeChange()) {
208 String name = component.getPipeRun().getUniqueName(toAdd.getName());
209 newComponent.setName(name);
211 pipeRun.addChild(newComponent);
212 if (toAdd.isVariable()) {
213 // TODO: these options are not stored into DB. Should they?!
214 if (toAdd.getType() == Type.INLINE) {
215 newPcp.setLength(length);
216 // newPcp.setFixed(true);
217 } else if (toAdd.getType() == Type.TURN) {
218 newPcp.setTurnAngle(angle);
219 // newPcp.setFixed(true);
222 newComponent.updateParameters();
224 Vector3d v = new Vector3d(dir);
225 v.scale(newComponent.getControlPoint().getInlineLength());
239 if (toPcp.isDualInline())
240 toPcp = toPcp.getSubPoint().get(0);
241 newPcp.insert(toPcp, Direction.NEXT);
242 newPcp.setWorldPosition(pos);
245 if (toPcp.isDualSub())
246 toPcp = toPcp.parent;
247 newPcp.insert(toPcp, Direction.PREVIOUS);
248 newPcp.setWorldPosition(pos);
251 PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
254 PipeRun other = new PipeRun();
255 String n = root.getUniqueName("PipeRun");
257 other.setPipeDiameter(diameter);
258 other.setTurnRadius(turnRadius);
259 root.addChild(other);
262 if (position == PositionType.NEXT) {
263 PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
264 } else if (position == PositionType.PREVIOUS){
265 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
267 newPcp.setWorldPosition(pos);
268 // TODO : chicken-egg problem
269 newComponent.updateParameters();
270 Vector3d v = new Vector3d(dir);
271 v.scale(newComponent.getControlPoint().getLength()*0.5);
282 newPcp.setWorldPosition(pos);
287 } catch (Exception e) {
288 ExceptionUtils.logAndShowError("Cannot add component", e);
292 public boolean mouseClicked(MouseEvent e) {
293 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
294 int type = panel.getPickType();
295 //panel.setPickType(0);
296 panel.setPickType(5);
297 vtkProp[] picked = panel.pick(e.getX(), e.getY());
298 panel.setPickType(type);
299 PositionType position = gizmo.getPickedPosition(picked);
301 if (position != null) {
303 panel.useDefaultAction();
307 return getDefaultAction().mouseClicked(e);
311 public boolean mouseWheelMoved(MouseWheelEvent e) {
312 return getDefaultAction().mouseWheelMoved(e);