forked from URJCMakerGroup/MakerWorkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shp_clss.py
1609 lines (1402 loc) · 55.8 KB
/
shp_clss.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
# ----------------------------------------------------------------------------
# -- Obj3D class and children
# ----------------------------------------------------------------------------
# -- (c) Felipe Machado
# -- Area of Electronic Technology. Rey Juan Carlos University (urjc.es)
# -- January-2018
# ----------------------------------------------------------------------------
# --- LGPL Licence
# ----------------------------------------------------------------------------
import FreeCAD
import Part
import DraftVecUtils
import os
import sys
import math
import inspect
import logging
# directory this file is
filepath = os.getcwd()
import sys
# to get the components
# In FreeCAD can be added: Preferences->General->Macro->Macro path
sys.path.append(filepath)
import fcfun
import kcomp
from fcfun import V0, VX, VY, VZ, V0ROT
from fcfun import VXN, VYN, VZN
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class Obj3D(object):
""" This is the the basic class, that provides reference axes and
methods to get positions
It is the parent class of other classes, no instantiation of this class
These objects have their own coordinate axes:
axis_d: depth
axis_w: width
axis_h: height
They have an origin point pos_o (created in a child class)
and have different interesting points
d_o
w_o
h_o
and methods to get to them
pos_o_adjustment : FreeCAD.Vector
if not V0 indicates that shape has not been placed at pos_o, so the FreeCAD object
will need to be placed at pos_o_adjust
"""
def __init__(self, axis_d=None, axis_w=None, axis_h=None):
# the TopoShape has an origin, and distance vectors from it to
# the different points along the coordinate system
self.d_o = {} # along axis_d
self.w_o = {} # along axis_w
self.h_o = {} # along axis_h
if axis_h is not None:
axis_h = DraftVecUtils.scaleTo(axis_h, 1)
else:
self.h_o[0] = V0
self.pos_h = 0
axis_h = V0
self.axis_h = axis_h
if axis_d is not None:
axis_d = DraftVecUtils.scaleTo(axis_d, 1)
else:
self.d_o[0] = V0
self.pos_d = 0
axis_d = V0
self.axis_d = axis_d
if axis_w is not None:
axis_w = DraftVecUtils.scaleTo(axis_w, 1)
else:
self.w_o[0] = V0
self.pos_w = 0
axis_w = V0
self.axis_w = axis_w
self.pos_o_adjust = V0
def vec_d(self, d):
""" creates a vector along axis_d (depth) with the length of argument d
Returns a FreeCAD.Vector
Parameter:
----------
d : float
depth: length of the vector along axis_d
"""
# self.axis_d is normalized, so no need to use DraftVecUtils.scaleTo
vec_d = DraftVecUtils.scale(self.axis_d, d)
return vec_d
def vec_w(self, w):
""" creates a vector along axis_w (width) with the length of argument w
Returns a FreeCAD.Vector
Parameter:
----------
w : float
width: length of the vector along axis_w
"""
# self.axis_w is normalized, so no need to use DraftVecUtils.scaleTo
vec_w = DraftVecUtils.scale(self.axis_w, w)
return vec_w
def vec_h(self, h):
""" creates a vector along axis_h (height) with the length of argument h
Returns a FreeCAD.Vector
Parameter:
----------
h : float
height: length of the vector along axis_h
"""
# self.axis_h is normalized, so no need to use DraftVecUtils.scaleTo
vec_h = DraftVecUtils.scale(self.axis_h, h)
return vec_h
def vec_d_w_h(self, d, w, h):
""" creates a vector with:
depth : along axis_d
width : along axis_w
height : along axis_h
Returns a FreeCAD.Vector
Parameters:
----------
d, w, h : float
depth, width and height
"""
vec = self.vec_d(d) + self.vec_w(w) + self.vec_h(h)
return vec
def set_pos_o(self, adjust=0):
""" calculates the position of the origin, and saves it in
attribute pos_o
Parameters:
-----------
adjust : int
1: If, when created, wasn't possible to set the piece at pos_o,
and it was placed at pos, then the position will be adjusted
"""
vec_from_pos_o = (self.get_o_to_d(self.pos_d)
+ self.get_o_to_w(self.pos_w)
+ self.get_o_to_h(self.pos_h))
vec_to_pos_o = vec_from_pos_o.negative()
self.pos_o = self.pos + vec_to_pos_o
if adjust == 1:
self.pos_o_adjust = vec_to_pos_o # self.pos_o - self.pos
def get_o_to_d(self, pos_d):
""" returns the vector from origin pos_o to pos_d
If it is symmetrical along axis_d, pos_d == 0 will be at the middle
Then, pos_d > 0 will be the points on the positive side of axis_d
and pos_d < 0 will be the points on the negative side of axis_d
d0_cen = 1
:
_____:_____
| : | self.d_o[1] is the vector from orig to -1
| : | self.d_o[0] is the vector from orig to 0
|_____:_____|......> axis_d
-2 -1 0 1 2
o---------> A: o to 1 :
o------> B: o to 0 : d_o[0]
o---> C: o to -1 : d_o[1]
o --> D: -1 to 0 : d_o[0] - d_o[1] : B - C
A = B + D
A = B + (B-C) = 2B - C
d0_cen = 0
:
:___________
| | self.d_o[1] is the vector from orig to 1
| |
|___________|......> axis_d
0 1 2 3 4
"""
abs_pos_d = abs(pos_d)
if self.d0_cen == 1:
if pos_d <= 0:
try:
vec = self.d_o[abs_pos_d]
except KeyError:
logger.error('pos_d key not defined ' + str(pos_d))
else:
return vec
else:
try:
vec_0_to_d = (self.d_o[0]).sub(self.d_o[pos_d]) # D= B-C
except KeyError:
logger.error('pos_d key not defined ' + str(pos_d))
else:
vec_orig_to_d = self.d_o[0] + vec_0_to_d # A = B + D
return vec_orig_to_d
else: # pos_d == 0 is at the end, distances are calculated directly
try:
vec = self.d_o[pos_d]
except KeyError:
logger.error('pos_d key not defined' + str(pos_d))
else:
return vec
def get_o_to_w(self, pos_w):
""" returns the vector from origin pos_o to pos_w
If it is symmetrical along axis_w, pos_w == 0 will be at the middle
Then, pos_w > 0 will be the points on the positive side of axis_w
and pos_w < 0 will be the points on the negative side of axis_w
See get_o_to_d drawings
"""
abs_pos_w = abs(pos_w)
if self.w0_cen == 1:
if pos_w <= 0:
try:
vec = self.w_o[abs_pos_w]
except KeyError:
logger.error('pos_w key not defined ' + str(pos_w))
else:
return vec
else:
try:
vec_0_to_w = (self.w_o[0]).sub(self.w_o[pos_w]) # D= B-C
except KeyError:
logger.error('pos_w key not defined ' + str(pos_w))
else:
vec_orig_to_w = self.w_o[0] + vec_0_to_w # A = B + D
return vec_orig_to_w
else: # pos_w == 0 is at the end, distances are calculated directly
try:
vec = self.w_o[pos_w]
except KeyError:
logger.error('pos_w key not defined' + str(pos_w))
else:
return vec
def get_o_to_h(self, pos_h):
""" returns the vector from origin pos_o to pos_h
If it is symmetrical along axis_h, pos_h == 0 will be at the middle
Then, pos_h > 0 will be the points on the positive side of axis_h
and pos_h < 0 will be the points on the negative side of axis_h
See get_o_to_d drawings
"""
abs_pos_h = abs(pos_h)
if self.h0_cen == 1:
if pos_h <= 0:
try:
vec = self.h_o[abs_pos_h]
except KeyError:
logger.error('pos_h key not defined ' + str(pos_h))
else:
return vec
else:
try:
vec_0_to_h = (self.h_o[0]).sub(self.h_o[pos_h]) # D= B-C
except KeyError:
logger.error('pos_h key not defined ' + str(pos_h))
else:
vec_orig_to_h = self.h_o[0] + vec_0_to_h # A = B + D
return vec_orig_to_h
else: # pos_h == 0 is at the end, distances are calculated directly
try:
vec = self.h_o[pos_h]
except KeyError:
logger.error('pos_h key not defined' + str(pos_h))
else:
return vec
def get_d_ab(self, pta, ptb):
""" returns the vector along axis_d from pos_d = pta to pos_d = ptb
"""
vec = self.get_o_to_d(ptb).sub(self.get_o_to_d(pta))
return vec
def get_w_ab(self, pta, ptb):
""" returns the vector along axis_h from pos_w = pta to pos_w = ptb
"""
vec = self.get_o_to_w(ptb).sub(self.get_o_to_w(pta))
return vec
def get_h_ab(self, pta, ptb):
""" returns the vector along axis_h from pos_h = pta to pos_h = ptb
"""
vec = self.get_o_to_h(ptb).sub(self.get_o_to_h(pta))
return vec
def get_pos_d(self, pos_d):
""" returns the absolute position of the pos_d point
"""
return self.pos_o + self.get_o_to_d(pos_d)
def get_pos_w(self, pos_w):
""" returns the absolute position of the pos_w point
"""
return self.pos_o + self.get_o_to_w(pos_w)
def get_pos_h(self, pos_h):
""" returns the absolute position of the pos_h point
"""
return self.pos_o + self.get_o_to_h(pos_h)
def get_pos_dwh(self, pos_d, pos_w, pos_h):
""" returns the absolute position of the pos_d, pos_w, pos_h point
"""
pos = (self.pos_o + self.get_o_to_d(pos_d)
+ self.get_o_to_w(pos_w)
+ self.get_o_to_h(pos_h))
return pos
class ShpCyl(Obj3D):
"""
Creates a shape of a cylinder
Makes a cylinder in any position and direction, with optional extra
heights and radius, and various locations in the cylinder
Parameters:
-----------
r : float
radius of the cylinder
h : float
height of the cylinder
axis_h : FreeCAD.Vector
vector along the cylinder height
axis_d : FreeCAD.Vector
vector along the cylinder radius, a direction perpendicular to axis_h
It is not necessary if pos_d == 0
It can be None, but if None, axis_w has to be None
axis_w : FreeCAD.Vector
vector along the cylinder radius
a direction perpendicular to axis_h and axis_d
It is not necessary if pos_w == 0
It can be None
pos_h : int
location of pos along axis_h (0, 1)
0: the cylinder pos is centered along its height
1: the cylinder pos is at its base (not considering xtr_h)
pos_d : int
location of pos along axis_d (0, 1)
0: pos is at the circunference center
1: pos is at the circunsference, on axis_d, at r from the circle center
(not at r + xtr_r)
pos_w : int
location of pos along axis_w (0, 1)
0: pos is at the circunference center
1: pos is at the circunsference, on axis_w, at r from the circle center
(not at r + xtr_r)
xtr_top : float
Extra height on top, it is not taken under consideration when
calculating the cylinder center along the height
xtr_bot : float
Extra height at the bottom, it is not taken under consideration when
calculating the cylinder center along the height or the position of
the base
xtr_r : float
Extra length of the radius, it is not taken under consideration when
calculating pos_d or pos_w
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
print_ax: FreeCAD.Vector
The best direction to print, pointing upwards
it can be V0 if there is no best direction
Attributes:
-----------
pos_o : FreeCAD.Vector
Position of the origin of the shape
h_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_h
d_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_d
w_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_w
h0_cen : int
d0_cen : int
w0_cen : int
indicates if pos_h = 0 (pos_d, pos_w) is at the center along
axis_h, axis_d, axis_w, or if it is at the end.
1 : at the center (symmetrical, or almost symmetrical)
0 : at the end
shp : OCC Topological Shape
The shape of this part
pos_h = 1, pos_d = 0, pos_w = 0
pos at 1:
axis_w
:
:
. .
. .
( o ) ---- axis_d This o will be pos_o (origin)
. .
. .
axis_h
:
:
...............
:____:____:....: xtr_top
| |
| |
| |
| |
| |
| |
|____1____|...............> axis_d
:....o....:....: xtr_bot This o will be pos_o
pos_h = 0, pos_d = 1, pos_w = 0
pos at x:
axis_w
:
:
: . .
: . .
x ) ----> axis_d
. .
. .
axis_h
:
:
...............
:____:____:....: xtr_top
| |
| |
| |
x |....>axis_d h = 0
| |
| |
|_________|.....
:....o....:....: xtr_bot This o will be pos_o
pos_h = 0, pos_d = 1, pos_w = 1
pos at x:
axis_w
:
:
: . .
: . .
( )
. .
x . . ....> axis_d
axis_h
:
:
...............
:____:____:....: xtr_top
|| |
|| |
|| |
|x |....>axis_d
|| |
|| |
||_________|.....
::....o....:....: xtr_bot
:;
xtr_r
"""
def __init__(self,
r, h, axis_h=VZ,
axis_d=None, axis_w=None,
pos_h=0, pos_d=0, pos_w=0,
xtr_top=0, xtr_bot=0, xtr_r=0, pos=V0):
Obj3D.__init__(self, axis_d, axis_w, axis_h)
# save the arguments as attributes:
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
for i in args:
if not hasattr(self, i): # so we keep the attributes already set
setattr(self, i, values[i])
# vectors from o (orig) along axis_h, to the pos_h points
# h_o is a dictionary created in Obj3D.__init__
self.h_o[0] = self.vec_h(h / 2. + xtr_bot)
self.h_o[1] = self.vec_h(xtr_bot)
# pos_h = 0 is at the center
self.h0_cen = 1
self.d_o[0] = V0
if self.axis_d is not None:
self.d_o[1] = self.vec_d(-r)
elif pos_d == 1:
logger.error('axis_d not defined while pos_d ==1')
# pos_d = 0 is at the center
self.d0_cen = 1
self.w_o[0] = V0
if self.axis_w is not None:
self.w_o[1] = self.vec_w(-r)
elif pos_w == 1:
logger.error('axis_w not defined while pos_w ==1')
# pos_w = 0 is at the center
self.w0_cen = 1
# calculates the position of the origin, and keeps it in attribute pos_o
self.set_pos_o()
shpcyl = fcfun.shp_cyl(r=r + xtr_r, # radius
h=h + xtr_bot + xtr_top, # height
normal=self.axis_h, # direction
pos=self.pos_o) # Position
self.shp = shpcyl
# cyl = ShpCyl (r=2, h=2, axis_h = VZ,
# axis_d = VX, axis_w = VY,
# pos_h = 1, pos_d = 1, pos_w = 0,
# xtr_top=0, xtr_bot=1, xtr_r=2,
# pos = V0)
# #pos = FreeCAD.Vector(1,2,0))
# Part.show(cyl.shp)
class ShpCylHole(Obj3D):
"""
Creates a shape of a hollow cylinder
Similar to fcfun shp_cylhole_gen, but creates the object with the useful
attributes and methods
Makes a hollow cylinder in any position and direction, with optional extra
heights, and inner and outer radius, and various locations in the cylinder
Parameters:
-----------
r_out : float
radius of the outside cylinder
r_in : float
radius of the inner hole of the cylinder
h : float
height of the cylinder
axis_h : FreeCAD.Vector
vector along the cylinder height
axis_d : FreeCAD.Vector
vector along the cylinder radius, a direction perpendicular to axis_h
it is not necessary if pos_d == 0
It can be None, but if None, axis_w has to be None
axis_w : FreeCAD.Vector
vector along the cylinder radius,
a direction perpendicular to axis_h and axis_d
it is not necessary if pos_w == 0
It can be None
pos_h : int
location of pos along axis_h (0, 1)
0: the cylinder pos is centered along its height, not considering
xtr_top, xtr_bot
1: the cylinder pos is at its base (not considering xtr_h)
pos_d : int
location of pos along axis_d (0, 1)
0: pos is at the circunference center
1: pos is at the inner circunsference, on axis_d, at r_in from the
circle center (not at r_in + xtr_r_in)
2: pos is at the outer circunsference, on axis_d, at r_out from the
circle center (not at r_out + xtr_r_out)
pos_w : int
location of pos along axis_w (0, 1)
0: pos is at the circunference center
1: pos is at the inner circunsference, on axis_w, at r_in from the
circle center (not at r_in + xtr_r_in)
2: pos is at the outer circunsference, on axis_w, at r_out from the
circle center (not at r_out + xtr_r_out)
xtr_top : float
Extra height on top, it is not taken under consideration when
calculating the cylinder center along the height
xtr_bot : float
Extra height at the bottom, it is not taken under consideration when
calculating the cylinder center along the height or the position of
the base
xtr_r_in : float
Extra length of the inner radius (hollow cylinder),
it is not taken under consideration when calculating pos_d or pos_w.
It can be negative, so this inner radius would be smaller
xtr_r_out : float
Extra length of the outer radius
it is not taken under consideration when calculating pos_d or pos_w.
It can be negative, so this outer radius would be smaller
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
Attributes:
-----------
pos_o : FreeCAD.Vector
Position of the origin of the shape
h_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_h
d_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_d
w_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_w
h0_cen : int
d0_cen : int
w0_cen : int
indicates if pos_h = 0 (pos_d, pos_w) is at the center along
axis_h, axis_d, axis_w, or if it is at the end.
1 : at the center (symmetrical, or almost symmetrical)
0 : at the end
shp : OCC Topological Shape
The shape of this part
pos_h = 1, pos_d = 0, pos_w = 0
pos at 1:
axis_w
:
:
. .
. . . .
( ( 0 ) ) ---- axis_d
. . . .
. .
axis_h
:
:
...............
:____:____:....: xtr_top
| : : |
| : : |
| : : |
| : 0 : | 0: pos would be at 0, if pos_h == 0
| : : |
| : : |
|_:__1__:_|....>axis_d
:.:..o..:.:....: xtr_bot This o will be pos_o (orig)
: : :
: :..:
: + :
:r_in:
: :
:....:
+
r_out
Values for pos_d (similar to pos_w along it axis)
axis_h
:
:
...............
:____:____:....: xtr_top
| : : |
| : : |
| : : |
2 1 0 : |....>axis_d (if pos_h == 0)
| : : |
| : : |
|_:_____:_|.....
:.:..o..:.:....: xtr_bot This o will be pos_o (orig)
: : :
: :..:
: + :
:r_in:
: :
:....:
+
r_out
"""
def __init__(self,
r_out, r_in, h,
axis_h=VZ, axis_d=None, axis_w=None,
pos_h=0, pos_d=0, pos_w=0,
xtr_top=0, xtr_bot=0,
xtr_r_out=0, xtr_r_in=0,
pos=V0):
Obj3D.__init__(self, axis_d, axis_w, axis_h)
# save the arguments as attributes:
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
for i in args:
if not hasattr(self, i):
setattr(self, i, values[i])
# THIS IS WORKING, but it seems that the signs are not right
# vectors from o (orig) along axis_h, to the pos_h points
# h_o is a dictionary created in Obj3D.__init__
self.h_o[0] = self.vec_h(h / 2. + xtr_bot)
self.h_o[1] = self.vec_h(xtr_bot)
# pos_h = 0 is at the center
self.h0_cen = 1
self.d_o[0] = V0
if self.axis_d is not None:
self.d_o[1] = self.vec_d(-r_in)
self.d_o[2] = self.vec_d(-r_out)
elif pos_d != 0:
logger.error('axis_d not defined while pos_d != 0')
# pos_d = 0 is at the center
self.d0_cen = 1
self.w_o[0] = V0
if self.axis_w is not None:
self.w_o[1] = self.vec_w(-r_in)
self.w_o[2] = self.vec_w(-r_out)
elif pos_w != 0:
logger.error('axis_w not defined while pos_w != 0')
# pos_w = 0 is at the center
self.w0_cen = 1
# calculates the position of the origin, and keeps it in attribute pos_o
self.set_pos_o()
shpcyl = fcfun.shp_cylholedir(r_out=r_out + xtr_r_out, # ext radius
r_in=r_in + xtr_r_in, # internal radius
h=h + xtr_bot + xtr_top, # height
normal=self.axis_h, # direction
pos=self.pos_o) # Position
self.shp = shpcyl
self.prnt_ax = self.axis_h
# cyl = ShpCylHole (r_in=2, r_out=6, h=4,
# #axis_h = FreeCAD.Vector(1,1,0),
# axis_h = VZ,
# #axis_d = VX, axis_w = VYN,
# axis_d = VX,
# pos_h = 1, pos_d = 1, pos_w = 0,
# xtr_top=1, xtr_bot=1,
# xtr_r_in=0, xtr_r_out=0,
# pos = V0)
# #pos = FreeCAD.Vector(1,2,3))
# Part.show(cyl.shp)
class ShpPrismHole(Obj3D):
"""
Creates a shape of a hollow prism
Similar to fcfun shp_regprism_dirxtr, but creates the object with the useful
attributes and methods
Makes a hollow prism in any position and direction, with different positions
heights, and inner and outer radius, and various locations in the cylinder
Parameters:
-----------
n_sides : int
number of sides of the polygon
r_out : float
circumradius of the polygon
h : float
total height of the cylinder
r_in : float
radius of the inner hole
if 0, no inner hole
h_offset : float
0: default
Distance from the top, just to place the prism, see pos_h
if negative, from the bottom
xtr_r_in : float
Extra length of the inner radius (hollow cylinder),
it is not taken under consideration when calculating pos_d or pos_w.
It can be negative, so this inner radius would be smaller
xtr_r_out : float
Extra length of the circumradius
it is not taken under consideration when calculating pos_d or pos_w.
It can be negative, so this outer radius would be smaller
axis_d_apo : int
0: default: axis_d points to the vertex
1: axis_d points to the center of a side
axis_h : FreeCAD.Vector
vector along the cylinder height
axis_d : FreeCAD.Vector
vector along the first vertex, a direction perpendicular to axis_h
it is not necessary if pos_d == 0
It can be None, but if None, axis_w has to be None
axis_w : FreeCAD.Vector
vector along the cylinder radius,
a direction perpendicular to axis_h and axis_d
it is not necessary if pos_w == 0
It can be None
pos_h : int
location of pos along axis_h
0: at the center
-1: at the base
1: at the top
-2: at the base + h_offset
2: at the top + h_offset
pos_d : int
location of pos along axis_d (-2, -1, 0, 1, 2)
0: pos is at the circunference center (axis)
1: pos is at the inner circunsference, on axis_d, at r_in from the
circle center (not at r_in + xtr_r_in)
2: pos is at the apothem, on axis_d
3: pos is at the outer circunsference, on axis_d, at r_out from the
circle center (not at r_out + xtr_r_out)
pos_w : int
location of pos along axis_w (-2, -1, 0, 1, 2)
0: pos is at the circunference center
1: pos is at the inner circunsference, on axis_w, at r_in from the
circle center (not at r_in + xtr_r_in)
2: pos is at the apothem, on axis_w
3: pos is at the outer circunsference, on axis_w, at r_out from the
circle center (not at r_out + xtr_r_out)
pos : FreeCAD.Vector
Position of the prism, taking into account where the center is
Attributes:
-----------
pos_o : FreeCAD.Vector
Position of the origin of the shape
h_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_h
d_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_d
w_o : dictionary of FreeCAD.Vector
vectors from the origin to the different points along axis_w
h0_cen : int
d0_cen : int
w0_cen : int
indicates if pos_h = 0 (pos_d, pos_w) is at the center along
axis_h, axis_d, axis_w, or if it is at the end.
1 : at the center (symmetrical, or almost symmetrical)
0 : at the end
shp : OCC Topological Shape
The shape of this part
__:__
/ \
/ / \ \........axis_d
\ \ / /
\____ /
01 23 pos_d
axis_h
:
:
____:____ .... 1
: : : : : h_offset
: : : :....: 2
| : : |
| : : |
| : o : | 0 This o will be pos_o (orig)
| : : |
| : : |..... -2
| : : | : off_set
:_:_____:_:....: bot_out -1 .....> axis_d
: : :
: :..:
: + :
:r_in:
: :
:....:
+
r_out
"""
def __init__(self, n_sides,
r_out, h, r_in,
h_offset=0,
xtr_r_in=0,
xtr_r_out=0,
axis_d_apo=0,
axis_h=VZ, axis_d=None, axis_w=None,
pos_h=0, pos_d=0, pos_w=0,
pos=V0):
Obj3D.__init__(self, axis_d, axis_w, axis_h)
# save the arguments as attributes:
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
for i in args:
if not hasattr(self, i):
setattr(self, i, values[i])
self.h0_cen = 1 # symmetric
# vectors from o (orig) along axis_h, to the pos_h points
# h_o is a dictionary created in Obj3D.__init__
self.h_o[0] = V0
self.h_o[1] = self.vec_h(-h / 2.)
self.h_o[2] = self.vec_h(-h / 2. + h_offset)
# apotheme
self.apo = r_out * math.cos(math.pi / n_sides)
self.d_o[0] = V0
if self.axis_d != V0:
# not considering the extra radius (usually tolerances)
self.d_o[1] = self.vec_d(-r_in)
self.d_o[2] = self.vec_d(-self.apo)
self.d_o[3] = self.vec_d(-r_out)
else:
if pos_d != 0:
logger.error('axis_d not defined while pos_d != 0')
self.axis_d = fcfun.get_fc_perpend1(self.axis_h)
# pos_d = 0 is at the center
self.d0_cen = 1
self.w_o[0] = V0
if self.axis_w != V0:
self.w_o[1] = self.vec_w(-r_in)
self.w_o[2] = self.vec_w(-self.apo)
self.w_o[3] = self.vec_w(-r_out)
elif pos_w != 0:
logger.error('axis_w not defined while pos_w != 0')
# pos_w = 0 is at the center
self.w0_cen = 1
# calculates the position of the origin, and keeps it in attribute pos_o
self.set_pos_o()
if axis_d_apo == 0:
self.axis_apo = self.axis_d
else: # rotate 360/(2*nsides)
self.axis_apo = DraftVecUtils.rotate(self.axis_d, math.pi / n_sides,
self.axis_h)
shp_prism = fcfun.shp_regprism_dirxtr(n_sides=n_sides,
radius=r_out + xtr_r_out,
length=h,
fc_normal=self.axis_h,
fc_verx1=self.axis_apo,
centered=0,
pos=self.get_pos_h(-1))
if r_in > 0:
shp_cyl = fcfun.shp_cylcenxtr(r=r_in + xtr_r_in,
h=h,
normal=self.axis_h,
ch=0,
xtr_top=1, # to cut
xtr_bot=1, # to cut
pos=self.get_pos_h(-1))
self.shp = shp_prism.cut(shp_cyl)
else:
self.shp = shp_prism
self.prnt_ax = self.axis_h
# prism = ShpPrismHole (n_sides = 4,
# r_out = 20,
# h = 4,
# r_in = 1,
# h_offset = 1,
# xtr_r_in = 2,
# xtr_r_out = 4,
# axis_d_apo = 0,
# axis_h = VZ, axis_d = VX, axis_w = VY,
# pos_h = -2, pos_d = 0, pos_w = 0,
# pos = V0)
# Part.show(prism.shp)
class ShpBolt(Obj3D):
"""
Creates a shape of a Bolt
Similar to fcfun.shp_bolt_dir, but creates the object with the useful
attributes and methods
Makes a bolt with various locations and head types
It is an approximate model. The thread is not made, it is just a little
smaller just to see where it is