-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpCmd.py
1184 lines (1135 loc) · 43 KB
/
pCmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#(c) 2019 R. T. LGPL: part of dodo tools w.b. for FreeCAD
__title__="pypeTools functions"
import FreeCAD, FreeCADGui, Part, fCmd, pFeatures
from DraftVecUtils import rounded
objToPaint=['Pipe','Elbow','Reduct','Flange','Cap']
from math import degrees
__author__="oddtopus"
__url__="github.com/oddtopus/dodo"
__license__="LGPL 3"
X=FreeCAD.Vector(1,0,0)
Y=FreeCAD.Vector(0,1,0)
Z=FreeCAD.Vector(0,0,1)
############### AUX FUNCTIONS ######################
def readTable(fileName="Pipe_SCH-STD.csv"):
'''
readTable(fileName)
Returns the list of dictionaries read from file in ./tablez
fileName: the file name without path; default="Pipe_SCH-STD.csv"
'''
from os.path import join, dirname, abspath
import csv
f=open(join(dirname(abspath(__file__)),"tablez",fileName),'r')
reader=csv.DictReader(f,delimiter=';')
table=[]
for row in reader:
table.append(row)
f.close()
return table
def shapeReferenceAxis(obj=None, axObj=None):
# function to get the reference axis of the shape for rotateTheTubeAx()
# used in rotateTheTubeEdge() and pipeForms.rotateForm().getAxis()
'''
shapeReferenceAxis(obj, axObj)
Returns the direction of an axis axObj
according the original Shape orientation of the object obj
If arguments are None axObj is the normal to one circular edge selected
and obj is the object selected.
'''
if obj==None and axObj==None:
selex=FreeCADGui.Selection.getSelectionEx()
if len(selex)==1 and len(selex[0].SubObjects)>0:
sub=selex[0].SubObjects[0]
if sub.ShapeType=='Edge' and sub.curvatureAt(0)>0:
axObj=sub.tangentAt(0).cross(sub.normalAt(0))
obj=selex[0].Object
X=obj.Placement.Rotation.multVec(FreeCAD.Vector(1,0,0)).dot(axObj)
Y=obj.Placement.Rotation.multVec(FreeCAD.Vector(0,1,0)).dot(axObj)
Z=obj.Placement.Rotation.multVec(FreeCAD.Vector(0,0,1)).dot(axObj)
axShapeRef=FreeCAD.Vector(X,Y,Z)
return axShapeRef
def isPipe(obj):
'True if obj is a tube'
return hasattr(obj,'PType') and obj.PType=='Pipe'
def isElbow(obj):
'True if obj is an elbow'
return hasattr(obj,'PType') and obj.PType=='Elbow'
def moveToPyLi(obj,plName):
'''
Move obj to the group of pypeLine plName
'''
pl=FreeCAD.ActiveDocument.getObjectsByLabel(plName)[0]
group=FreeCAD.ActiveDocument.getObjectsByLabel(str(pl.Group))[0]
group.addObject(obj)
if hasattr(obj,'PType'):
if obj.PType in objToPaint:
obj.ViewObject.ShapeColor=pl.ViewObject.ShapeColor
elif obj.PType == 'PypeBranch':
for e in [FreeCAD.ActiveDocument.getObject(name) for name in obj.Tubes+obj.Curves]:
e.ViewObject.ShapeColor=pl.ViewObject.ShapeColor
def portsPos(o):
'''
portsPos(o)
Returns the position of Ports of the pype-object o
'''
if hasattr(o,'Ports') and hasattr(o,'Placement'): return [rounded(o.Placement.multVec(p)) for p in o.Ports]
def portsDir(o):
'''
portsDir(o)
Returns the orientation of Ports of the pype-object o
'''
dirs=list()
two_ways=['Pipe','Reduct','Flange']
if hasattr(o,'PType'):
if o.PType in two_ways:
dirs=[o.Placement.Rotation.multVec(p) for p in [FreeCAD.Vector(0,0,-1),FreeCAD.Vector(0,0,1)]]
elif hasattr(o,'Ports') and hasattr(o,'Placement'):
dirs=list()
for p in o.Ports:
if p.Length:
dirs.append(rounded(o.Placement.Rotation.multVec(p).normalize()))
else:
dirs.append(rounded(o.Placement.Rotation.multVec(FreeCAD.Vector(0,0,-1)).normalize()))
return dirs
################## COMMANDS ########################
def simpleSurfBend(path=None,profile=None):
'select the centerline and the O.D. and let it sweep'
curva=FreeCAD.activeDocument().addObject("Part::Feature","CurvaSemplice")
if path==None or profile==None:
curva.Shape=Part.makeSweepSurface(*fCmd.edges()[:2])
elif path.ShapeType==profile.ShapeType=='Edge':
curva.Shape=Part.makeSweepSurface(path,profile)
def makePipe(propList=[], pos=None, Z=None):
'''add a Pipe object
makePipe(propList,pos,Z);
propList is one optional list with 4 elements:
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
H (float): length of pipe
Default is "DN50 (SCH-STD)"
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Tubo")
if len(propList)==4:
pFeatures.Pipe(a,*propList)
else:
pFeatures.Pipe(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def doPipes(propList=['DN50',60.3,3,1000], pypeline=None):
'''
propList = [
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
H (float): length of pipe ]
pypeline = string
'''
FreeCAD.activeDocument().openTransaction('Insert pipe')
plist=list()
if len(fCmd.edges())==0: #..no edges selected
vs=[v for sx in FreeCADGui.Selection.getSelectionEx() for so in sx.SubObjects for v in so.Vertexes]
if len(vs)==0: # ...no vertexes selected
plist.append(makePipe(propList))
else: # ... one or more vertexes
for v in vs: plist.append(makePipe(propList,v.Point))
else:
selex=FreeCADGui.Selection.getSelectionEx()
for objex in selex:
o=objex.Object
if fCmd.faces(): # Face selected...
for face in fCmd.faces():
x=(face.ParameterRange[0]+face.ParameterRange[1])/2
y=(face.ParameterRange[2]+face.ParameterRange[3])/2
plist.append(makePipe(propList,face.valueAt(x,y),face.normalAt(x,y)))
FreeCAD.activeDocument().commitTransaction()
FreeCAD.activeDocument().recompute()
return
for edge in fCmd.edges([objex]): # ...one or more edges...
if edge.curvatureAt(0)==0: # ...straight edges
pL=propList
pL[3]=edge.Length
plist.append(makePipe(pL,edge.valueAt(0),edge.tangentAt(0)))
else: # ...curved edges
pos=edge.centerOfCurvatureAt(0)
Z=edge.tangentAt(0).cross(edge.normalAt(0))
if isElbow(o):
p0,p1=[o.Placement.Rotation.multVec(p) for p in o.Ports]
if not fCmd.isParallel(Z,p0):
Z=p1
plist.append(makePipe(propList,pos,Z))
if pypeline:
for p in plist:
moveToPyLi(p,pypeline)
FreeCAD.activeDocument().commitTransaction()
FreeCAD.activeDocument().recompute()
return plist
def makeElbow(propList=[], pos=None, Z=None):
'''Adds an Elbow object
makeElbow(propList,pos,Z);
propList is one optional list with 5 elements:
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
BA (float): bend angle
BR (float): bend radius
Default is "DN50"
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Curva")
if len(propList)==5:
pFeatures.Elbow(a,*propList)
else:
pFeatures.Elbow(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
#rot=FreeCAD.Rotation(FreeCAD.Vector(0,-1,0),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def makeElbowBetweenThings(thing1=None, thing2=None, propList=None):
'''
makeElbowBetweenThings(thing1, thing2, propList=None):
Place one elbow at the intersection of thing1 and thing2.
Things can be any combination of intersecting beams, pipes or edges.
If nothing is passed as argument, the function attempts to take the
first two edges selected in the view.
propList is one optional list with 5 elements:
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
BA (float): bend angle - that will be recalculated! -
BR (float): bend radius
Default is "DN50 (SCH-STD)"
Remember: property PRating must be defined afterwards
'''
if not (thing1 and thing2):
thing1,thing2=fCmd.edges()[:2]
P=fCmd.intersectionCLines(thing1,thing2)
directions=list()
try:
for thing in [thing1,thing2]:
if fCmd.beams([thing]):
directions.append(rounded((fCmd.beamAx(thing).multiply(thing.Height/2)+thing.Placement.Base)-P))
elif hasattr(thing,'ShapeType') and thing.ShapeType=='Edge':
directions.append(rounded(thing.CenterOfMass-P))
except:
return None
ang=180-degrees(directions[0].getAngle(directions[1]))
if ang==0 or ang==180: return None
if not propList:
propList=["DN50",60.3,3.91,ang,45.24]
else:
propList[3]=ang
elb=makeElbow(propList,P,directions[0].negative().cross(directions[1].negative()))
# mate the elbow ends with the pipes or edges
b=fCmd.bisect(directions[0],directions[1])
elbBisect=rounded(fCmd.beamAx(elb,FreeCAD.Vector(1,1,0))) #if not rounded, fail in plane xz
rot=FreeCAD.Rotation(elbBisect,b)
elb.Placement.Rotation=rot.multiply(elb.Placement.Rotation)
# trim the adjacent tubes
#FreeCAD.activeDocument().recompute() # may delete this row?
portA=elb.Placement.multVec(elb.Ports[0])
portB=elb.Placement.multVec(elb.Ports[1])
for tube in [t for t in [thing1,thing2] if fCmd.beams([t])]:
vectA=tube.Shape.Solids[0].CenterOfMass-portA
vectB=tube.Shape.Solids[0].CenterOfMass-portB
if fCmd.isParallel(vectA,fCmd.beamAx(tube)):
fCmd.extendTheBeam(tube,portA)
else:
fCmd.extendTheBeam(tube,portB)
return elb
def doElbow(propList=['DN50',60.3,3,90,45.225], pypeline=None):
'''
propList = [
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
BA (float): bend angle
BR (float): bend radius ]
pypeline = string
'''
elist=[]
FreeCAD.activeDocument().openTransaction('Insert elbow')
selex=FreeCADGui.Selection.getSelectionEx()
if len(selex)==0: # no selection -> insert one elbow at origin
elist.append(makeElbow(propList))
elif len(selex)==1 and len(selex[0].SubObjects)==1: #one selection -> ...
if selex[0].SubObjects[0].ShapeType=="Vertex": # ...on vertex
elist.append(makeElbow(propList,selex[0].SubObjects[0].Point))
elif selex[0].SubObjects[0].ShapeType=="Edge" and selex[0].SubObjects[0].curvatureAt(0)!=0: # ...on center of curved edge
P=selex[0].SubObjects[0].centerOfCurvatureAt(0)
N=selex[0].SubObjects[0].normalAt(0).cross(selex[0].SubObjects[0].tangentAt(0)).normalize()
elb=makeElbow(propList,P)
if isPipe(selex[0].Object): #..on the edge of a pipe
ax=selex[0].Object.Shape.Solids[0].CenterOfMass-P
rot=FreeCAD.Rotation(elb.Ports[0],ax)
elb.Placement.Rotation=rot.multiply(elb.Placement.Rotation)
Port0=getElbowPort(elb)
elb.Placement.move(P-Port0)
elif isElbow(selex[0].Object): #..on the edge of an elbow
p0,p1=[selex[0].Object.Placement.Rotation.multVec(p) for p in selex[0].Object.Ports]
if fCmd.isParallel(p0,N):
elb.Placement.Rotation=FreeCAD.Rotation(elb.Ports[0],p0*-1)
else:
elb.Placement.Rotation=FreeCAD.Rotation(elb.Ports[0],p1*-1)
elb.Placement.move(P-getElbowPort(elb))
else: #..on any other curved edge
print('hello')
rot=FreeCAD.Rotation(elb.Ports[0],N)
elb.Placement.Rotation=rot.multiply(elb.Placement.Rotation)
# elb.Placement.move(elb.Placement.Rotation.multVec(elb.Ports[0])*-1)
v=portsDir(elb)[0].negative()*elb.Ports[0].Length
elb.Placement.move(v)
elist.append(elb)
FreeCAD.activeDocument().recompute()
else: # multiple selection -> insert one elbow at intersection of two edges or beams or pipes ##
things=[]
for objEx in selex:
if len(fCmd.beams([objEx.Object]))==1: # if the object is a beam or pipe, append it to the "things"..
things.append(objEx.Object)
else: # ..else append its edges
for edge in fCmd.edges([objEx]):
things.append(edge)
if len(things)>=2:
break
try: #create the feature
elb=elist.append(makeElbowBetweenThings(*things[:2],propList=propList))
except:
FreeCAD.Console.PrintError('Creation of elbow is failed\n')
if pypeline:
for e in elist:
moveToPyLi(e,pypeline)
FreeCAD.activeDocument().commitTransaction()
FreeCAD.activeDocument().recompute()
return elist
def makeFlange(propList=[], pos=None, Z=None):
'''Adds a Flange object
makeFlange(propList,pos,Z);
propList is one optional list with 8 elements:
DN (string): nominal diameter
FlangeType (string): type of Flange
D (float): flange diameter
d (float): bore diameter
df (float): bolts holes distance
f (float): bolts holes diameter
t (float): flange thickness
n (int): nr. of bolts
trf (float): raised-face thikness - OPTIONAL -
drf (float): raised-face diameter - OPTIONAL -
twn (float): welding-neck thikness - OPTIONAL -
dwn (float): welding-neck diameter - OPTIONAL -
ODp (float): outside diameter of pipe for wn flanges - OPTIONAL -
Default is "DN50 (PN16)"
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Flangia")
if len(propList)>=8:
pFeatures.Flange(a,*propList)
else:
pFeatures.Flange(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def doFlanges(propList=["DN50", "SO", 160, 60.3, 132, 14, 15, 4, 0, 0, 0, 0, 0], pypeline=None):
'''
propList = [
DN (string): nominal diameter
FlangeType (string): type of Flange
D (float): flange diameter
d (float): bore diameter
df (float): bolts holes distance
f (float): bolts holes diameter
t (float): flange thickness
n (int): nr. of bolts
trf (float): raised-face thikness - OPTIONAL -
drf (float): raised-face diameter - OPTIONAL -
twn (float): welding-neck thikness - OPTIONAL -
dwn (float): welding-neck diameter - OPTIONAL -
ODp (float): outside diameter of pipe for wn flanges - OPTIONAL -
pypeline = string
'''
flist=[]
tubes=[t for t in fCmd.beams() if hasattr(t,'PSize')]
FreeCAD.activeDocument().openTransaction('Insert flange')
if len(fCmd.edges())==0:
vs=[v for sx in FreeCADGui.Selection.getSelectionEx() for so in sx.SubObjects for v in so.Vertexes]
if len(vs)==0:
flist.append(makeFlange(propList))
else:
for v in vs:
flist.append(makeFlange(propList,v.Point))
elif tubes:
selex=FreeCADGui.Selection.getSelectionEx()
for sx in selex:
if isPipe(sx.Object) and fCmd.edges([sx]):
for edge in fCmd.edges([sx]):
if edge.curvatureAt(0)!=0:
flist.append(makeFlange(propList,edge.centerOfCurvatureAt(0),sx.Object.Shape.Solids[0].CenterOfMass-edge.centerOfCurvatureAt(0)))
else:
for edge in fCmd.edges():
if edge.curvatureAt(0)!=0:
flist.append(makeFlange(propList,edge.centerOfCurvatureAt(0),edge.tangentAt(0).cross(edge.normalAt(0))))
if pypeline:
for f in flist:
moveToPyLi(f,pypeline)
FreeCAD.activeDocument().commitTransaction()
FreeCAD.activeDocument().recompute()
return flist
def makeReduct(propList=[], pos=None, Z=None, conc=True):
'''Adds a Reduct object
makeReduct(propList=[], pos=None, Z=None, conc=True)
propList is one optional list with 6 elements:
PSize (string): nominal diameter
OD (float): major diameter
OD2 (float): minor diameter
thk (float): major thickness
thk2 (float): minor thickness
H (float): length of reduction
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
conc (bool): True for concentric or Flase for eccentric reduction
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Riduz")
propList.append(conc)
pFeatures.Reduct(a,*propList)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def makeUbolt(propList=[], pos=None, Z=None):
'''Adds a Ubolt object:
makeUbolt(propList,pos,Z);
propList is one optional list with 5 elements:
PSize (string): nominal diameter
ClampType (string): the clamp type or standard
C (float): the diameter of the U-bolt
H (float): the total height of the U-bolt
d (float): the rod diameter
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","U-Bolt")
if len(propList)==5:
pFeatures.Ubolt(a,*propList)
else:
pFeatures.Ubolt(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def makeShell(L=1000,W=1500,H=1500,thk1=6,thk2=8):
'''
makeShell(L,W,H,thk1,thk2)
Adds the shell of a tank, given
L(ength): default=800
W(idth): default=400
H(eight): default=500
thk (thickness): default=6
'''
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Serbatoio")
pFeatures.Shell(a,L,W,H,thk1,thk2)
a.ViewObject.Proxy=0
a.Placement.Base=FreeCAD.Vector(0,0,0)
a.ViewObject.ShapeColor=0.0,0.0,1.0
a.ViewObject.Transparency=85
FreeCAD.ActiveDocument.recompute()
return a
def makeCap(propList=[], pos=None, Z=None):
'''add a Cap object
makeCap(propList,pos,Z);
propList is one optional list with 4 elements:
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness
Default is "DN50 (SCH-STD)"
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
Remember: property PRating must be defined afterwards
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Fondo")
if len(propList)==3:
pFeatures.Cap(a,*propList)
else:
pFeatures.Cap(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def doCaps(propList=['DN50',60.3,3], pypeline=None):
'''
propList = [
DN (string): nominal diameter
OD (float): outside diameter
thk (float): shell thickness ]
pypeline = string
'''
clist=[]
FreeCAD.activeDocument().openTransaction('Insert cap')
if len(fCmd.edges())==0:
vs=[v for sx in FreeCADGui.Selection.getSelectionEx() for so in sx.SubObjects for v in so.Vertexes]
if len(vs)==0: # nothing is selected
clist.append(makeCap(propList))
else:
for v in vs: # vertexes are selected
clist.append(makeCap(propList,v.Point))
else:
for edge in fCmd.edges():
if edge.curvatureAt(0)!=0: # curved edges are selected...
objs=[o for o in FreeCADGui.Selection.getSelection() if hasattr(o,'PSize') and hasattr(o,'OD') and hasattr(o,'thk')]
Z=None
if len(objs)>0: # ...pype objects are selected
Z=edge.centerOfCurvatureAt(0)-objs[0].Shape.Solids[0].CenterOfMass
else: # ...no pype objects are selected
Z=edge.tangentAt(0).cross(edge.normalAt(0))
clist.append(makeCap(propList,edge.centerOfCurvatureAt(0),Z))
if pypeline:
for c in clist:
moveToPyLi(c,pypeline)
FreeCAD.activeDocument().commitTransaction()
FreeCAD.activeDocument().recompute()
def makeW():
edges=fCmd.edges()
if len(edges)>1:
first=edges[0]
last=edges[-1]
points=list()
while len(edges)>1: points.append(fCmd.intersectionCLines(edges.pop(0),edges[0]))
delta1=(first.valueAt(0)-points[0]).Length
delta2=(first.valueAt(first.LastParameter)-points[0]).Length
if delta1>delta2:
points.insert(0,first.valueAt(0))
else:
points.insert(0,first.valueAt(first.LastParameter))
delta1=(last.valueAt(0)-points[0]).Length
delta2=(last.valueAt(last.LastParameter)-points[0]).Length
if delta1>delta2:
points.append(last.valueAt(0))
else:
points.append(last.valueAt(last.LastParameter))
from Draft import makeWire
try:
p=makeWire(points)
except:
FreeCAD.Console.PrintError('Missing intersection\n')
return None
p.Label='Path'
drawAsCenterLine(p)
return p
elif FreeCADGui.Selection.getSelection():
obj=FreeCADGui.Selection.getSelection()[0]
if hasattr(obj,'Shape') and type(obj.Shape)==Part.Wire:
drawAsCenterLine(obj)
return obj
else:
return None
def makePypeLine2(DN="DN50",PRating="SCH-STD",OD=60.3,thk=3,BR=None, lab="Tubatura", pl=None, color=(0.8,0.8,0.8)):
'''
makePypeLine2(DN="DN50",PRating="SCH-STD",OD=60.3,thk=3,BR=None, lab="Tubatura",pl=None, color=(0.8,0.8,0.8))
Adds a PypeLine2 object creating pipes over the selected edges.
Default tube is "DN50", "SCH-STD"
Bending Radius is set to 0.75*OD.
'''
if not BR:
BR=0.75*OD
# create the pypeLine group
if not pl:
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython",lab)
pFeatures.PypeLine2(a,DN,PRating,OD,thk,BR, lab)
pFeatures.ViewProviderPypeLine(a.ViewObject) # a.ViewObject.Proxy=0
a.ViewObject.ShapeColor=color
if len(FreeCADGui.Selection.getSelection())==1:
obj=FreeCADGui.Selection.getSelection()[0]
isWire=hasattr(obj,'Shape') and obj.Shape.Edges #type(obj.Shape)==Part.Wire
isSketch=hasattr(obj,'TypeId') and obj.TypeId=='Sketcher::SketchObject'
if isWire or isSketch:
a.Base=obj
a.Proxy.update(a)
if isWire:
drawAsCenterLine(obj)
elif fCmd.edges():
path=makeW()
a.Base=path
a.Proxy.update(a)
else:
a=FreeCAD.ActiveDocument.getObjectsByLabel(pl)[0]
group=FreeCAD.ActiveDocument.getObjectsByLabel(a.Group)[0]
a.Proxy.update(a,fCmd.edges())
FreeCAD.Console.PrintWarning("Objects added to pypeline's group "+a.Group+"\n")
return a
def makeBranch(base=None, DN="DN50",PRating="SCH-STD",OD=60.3,thk=3,BR=None, lab="Traccia", color=(0.8,0.8,0.8)):
'''
makeBranch(base=None, DN="DN50",PRating="SCH-STD",OD=60.3,thk=3,BR=None, lab="Traccia" color=(0.8,0.8,0.8))
Draft function for PypeBranch.
'''
if not BR:
BR=0.75*OD
if not base:
if FreeCADGui.Selection.getSelection():
obj=FreeCADGui.Selection.getSelection()[0]
isWire=hasattr(obj,'Shape') and type(obj.Shape)==Part.Wire
isSketch=hasattr(obj,'TypeId') and obj.TypeId=='Sketcher::SketchObject'
if isWire or isSketch:
base=obj
if isWire:
drawAsCenterLine(obj)
elif fCmd.edges():
base=makeW()
if base:
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython",lab)
pFeatures.PypeBranch2(a,base,DN,PRating,OD,thk,BR)
pFeatures.ViewProviderPypeBranch(a.ViewObject)
return a
else:
FreeCAD.Console.PrintError('Select a valid path.\n')
def updatePLColor(sel=None, color=None):
if not sel:
sel=FreeCADGui.Selection.getSelection()
if sel:
pl=sel[0]
if hasattr(pl,'PType') and pl.PType=='PypeLine':
if not color:
color=pl.ViewObject.ShapeColor
group=FreeCAD.activeDocument().getObjectsByLabel(pl.Group)[0]
for o in group.OutList:
if hasattr(o,'PType'):
if o.PType in objToPaint:
o.ViewObject.ShapeColor=color
elif o.PType == 'PypeBranch':
for e in [FreeCAD.ActiveDocument.getObject(name) for name in o.Tubes+o.Curves]:
e.ViewObject.ShapeColor=color
else:
FreeCAD.Console.PrintError('Select first one pype line\n')
def alignTheTube():
'''
Mates the selected 2 circular edges
of 2 separate objects.
'''
try:
t1=FreeCADGui.Selection.getSelection()[0]
t2=FreeCADGui.Selection.getSelection()[-1]
except:
FreeCAD.Console.PrintError("Select at least one object.\n")
return None
d1,d2=fCmd.edges()[:2]
if d1.curvatureAt(0)!=0 and d2.curvatureAt(0)!=0:
n1=d1.tangentAt(0).cross(d1.normalAt(0))
n2=d2.tangentAt(0).cross(d2.normalAt(0))
else:
FreeCAD.Console.PrintError("Select 2 curved edges.\n")
return None
rot=FreeCAD.Rotation(n2,n1)
t2.Placement.Rotation=rot.multiply(t2.Placement.Rotation)
#traslazione centri di curvatura
d1,d2=fCmd.edges() #redo selection to get new positions
dist=d1.centerOfCurvatureAt(0)-d2.centerOfCurvatureAt(0)
t2.Placement.move(dist)
#verifica posizione relativa
try:
com1,com2=[t.Shape.Solids[0].CenterOfMass for t in [t1,t2]]
if isElbow(t2):
pass
elif (com1-d1.centerOfCurvatureAt(0)).dot(com2-d1.centerOfCurvatureAt(0))>0:
reverseTheTube(FreeCADGui.Selection.getSelectionEx()[:2][1])
except:
pass
#TARGET [solved]: verify if t1 or t2 belong to App::Part and changes the Placement consequently
if fCmd.isPartOfPart(t1):
part=fCmd.isPartOfPart(t1)
t2.Placement=part.Placement.multiply(t2.Placement)
if fCmd.isPartOfPart(t2):
part=fCmd.isPartOfPart(t2)
t2.Placement=part.Placement.inverse().multiply(t2.Placement)
def rotateTheTubeAx(obj=None,vShapeRef=None, angle=45):
'''
rotateTheTubeAx(obj=None,vShapeRef=None,angle=45)
Rotates obj around the vShapeRef axis of its Shape by an angle.
obj: if not defined, the first in the selection set
vShapeRef: if not defined, the Z axis of the Shape
angle: default=45 deg
'''
if obj==None:
obj=FreeCADGui.Selection.getSelection()[0]
if vShapeRef==None:
vShapeRef=FreeCAD.Vector(0,0,1)
rot=FreeCAD.Rotation(fCmd.beamAx(obj,vShapeRef),angle)
obj.Placement.Rotation=rot.multiply(obj.Placement.Rotation)
def reverseTheTube(objEx):
'''
reverseTheTube(objEx)
Reverse the orientation of objEx spinning it 180 degrees around the x-axis
of its shape.
If an edge is selected, it's used as pivot.
'''
disp=None
selectedEdges=[e for e in objEx.SubObjects if e.ShapeType=='Edge']
if selectedEdges:
for edge in fCmd.edges([objEx]):
if edge.curvatureAt(0):
disp=edge.centerOfCurvatureAt(0)-objEx.Object.Placement.Base
break
elif fCmd.beams([objEx.Object]):
ax=fCmd.beamAx(objEx.Object)
disp=ax*((edge.CenterOfMass-objEx.Object.Placement.Base).dot(ax))
rotateTheTubeAx(objEx.Object,FreeCAD.Vector(1,0,0),180)
if disp:
objEx.Object.Placement.move(disp*2)
def rotateTheTubeEdge(ang=45):
if len(fCmd.edges())>0 and fCmd.edges()[0].curvatureAt(0)!=0:
originalPos=fCmd.edges()[0].centerOfCurvatureAt(0)
obj=FreeCADGui.Selection.getSelection()[0]
rotateTheTubeAx(vShapeRef=shapeReferenceAxis(),angle=ang)
newPos=fCmd.edges()[0].centerOfCurvatureAt(0)
obj.Placement.move(originalPos-newPos)
def placeTheElbow(c,v1=None,v2=None,P=None):
'''
placeTheElbow(c,v1,v2,P=None)
Puts the curve C between vectors v1 and v2.
If point P is given, translates it in there.
NOTE: v1 and v2 oriented in the same direction along the path!
'''
if not (v1 and v2):
v1,v2=[e.tangentAt(0) for e in fCmd.edges()]
try:
P=fCmd.intersectionCLines(*fCmd.edges())
except: pass
if hasattr(c,'PType') and hasattr(c,'BendAngle') and v1 and v2:
v1.normalize()
v2.normalize()
ortho=rounded(fCmd.ortho(v1,v2))
bisect=rounded(v2-v1)
ang=degrees(v1.getAngle(v2))
c.BendAngle=ang
rot1=FreeCAD.Rotation(rounded(fCmd.beamAx(c,FreeCAD.Vector(0,0,1))),ortho)
c.Placement.Rotation=rot1.multiply(c.Placement.Rotation)
rot2=FreeCAD.Rotation(rounded(fCmd.beamAx(c,FreeCAD.Vector(1,1,0))),bisect)
c.Placement.Rotation=rot2.multiply(c.Placement.Rotation)
if not P:
P=c.Placement.Base
c.Placement.Base=P
def placeoTherElbow(c,v1=None,v2=None,P=None):
'''
Like placeTheElbow() but with more math.
'''
if not (v1 and v2):
v1,v2=[e.tangentAt(0) for e in fCmd.edges()]
try:
P=fCmd.intersectionCLines(*fCmd.edges())
except: pass
if hasattr(c,'PType') and hasattr(c,'BendAngle') and v1 and v2:
v1.normalize()
v2.normalize()
ortho=rounded(fCmd.ortho(v1,v2))
bisect=rounded(v2-v1)
cBisect=rounded(c.Ports[1].normalize()+c.Ports[0].normalize()) # math
cZ=c.Ports[0].cross(c.Ports[1]) # more math
ang=degrees(v1.getAngle(v2))
c.BendAngle=ang
rot1=FreeCAD.Rotation(rounded(fCmd.beamAx(c,cZ)),ortho)
c.Placement.Rotation=rot1.multiply(c.Placement.Rotation)
rot2=FreeCAD.Rotation(rounded(fCmd.beamAx(c,cBisect)),bisect)
c.Placement.Rotation=rot2.multiply(c.Placement.Rotation)
if not P:
P=c.Placement.Base
c.Placement.Base=P
def placeThePype(pypeObject, port=0, target=None, targetPort=0):
'''
placeThePype(pypeObject, port=None, target=None, targetPort=0)
pypeObject: a FeaturePython with PType property
port: an optional port of pypeObject
Aligns pypeObject's Placement to the Port of another pype which is selected in the viewport.
The pype shall be selected to the circular edge nearest to the port concerned.
'''
pos=Z=FreeCAD.Vector()
if target and hasattr(target,'PType') and hasattr(target,'Ports'): # target is given
pos=portsPos(target)[targetPort]
Z=portsDir(target)[targetPort]
else: # find target
try:
selex=FreeCADGui.Selection.getSelectionEx()
target=selex[0].Object
so=selex[0].SubObjects[0]
except:
FreeCAD.Console.PrintError('No geometry selected\n')
return
if type(so)==Part.Vertex: pick=so.Point
else: pick=so.CenterOfMass
if hasattr(target,'PType') and hasattr(target,'Ports'): # ...selection is another pype-object
pos, Z = nearestPort(target, pick)[1:]
elif fCmd.edges([selex[0]]): # one or more edges selected...
edge=fCmd.edges([selex[0]])[0]
if edge.curvatureAt(0)!=0: # ...and the first is curve
pos=edge.centerOfCurvatureAt(0)
Z=edge.tangentAt(0).cross(edge.normalAt(0))
# now place pypeObject on target
pOport=pypeObject.Ports[port]
if pOport==FreeCAD.Vector():
pOport=pypeObject.Ports[port]
if pOport==FreeCAD.Vector(): pOport=FreeCAD.Vector(0,0,-1)
pypeObject.Placement=FreeCAD.Placement(pos+Z*pOport.Length,FreeCAD.Rotation(pOport*-1,Z))
def nearestPort (pypeObject,point):
try:
pos=portsPos(pypeObject)[0]; Z=portsDir(pypeObject)[0]
i=nearest=0
for p in portsPos(pypeObject)[1:] :
i+=1
if (p-point).Length<(pos-point).Length:
pos=p
Z=portsDir(pypeObject)[i]
nearest=i
return nearest, pos, Z
except:
return None
def extendTheTubes2intersection(pipe1=None,pipe2=None,both=True):
'''
Does what it says; also with beams.
If arguments are None, it picks the first 2 selected beams().
'''
if not (pipe1 and pipe2):
try:
pipe1,pipe2=fCmd.beams()[:2]
except:
FreeCAD.Console.PrintError('Insufficient arguments for extendTheTubes2intersection\n')
P=fCmd.intersectionCLines(pipe1,pipe2)
if P!=None:
fCmd.extendTheBeam(pipe1,P)
if both:
fCmd.extendTheBeam(pipe2,P)
def laydownTheTube(pipe=None, refFace=None, support=None):
'''
laydownTheTube(pipe=None, refFace=None, support=None)
Makes one pipe touch one face if the center-line is parallel to it.
If support is not None, support is moved towards pipe.
'''
if not(pipe and refFace): # without argument take from selection set
refFace=[f for f in fCmd.faces() if type(f.Surface)==Part.Plane][0]
pipe=[p for p in fCmd.beams() if hasattr(p,'OD')] [0]
try:
if type(refFace.Surface)==Part.Plane and fCmd.isOrtho(refFace,fCmd.beamAx(pipe)) and hasattr(pipe,'OD'):
dist=rounded(refFace.normalAt(0,0).multiply(refFace.normalAt(0,0).dot(pipe.Placement.Base-refFace.CenterOfMass)-float(pipe.OD)/2))
if support:
support.Placement.move(dist)
else:
pipe.Placement.move(dist.multiply(-1))
else:
FreeCAD.Console.PrintError('Face is not flat or not parallel to axis of pipe\n')
except:
FreeCAD.Console.PrintError('Wrong selection\n')
def breakTheTubes(point,pipes=[],gap=0):
'''
breakTheTube(point,pipes=[],gap=0)
Breaks the "pipes" at "point" leaving a "gap".
'''
pipes2nd=list()
if not pipes:
pipes=[p for p in fCmd.beams() if isPipe(p)]
if pipes:
for pipe in pipes:
if point<float(pipe.Height) and gap<(float(pipe.Height)-point):
propList=[pipe.PSize,float(pipe.OD),float(pipe.thk),float(pipe.Height)-point-gap]
pipe.Height=point
Z=fCmd.beamAx(pipe)
pos=pipe.Placement.Base+Z*(float(pipe.Height)+gap)
pipe2nd=makePipe(propList,pos,Z)
pipes2nd.append(pipe2nd)
#FreeCAD.activeDocument().recompute()
return pipes2nd
def drawAsCenterLine(obj):
try:
obj.ViewObject.LineWidth=4
obj.ViewObject.LineColor=1.0,0.3,0.0
obj.ViewObject.DrawStyle='Dashdot'
except:
FreeCAD.Console.PrintError('The object can not be center-lined\n')
def getElbowPort(elbow, portId=0):
'''
getElbowPort(elbow, portId=0)
Returns the position of the specified port of elbow.
'''
if isElbow(elbow):
return elbow.Placement.multVec(elbow.Ports[portId])
def rotateTheElbowPort(curve=None, port=0, ang=45):
'''
rotateTheElbowPort(curve=None, port=0, ang=45)
Rotates one curve aroud one of its circular edges.
'''
if curve==None:
try:
curve=FreeCADGui.Selection.getSelection()[0]
if not isElbow(curve):
FreeCAD.Console.PrintError('Please select an elbow.\n')
return
except:
FreeCAD.Console.PrintError('Please select something before.\n')
rotateTheTubeAx(curve,curve.Ports[port],ang)
def join(obj1,port1,obj2,port2):
'''
join(obj1,port1,obj2,port2)
\t obj1, obj2 = two "Pype" parts
\t port1, port2 = their respective ports to join
'''
if hasattr(obj1,'PType') and hasattr(obj2,'PType'):
if port1>len(obj1.Ports)-1 or port2>len(obj2.Ports)-1:
FreeCAD.Console.PrintError('Wrong port(s) number\n')
else:
v1=portsDir(obj1)[port1]
v2=portsDir(obj2)[port2]
rot=FreeCAD.Rotation(v2,v1.negative())
obj2.Placement.Rotation=rot.multiply(obj2.Placement.Rotation)
p1=portsPos(obj1)[port1]
p2=portsPos(obj2)[port2]
obj2.Placement.move(p1-p2)
else:
FreeCAD.Console.PrintError('Object(s) are not pypes\n')
def makeValve(propList=[], pos=None, Z=None):
'''add a Valve object
makeValve(propList,pos,Z);
propList is one optional list with at least 4 elements:
DN (string): nominal diameter
VType (string): type of valve
OD (float): outside diameter
ID (float): inside diameter
H (float): length of pipe
Kv (float): valve's flow factor (optional)
Default is "DN50 ball valve ('ball')"
pos (vector): position of insertion; default = 0,0,0
Z (vector): orientation: default = 0,0,1
'''
if pos==None:
pos=FreeCAD.Vector(0,0,0)
if Z==None:
Z=FreeCAD.Vector(0,0,1)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Valvola")
if len(propList):
pFeatures.Valve(a,*propList)
else:
pFeatures.Valve(a)
a.ViewObject.Proxy=0
a.Placement.Base=pos
rot=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),Z)
a.Placement.Rotation=rot.multiply(a.Placement.Rotation)
return a
def doValves(propList=["DN50", "ball", 72, 50, 40, 150],pypeline=None, pos=0):
'''
propList = [