forked from URJCMakerGroup/MakerWorkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comps.py
4786 lines (4182 loc) · 177 KB
/
comps.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
# ----------------------------------------------------------------------------
# -- Components
# -- comps library
# -- Python classes that creates useful parts for FreeCAD
# ----------------------------------------------------------------------------
# -- (c) Felipe Machado
# -- Area of Electronics. Rey Juan Carlos University (urjc.es)
# -- October-2016
# ----------------------------------------------------------------------------
# --- LGPL Licence
# ----------------------------------------------------------------------------
import FreeCAD
import Part
import logging
import os
import inspect
import Draft
import DraftGeomUtils
import DraftVecUtils
import math
# import copy;
# import Mesh;
# ---------------------- can be taken away after debugging
# 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)
# ---------------------- can be taken away after debugging
import kcomp # before, it was called mat_cte
import fcfun
import shp_clss
import fc_clss
from fcfun import V0, VX, VY, VZ, V0ROT, addBox, addCyl, addCyl_pos, fillet_len
from fcfun import VXN, VYN, VZN
from fcfun import addBolt, addBoltNut_hole, NutHole
logging.basicConfig(level=logging.DEBUG,
format='%(%(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
#
# _______ _______________________________ TotH = H
# | ___ |
# | / \ | __________ HoleH = h
# | \___/ | __
# __| |__ /| __
# |_____________|/ __ TotD = L ___________________
#
# <- TotW = W->
#
# hole_x: 1 the depth along X axis
# Hole facing X
# 0 the depth along Y axis
# Hole facing Y
# cx: 1 if you want the coordinates referenced to the x center of the piece
# it can be done because it is a new shape formed from the union
# cy: 1 if you want the coordinates referenced to the y center of the piece
# upsdown: NOT YET
# 0: Normal vertical position, referenced to 0
# 1: z0 is the center of the hexagon (nut)
# it can be done because it is a new shape formed from the union
class Sk(object):
"""
SK dimensions:
dictionary for the dimensions
mbolt: is mounting bolt. it corresponds to its metric
tbolt: is the tightening bolt.
SK12 = { 'd':12.0, 'H':37.5, 'W':42.0, 'L':14.0, 'B':32.0, 'S':5.5,
'h':23.0, 'A':21.0, 'b': 5.0, 'g':6.0, 'I':20.0,
'mbolt': 5, 'tbolt': 4}
"""
# separation of the upper side (it is not defined). Change it
# measured for sk12 is 1.2
up_sep_dist = 1.2
# tolerances for holes
holtol = 1.1
def __init__(self, size, name, hole_x=1, cx=0, cy=0):
self.size = size
self.name = name
self.cx = cx
self.cy = cy
skdict = kcomp.SK.get(size)
if skdict is None:
logger.warning("Sk size %d not supported", size)
else:
doc = FreeCAD.ActiveDocument
# Total height:
sk_z = skdict['H']
self.TotH = sk_z
# Total width (Y):
sk_w = skdict['W']
self.TotW = sk_w
# Total depth (x):
sk_d = skdict['L']
self.TotD = sk_d
# Base height
sk_base_h = skdict['g']
# center width
sk_center_w = skdict['I']
# Axis height:
sk_axis_h = skdict['h']
self.HoleH = sk_axis_h
# tightening bolt with added tolerances:
# Bolt's head radius
tbolt_head_r = (self.holtol
* kcomp.D912_HEAD_D[skdict['tbolt']]) / 2.0
# Bolt's head length
tbolt_head_l = (self.holtol
* kcomp.D912_HEAD_L[skdict['tbolt']])
# Mounting bolt radius with added tolerance
mbolt_r = self.holtol * skdict['mbolt'] / 2.
# the total dimensions: LxWxH
# we will cut it
total_box = addBox(x=sk_d,
y=sk_w,
z=sk_z,
name="total_box",
cx=False, cy=True)
# what we have to cut from the sides
side_box_y = (sk_w - skdict['I']) / 2.
side_box_z = sk_z - skdict['g']
side_cut_box_r = addBox(sk_d, side_box_y, side_box_z,
"side_box_r")
side_cut_pos_r = FreeCAD.Vector(0,
skdict['I'] / 2.,
skdict['g'])
side_cut_box_r.Placement.Base = side_cut_pos_r
side_cut_box_l = addBox(sk_d, side_box_y, side_box_z,
"side_box_l")
side_cut_pos_l = FreeCAD.Vector(0, -sk_w / 2., skdict['g'])
side_cut_box_l.Placement.Base = side_cut_pos_l
# union
side_boxes = doc.addObject("Part::Fuse", "side_boxes")
side_boxes.Base = side_cut_box_r
side_boxes.Tool = side_cut_box_l
# difference
sk_shape = doc.addObject("Part::Cut", "sk_shape")
sk_shape.Base = total_box
sk_shape.Tool = side_boxes
# Shaft hole, its height has +2 to make it throughl L all de way
shaft_hole = addCyl(skdict['d'] / 2.,
sk_d + 2,
"shaft_hole")
"""
First argument defines de position: -1, 0, h
Second argument rotation: 90 degrees rotation in Y.
Third argument the center of the rotation, in this case,
it is in the cylinder
axis at the base of the cylinder
"""
shaft_hole.Placement = FreeCAD.Placement(FreeCAD.Vector(-1, 0, skdict['h']),
FreeCAD.Rotation(VY, 90),
V0)
# the upper sepparation
up_sep = addBox(sk_d + 2,
self.up_sep_dist,
sk_z - skdict['h'] + 1,
"up_sep")
up_sep_pos = FreeCAD.Vector(-1,
-self.up_sep_dist / 2,
skdict['h'] + 1)
up_sep.Placement.Base = up_sep_pos
# Tightening bolt shaft hole, its height has +2 to make it
# throughl L all de way
# skdict['tbolt'] is the diameter of the bolt: (M..) M4, ...
# tbolt_head_r: is the radius of the tightening bolt's head
# (including tolerance), which its bottom either
# - is at the middle point between
# - A: the total height :sk_z
# - B: the top of the shaft hole: skdict['h']+skdict['d']/2
# - so the result will be (A + B)/2
# or it is aligned with the top of the 12mm shaft, whose height is:
# skdict['h']+skdict['d']/2
tbolt_shaft = addCyl(skdict['tbolt'] / 2, skdict['I'] + 2,
"tbolt_shaft")
tbolt_shaft_pos = FreeCAD.Vector(sk_d / 2.,
skdict['I'] / 2. + 1,
skdict['h'] + skdict['d'] / 2. + tbolt_head_r / self.holtol)
# (sk_z + skdict['h']+skdict['d']/2.)/2.)
tbolt_shaft.Placement = FreeCAD.Placement(tbolt_shaft_pos,
FreeCAD.Rotation(VX, 90),
V0)
# Head of the thigthening bolt
tbolt_head = addCyl(tbolt_head_r, tbolt_head_l + 1, "tbolt_head")
tbolt_head_pos = FreeCAD.Vector(sk_d / 2., skdict['I'] / 2. + 1,
skdict['h'] + skdict['d'] / 2 + tbolt_head_r / self.holtol)
# (sk_z + skdict['h']+skdict['d']/2.)/2.)
tbolt_head.Placement = FreeCAD.Placement(tbolt_head_pos,
FreeCAD.Rotation(VX, 90),
V0)
# Make an union of all these parts
fuse_shaft_holes = doc.addObject("Part::MultiFuse",
"fuse_shaft_holes")
fuse_shaft_holes.Shapes = [tbolt_head,
tbolt_shaft,
up_sep, shaft_hole]
# Cut from the sk_shape
sk_shape_w_holes = doc.addObject("Part::Cut", "sk_shape_w_holes")
sk_shape_w_holes.Base = sk_shape
sk_shape_w_holes.Tool = fuse_shaft_holes
# Mounting bolts
mbolt_sh_r = addCyl(mbolt_r, skdict['g'] + 2., "mbolt_sh_r")
mbolt_sh_l = addCyl(mbolt_r, skdict['g'] + 2., "mbolt_sh_l")
mbolt_sh_r_pos = FreeCAD.Vector(sk_d / 2,
skdict['B'] / 2.,
-1)
mbolt_sh_l_pos = FreeCAD.Vector(sk_d / 2,
-skdict['B'] / 2.,
-1)
mbolt_sh_r.Placement.Base = mbolt_sh_r_pos
mbolt_sh_l.Placement.Base = mbolt_sh_l_pos
# Equivalent expressions to the ones above
# mbolt_sh_l.Placement = FreeCAD.Placement(mbolt_sh_l_pos, v0rot, v0)
# mbolt_sh_r.Placement = FreeCAD.Placement(mbolt_sh_r_pos, v0rot, v0)
mbolts_sh = doc.addObject("Part::Fuse", "mbolts_sh")
mbolts_sh.Base = mbolt_sh_r
mbolts_sh.Tool = mbolt_sh_l
# Instead of moving all the objects from the beginning. I do it here
# so it is easier, and since a new object will be created, it is
# referenced correctly
# Now, it is centered on Y, having the width on X, hole facing X
# on the positive side of X
if hole_x == 1:
# this is how it is, no rotation
rot = FreeCAD.Rotation(VZ, 0)
if cx == 1: # we want centered on X,bring back the half of depth
xpos = -self.TotD / 2.
else:
xpos = 0 # how it is
if cy == 1: # centered on Y, how it is
ypos = 0
else:
ypos = self.TotW / 2.0 # bring forward the width
else: # hole facing Y
rot = FreeCAD.Rotation(VZ, 90)
# After rotating, it is centered on X,
if cx == 1: # centered on X, how it is
xpos = 0
else:
xpos = self.TotW / 2.0
if cy == 1: # we want centered on Y, bring back
ypos = - self.TotD / 2.0
else:
ypos = 0
sk_shape_w_holes.Placement.Base = FreeCAD.Vector(xpos, ypos, 0)
mbolts_sh.Placement.Base = FreeCAD.Vector(xpos, ypos, 0)
sk_shape_w_holes.Placement.Rotation = rot
mbolts_sh.Placement.Rotation = rot
sk_final = doc.addObject("Part::Cut", name)
sk_final.Base = sk_shape_w_holes
sk_final.Tool = mbolts_sh
self.fco = sk_final # the FreeCad Object
class Sk_dir(object):
# Similar to Sk, but in any direction
"""
SK dimensions:
dictionary for the dimensions
::
mbolt: is mounting bolt. it corresponds to its metric
tbolt: is the tightening bolt.
SK12 = { 'd':12.0, 'H':37.5, 'W':42.0, 'L':14.0, 'B':32.0, 'S':5.5,
'h':23.0, 'A':21.0, 'b': 5.0, 'g':6.0, 'I':20.0,
'mbolt': 5, 'tbolt': 4}
::
fc_axis_h
:
___:___ _______________________________ tot_h
| ___ |
| / \ | __________ HoleH = h
| \___/ | __
__| |__ /| __
|_____________|/ __ TotD = L ___________________
___:___ ___
| ___ | |...|
| / 2 \ | 3 1 |.....> fc_axis_d
| \_*_/ | |...|
____| |____ |___|
8_:5_____4_____::_|..fc_axis_w 6_7_|....... fc_axis_d
: : : :
:... tot_w .......: :...:
tot_d
Parameters
----------
fc_axis_h : FreeCAD.Vector
Axis on the height direction
fc_axis_d : FreeCAD.Vector
Axis on the depth (rod) direction
fc_axis_w : FreeCAD.Vector
Width (perpendicular) dimension, only useful if I finally
include the tightening bolt, or if ref_wc != 1
ref_hr : int
* 1: reference at the Rod Height dimension (rod center):
points 1, 2, 3
* 0: reference at the base: points 4, 5
ref_wc : int
* 1: reference at the center on the width dimension (fc_axis_w)
points: 2, 4,
* 0: reference at one of the bolt holes, point 5
* -1: reference at one end. point 8
ref_dc : int
* 1: reference at the center of the depth dimension
(fc_axis_d) points: 1,7
* 0: reference at one of the ends on the depth dimension
points 3, 6
pillow : int
* 1 to make it the same height of a pillow block
pos : FreeCAD.Vector
Placement
wfco : int
* 1 to create a FreeCAD Object
tol : float
Tolerance of the axis
name : str
FreeCAD Object name
Returns
-------
FreeCAD Object
FreeCAD Object of a shaft holder
"""
# separation of the upper side (it is not defined). Change it
# measured for sk12 is 1.2
up_sep_dist = 1.2
# tolerances for holes
holtol = 1.1
def __init__(self, size,
fc_axis_h=VZ,
fc_axis_d=VX,
fc_axis_w=V0,
ref_hr=1,
ref_wc=1,
ref_dc=1,
pillow=0, # make it the same height of a pillow block
pos=V0,
wfco=1,
tol=0.3,
name="shaft_holder"):
self.size = size
self.wfco = wfco
self.name = name
self.pos = pos
self.tol = tol
self.ref_hr = ref_hr
self.ref_wc = ref_wc
self.ref_dc = ref_dc
doc = FreeCAD.ActiveDocument
if pillow == 0:
skdict = kcomp.SK.get(size)
else:
skdict = kcomp.PILLOW_SK.get(size)
if skdict is None:
logger.error("Sk size %d not supported", size)
# normalize de axis
axis_h = DraftVecUtils.scaleTo(fc_axis_h, 1)
axis_d = DraftVecUtils.scaleTo(fc_axis_d, 1)
if fc_axis_w == V0:
axis_w = axis_h.cross(axis_d)
else:
axis_w = DraftVecUtils.scaleTo(fc_axis_w, 1)
axis_h_n = axis_h.negative()
axis_d_n = axis_d.negative()
axis_w_n = axis_w.negative()
# Total height:
sk_h = skdict['H']
self.tot_h = sk_h
# Total width (Y):
sk_w = skdict['W']
self.tot_w = sk_w
# Total depth (x):
sk_d = skdict['L']
self.tot_d = sk_d
# Base height
sk_base_h = skdict['g']
# center width
sk_center_w = skdict['I']
# Axis height:
sk_axis_h = skdict['h']
self.axis_h = sk_axis_h
# Mounting bolts separation
sk_mbolt_sep = skdict['B']
# tightening bolt with added tolerances:
tbolt_d = skdict['tbolt']
# Bolt's head radius
tbolt_head_r = (self.holtol
* kcomp.D912_HEAD_D[skdict['tbolt']]) / 2.0
# Bolt's head length
tbolt_head_l = (self.holtol
* kcomp.D912_HEAD_L[skdict['tbolt']])
# Mounting bolt radius with added tolerance
mbolt_r = self.holtol * skdict['mbolt'] / 2.
if ref_hr == 1: # distance vectors on axis_h
ref2rod_h = V0
ref2base_h = DraftVecUtils.scale(axis_h, -sk_axis_h)
else:
ref2rod_h = DraftVecUtils.scale(axis_h, sk_axis_h)
ref2base_h = V0
if ref_wc == 1: # distance vectors on axis_w
ref2cen_w = V0
ref2bolt_w = DraftVecUtils.scale(axis_w, -sk_mbolt_sep / 2.)
ref2end_w = DraftVecUtils.scale(axis_w, -sk_w / 2.)
elif ref_wc == 0:
ref2cen_w = DraftVecUtils.scale(axis_w, sk_mbolt_sep / 2.)
ref2bolt_w = V0
ref2end_w = DraftVecUtils.scale(axis_w, -(sk_w - sk_mbolt_sep) / 2.)
else: # ref_wc == -1 at the end on the width dimension
ref2cen_w = DraftVecUtils.scale(axis_w, sk_w / 2.)
ref2bolt_w = DraftVecUtils.scale(axis_w, (sk_w - sk_mbolt_sep) / 2.)
if ref_dc == 1: # distance vectors on axis_d
ref2cen_d = V0
ref2end_d = DraftVecUtils.scale(axis_d, -sk_d / 2.)
else:
ref2cen_d = DraftVecUtils.scale(axis_d, sk_d / 2.)
ref2end_d = V0
basecen_pos = pos + ref2base_h + ref2cen_w + ref2cen_d
# Making the tall box:
shp_tall = fcfun.shp_box_dir(box_w=sk_center_w,
box_d=sk_d,
box_h=sk_h,
fc_axis_w=axis_w,
fc_axis_h=axis_h,
fc_axis_d=axis_d,
cw=1, cd=1, ch=0, pos=basecen_pos)
# Making the wide box:
shp_wide = fcfun.shp_box_dir(box_w=sk_w,
box_d=sk_d,
box_h=sk_base_h,
fc_axis_w=axis_w,
fc_axis_h=axis_h,
fc_axis_d=axis_d,
cw=1, cd=1, ch=0, pos=basecen_pos)
shp_sk = shp_tall.fuse(shp_wide)
doc.recompute()
shp_sk = shp_sk.removeSplitter()
holes = []
# Shaft hole,
rodcen_pos = pos + ref2rod_h + ref2cen_w + ref2cen_d
rod_hole = fcfun.shp_cylcenxtr(r=size / 2. + self.tol,
h=sk_d,
normal=axis_d,
ch=1,
xtr_top=1,
xtr_bot=1,
pos=rodcen_pos)
holes.append(rod_hole)
# the upper sepparation
shp_topopen = fcfun.shp_box_dir_xtr(box_w=self.up_sep_dist,
box_d=sk_d,
box_h=sk_h - sk_axis_h,
fc_axis_w=axis_w,
fc_axis_h=axis_h,
fc_axis_d=axis_d,
cw=1, cd=1, ch=0,
xtr_h=1, xtr_d=1, xtr_nd=1,
pos=rodcen_pos)
holes.append(shp_topopen)
# Tightening bolt hole
# tbolt_d is the diameter of the bolt: (M..) M4, ...
# tbolt_head_r: is the radius of the tightening bolt's head
# (including tolerance), which its bottom either
# - is at the middle point between
# - A: the total height :sk_h
# - B: the top of the shaft hole: axis_h + size/2.
# - so the result will be (A + B)/2
# tot_h - (axis_h + size/2.)
# _______..A........................
# | ___ |.B.......+ rodtop2top_dist = sk_h - (axis_h + size/2.)
# | / \ |.......+ size/2.
# | \___/ | :
# __| |__ + axis_h
# |_____________|....:
rodtop2top_dist = sk_h - (sk_axis_h + size / 2.)
tbolt_pos = (rodcen_pos
+ DraftVecUtils.scale(axis_w, sk_center_w / 2.)
+ DraftVecUtils.scale(axis_h, size / 2.)
+ DraftVecUtils.scale(axis_h, rodtop2top_dist / 2.))
shp_tbolt = fcfun.shp_bolt_dir(r_shank=tbolt_d / 2.,
l_bolt=sk_center_w,
r_head=tbolt_head_r,
l_head=tbolt_head_l,
hex_head=0,
xtr_head=1,
xtr_shank=1,
support=0,
fc_normal=axis_w_n,
fc_verx1=axis_h,
pos=tbolt_pos)
holes.append(shp_tbolt)
# Mounting bolts
cen2mbolt_w = DraftVecUtils.scale(axis_w, sk_mbolt_sep / 2.)
for w_pos in [cen2mbolt_w.negative(), cen2mbolt_w]:
mbolt_pos = basecen_pos + w_pos
mbolt_hole = fcfun.shp_cylcenxtr(r=mbolt_r,
h=sk_d,
normal=axis_h,
ch=0,
xtr_top=1,
xtr_bot=1,
pos=mbolt_pos)
holes.append(mbolt_hole)
shp_holes = fcfun.fuseshplist(holes)
shp_sk = shp_sk.cut(shp_holes)
self.shp = shp_sk
if wfco == 1:
# a freeCAD object is created
fco = doc.addObject("Part::Feature", name)
fco.Shape = self.shp
self.fco = fco
def color(self, color=(1, 1, 1)):
if self.wfco == 1:
self.fco.ViewObject.ShapeColor = color
else:
logger.debug("Object with no fco")
# doc =FreeCAD.newDocument()
# h_sk = Sk_dir (size = 8,
# fc_axis_h = VX,
# fc_axis_d = VZ,
# fc_axis_w = V0,
# ref_hr = 0,
# ref_wc = 0,
# ref_dc = 0,
# pillow = 0,
# pos = V0,
# tol = 0.3,
# wfco = 1,
# name= "sk8_tol03")
# --------------------------------------------------------------------
# Creates a Misumi Aluminun Profile 30x30 Series 6 Width 8
# length: the length of the profile
# axis 'x', 'y' or 'z'
# 'x' will along the x axis
# 'y' will along the y axis
# 'z' will be vertical
# cx: 1 if you want the coordinates referenced to the x center of the piece
# it can be done because it is a new shape formed from the union
# cy: 1 if you want the coordinates referenced to the y center of the piece
# cz: 1 if you want the coordinates referenced to the z center of the piece
# ATTRIBUTES:
# fco: The freecad object
# Sk: The sketch of the aluminum profile
class MisumiAlu30s6w8(object):
# filename of Aluminum profile sketch
skfilename = "misumi_profile_hfs_serie6_w8_30x30.FCStd"
ALU_W = 30.0
ALU_Wh = ALU_W / 2.0 # half of it
def __init__(self, length, name, axis='x',
cx=False, cy=False, cz=False):
doc = FreeCAD.ActiveDocument
self.length = length
self.name = name
self.axis = axis
self.cx = cx
self.cy = cy
self.cz = cz
# filepath
path = os.getcwd()
# logging.debug(path)
self.skpath = path + '/../../freecad/comps/'
doc_sk = FreeCAD.openDocument(self.skpath + self.skfilename)
list_obj_alumprofile = []
for obj in doc_sk.Objects:
# if (hasattr(obj,'ViewObject') and obj.ViewObject.isVisible()
# and hasattr(obj,'Shape') and len(obj.Shape.Faces) > 0 ):
# # len(obj.Shape.Faces) > 0 to avoid sketches
# list_obj_alumprofile.append(obj)
if len(obj.Shape.Faces) == 0:
orig_alumsk = obj
FreeCAD.ActiveDocument = doc
self.Sk = doc.addObject("Sketcher::SketchObject", 'sk_' + name)
self.Sk.Geometry = orig_alumsk.Geometry
print(orig_alumsk.Geometry)
print(orig_alumsk.Constraints)
self.Sk.Constraints = orig_alumsk.Constraints
self.Sk.ViewObject.Visibility = False
FreeCAD.closeDocument(doc_sk.Name)
FreeCAD.ActiveDocument = doc # otherwise, clone will not work
doc.recompute()
# The sketch is on plane XY, facing Z
if axis == 'x':
self.Dir = (length, 0, 0)
# rotation on Y
rot = FreeCAD.Rotation(VY, 90)
if cx is True:
xpos = - self.length / 2.0
else:
xpos = 0
if cy is True:
ypos = 0
else:
ypos = self.ALU_Wh # half of the aluminum profile width
if cz is True:
zpos = 0
else:
zpos = self.ALU_Wh
elif axis == 'y':
self.Dir = (0, length, 0)
# rotation on X
rot = FreeCAD.Rotation(VX, -90)
if cx is True:
xpos = 0
else:
xpos = self.ALU_Wh
if cy is True:
ypos = - self.length / 2.0
else:
ypos = 0
if cz is True:
zpos = 0
else:
zpos = self.ALU_Wh
elif axis == 'z':
self.Dir = (0, 0, length)
# no rotation
rot = FreeCAD.Rotation(VZ, 0)
if cx is True:
xpos = 0
else:
xpos = self.ALU_Wh
if cy is True:
ypos = 0
else:
ypos = self.ALU_Wh
if cz is True:
zpos = - self.length / 2.0
else:
zpos = 0
else:
logging.debug("wrong argument")
self.Sk.Placement.Rotation = rot
self.Sk.Placement.Base = FreeCAD.Vector(xpos, ypos, zpos)
alu_extr = doc.addObject("Part::Extrusion", name)
alu_extr.Base = self.Sk
alu_extr.Dir = self.Dir
alu_extr.Solid = True
self.fco = alu_extr # the FreeCad Object
# ----------- class RectRndBar ---------------------------------------------
# Creates a rectangular bar with rounded edges, and with the possibility
# to be hollow
#
# Base: the length of the base of the rectangle
# Height: the length of the height of the rectangle
# Length: the length of the bar, the extrusion
# Radius: the radius of the rounded edges (fillet)
# Thick: the thickness of the bar (hollow bar)
# If it is zero or larger than base or
# height, it will be full
# inrad_same : True: inradius = radius. When the radius is very small
# False: inradius = radius - thick
# axis 'x', 'y' or 'z'
# direction of the bar
# 'x' will along the x axis
# 'y' will along the y axis
# 'z' will be vertical
# baseaxis 'x', 'y' or 'z'
# in which axis the base is on. Cannot be the same as axis
# cx: 1 if you want the coordinates referenced to the x center of the piece
# it can be done because it is a new shape formed from the union
# cy: 1 if you want the coordinates referenced to the y center of the piece
# cz: 1 if you want the coordinates referenced to the z center of the piece
# attributes:
# inRad : radius of the inner radius
# inBase : length of the inner rectangle
# inHeight : height of the inner rectangle
# hollow : True, if it is hollow, False if it is not
# face : the face has been extruded
# fco : FreeCad Object
class RectRndBar(object):
def __init__(self, Base, Height, Length, Radius, Thick=0,
inrad_same=False, axis='x',
baseaxis='y', name="rectrndbar",
cx=False, cy=False, cz=False):
doc = FreeCAD.ActiveDocument
self.Base = Base
self.Height = Height
self.Length = Length
self.Radius = Radius
self.Thick = Thick
self.inrad_same = inrad_same
self.name = name
self.axis = axis
self.baseaxis = baseaxis
self.cx = cx
self.cy = cy
self.cz = cz
self.inBase = Base - 2 * Thick
self.inHeight = Height - 2 * Thick
if Thick == 0 or Thick >= Base or Thick >= Height:
self.Thick = 0
self.hollow = False
self.inRad = 0
self.inrad_same = False
self.inBase = 0
self.inHeight = 0
else:
self.hollow = True
if inrad_same is True:
self.inRad = Radius
else:
if Radius > Thick:
self.inRad = Radius - Thick
else:
self.inRad = 0 # a rectangle, with no rounded edges (inside)
wire_ext = fcfun.shpRndRectWire(x=Base, y=Height, r=Radius,
zpos=Length / 2.0)
face_ext = Part.Face(wire_ext)
if self.hollow is True:
wire_int = fcfun.shpRndRectWire(x=self.inBase,
y=self.inHeight,
r=self.inRad,
zpos=Length / 2.0)
face_int = Part.Face(wire_int)
face = face_ext.cut(face_int)
else:
face = face_ext # is not hollow
self.face = face
# Rotate and extrude in the appropriate direction
# now is facing Z, I use vec2
if axis == 'x': # rotate to Z, the 1 or -1 makes the extrusion different
vec2 = (1, 0, 0)
dir_extr = FreeCAD.Vector(Length, 0, 0)
elif axis == 'y':
vec2 = (0, -1, 0)
dir_extr = FreeCAD.Vector(0, Length, 0)
elif axis == 'z':
vec2 = (0, 0, 1)
dir_extr = FreeCAD.Vector(0, 0, Length)
if baseaxis == 'x':
vec1 = (1, 0, 0)
elif baseaxis == 'y':
vec1 = (0, 1, 0)
elif baseaxis == 'z':
vec1 = (0, 0, 1)
vrot = fcfun.calc_rot(vec1, vec2)
vdesp = fcfun.calc_desp_ncen(Length=self.Base,
Width=self.Height,
Height=self.Length,
vec1=vec1, vec2=vec2,
cx=cx, cy=cy, cz=cz)
face.Placement.Base = vdesp
face.Placement.Rotation = vrot
shp_extr = face.extrude(dir_extr)
rndbar = doc.addObject("Part::Feature", name)
rndbar.Shape = shp_extr
self.fco = rndbar
# ----------- end class RectRndBar ----------------------------------------
# ----------- class AluProf ---------------------------------------------
# Creates a generic aluminum profile
# :----- width ----:
# : slot :
# : :--: :
# :______: :______:
# | __| |__ |
# | |\ \ / /| |
# |_| \ \____/ / |_| ...........
# | | ...... insquare
# | ( ) | ......indiam :
# _ | ____ | ..............:
# | | / / \ \ | |
# | |/ /_ _\ \| | ....
# |______| |______| ....thick
#
# width: the Width of the profile, it is squared
# length: the length of the bar, the extrusion
# thick: the thickness of the side
# slot: the width of the rail
# insquare: the width of the inner square
# indiam: the diameter of the inner hole. If 0, there is no hole
# axis 'x', 'y' or 'z'
# direction of the bar
# 'x' will be along the x axis
# 'y' will be along the y axis
# 'z' will be vertical
# cx: 1 if you want the coordinates referenced to the x center of the piece
# it can be done because it is a new shape formed from the union
# cy: 1 if you want the coordinates referenced to the y center of the piece
# cz: 1 if you want the coordinates referenced to the z center of the piece
# attributes:
# the arguments and
# ax_center : 1 if the profile is centered along its axis. 0 if not
# face : the face that has been extruded
# shp : the shape
# fco : the freecad object
# How rotation y movement works
# Initial
# Y
# :
# :
# _ : _
# |_|_:_|_|
# ........|.:.|........ X
# _|_:_|_
# |_| : |_|
# :
# :
# :
#
#
# Final axis = 'x' -> Rotation -90 on axis Y: pitch -90
# Z cx = 0
# : cy = 0 -> Translation Y: width/2
# : cz = 1 -> Translation Z: 0
# :
# :_ _
# |_|___|_|
# ..........:.|...|........ Y
# :_|___|_
# |_| |_|
# :
# :
# :
#
#
# Final axis = 'x' -> Rotation -90 on axis Y: pitch -90
# Z cx = 0
# : cy = 0 -> Translation Y: width/2
# : cz = 0 -> Translation Z: width/2
# :
# :_ _
# |_|___|_|
# : | |
# :_|___|_
# ..........|_|...|_|.......Y
# :
# :
# :
#
#
#
#
class AluProf(object):
def __init__(self, width, length, thick, slot, insquare,
indiam, axis='x',
name="genaluprof",
cx=False, cy=False, cz=False):
doc = FreeCAD.ActiveDocument
self.width = width
self.length = length
self.thick = thick
self.slot = slot
self.name = name
self.insquare = insquare
self.indiam = indiam
self.name = name
self.axis = axis
self.fcvec_axis = fcfun.getfcvecofname(axis)
self.cx = cx
self.cy = cy
self.cz = cz
fcvec_axis = self.fcvec_axis
# vectors of the points
vecpoints = fcfun.aluprof_vec(width, thick, slot, insquare)
doc.recompute()
# wire Face
wire_aluprof = fcfun.wire_sim_xy(vecpoints)
# if it is not centered on X, and the axis doesn't go along X
if cx == 0 and axis != 'x':
posx = width / 2.
else:
posx = 0
if cy == 0 and axis != 'y':
posy = width / 2.
else:
posy = 0
if cz == 0 and axis != 'z':
posz = width / 2.
else:
posz = 0
if axis == 'x':
ax_center = cx
elif axis == 'y':
ax_center = cy
elif axis == 'z':
ax_center = cz
else:
ax_center = 0
logger.error("axis %s not defined. Supported: 'x', 'y', 'z', axis")
self.ax_center = ax_center
doc.recompute()
pos = FreeCAD.Vector(posx, posy, posz) # Position
vec_axis = fcfun.getfcvecofname(axis)
face_ext = Part.Face(wire_aluprof)
# since vec2 of calc_rot is referenced to VNZ, vec_facenomal is negated
vec_naxis = DraftVecUtils.neg(vec_axis)