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