-
Notifications
You must be signed in to change notification settings - Fork 11
/
fc_clss.py
1452 lines (1228 loc) · 50.3 KB
/
fc_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
# ----------------------------------------------------------------------------
# -- Classes for FreeCAD pieces, parts, part sets
# ----------------------------------------------------------------------------
# -- (c) Felipe Machado
# -- Area of Electronic Technology. Rey Juan Carlos University (urjc.es)
# -- https://github.com/felipe-m/fcad-comps
# -- December-2017
# ----------------------------------------------------------------------------
# --- LGPL Licence
# ----------------------------------------------------------------------------
import os
import sys
import inspect
import logging
import math
import FreeCAD
import FreeCADGui
import Part
import DraftVecUtils
import Mesh
import MeshPart
# to get the current directory. Freecad has to be executed from the same
# directory this file is
filepath = os.getcwd()
# to get the components
# In FreeCAD can be added: Preferences->General->Macro->Macro path
sys.path.append(filepath)
# sys.path.append(filepath + '/' + 'comps')
sys.path.append(filepath + '/../../' + 'comps')
import kcomp # import material constants and other constants
import fcfun # import my functions for freecad. FreeCad Functions
import shp_clss
import kparts
from fcfun import V0, VX, VY, VZ, V0ROT
from fcfun import VXN, VYN, VZN
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Possible names: Single Part, Element, Piece
# Either:
# - have an attribute to indicate what kind of part is it, or
# - have subclasses to indicate the kind of part that it is
#
# Kind of possible parts are:
# 0: Not to print: It is just an outline, a rough/vague 3D model,
# for example bolts or bearings
# Some times the models don't have details, for example,
# bearings are just cylinders with a inner hole. Or bolts
# that don't have threads
# 0?: Not to print: It can be an exact model, like a washer, but it is not to
# print, tolerances are not inluced
# 1: Dimensional model: it can be printed to assemble a model, but the part
# will not work as defined. For example, optical cubes
# that they just have the dimensions, but they don't
# have the inner holes or the threads for the bolts
# 2: Printable model: it can be printed, but better to buy it,
# the printed part may work well.
# washer. You can print it, but better get it
# 3: To be printed: an object designed to be printed
# exact. Not to print because it has no tolerances
# with tolerances (to be printed)
# rough
class SinglePart(object):
"""
This is a 3D model that only has one part.
It can be either a part that forms a whole object with other parts,
or just a piece that can be used standalone
This is a Parent Class of all kind of objects
All the pieces have 3 axis that define their 3 main perpendicular directions
Depth, Width, Height
Attributes
------------
fco: FreeCAD Object
The freecad object of this part
place : FreeCAD Object
When this part is an object of another one (set). This is the vector to
place this part into the the set
place : FreeCAD.Vector
Position of the object
There is the parameter pos, where the piece is built and can be at
any position.
Once the piece is built, its placement (Placement.Base) is at V0:
FreeCAD.Vector(0,0,0), however it can be located at any place, since
pos could have been set anywhere.
The attribute place will move again the whole piece, including its parts
color : Tuple of 3 floats, each of them from 0. to 1.
They define the RGB colors.
0.: no color on that channel
1.: full intensity on that channel
"""
def __init__(self):
# bring the active document
self.doc = FreeCAD.ActiveDocument
# placement of the piece at V0, although pos can set it anywhere
self.place = V0 # check this and rel_place
# self.displacement = V0
self.rel_place = V0
self.extra_mov = V0
self.create_fco(self.name)
# self.tol = tol
# self.model_type = model_type
def get_parts(self):
""" returns an empty list, because it is a SinglePart.
Therefore has no parts
"""
return []
def set_color(self, color=(1., 1., 1.)):
""" Sets a new color for the piece
Parameters
-----------
color : tuple of 3 floats from 0. to 1.
RGB colors
"""
# just in case the value is 0 or 1, and it is an int
self.color = (float(color[0]), float(color[1]), float(color[2]))
self.fco.ViewObject.ShapeColor = self.color
def set_line_color(self, color=(1., 1., 1.)):
""" Sets a new color for the vertex lines of the piece
Parameters
-----------
color : tuple of 3 floats from 0. to 1.
RGB colors
"""
# just in case the value is 0 or 1, and it is an int
self.line_color = (float(color[0]), float(color[1]), float(color[2]))
self.fco.ViewObject.LineColor = self.line_color
def set_line_width(self, width=1.):
""" Sets the line width of the vertexes
Parameters
-----------
width : float from 0. to 1.
"""
# just in case the value is 0 or 1, and it is an int
self.line_width = float(width)
self.fco.ViewObject.LineWidth = self.line_width
def set_point_size(self, size=1.):
""" Sets the point size
Parameters
-----------
size : float
it seems it goes from 1. up to 64
"""
self.point_size = size
self.fco.ViewObject.PointSize = self.point_size
def set_name(self, name='', default_name='', change=0):
""" Sets the name attribute to the value of parameter name
if name is empty, it will take default_name.
if change == 1, it will change the self.name attribute to name,
default_name
if change == 0, if self.name is not empty, it will preserve it
Parameters
-----------
name : str
This is the name, but it can be empty.
default_name : str
This is the default_name, if not name
change : int
1: change the value of self.name
0: preserve the value of self.name if it exists
"""
# attribute name has not been created
if (not hasattr(self, 'name') or # attribute name has not been created
not self.name or # attribute name is empty
change == 1): # attribute has to be changed
if not name:
self.name = default_name
else:
self.name = name
def create_fco(self, name=''):
""" creates a FreeCAD object of the TopoShape in self.shp
Parameters
-----------
name : str
it is optional if there is a self.name
"""
if not name:
name = self.name
fco = fcfun.add_fcobj(self.shp, name, self.doc)
self.fco = fco
# -----
def place_fcos(self, displacement=V0):
""" Place the freecad objects
"""
# if type(place) is tuple:
# place = FreeCAD.Vector(place) # change to FreeCAD.Vector
tot_displ = (self.pos_o_adjust + displacement
+ self.rel_place + self.extra_mov)
self.tot_displ = tot_displ
self.fco.Placement.Base = tot_displ
def set_place(self, place=V0):
""" Sets a new placement for the piece
Parameters
-----------
place : FreeCAD.Vector
new position of the pieces
"""
if type(place) is tuple:
place = FreeCAD.Vector(place) # change to FreeCAD.Vector
if type(place) is FreeCAD.Vector:
self.fco.Placement.Base = place
self.place = place
# ----- Export to STL method
def export_stl(self, prefix="", name="", stl_path=""):
""" exports to stl the piece to print
Parameters
-----------
prefix : str
Prefix to the piece, may be useful if name is not given
and want to add a prefix to the self.name
an underscore will be added between prefix and name
name : str
Name of the piece, if not given, it will take self.name
stl_path : the path to save the stl files
"""
if not name:
filename = self.name
if prefix:
filename = prefix + '_' + filename
if not stl_path:
stl_filename = filename + '.stl'
else:
stl_filename = stl_path + filename + '.stl'
# I think this is a bug, before it may be called pos0, but
# now should be pos_o
pos_o = self.pos_o
rotation = FreeCAD.Rotation(self.prnt_ax, VZ)
shp = self.shp
# ----------- moving the shape doesn't work:
# I think that is because it is bound to a FreeCAD object
# shp.Placement.Base = self.pos.negative() + self.place.negative()
# shp.translate (self.pos.negative() + self.place.negative())
# shp.rotate (V0, rotation.Axis, math.degrees(rotation.Angle))
# shp.Placement.Base = self.place
# shp.Placement.Rotation = fcfun.V0ROT
# ----------- option 1. making a copy of the shape
# and then deleting it (nullify)
# shp_cpy = shp.copy()
# shp_cpy.translate (pos_o.negative() + self.place.negative())
# shp_cpy.rotate (V0, rotation.Axis, math.degrees(rotation.Angle))
# shp_cpy.exportStl(stl_path + filename + 'stl')
# shp_cpy.nullify()
# ----------- option 2. moving the freecad object
# place is no longer used, it should be rel_place or abs_place
self.fco.Placement.Base = pos_o.negative() + self.place.negative()
self.fco.Placement.Rotation = rotation
self.doc.recompute()
# exportStl is not working well with FreeCAD 0.17
# self.fco.Shape.exportStl(self.stl_path + filename + '.stl')
mesh_shp = MeshPart.meshFromShape(self.fco.Shape,
LinearDeflection=kparts.LIN_DEFL,
AngularDeflection=kparts.ANG_DEFL)
mesh_shp.write(stl_filename)
del mesh_shp
self.fco.Placement.Base = self.place
self.fco.Placement.Rotation = V0ROT
self.doc.recompute()
def save_fcad(self, prefix="", name=""):
""" Save the FreeCAD document, actually, it may not be a class method
only for the name
prefix : str
Prefix to the piece, may be useful if name is not given
and want to add a prefix to the self.name
an underscore will be added between prefix and name
name : str
Name of the piece, if not given, it will take self.name
"""
if not name:
filename = self.name
if prefix:
filename = prefix + '_' + filename
fcad_filename = self.fcad_path + name + '.FCStd'
print(fcad_filename)
self.doc.saveAs(fcad_filename)
# Possible names: Parts , Pieces, Elements,
# Group Ensemble, Set,
class PartsSet(shp_clss.Obj3D):
"""
This is a 3D model that has a set of parts (SinglePart or others)
All the pieces have 3 axis that define their 3 main perpendicular directions
Depth, Width, Height
place : FreeCAD.Vector
Position of the tensioner set.
There is the parameter pos, where the piece is built and can be at
any position.
Once the piece is built, its placement (Placement.Base) is at V0:
FreeCAD.Vector(0,0,0), however it can be located at any place, since
pos could have been set anywhere.
The attribute place will move again the whole piece, including its parts
color : Tuple of 3 elements, each of them from 0 to 1
They define the RGB colors.
0: no color on that channel
1: full intensity on that channel
"""
def __init__(self, axis_d, axis_w, axis_h):
# bring the active document
self.doc = FreeCAD.ActiveDocument
shp_clss.Obj3D.__init__(self, axis_d, axis_w, axis_h)
self.parts_lst = [] # list of all the parts (SinglePart, ...)
self.place = V0 # check these places, unify them
self.abs_place = V0
self.rel_place = V0
self.extra_mov = V0
self.displacement = V0
def append_part(self, part):
""" Appends a new part to the list of parts
"""
self.parts_lst.append(part)
def get_parts(self):
""" get a list of the parts,
"""
return self.parts_lst
def make_group(self):
self.fco = self.doc.addObject("Part::Compound", self.name)
list_fco = []
part_list = self.get_parts()
for part in part_list:
try:
fco_i = part.fco
except AttributeError:
logger.error('part is not a single part or compound')
else:
list_fco.append(fco_i)
self.fco.Links = list_fco
self.doc.recompute()
def get_abs_place(self):
""" gets the placement of the object, with any adjustment
So the shape has been created at pos, and this is any movement done
after this movement of the freecadobject
"""
return self.abs_place
def get_rel_place(self):
""" gets the placement of the object, with any adjustment
So the shape has been created at pos, and this is any movement done
after this movement of the freecadobject
"""
return self.rel_place
def set_part_place(self, child_part, vec_o_to_childpart=V0, add=0):
""" Modifies the attribute child_part.place, which defines the
displacement of the child_part respect to self.pos_o
Adds this displacement to the part's children
"""
rel_place = self.pos_o_adjust + vec_o_to_childpart
if add == 0:
child_part.rel_place = rel_place
else:
child_part.rel_place = child_part.rel_place + rel_place
# child_part.abs_place = self.get_abs_place() + child_part.rel_place
# try:
# child_part.fco.Placement.Base = child_part.abs_place
# except AttributeError: # only SimpleParts objects have fco,not PartsSet
# pass
# add this displacement to all the children
# part_list = child_part.get_parts()
# for grandchild_i in part_list:
# child_part.set_part_place(grandchild_i, add = 1)
def mov_place(self, child_part, vec_o_to_childpart=V0):
""" Modifies the attribute child_part.place, which defines the
displacement of the child_part respect to self.pos_o
Adds this displacement to the part's children
"""
displacement = (self.pos_o - self.pos) + vec_o_to_childpart + self.place
child_part.place = child_part.place + displacement
try:
child_part.fco.Placement.Base = child_part.place
except AttributeError: # only SimpleParts objects have fco, not PartsSet
pass
# add this displacement to all the children
part_list = child_part.get_parts()
for grandchild_i in part_list:
child_part.add_part_place(grandchild_i)
def set_color(self, color=(1., 1., 1.), part_i=0):
""" Sets a new color for the whole set of parts or for the selected
parts
Parameters
-----------
color : tuple of 3 floats from 0. to 1.
RGB colors
part_i : int
index of the part to change the color
0: all the parts
1... the index of the part
"""
# just in case the value is 0 or 1, and it is an int
color = (float(color[0]), float(color[1]), float(color[2]))
if part_i == 0:
self.color = color # only if all the parts have the same color
for part in self.parts_lst: # list of SinglePart objects
part.set_color(color)
else:
self.parts_lst[part_i - 1].set_color(color)
# -----
def place_fcos(self, displacement=V0):
""" Place the freecad objects
"""
# if type(place) is tuple:
# place = FreeCAD.Vector(place) # change to FreeCAD.Vector
# having pos_o_adjust and rel_place made the sum twice
# tot_displ = ( self.pos_o_adjust + displacement
tot_displ = (displacement
+ self.rel_place + self.extra_mov)
self.tot_displ = tot_displ
# if this set has been grouped, we don't have to go to its children
try:
self.fco.Placement.Base = tot_displ
except AttributeError:
# Not grouped: set the new position for every freecad object
for part in self.parts_lst:
part.place_fcos(tot_displ)
# ----- Export to STL method
def export_stl(self, part_i=0, prefix=""):
""" exports to stl the part or the parts to print
Save them in a STL file
Parameters
-----------
part_i : int
index of the part to print
0: all the printable parts
1... index of the part
prefix : str
Prefix to all the parts
"""
if part_i == 0: # export all the parts
for part in self.parts_lst:
part.export_stl(prefix=prefix)
else:
self.parts_lst[part_i - 1].export_stl(prefix=prefix)
def save_fcad(self, prefix="", name=""):
""" Save the FreeCAD document, actually, it may not be a class method
only for the name
prefix : str
Prefix to the name, may be useful if name is not given
and want to add a prefix to the self.name
an underscore will be added between prefix and name
name : str
Name of the part, if not given, it will take self.name
"""
if not name:
filename = self.name
if prefix:
filename = prefix + '_' + filename
fcad_filename = self.fcad_path + name + '.FCStd'
print(fcad_filename)
self.doc.saveAs(fcad_filename)
def set_name(self, name='', default_name='', change=0):
""" Sets the name attribute to the value of parameter name
if name is empty, it will take default_name.
if change == 1, it will change the self.name attribute to name,
default_name
if change == 0, if self.name is not empty, it will preserve it
Parameters:
-----------
name : str
This is the name, but it can be empty.
default_name : str
This is the default_name, if not name
change : int
1: change the value of self.name
0: preserve the value of self.name if it exists
"""
# attribute name has not been created
if (not hasattr(self, 'name') or # attribute name has not been created
not self.name or # attribute name is empty
change == 1): # attribute has to be changed
if not name:
self.name = default_name
else:
self.name = name
class Washer(SinglePart, shp_clss.ShpCylHole):
""" Washer, that is, a cylinder with a inner hole
Parameters
-----------
r_out : float
external (outside) radius
r_in : float
internal radius
h : float
height
axis_h : FreeCAD.Vector
vector along the cylinder height
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
tol : float
Tolerance for the inner and outer radius.
Being an outline, probably it is not to print, so by default: tol = 0
It is the tolerance for the diameter, so the radius will be added/subs
have of this tolerance
tol will be added to the inner radius (so it will be larger)
tol will be subtracted to the outer radius (so it will be smaller)
model_type : int
type of model:
exact, outline
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
Attributes
-----------
All the parameters and attributes of parent classes SinglePart ShpCylHole
metric : int or float (in case of M2.5) or even str for inches ?
Metric of the washer
"""
def __init__(self, r_out, r_in, h, axis_h, pos_h, tol=0, pos=V0,
model_type=0, # exact
name=''):
# sets the object name if not already set by a child class
if not hasattr(self, 'metric'):
self.metric = int(2 * r_in)
default_name = 'washer' + str(self.metric)
self.set_name(name, default_name, change=0)
tol_r = tol / 2.
# First the shape is created
shp_clss.ShpCylHole.__init__(self, r_out=r_out, r_in=r_in,
h=h, axis_h=axis_h,
pos_h=pos_h,
# inside tolerance is more
xtr_r_in=tol_r,
# outside tolerance is less
xtr_r_out=- tol_r,
pos=pos)
# Then the Part
SinglePart.__init__(self)
# 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 by CylHole
setattr(self, i, values[i])
class Din125Washer(Washer):
""" Din 125 Washer, this is the regular washer
Parameters
-----------
metric : int (maybe float: 2.5)
axis_h : FreeCAD.Vector
Vector along the cylinder height
pos_h : int
Location of pos along axis_h (0,1)
* 0: the cylinder pos is at its base
* 1: the cylinder pos is centered along its height
tol : float
Tolerance for the inner and outer radius.
It is the tolerance for the diameter, so the radius will be added/subs
have of this tolerance.
* tol will be added to the inner radius (so it will be larger).
* tol will be subtracted to the outer radius (so it will be smaller).
model_type : int
Type of model:
* 0: exact
* 1: outline
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
Note
----
All the parameters and attributes of parent class CylHole
Attributes
-----------
metric : int or float (in case of M2.5) or even str for inches ?
Metric of the washer
model_type : int
"""
def __init__(self, metric, axis_h, pos_h, tol=0, pos=V0,
model_type=0, # exact
name=''):
# sets the object name if not already set by a child class
self.metric = metric
default_name = 'din125_washer_m' + str(self.metric)
self.set_name(name, default_name, change=0)
washer_dict = kcomp.D125[metric]
Washer.__init__(self,
r_out=washer_dict['do'] / 2.,
r_in=washer_dict['di'] / 2.,
h=washer_dict['t'],
axis_h=axis_h,
pos_h=pos_h,
tol=tol, pos=pos,
model_type=model_type)
class Din9021Washer(Washer):
""" Din 9021 Washer, this is the larger washer
Parameters
-----------
metric : int (maybe float: 2.5)
axis_h : FreeCAD.Vector
Vector along the cylinder height
pos_h : int
Location of pos along axis_h (0,1)
* 0: the cylinder pos is at its base
* 1: the cylinder pos is centered along its height
tol : float
Tolerance for the inner and outer radius.
It is the tolerance for the diameter, so the radius will be added/subs
have of this tolerance
* tol will be added to the inner radius (so it will be larger)
* tol will be subtracted to the outer radius (so it will be smaller)
model_type : int
Type of model:
* 0: exact
* 1: outline
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
Note
----
All the parameters and attributes of parent class CylHole
Attributes
-----------
metric : int or float (in case of M2.5) or even str for inches ?
Metric of the washer
model_type : int
"""
def __init__(self, metric, axis_h, pos_h, tol=0, pos=V0,
model_type=0, # exact
name=''):
# sets the object name if not already set by a child class
self.metric = metric
default_name = 'din9021_washer_m' + str(self.metric)
self.set_name(name, default_name, change=0)
washer_dict = kcomp.D9021[metric]
Washer.__init__(self,
r_out=washer_dict['do'] / 2.,
r_in=washer_dict['di'] / 2.,
h=washer_dict['t'],
axis_h=axis_h,
pos_h=pos_h,
tol=tol, pos=pos,
model_type=model_type)
# doc = FreeCAD.newDocument()
# washer = Din125Washer( metric = 5,
# axis_h = VZ, pos_h = 1, tol = 0, pos = V0,
# model_type = 0, # exact
# name = '')
# wash = Din9021Washer( metric = 5,
# axis_h = VZ, pos_h = 1, tol = 0,
# pos = washer.pos + DraftVecUtils.scale(VZ,washer.h),
# model_type = 0, # exact
# name = '')
class BearingOutl(SinglePart, shp_clss.ShpCylHole):
""" Bearing outline , that is, a cylinder with a inner hole.
It does not include the balls and parts
Parameters
-----------
bearing_nb : int
Bearing number code
axis_h : FreeCAD.Vector
vector along the cylinder height
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
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 one radius (perpendicular to axis_h and axis_d)
it can be None
pos_d : int
location of pos along axis_d (0, 1)
0: pos is at the circumference 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 circumference 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)
tol : float
Tolerance for the inner and outer radius.
It is the tolerance for the diameter, so the radius will be added/subs
have of this tolerance
tol will be added to the inner radius (so it will be larger)
tol will be subtracted to the outer radius (so it will be smaller)
pos : FreeCAD.Vector
Position of the cylinder, taking into account where the center is
Attributes
-----------
All the parameters and attributes of parent classes SinglePart ShpCylHole
metric : int or float (in case of M2.5) or even str for inches ?
Metric of the washer
model_type : int
type of model:
1: outline (is not an exact model)
bearing_nb : int
number of the bearing, such as 624, 608, ... see kcomp.BEARING
bear_d : dictionary
dictionary with the dimensions of the bearing
"""
def __init__(self, bearing_nb, axis_h, pos_h,
axis_d=None, axis_w=None,
pos_d=0, pos_w=0, tol=0, pos=V0,
name=''):
self.model_type = 1 # outline
# sets the object name if not already set by a child class
default_name = 'bearing_' + str(bearing_nb)
self.set_name(name, default_name, change=0)
self.bearing_nb = bearing_nb
print(bearing_nb)
print(default_name)
try:
bear_d = kcomp.BEARING[bearing_nb]
self.bear_d = bear_d
except KeyError:
logger.error('Bearing key not found: ' + str(bearing_nb))
else: # no exception:
if tol == 0:
tol_r = 0
else:
tol_r = tol / 2. # just in case there is tolerance
# First the shape is created
shp_clss.ShpCylHole.__init__(self,
r_out=bear_d['do'] / 2.,
r_in=bear_d['di'] / 2.,
h=bear_d['t'],
axis_h=axis_h,
axis_d=axis_d,
axis_w=axis_w,
pos_d=pos_d,
pos_w=pos_w,
pos_h=pos_h,
# inside tolerance is more
xtr_r_in=tol_r,
# outside tolerance is less
xtr_r_out=- tol_r,
pos=pos)
# Then the Part
SinglePart.__init__(self)
# bear = BearingOutl( bearing_nb = 608,
# axis_h = VZN, pos_h = 1, tol = 0,
# pos = washer.pos + DraftVecUtils.scale(VZN,washer.h),
# name = '')
class Nut(SinglePart, shp_clss.ShpPrismHole):
"""
Creates a Nut, using shp_clss.ShpPrismHole
See comments of ShpPrismHole
Parameters
-----------
r_out : float
circumradius of the hexagon (side)
h : float
height of the cylinder
r_in : float
radius of the inner hole
axis_d_apo : int
0: default: axis_d points to the vertex
1: axis_d points to the center of a side
h_offset : float
0: default
Distance from the top, just to place the nut, see pos_h
if negative, from the bottom
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 circumference center (axis)
1: pos is at the inner circunsference, on axis_d, at r_in from the
circle center
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
pos_w : int
location of pos along axis_w (-2, -1, 0, 1, 2)
0: pos is at the circumference center
1: pos is at the inner circunsference, on axis_w, at r_in from the
circle center
2: pos is at the apothem, on axis_w
3: pos is at the outer circunsference, on axis_w, at r_out from the
center
pos : FreeCAD.Vector
Position of the prism, taking into account where the center is
"""
def __init__(self, r_out, h, r_in,
axis_d_apo=0, h_offset=0,
axis_h=VZ, axis_d=None, axis_w=None,
pos_h=0, pos_d=0, pos_w=0, pos=V0,
model_type=0, name=''):
# sets the object name if not already set by a child class
if not hasattr(self, 'metric'):
self.metric = int(2 * r_in)
default_name = 'nut_m' + str(self.metric)
self.set_name(name, default_name, change=0)
# First the shape is created
shp_clss.ShpPrismHole.__init__(self, n_sides=6,
r_out=r_out, h=h,
r_in=r_in,
axis_d_apo=axis_d_apo,
h_offset=h_offset,
axis_h=axis_h,
axis_d=axis_d,
axis_w=axis_w,
pos_h=pos_h,
pos_d=pos_d,
pos_w=pos_w,
pos=pos)
# Then the Part
SinglePart.__init__(self)
# 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 by CylHole
setattr(self, i, values[i])
# doc = FreeCAD.newDocument()
# nut = Nut ( r_out = 10,
# h = 4,
# r_in = 5,
# h_offset = 1,
# axis_d_apo = 0,
# axis_h = VZ, axis_d = VX, axis_w = VY,
# pos_h = 2, pos_d = 0, pos_w = 0,
# pos = V0)
class Din934Nut(Nut):
""" Din 934 Nut
Parameters
-----------
metric : int (maybe float: 2.5)
axis_h :
axis_d_apo : int
* 0: default: axis_d points to the vertex