]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
2cef7060c14ede3b748ddb821146d26d8bc6ecdb
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / ComponentUtils.java
1 package org.simantics.plant3d.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.stream.Collectors;
9
10 import javax.vecmath.Vector3d;
11
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.RequestProcessor;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.common.utils.NameUtils;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.g3d.math.MathTools;
20 import org.simantics.g3d.scenegraph.GeometryProvider;
21 import org.simantics.g3d.scenegraph.ParametricGeometryProvider;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.plant3d.geometry.ParameterRead;
24 import org.simantics.plant3d.ontology.Plant3D;
25 import org.simantics.plant3d.scenegraph.EndComponent;
26 import org.simantics.plant3d.scenegraph.Equipment;
27 import org.simantics.plant3d.scenegraph.InlineComponent;
28 import org.simantics.plant3d.scenegraph.Nozzle;
29 import org.simantics.plant3d.scenegraph.P3DRootNode;
30 import org.simantics.plant3d.scenegraph.PipeRun;
31 import org.simantics.plant3d.scenegraph.PipelineComponent;
32 import org.simantics.plant3d.scenegraph.TurnComponent;
33 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
34 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
35 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
39
40 public class ComponentUtils {
41
42         private final static Logger LOGGER = LoggerFactory.getLogger(ComponentUtils.class);
43         
44         private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
45         private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
46         private static Map<String,String> names = new HashMap<String,String>();
47         
48         public static void preloadCache(RequestProcessor session) {
49                 try {
50                         session.syncRequest(new ReadRequest() {
51                                 
52                                 @Override
53                                 public void run(ReadGraph graph) throws DatabaseException {
54                                         List<String> types = new ArrayList<String>();
55                                         types.add(Plant3D.URIs.Builtin_Straight);
56                                         types.add(Plant3D.URIs.Builtin_Elbow);
57                                         types.add(Plant3D.URIs.Builtin_ConcentricReducer);
58                                         types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
59                                         types.add(Plant3D.URIs.Builtin_EccentricReducer);
60                                         types.add(Plant3D.URIs.Builtin_Elbow45);
61                                         types.add(Plant3D.URIs.Builtin_Elbow90);
62                                         
63                                         for (String typeURI : types) {
64                                                 load(graph, typeURI);
65                                         }
66                                 }
67                         });
68                 } catch (DatabaseException e) {
69                         LOGGER.error("ComponentUtils.preloadCache() failed unexpectedly", e);
70                 }
71         }
72         
73         private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
74                 
75                 Layer0 l0 = Layer0.getInstance(graph);
76                 Plant3D p3d = Plant3D.getInstance(graph);
77                 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
78                 if (geom == null) {
79                         for (Resource a : graph.getObjects(type, l0.Asserts)) {
80                                 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
81                                         geom = graph.getPossibleObject(a, l0.HasObject);
82                                         break;
83                                 }
84                         }
85                 }
86                 if (geom != null) {
87                         GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
88                         if (provider instanceof ParametricGeometryProvider) {
89                         Map<String,Object> params = graph.syncRequest(new ParameterRead(type));
90                         if (params.size() > 0)
91                             ((ParametricGeometryProvider)provider).setProperties(params);
92                         }
93                         return provider;
94                 }
95                 return null;
96         }
97         
98         private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
99                 Plant3D p3d = Plant3D.getInstance(graph);
100                 if (graph.isInheritedFrom(type, p3d.InlineComponent))
101                         return InlineComponent.class;
102                 if (graph.isInheritedFrom(type, p3d.TurnComponent))
103                         return TurnComponent.class;
104                 if (graph.isInheritedFrom(type, p3d.EndComponent))
105                         return EndComponent.class;
106                 if (graph.isInheritedFrom(type, p3d.Nozzle))
107                         return Nozzle.class;
108                 return null;
109         }
110         
111         private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
112                 Plant3D p3d = Plant3D.getInstance(graph);
113                 Resource type = graph.getResource(typeURI);
114                 
115                 GeometryProvider provider = getProvider(graph, type);
116                 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
117                         providers.put(typeURI, provider);
118                         if (graph.isInheritedFrom(type, p3d.PipelineComponent))
119                                 clazzes.put(typeURI,getClazz(graph, type));
120                         names.put(typeURI, NameUtils.getSafeName(graph, type));
121                         return;
122                 }
123                 throw new DatabaseException("Cannot find component for " + typeURI);
124         }
125         
126         private static void load(final String typeURI) throws DatabaseException {
127                 Simantics.getSession().syncRequest(new ReadRequest() {
128                         
129                         @Override
130                         public void run(ReadGraph graph) throws DatabaseException {
131                                 load(graph,typeURI);    
132                         }
133                 });
134         }
135         
136         /**
137          * Creates a component
138          * 
139          * Does not set the name or add the component to a piperun.
140          * @param root
141          * @param typeURI
142          * @return
143          * @throws Exception
144          */
145         public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
146                 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
147                 GeometryProvider provider = providers.get(typeURI);
148                 if (type == null || provider == null) {
149                         load(typeURI);
150                         type = clazzes.get(typeURI);
151                         provider = providers.get(typeURI);
152                 }
153                 //PipelineComponent component = type.newInstance();
154                 PipelineComponent component = null;
155                 if (type == InlineComponent.class) {
156                         component = root.createInline();
157                 } else if (type == TurnComponent.class) {
158                         component = root.createTurn();
159                 } else if (type == EndComponent.class) {
160                         component = root.createTurn();
161                 } else if (type == Nozzle.class) {
162                         component = root.createNozzle();
163                 }
164                 component.setType(typeURI);
165                 component.setGeometry(provider);
166                 return component;
167         }
168         
169         /**
170          * Creates a equipment
171          * 
172          * Does not set the name
173          * 
174          * @param root
175          * @param typeURI
176          * @return
177          * @throws Exception
178          */
179         
180         public static Equipment createEquipment(P3DRootNode root, String typeURI) throws Exception {
181                 GeometryProvider provider = providers.get(typeURI);
182                 if (provider == null) {
183                         load(typeURI);
184                         provider = providers.get(typeURI);
185                 }
186                 Equipment equipment = root.createEquipment();
187                 equipment.setType(typeURI);
188                 equipment.setGeometry(provider);
189                 root.addChild(equipment);
190                 return equipment;
191         }
192         
193         public static Equipment createEquipmentWithNozzles(P3DRootNode root, String typeURI, String nozzleTypeUri) throws Exception {
194         GeometryProvider provider = providers.get(typeURI);
195         if (provider == null) {
196             load(typeURI);
197             provider = providers.get(typeURI);
198         }
199         Equipment equipment = root.createEquipment();
200         equipment.setType(typeURI);
201         equipment.setGeometry(provider);
202         root.addChild(equipment);
203         
204         for (int i = 0; i < equipment.numberOfFixedNozzles(); i++) {
205             createNozzle(root, equipment, new Item(nozzleTypeUri, "Nozzle"));
206             
207         }
208         
209         return equipment;
210     }
211         
212         public static InlineComponent createStraight(P3DRootNode root) throws Exception{
213                 InlineComponent component = root.createInline();
214                 component.setType(Plant3D.URIs.Builtin_Straight);
215                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
216                 return component;
217         }
218         
219         public static TurnComponent createTurn(P3DRootNode root) throws Exception {
220                 TurnComponent elbow = root.createTurn();
221                 elbow.setType(Plant3D.URIs.Builtin_Elbow);
222                 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
223                 return elbow;
224         }
225         
226         public static InlineComponent createReducer(P3DRootNode root) throws Exception {
227                 InlineComponent component = root.createInline();
228                 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
229                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
230                 return component;
231         }
232         
233         public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
234                 InlineComponent component = root.createInline();
235                 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
236                 return component;
237         }
238         
239         public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
240                 Equipment equipment = createEquipment(root, equipmentType.getUri());
241                 String n = root.getUniqueName(equipmentType.getName());
242                 equipment.setName(n);
243                 return equipment;
244         }
245         
246         public static Equipment createEquipmentWithNozzles(P3DRootNode root, Item equipmentType, Item nozzleType) throws Exception {
247         Equipment equipment = createEquipmentWithNozzles(root, equipmentType.getUri(), nozzleType.getUri());
248         String n = root.getUniqueName(equipmentType.getName());
249         equipment.setName(n);
250         return equipment;
251     }
252         
253         public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
254                 return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
255         }
256         
257         public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
258                 Nozzle nozzle = root.createNozzle();
259                 nozzle.setType(nozzleType.getUri());
260                 String n = root.getUniqueName(nozzleType.getName());
261                 nozzle.setName(n);
262                 PipeRun pipeRun = new PipeRun();
263                 n = root.getUniqueName("PipeRun");
264                 pipeRun.setName(n);
265                 nozzle.setPipeRun(pipeRun);
266                 
267                 equipment.addChild(nozzle);
268                 root.addChild(pipeRun);
269                 // root.getNodeMap().commit("Add nozzle " + n);
270                 return nozzle;
271         }
272         
273         public static class InsertInstruction {
274                 public String typeUri;
275                 
276                 public PositionType position = PositionType.NEXT;
277                 public PositionType insertPosition = PositionType.NEXT;
278
279                 // Component name
280                 public String name;
281                 
282                 // Reducer requires pipe specs
283                 public Double diameter;
284                 public Double turnRadius;
285                 
286                 // Variable length 
287                 public Double length;
288                 
289                 // Variable angle
290                 public Double angle;
291                 
292                 // Rotation angle used with turns and rotated inline.
293                 public Double rotationAngle;
294
295                 public String getTypeUri() {
296                         return typeUri;
297                 }
298
299                 public void setTypeUri(String typeUri) {
300                         this.typeUri = typeUri;
301                 }
302
303                 public PositionType getPosition() {
304                         return position;
305                 }
306
307                 public void setPosition(PositionType position) {
308                         this.position = position;
309                 }
310
311                 public PositionType getInsertPosition() {
312                         return insertPosition;
313                 }
314
315                 public void setInsertPosition(PositionType insertPosition) {
316                         this.insertPosition = insertPosition;
317                 }
318
319                 public String getName() {
320                         return name;
321                 }
322
323                 public void setName(String name) {
324                         this.name = name;
325                 }
326
327                 public Double getDiameter() {
328                         return diameter;
329                 }
330
331                 public void setDiameter(Double diameter) {
332                         this.diameter = diameter;
333                 }
334
335                 public Double getTurnRadius() {
336                         return turnRadius;
337                 }
338
339                 public void setTurnRadius(Double turnRadius) {
340                         this.turnRadius = turnRadius;
341                 }
342
343                 public Double getLength() {
344                         return length;
345                 }
346
347                 public void setLength(Double length) {
348                         this.length = length;
349                 }
350
351                 public Double getAngle() {
352                         return angle;
353                 }
354
355                 public void setAngle(Double angle) {
356                         this.angle = angle;
357                 }
358                 
359                 public Double getRotationAngle() {
360             return rotationAngle;
361         }
362                 
363                 public void setRotationAngle(Double rotationAngle) {
364             this.rotationAngle = rotationAngle;
365         }
366
367         }
368         
369         public static PipelineComponent addComponent(P3DRootNode root, PipelineComponent component,  InsertInstruction inst) throws Exception {
370                 
371                 PipelineComponent newComponent = ComponentUtils.createComponent(root, inst.typeUri);
372                 if (inst.name != null)
373                         newComponent.setName(inst.name);
374                 
375                 PipeControlPoint newPcp = newComponent.getControlPoint();
376                 
377                 PipeControlPoint toPcp = component.getControlPoint();
378                 PipeRun pipeRun = toPcp.getPipeRun();
379                 
380                 String typeName = names.get(inst.typeUri);
381                 if (typeName == null)
382                         typeName = "Component";
383                 
384                 Vector3d dir = null;
385                 Vector3d pos = null;
386         
387                 PositionType position = inst.position;
388                 PositionType insertPosition = inst.insertPosition;
389                 boolean lengthAdjustable = false;
390                 if (newComponent instanceof InlineComponent) {
391                         lengthAdjustable = ((InlineComponent)newComponent).isVariableLength() || ((InlineComponent)newComponent).isModifialble(); 
392                 }
393                 boolean insertAdjustable = false;
394                 if (component instanceof InlineComponent) {
395                         insertAdjustable = ((InlineComponent)component).isVariableLength();
396                 }
397                 boolean sizeChange = false;
398                 if (newComponent instanceof InlineComponent) {
399                         sizeChange = ((InlineComponent)newComponent).isSizeChange();
400                 }
401                 
402                 if (toPcp.isInline()) {
403                         switch (position) {
404                         case NEXT: 
405                                 if (toPcp.isDualInline()) {
406                                         toPcp = toPcp.getDualSub();
407                                         pipeRun = toPcp.getPipeRun();
408                                 }
409                                 
410                                 break;
411                         case PREVIOUS:
412                                 if (toPcp.isDualSub()) {
413                                         toPcp = toPcp.parent;
414                                         pipeRun = toPcp.getPipeRun();
415                                 }
416                                 break;
417                         default:
418                                 break;
419                         }
420                         Vector3d start = new Vector3d();
421                         Vector3d end = new Vector3d();
422                         dir = new Vector3d();
423                         toPcp.getInlineControlPointEnds(start, end, dir);
424                         dir.normalize();
425                         switch (position) {
426                         case NEXT:
427                                 pos = new Vector3d(end);
428                                 break;
429                         case PREVIOUS:
430                                 pos = new Vector3d(start);
431                                 break;
432                         case SPLIT:
433                                 pos = new Vector3d(toPcp.getWorldPosition());
434                                 break;
435                         default:
436                                 break;
437                         }
438
439                 } else if (toPcp.isDirected()) {
440                         dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
441                         pos = new Vector3d(toPcp.getWorldPosition());
442                 } else if (toPcp.isTurn() && toPcp.asFixedAngle()) {
443                         dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
444                         pos = new Vector3d(toPcp.getWorldPosition());
445                         if (!lengthAdjustable) {
446                                 Vector3d v = new Vector3d(dir);
447                                 v.scale(toPcp.getInlineLength());
448                                 pos.add(v);
449                         } else {
450                                 if (insertPosition == PositionType.NEXT) {
451                                         Vector3d v = new Vector3d(dir);
452                                         v.scale(toPcp.getInlineLength());
453                                         pos.add(v);
454                                 } else if (insertPosition == PositionType.SPLIT) {
455                                         // scale 0.5*length so that we don't remove the length twice from the new component
456                                         Vector3d v = new Vector3d(dir);
457                                         v.scale(toPcp.getInlineLength()*0.5);  
458                                         pos.add(v);
459                                 }
460                         }
461                 }
462                 
463                 String name = component.getPipeRun().getUniqueName(typeName);
464                 newComponent.setName(name);
465
466                 pipeRun.addChild(newComponent);
467                 if (newPcp.isSizeChange())
468                         newComponent.setAlternativePipeRun(pipeRun);
469
470                 if (newComponent instanceof InlineComponent) {
471                     InlineComponent inlineComponent = (InlineComponent)newComponent;
472                     if (inlineComponent.isVariableLength()|| inlineComponent.isModifialble()) {
473                         newPcp.setLength(inst.length);
474                         newComponent.setParameter("length", inst.length);
475                     }
476                     if (inst.rotationAngle != null)
477                         ((InlineComponent) newComponent).setRotationAngle(inst.rotationAngle);
478                 } else if (newComponent instanceof TurnComponent) {
479                     TurnComponent turnComponent = (TurnComponent)newComponent;
480                     if  (turnComponent.isVariableAngle()) {
481                                 newPcp.setTurnAngle(inst.angle);
482                                 newComponent.setParameter("turnAngle", inst.angle);
483                     }
484                     if (inst.rotationAngle != null)
485                 ((TurnComponent) newComponent).setRotationAngle(inst.rotationAngle);
486                 }
487                 
488                 
489                 newComponent.updateParameters();
490                 
491                 Vector3d v = new Vector3d(dir);
492                 if (insertAdjustable) {
493                         if (insertPosition == PositionType.NEXT)
494                                 v.scale(newComponent.getControlPoint().getInlineLength());
495                         else if (insertPosition == PositionType.SPLIT)
496                                 v.set(0, 0, 0);
497                         else if (insertPosition == PositionType.PREVIOUS)
498                                 v.scale(-newComponent.getControlPoint().getInlineLength());
499                 } else {
500                         v.scale(newComponent.getControlPoint().getInlineLength());
501                 }
502                 switch (position) {
503                 case NEXT:
504                         pos.add(v);
505                         break;
506                 case PREVIOUS:
507                         pos.sub(v);
508                         break;
509                 case SPLIT:
510                         break;
511                 default:
512                         break;
513                 }
514                 
515                 switch (position) {
516                 case NEXT: 
517                         if (toPcp.isDualInline())
518                                 toPcp = toPcp.getDualSub();
519                         newPcp.insert(toPcp, Direction.NEXT);
520                         newPcp.setWorldPosition(pos);
521                         break;
522                 case PREVIOUS:
523                         if (toPcp.isDualSub())
524                                 toPcp = toPcp.parent;
525                         newPcp.insert(toPcp, Direction.PREVIOUS);
526                         newPcp.setWorldPosition(pos);
527                         break;
528                 case SPLIT:
529                         PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
530                 default:
531                         break;
532                 }
533                 
534                 // Move the size change and the rest of the components in the pipe run to a new pipe run
535                 if (sizeChange) {
536                         PipeRun other = new PipeRun();
537                         String n = root.getUniqueName("PipeRun");
538                         other.setName(n);
539                         other.setPipeDiameter(inst.diameter);
540                         other.setTurnRadius(inst.turnRadius);
541                         root.addChild(other);
542                         
543                         other.addChild(newComponent.getControlPoint().getDualSub());
544                         newComponent.setAlternativePipeRun(other);
545                         
546                         boolean forward = position != PositionType.PREVIOUS;
547                         PipelineComponent comp = forward ? newComponent.getNext() : newComponent.getPrevious();
548                         while (comp != null && comp.getPipeRun() == pipeRun) {
549                                 if (comp.getParent() == pipeRun) {
550                                         comp.deattach();
551                                         other.addChild(comp);
552                                 } else {
553                                         comp.setPipeRun(other);
554                                 }
555                                 
556                                 // Reset parameters to match new pipe run
557                                 comp.updateParameters();
558                                 
559                                 comp = forward ? comp.getNext() : comp.getPrevious();
560                         }
561                         
562                         newComponent.updateParameters();
563                 }
564                 
565                 return newComponent;
566         }
567         
568         public static boolean connect(PipelineComponent current, PipelineComponent endTo) throws Exception {
569                 return connect(current, endTo, null, null);
570         }
571         
572         /**
573          * Connects component to another component
574          * @param current
575          * @param endTo
576          * @param endType
577          * @param position
578          * @return
579          * @throws Exception
580          */
581         public static boolean connect(PipelineComponent current, PipelineComponent endTo, PositionType endType, Vector3d position) throws Exception{
582                 PipeControlPoint endCP = endTo.getControlPoint();
583                 boolean reversed;
584                 if (current.getNext() == null)
585                         reversed = false;
586                 else if (current.getPrevious() == null)
587                         reversed = true;
588                 else
589                         return false;
590                 
591                 PipeRun pipeRun = current.getPipeRun();
592                 P3DRootNode root = (P3DRootNode)current.getRootNode();
593                 PipeControlPoint currentCP = current.getControlPoint();
594                 
595                 if (endType == null || endType == PositionType.NEXT || endType == PositionType.PREVIOUS) {
596                         
597                         
598                         
599                         boolean requiresReverse = false;
600                         if (!reversed && endCP.getPrevious() != null) {
601                                 if (endCP.getNext() != null)
602                                         return false;
603                                 requiresReverse = true;
604                         } else if (reversed && endCP.getNext() != null) {
605                                 if (endCP.getPrevious() != null)
606                                         return false;
607                                 requiresReverse = true;
608                         }
609                         PipeRun other = endCP.getPipeRun();
610                         boolean mergeRuns = other == null ? true : pipeRun.canMerge(other);
611                         
612                         if (requiresReverse) {
613                                 // Pipe line must be traversible with next/previous relations without direction change.
614                                 // Now the component, where we are connecting the created pipeline is defined in different order.
615                                 PipingRules.reverse(other);
616                                 
617                         }
618
619                         if (mergeRuns) {
620                                 // Runs have compatible specs and must be merged
621                                 if (other != null && pipeRun != other)
622                                         pipeRun.merge(other);
623                                 else if (other == null) {
624                                         if (!(endTo instanceof Nozzle)) {
625                                                 pipeRun.addChild(endTo);
626                                         } else {
627                                                 endTo.setPipeRun(pipeRun);
628                                         }
629                                 }
630                                 if (!reversed) {
631                                         currentCP.setNext(endCP);
632                                         endCP.setPrevious(currentCP);
633                                 } else {
634                                         currentCP.setPrevious(endCP);
635                                         endCP.setNext(currentCP);
636                                 }
637                         } else {
638                                 // Runs do not have compatible specs, and a reducer must be attached in between.
639                                 InlineComponent reducer = ComponentUtils.createReducer(root);
640                                 PipeControlPoint pcp = reducer.getControlPoint();
641                                 
642                                 Vector3d endPos = endCP.getWorldPosition();
643                                 Vector3d currentPos = currentCP.getWorldPosition();
644                                 Vector3d v = new Vector3d(endPos);
645                                 v.sub(currentPos);
646                                 v.scale(0.5);
647                                 v.add(currentPos);
648                                 
649                                 PipingRules.addSizeChange(reversed, pipeRun, other, reducer, currentCP, endCP);
650                                 
651                                 pcp.setWorldPosition(v);
652                                 reducer.updateParameters();
653                         }
654                         PipingRules.positionUpdate(endCP);
655                         return true;
656                         
657                 } else if (endType == PositionType.SPLIT) {
658                         InlineComponent branchSplit = createBranchSplit((InlineComponent)endTo, position);
659                         if (branchSplit == null)
660                                 return false;
661                         PipeControlPoint branchSplitCP = branchSplit.getControlPoint();
662                         PipeControlPoint pcp = new PipeControlPoint(branchSplit,pipeRun);
663                         branchSplitCP.children.add(pcp);
664                         pcp.parent = branchSplitCP;
665                         pcp.setWorldOrientation(branchSplitCP.getWorldOrientation());
666                         pcp.setWorldPosition(branchSplitCP.getWorldPosition());
667                                                 
668                         
669                         if(!reversed) {
670                                 pcp.setPrevious(currentCP);
671                                 currentCP.setNext(pcp);
672                         } else {
673                                 pcp.setNext(currentCP);
674                                 currentCP.setPrevious(pcp);
675                         }
676                         PipingRules.positionUpdate(endCP);
677                         return true;
678                 }
679                 return false;
680         }
681         
682         public static InlineComponent createBranchSplit(InlineComponent component, Vector3d pos) throws Exception{
683                 if (!component.isVariableLength())
684                         return null;
685                 PipeRun pipeRun = component.getPipeRun();
686                 Vector3d sStart = new Vector3d();
687                 Vector3d sEnd = new Vector3d();
688                 component.getControlPoint().getInlineControlPointEnds(sStart, sEnd);
689                 
690                 if (MathTools.distance(sStart, sEnd) < (pipeRun.getPipeDiameter()*0.5))
691                         return null;
692                 
693                 
694                 Vector3d p = MathTools.closestPointOnEdge(new Vector3d(pos), sStart, sEnd);
695                 if (p == sStart) {
696                         Vector3d v = new Vector3d(sEnd);
697                         v.sub(sStart);
698                         v.normalize();
699                         v.scale(component.getPipeRun().getPipeDiameter()*0.5);
700                         p.add(v);
701                 } else if (p == sEnd) {
702                         Vector3d v = new Vector3d(sStart);
703                         v.sub(sEnd);
704                         v.normalize();
705                         v.scale(component.getPipeRun().getPipeDiameter()*0.5);
706                         p.add(v);
707                 }
708                 
709                 P3DRootNode root = (P3DRootNode)component.getRootNode();
710                 InlineComponent branchSplit = ComponentUtils.createBranchSplit(root);
711                 String branchName = component.getPipeRun().getUniqueName("Branch");
712                 branchSplit.setName(branchName);
713                 component.getPipeRun().addChild(branchSplit);
714                 PipeControlPoint branchSplitCP = branchSplit.getControlPoint();
715                 branchSplitCP.setWorldPosition(p);
716                 PipingRules.splitVariableLengthComponent(branchSplit, component, false);
717                 return branchSplit;
718         }
719
720         public static Collection<String> getPipelineComponentNames(P3DRootNode root) {
721                 Collection<String> usedNames = root.getChild().stream()
722                                 .filter(n -> n instanceof PipeRun)
723                                 .flatMap(n -> ((PipeRun)n).getChild().stream())
724                                 .filter(n -> n instanceof PipelineComponent)
725                                 .map(n -> ((PipelineComponent)n).getName())
726                                 .collect(Collectors.toSet());
727                 return usedNames;
728         }
729 }