]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
First version of fixed nozzle positions
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / AddComponentAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseWheelEvent;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import javax.vecmath.Vector3d;
10
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;
33
34 import vtk.vtkProp;
35
36 public class AddComponentAction extends vtkSwtAction {
37         
38
39         private P3DRootNode root;
40         private PipelineComponent component;
41         private NodeMap<vtkProp,INode> nodeMap;
42         
43         private TerminalSelectionGizmo gizmo;
44         
45         private Set<PositionType> allowed = new HashSet<PositionType>();
46         
47         private Item toAdd = null;
48         private PositionType insertPosition;
49         private boolean insertAdjustable;
50         private boolean lengthAdjustable;
51         
52         public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
53                 super(panel);
54                 this.root = root;
55                 setText("Add Component");
56                 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
57                 nodeMap = root.getNodeMap();
58                 gizmo = new TerminalSelectionGizmo(panel);
59         }
60         
61         public void setComponent(PipelineComponent component) {
62                 this.component = component;
63                 
64                 allowed.clear();
65                 if (component instanceof Nozzle) {
66                     if (component.getNext() == null && component.getPrevious() == null) {
67                     allowed.add(PositionType.NEXT);
68                 }  
69                 } else {
70                 if (component.getNext() == null) {
71                         allowed.add(PositionType.NEXT);
72                 }
73                 if (component.getPrevious() == null) {
74                         allowed.add(PositionType.PREVIOUS);
75                 }
76                 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
77                         allowed.add(PositionType.SPLIT);
78                 }
79                 }
80                 setEnabled(allowed.size() > 0);
81         }
82         
83         private Double length;
84         private Double angle;
85         private Double diameter;
86         private Double turnRadius;
87         
88         @Override
89         public void run() {
90             
91                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component);
92                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
93                         return;
94                 toAdd = dialog.getSelected();
95                 if (toAdd == null)
96                         return;
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);
106                 super.run();
107                 panel.refresh();
108         }
109         
110         @Override
111         public boolean keyPressed(KeyEvent e) {
112                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
113                         panel.useDefaultAction();
114                 return true;
115                 
116         }
117         
118         public void attach() {
119                 if (component == null)
120                         return;
121                 
122                 super.attach();
123                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
124                         public void run() {
125                                 attachUI();
126                         }
127                 });
128                 
129         }
130         
131         public void deattach() {
132 //              deactivate();
133                 component = null;
134                 nodeMap.commit("Add component");
135                 deattachUI();
136                 super.deattach();
137                 panel.refresh();
138         }
139         
140         private void attachUI() {
141                 //panel.setCursor(activeCursor);
142                 gizmo.attach(panel);
143         }
144         
145         private void deattachUI() {
146                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
147                 gizmo.deattach();
148         }
149         
150         @Override
151         public boolean mouseMoved(MouseEvent e) {
152                 return getDefaultAction().mouseMoved(e);
153         }
154         
155         @Override
156         public boolean mousePressed(MouseEvent e) {
157                 return getDefaultAction().mousePressed(e);
158         }
159
160         @Override
161         public boolean mouseReleased(MouseEvent e) {
162                 return getDefaultAction().mouseReleased(e);
163         }
164         
165         @Override
166         public boolean mouseDragged(MouseEvent e) {
167                 return getDefaultAction().mouseDragged(e);
168         }
169         
170         public void doInsert(PositionType position) {
171                 try  {
172                         PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
173                         PipeControlPoint newPcp = newComponent.getControlPoint();
174                         
175                         PipeControlPoint toPcp = component.getControlPoint();
176                         PipeRun pipeRun = toPcp.getPipeRun();
177                         
178                         Vector3d dir = null;
179                         Vector3d pos = null;
180                 
181                         
182                         if (toPcp.isInline()) {
183                             switch (position) {
184                     case NEXT: 
185                         if (toPcp.isDualInline())
186                             toPcp = toPcp.getSubPoint().get(0);
187                         
188                         break;
189                     case PREVIOUS:
190                         if (toPcp.isDualSub())
191                             toPcp = toPcp.parent;
192                     }
193                             Vector3d start = new Vector3d();
194                             Vector3d end = new Vector3d();
195                     dir = new Vector3d();
196                     toPcp.getInlineControlPointEnds(start, end, dir);
197                     dir.normalize();
198                     if (!insertAdjustable || insertPosition == PositionType.NEXT) {
199                     switch (position) {
200                     case NEXT:
201                         pos = new Vector3d(end);
202                         break;
203                     case PREVIOUS:
204                         pos = new Vector3d(start);
205                         break;
206                     case SPLIT:
207                         break;
208                     }
209                     } else if (insertPosition == PositionType.SPLIT) {
210                         pos = new Vector3d(toPcp.getWorldPosition());
211                     } else {
212                         switch (position) {
213                     case NEXT:
214                         pos = new Vector3d(start);
215                         break;
216                     case PREVIOUS:
217                         pos = new Vector3d(end);
218                         break;
219                     case SPLIT:
220                         break;
221                     }
222                     }
223                    
224                         } else if (toPcp.isDirected()) {
225                             dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
226                             pos = new Vector3d(toPcp.getWorldPosition());
227                         } else if (toPcp.isTurn() && toPcp.isFixed()) {
228                             dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
229                 pos = new Vector3d(toPcp.getWorldPosition());
230                 if (!lengthAdjustable) {
231                     Vector3d v = new Vector3d(dir);
232                     v.scale(toPcp.getInlineLength());
233                     pos.add(v);
234                 } else {
235                     if (insertPosition == PositionType.NEXT) {
236                         Vector3d v = new Vector3d(dir);
237                         v.scale(toPcp.getInlineLength());
238                         pos.add(v);
239                     } else if (insertPosition == PositionType.SPLIT) {
240                         // scale 0.5*length so that we don't remove the length twice from the new component
241                         Vector3d v = new Vector3d(dir);
242                         v.scale(toPcp.getInlineLength()*0.5);  
243                         pos.add(v);
244                     }
245                 }
246                         }
247                         
248                         
249                         if (!toAdd.isSizeChange()) {
250                                 String name = component.getPipeRun().getUniqueName(toAdd.getName());
251                                 newComponent.setName(name);
252
253                                 pipeRun.addChild(newComponent);
254                                 if (toAdd.isVariable()) {
255                                         // TODO: these options are not stored into DB. Should they?!
256                                         if (toAdd.getType() == Type.INLINE) {
257                                                 newPcp.setLength(length);
258 //                                              newPcp.setFixed(true);
259                                         } else if (toAdd.getType() == Type.TURN) {
260                                                 newPcp.setTurnAngle(angle);
261 //                                              newPcp.setFixed(true);
262                                         }
263                                 }
264                                 newComponent.updateParameters();
265                                 
266                             Vector3d v = new Vector3d(dir);
267                             if (insertAdjustable) {
268                             if (insertPosition == PositionType.NEXT)
269                                 v.scale(newComponent.getControlPoint().getInlineLength());
270                             else if (insertPosition == PositionType.SPLIT)
271                                 v.set(0, 0, 0);
272                             else if (insertPosition == PositionType.PREVIOUS)
273                                 v.scale(-newComponent.getControlPoint().getInlineLength());
274                             } else {
275                                 v.scale(newComponent.getControlPoint().getInlineLength());
276                             }
277                 switch (position) {
278                 case NEXT:
279                     pos.add(v);
280                     break;
281                 case PREVIOUS:
282                     pos.sub(v);
283                     break;
284                 case SPLIT:
285                     break;
286                 }
287                                 
288                                 switch (position) {
289                                 case NEXT: 
290                                         if (toPcp.isDualInline())
291                                                 toPcp = toPcp.getSubPoint().get(0);
292                                         newPcp.insert(toPcp, Direction.NEXT);
293                                         newPcp.setWorldPosition(pos);
294                                         break;
295                                 case PREVIOUS:
296                                         if (toPcp.isDualSub())
297                                                 toPcp = toPcp.parent;
298                                         newPcp.insert(toPcp, Direction.PREVIOUS);
299                                         newPcp.setWorldPosition(pos);
300                                         break;
301                                 case SPLIT:
302                                         PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
303                                 }
304                         } else {
305                                 PipeRun other = new PipeRun();
306                                 String n = root.getUniqueName("PipeRun");
307                                 other.setName(n);
308                                 other.setPipeDiameter(diameter);
309                                 other.setTurnRadius(turnRadius);
310                                 root.addChild(other);
311                                 
312                                 
313                                 if (position == PositionType.NEXT) {
314                                         PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
315                         } else if (position == PositionType.PREVIOUS){
316                                 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
317                         }
318                                 newPcp.setWorldPosition(pos);
319                                 // TODO : chicken-egg problem
320                                 newComponent.updateParameters();
321                             Vector3d v = new Vector3d(dir);
322                 v.scale(newComponent.getControlPoint().getLength()*0.5);
323                 switch (position) {
324                 case NEXT:
325                     pos.add(v);
326                     break;
327                 case PREVIOUS:
328                     pos.sub(v);
329                     break;
330                 case SPLIT:
331                     break;
332                 }
333                 newPcp.setWorldPosition(pos);
334                                 
335                         }
336                         
337                         
338                 } catch (Exception e) {
339                         ExceptionUtils.logAndShowError("Cannot add component", e);
340                 }
341         }
342         
343         public boolean mouseClicked(MouseEvent e) {
344                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
345                         int type = panel.getPickType();
346                         //panel.setPickType(0);
347                         panel.setPickType(5);
348                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
349                         panel.setPickType(type);
350                         PositionType position = gizmo.getPickedPosition(picked);
351
352                         if (position != null) {
353                                 doInsert(position);
354                                 panel.useDefaultAction();
355                                 return true;
356                         }
357                 } 
358                 return getDefaultAction().mouseClicked(e);
359         }
360         
361         @Override
362         public boolean mouseWheelMoved(MouseWheelEvent e) {
363             return getDefaultAction().mouseWheelMoved(e);
364         }
365 }