]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
Merge "Support for fixed turn components"
[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.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;
32
33 import vtk.vtkProp;
34
35 public class AddComponentAction extends vtkSwtAction {
36         
37
38         private P3DRootNode root;
39         private PipelineComponent component;
40         private NodeMap<vtkProp,INode> nodeMap;
41         
42         private TerminalSelectionGizmo gizmo;
43         
44         private Set<PositionType> allowed = new HashSet<PositionType>();
45         
46         private Item toAdd = null;
47         
48         public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
49                 super(panel);
50                 this.root = root;
51                 setText("Add Component");
52                 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
53                 nodeMap = root.getNodeMap();
54                 gizmo = new TerminalSelectionGizmo(panel);
55         }
56         
57         public void setComponent(PipelineComponent component) {
58                 this.component = component;
59                 
60                 allowed.clear();
61                 if (component.getNext() == null) {
62                         allowed.add(PositionType.NEXT);
63                 }
64                 if (component.getPrevious() == null) {
65                         allowed.add(PositionType.PREVIOUS);
66                 }
67                 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
68                         allowed.add(PositionType.SPLIT);
69                 }
70                 setEnabled(allowed.size() > 0);
71         }
72         
73         private Double length;
74         private Double angle;
75         private Double diameter;
76         private Double turnRadius;
77         
78         @Override
79         public void run() {
80                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed);
81                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
82                         return;
83                 toAdd = dialog.getSelected();
84                 if (toAdd == null)
85                         return;
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);
92                 super.run();
93                 panel.refresh();
94         }
95         
96         @Override
97         public boolean keyPressed(KeyEvent e) {
98                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
99                         panel.useDefaultAction();
100                 return true;
101                 
102         }
103         
104         public void attach() {
105                 if (component == null)
106                         return;
107                 
108                 super.attach();
109                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
110                         public void run() {
111                                 attachUI();
112                         }
113                 });
114                 
115         }
116         
117         public void deattach() {
118 //              deactivate();
119                 component = null;
120                 nodeMap.commit();
121                 deattachUI();
122                 super.deattach();
123                 panel.refresh();
124         }
125         
126         private void attachUI() {
127                 //panel.setCursor(activeCursor);
128                 gizmo.attach(panel);
129         }
130         
131         private void deattachUI() {
132                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
133                 gizmo.deattach();
134         }
135         
136         @Override
137         public boolean mouseMoved(MouseEvent e) {
138                 return getDefaultAction().mouseMoved(e);
139         }
140         
141         @Override
142         public boolean mousePressed(MouseEvent e) {
143                 return getDefaultAction().mousePressed(e);
144         }
145
146         @Override
147         public boolean mouseReleased(MouseEvent e) {
148                 return getDefaultAction().mouseReleased(e);
149         }
150         
151         @Override
152         public boolean mouseDragged(MouseEvent e) {
153                 return getDefaultAction().mouseDragged(e);
154         }
155         
156         public void doInsert(PositionType position) {
157                 try  {
158                         PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
159                         PipeControlPoint newPcp = newComponent.getControlPoint();
160                         
161                         PipeControlPoint toPcp = component.getControlPoint();
162                         PipeRun pipeRun = toPcp.getPipeRun();
163                         
164                         Vector3d dir = null;
165                         Vector3d pos = null;
166                 
167                         
168                         if (toPcp.isInline()) {
169                             switch (position) {
170                     case NEXT: 
171                         if (toPcp.isDualInline())
172                             toPcp = toPcp.getSubPoint().get(0);
173                         
174                         break;
175                     case PREVIOUS:
176                         if (toPcp.isDualSub())
177                             toPcp = toPcp.parent;
178                     }
179                             Vector3d start = new Vector3d();
180                             Vector3d end = new Vector3d();
181                     dir = new Vector3d();
182                     toPcp.getInlineControlPointEnds(start, end, dir);
183                     dir.normalize();
184                     switch (position) {
185                 case NEXT:
186                     pos = new Vector3d(end);
187                     break;
188                 case PREVIOUS:
189                     pos = new Vector3d(start);
190                     break;
191                 case SPLIT:
192                     break;
193                 }
194                    
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());
203                 pos.add(v);
204                         }
205                         
206                         
207                         if (!toAdd.isSizeChange()) {
208                                 String name = component.getPipeRun().getUniqueName(toAdd.getName());
209                                 newComponent.setName(name);
210
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);
220                                         }
221                                 }
222                                 newComponent.updateParameters();
223                                 
224                             Vector3d v = new Vector3d(dir);
225                 v.scale(newComponent.getControlPoint().getInlineLength());
226                 switch (position) {
227                 case NEXT:
228                     pos.add(v);
229                     break;
230                 case PREVIOUS:
231                     pos.sub(v);
232                     break;
233                 case SPLIT:
234                     break;
235                 }
236                                 
237                                 switch (position) {
238                                 case NEXT: 
239                                         if (toPcp.isDualInline())
240                                                 toPcp = toPcp.getSubPoint().get(0);
241                                         newPcp.insert(toPcp, Direction.NEXT);
242                                         newPcp.setWorldPosition(pos);
243                                         break;
244                                 case PREVIOUS:
245                                         if (toPcp.isDualSub())
246                                                 toPcp = toPcp.parent;
247                                         newPcp.insert(toPcp, Direction.PREVIOUS);
248                                         newPcp.setWorldPosition(pos);
249                                         break;
250                                 case SPLIT:
251                                         PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
252                                 }
253                         } else {
254                                 PipeRun other = new PipeRun();
255                                 String n = root.getUniqueName("PipeRun");
256                                 other.setName(n);
257                                 other.setPipeDiameter(diameter);
258                                 other.setTurnRadius(turnRadius);
259                                 root.addChild(other);
260                                 
261                                 
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);
266                         }
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);
272                 switch (position) {
273                 case NEXT:
274                     pos.add(v);
275                     break;
276                 case PREVIOUS:
277                     pos.sub(v);
278                     break;
279                 case SPLIT:
280                     break;
281                 }
282                 newPcp.setWorldPosition(pos);
283                                 
284                         }
285                         
286                         
287                 } catch (Exception e) {
288                         ExceptionUtils.logAndShowError("Cannot add component", e);
289                 }
290         }
291         
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);
300
301                         if (position != null) {
302                                 doInsert(position);
303                                 panel.useDefaultAction();
304                                 return true;
305                         }
306                 } 
307                 return getDefaultAction().mouseClicked(e);
308         }
309         
310         @Override
311         public boolean mouseWheelMoved(MouseWheelEvent e) {
312             return getDefaultAction().mouseWheelMoved(e);
313         }
314 }