-
Notifications
You must be signed in to change notification settings - Fork 5
/
freepyscad.py
2354 lines (2241 loc) · 79.1 KB
/
freepyscad.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
#########################################################
# This file is distributed against the GPL V3 licence #
# This file is made by supermerill (Remi Durand) #
#########################################################
######################################
# print("You can use something like 'from ", __name__," import *' for a more convenient experience")
#
# usage: scene().show( many statement )
# if you want to make a function, don't forget to return the root node!
# Each function like cube() or union() return an object (EasyNode)
# which store the know-how of how to build the object tree if needed.
# The scene will only redraw a node and these children if one of the
# child is changed.
#
#
# move() rotate() scale() color() return a different kind of object
# because they are not a mod but a modifier onto a node. They can
# be use almost like the other one, no need to worry.
#
# Color, multmatrix and scale can't be applied on a node so they are
# passed to the leafs.
#
# more info on the github wiki
######################################
import FreeCAD, Part, math, Draft, InvoluteGearFeature, importSVG, Mesh
import CompoundTools.CompoundFilter # freecad0.19
import os.path
from FreeCAD import Base
######### basic functions #########
def _getColorCode(str):
if(str == "red"): return (1.0,0.0,0.0,0.0)
if(str == "lime"): return (0.0,1.0,0.0,0.0)
if(str == "blue"): return (0.0,0.0,1.0,0.0)
if(str == "cyan"): return (0.0,1.0,1.0,0.0)
if(str == "aqua"): return (0.0,1.0,1.0,0.0)
if(str == "fuchsia"): return (1.0,0.0,1.0,0.0)
if(str == "yellow"): return (1.0,1.0,0.0,0.0)
if(str == "black"): return (0.0,0.0,0.0,0.0)
if(str == "black"): return (0.0,0.0,0.0,0.0)
if(str == "gray"): return (0.5,0.5,0.5,0.0)
if(str == "silver"): return (.75,.75,.75,0.0)
if(str == "white"): return ( 1, 1, 1,0.0)
if(str == "maroon"): return (0.5,0.0,0.0,0.0)
if(str == "green"): return (0.0,0.5,0.0,0.0)
if(str == "navy"): return (0.0,0.0,0.5,0.0)
if(str == "teal"): return (0.0,0.5,0.5,0.0)
if(str == "purple"): return (0.5,0.0,0.5,0.0)
if(str == "olive"): return (0.5,0.5,0.0,0.0)
return (0.5,0.5,0.5,0.5)
def _getTuple2(a,b,default_first_item):
if(isinstance(a, list)):
if(len(a)==0):
a=default_first_item
elif(len(a)==1):
a=a[0]
elif(len(a)==2):
b=a[1]
a=a[0]
return (a,b)
def _getTuple2Abs(a,b,default_first_item):
(a,b) = _getTuple2(a,b,default_first_item)
return (abs(a), abs(b))
def _getTuple3(a,b,c,default_first_item):
if(isinstance(a, list)):
if(len(a)==0):
a=default_first_item
elif(len(a)==1):
a=a[0]
elif(len(a)==2):
b=a[1]
a=a[0]
elif(len(a)==3):
c=a[2]
b=a[1]
a=a[0]
return (a,b,c)
def _getTuple3Abs(a,b,c,default_first_item):
(a,b,c) = _getTuple3(a,b,c,default_first_item)
return (abs(a),abs(b),abs(c))
def _getTuple4(a,b,c,d,default_first_item):
if(isinstance(a, list)):
if(len(a)==0):
a=default_first_item
elif(len(a)==1):
a=a[0]
elif(len(a)==2):
b=a[1]
a=a[0]
elif(len(a)==3):
c=a[2]
b=a[1]
a=a[0]
elif(len(a)==4):
d=a[3]
c=a[2]
b=a[1]
a=a[0]
return (a,b,c,d)
def _getTuple4Abs(a,b,c,d,default_first_item):
(a,b,c,d) = _getTuple4(a,b,c,d,default_first_item)
return (abs(a),abs(b),abs(c),abs(d))
def _moy_vect(points=[]):
moy = [0.0,0.0,0.0]
nb=0
for p in points:
moy[0]+=p[0]
moy[1]+=p[1]
moy[2]+=p[2]
nb+=1
moy[0]/=nb
moy[1]/=nb
moy[2]/=nb
return moy
######### scene class #########
if(not 'tree_storage' in globals()):
global tree_storage
tree_storage = {}
class Scene:
def __init__(self):
doc = FreeCAD.ActiveDocument
#when draw() don't refresh correctly
def redraw(self, *tupleOfNodes):
for obj in FreeCAD.ActiveDocument.Objects :
FreeCAD.ActiveDocument.removeObject(obj.Name)
for node in tupleOfNodes:
node.create()
global tree_storage
tree_storage[FreeCAD.ActiveDocument] = tupleOfNodes
FreeCAD.ActiveDocument.recompute()
def draw(self, *tupleOfNodes):
global tree_storage
try:
if(not FreeCAD.ActiveDocument in tree_storage):
tree_storage[FreeCAD.ActiveDocument] = []
keep_nodes = []
i=0
doc = FreeCAD.ActiveDocument
newArrayOfNodes = []
# This algo can't support a shuffle (or insert) of new object in the root.
# But it works
for node_idx in range(len(tupleOfNodes)):
if(len(tree_storage[FreeCAD.ActiveDocument])<=node_idx):
newArrayOfNodes.append(tupleOfNodes[node_idx].create())
else:
newArrayOfNodes.append(tupleOfNodes[node_idx].check(doc, tree_storage[FreeCAD.ActiveDocument][node_idx]))
node_idx+=1
if(len(tree_storage[FreeCAD.ActiveDocument])>len(tupleOfNodes)):
for node_idx in range(len(tupleOfNodes), len(tree_storage[FreeCAD.ActiveDocument])):
tree_storage[FreeCAD.ActiveDocument][node_idx].destroyObj(doc)
except:
print("Unexpected error:")
# if True:
tree_storage[FreeCAD.ActiveDocument] = []
for obj in doc.Objects :
doc.removeObject(obj.Name)
FreeCAD.ActiveDocument.recompute()
raise
tree_storage[FreeCAD.ActiveDocument] = tuple(newArrayOfNodes)
FreeCAD.ActiveDocument.recompute()
return self;
def __call__(self, *tupleOfNodes):
return self.draw(*tupleOfNodes)
def scene():
return Scene()
######### Basic class a utilities #########
_idx_EasyNode = 0
_default_size = 1
class EasyNode:
def __init__(self):
self.childs = []
self.actions = []
self.actions_after = []
self.simple = True
def addAction(self, method, args):
self.actions.append((method,args))
def __hash__(self):
if(hasattr(self, '_hash')):
if(self._hash):
return 0
else:
self._hash = True
else:
self._hash = True
hashval = 0
i=0
for action in self.actions:
# print("action="+str(action))
hashval = hashval + (hash(action[0].__name__))
i+=517
for arg in action[1]:
if(isinstance(arg, EasyNode) or isinstance(arg, tuple) or isinstance(arg, list)):
temp = 0
else:
temp = (hash(arg)*i)
hashval = (hashval+temp) if temp<10000 else hashval ^ temp
i+=317
return hashval
def printhash(self):
hashval = 0
i=0
for action in self.actions:
hashval = hashval + (hash(action[0].__name__))
print("hash of act "+action[0].__name__+" = ",str(hash(action[0].__name__))+" => "+str(hashval))
i+=517
for arg in action[1]:
print(isinstance(arg,EasyNode))
for arg in action[1]:
# print("arg : "+str(arg)+" "+str( isinstance(arg, EasyNode)))
# try:
# print(dir(arg))
# except:
# print("not a class")
if(isinstance(arg, EasyNode) or isinstance(arg, tuple)):
temp = 0
print("arg passed")
else:
temp = (hash(arg)*i)
hashval = (hashval+temp) if temp<10000 else hashval ^ temp
print("hash of arg "+str(arg)+" = "+str(temp*i)+" => "+str(hashval))
i+=317
print("final => "+str(hashval))
return hashval
def hashWhithChilds(self):
hashval = hash(self)
i=0;
for child in self.childs:
hashval = hashval ^ (child.hashWhithChilds()*i)
i+=317
return hashval
def create(self):
for action in self.actions :
action[0](*action[1])
for action in self.actions_after :
action[0](*action[1])
return self
def destroyObj(self, doc):
if(hasattr(self,'obj')):
print("destroy "+self.obj.Name)
doc.removeObject(self.obj.Name)
for child in self.childs:
child.destroyObj(doc)
# return the node to use
def check(self, doc, clone):
print("check "+self.name)
ok = True
if(self.simple):
#must be done before, to update things?
hash(self)
hash(clone)
if(hash(self) == hash(clone) and len(self.childs) == len(clone.childs)):
i=0
for item in self.childs:
print(hash(self.childs[i])," == ",hash(clone.childs[i]))
ok = ok and hash(self.childs[i]) == hash(clone.childs[i])
# if(not hash(self.childs[i]) == hash(clone.childs[i])):
# self.childs[i].printhash()
# clone.childs[i].printhash();
i+=1
print("simple, same hash and childs are ",ok)
# print(hash(self)," == ",hash(clone))
# self.printhash()
# clone.printhash();
else:
ok = False
print("simple but hash: ",hash(self)," != ",hash(clone),hash(self)==hash(clone)," or ",len(self.childs)," != ",len(clone.childs),len(self.childs)==len(clone.childs))
else:
ok = (self.hashWhithChilds() == clone.hashWhithChilds())
print("it's complicated ",ok)
if(ok):
print("keep "+clone.name+" and ditch "+self.name)
#ok, do not modify it but check the childs
self.obj = clone.obj
newNodes = []
i=0
ok = True
for child in self.childs:
good = self.childs[i].check(doc, clone.childs[i])
print("use the new one in the old coll? "+str(good == self.childs[i])+" ( "+str(good)+" =?= "+str(self.childs[i]))
if(not good == clone.childs[i]):
clone.childs[i] = good
ok = False
i+=1
if(not ok):
print("reflow "+clone.name)
clone.clear_childs().layout_childs(*tuple(clone.childs))
return clone
else:
print("recreate "+self.name)
# redo everything
clone.destroyObj(doc)
self.create()
return self
def add(self, *tupleOfNodes):
if(len(tupleOfNodes)==1 and (isinstance(tupleOfNodes[0], list) or isinstance(tupleOfNodes[0], tuple))):
tupleOfNodes = tuple(tupleOfNodes[0])
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
self.childs.append(enode)
else:
print("error, trying to add '"+str(enode)+"' into a union of EsayNode")
self.actions.append((self.create_childs,tupleOfNodes))
self.actions.append((self.layout_childs,tupleOfNodes))
return self
def __call__(self, *tupleOfNodes):
return self.add(*tupleOfNodes)
def create_childs(self, *tupleOfNodes):
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
enode.create()
def layout_childs(self, *tupleOfNodes):
arrayShape = self.obj.Shapes
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
arrayShape.append(enode.obj)
if enode.obj.ViewObject is not None:
enode.obj.ViewObject.Visibility = False
else:
print("error, trying to layout '"+str(enode)+"' into a union of EsayNode")
self.obj.Shapes = arrayShape
return self
def clear_childs(self, *tupleOfNodes):
self.obj.Shapes = []
return self
# def printmyargs(self, *args):
# print(args)
def translate(self, x=0,y=0,z=0):
return move(x,y,z)
def move(self, x=0,y=0,z=0):
self.actions.append((self.move_action,(x,y,z)))
return self
def move_action(self, x=0,y=0,z=0):
(x,y,z) = _getTuple3(x,y,z,0)
self.obj.Placement.move(Base.Vector(x, y, z))
return self
def rotate(self, x=0,y=0,z=0):
self.actions.append((self.rotate_action,(x,y,z)))
return self
def rotate_action(self, x=0,y=0,z=0):
(x,y,z) = _getTuple3(x,y,z,0)
myMat = Base.Matrix()
myMat.rotateX(x*math.pi/180)
myMat.rotateY(y*math.pi/180)
myMat.rotateZ(z*math.pi/180)
self.obj.Placement=FreeCAD.Placement(myMat).multiply(self.obj.Placement)
return self
def scale(self, x=0,y=0,z=0,_instantly=False):
self.actions_after.append((self.scale_action,(x,y,z)))
self.simple = False
return self
def scale_action(self, x=0,y=0,z=0):
for node in self.childs:
node.scale(x,y,z,_instantly=True)
return self
def multmatrix(self, mat):
self.actions_after.append((self.multmatrix_action,(mat,)))
self.simple = False
return self
def multmatrix_action(self, mat):
for node in self.childs:
node.multmatrix(mat)
return self
def color(self, r=0.0,v=0.0,b=0,a=0.0,_instantly=False):
self.actions_after.append((self.color_action,(r,v,b,a)))
self.simple= False
return self
def color_action(self, r=0.0,v=0.0,b=0,a=0.0):
for node in self.childs:
node.color(r,v,b,a,_instantly=True)
self.simple = False
return self
def x(self):
self.actions.append((self.xyz_action,(1,0,0)))
return self
def y(self):
self.actions.append((self.xyz_action,(0,1,0)))
return self
def z(self):
self.actions.append((self.xyz_action,(0,0,1)))
return self
def xy(self):
self.actions.append((self.xyz_action,(1,1,0)))
return self
def xz(self):
self.actions.append((self.xyz_action,(1,0,1)))
return self
def yz(self):
self.actions.append((self.xyz_action,(0,1,1)))
return self
def xyz(self):
self.actions.append((self.xyz_action,(1,1,1)))
return self
def xyz_action(self,x,y,z):
self.move_action(-self.centerx*x,-self.centery*y,-self.centerz*z)
return self
def center(self):
self.actions.append((self.center_action,(1,1,1)))
return self
def center_action(self,x,y,z):
self.move_action(-self.centerx if self.centerx>0 else 0.0,-self.centery if self.centery>0 else 0.0,-self.centerz if self.centerz>0 else 0.0)
return self
def _easyNodeStub(obj, name):
global _idx_EasyNode
node = EasyNode()
_idx_EasyNode += 1
node.name = name+"_"+str(_idx_EasyNode)
node.obj = obj
return node
def useCenter(node, center):
if(center != None and callable(center)):
center(node)
elif(center != None and isinstance(center, bool) and center):
node.center()
class EasyNodeColored(EasyNode):
def color(self, r=0.0,v=0.0,b=0.0,a=0.0,_instantly=False):
if isinstance(r, str):
if(a!=0.0):
colorcode = _getColorCode(r)
colorcode[3] = a
if(_instantly):
self.color_action(colorcode)
else:
self.actions.append((self.color_action,(colorcode,)))
else:
if(_instantly):
self.color_action(_getColorCode(r))
else:
self.actions.append((self.color_action,(_getColorCode(r),)))
else:
#fixme: try to see in an array is not hidden inside the r
(r,v,b,a) = _getTuple4Abs(r,v,b,a,0.0)
if(_instantly):
self.color_action((max(0.0,min(r,1.0)),max(0.0,min(v,1.0)),max(0.0,min(b,1.0)),max(0.0,min(a,1.0))))
else:
self.actions.append((self.color_action,((max(0.0,min(r,1.0)),max(0.0,min(v,1.0)),max(0.0,min(b,1.0)),max(0.0,min(a,1.0))),)))
return self
def color_action(self, colorcode):
if self.obj.ViewObject is not None:
self.obj.ViewObject.DiffuseColor = [colorcode]
return self
def magic_color(node, r=0.0,v=0.0,b=0.0,a=0.0):
node.color(r,v,b,a)
return node
class EasyNodeLeaf(EasyNodeColored):
# def __init__(self):
# self.childs = []
# self.actions = []
# self.actions_after = []
# self.simple = True
# self.centerx=0.0
# self.centery=0.0
# self.centerz=0.0
def scale(self, x=0.0,y=0.0,z=0,_instantly=False):
if(_instantly):
self.scale_action(x,y,z)
else:
self.actions.append((self.scale_action,(x,y,z)))
return self
def scale_action(self, x,y,z):
(x,y,z) = _getTuple3(x,y,z,0.0)
myMat = Base.Matrix()
myMat.scale(x,y,z)
self.obj.Shape = self.obj.Shape.transformGeometry(myMat)
return self
def multmatrix_action(self, mat):
flatarray = []
for array in mat:
for num in array:
flatarray.append(num)
if matsize != len(flatarray):
print("error, wrong matrix")
else:
myMat = Base.Matrix()
myMat.A = flatarray
self.obj.Shape = self.obj.Shape.transformGeometry(myMat)
return self
# def import_)
######### transformations #########
class EasyNodeUnion(EasyNode):
is2D = False
def layout_childs(self, *tupleOfNodes):
if(self.is2D):
arrayobj= []
for node in self.childs:
arrayobj.append(node.obj)
newObj = Draft.upgrade(arrayobj,delete=True)[0][0]
# for()
newObj.Placement = Base.Placement(self.obj.Placement)
FreeCAD.ActiveDocument.removeObject(self.obj.Name)
self.obj = newObj
else:
arrayShape = self.obj.Shapes
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
arrayShape.append(enode.obj)
if enode.obj.ViewObject is not None:
enode.obj.ViewObject.Visibility = False
else:
print("error, trying to layout '"+str(enode)+"' into a union of EsayNode")
self.obj.Shapes = arrayShape
return self
def union(name=None):
global _idx_EasyNode
node = EasyNodeUnion()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "union_"+str(_idx_EasyNode)
else:
node.name = name
def createUnion(node,name):
if(len(node.childs)>0 and isinstance(node.childs[0], EasyNode2D)):
node.is2D = True
#Draft.upgrade(*tuple(node.childs))
node.obj = FreeCAD.ActiveDocument.addObject("Part::MultiFuse", node.name)
node.addAction(createUnion, (node,name))
return node
def inter(name=None):
global _idx_EasyNode
node = EasyNode()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "inter_"+str(_idx_EasyNode)
else:
node.name = name
def createInter(node,name):
node.obj = FreeCAD.ActiveDocument.addObject("Part::MultiCommon", node.name)
node.addAction(createInter, (node,name))
return node
def intersection(name=None):
return inter(name)
class EasyNodeDiff(EasyNode):
def __init__(self):
self.my_union = None
self.unionChilds = []
self.childs = []
self.actions = []
self.actions_after = []
self.simple = True
def add(self, *tupleOfNodes):
if(len(tupleOfNodes)==1 and (isinstance(tupleOfNodes[0], list) or isinstance(tupleOfNodes[0], tuple))):
tupleOfNodes = tuple(tupleOfNodes[0])
good_array = []
minidx = 0
newunion = False
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
good_array.append(enode)
if(len(good_array)==0):
return self
if(len(self.childs)==2 and len(self.childs[1].childs)>1):
# simple case: give them to the union
for enode in good_array :
self.childs.append(enode)
self.unionChilds.append(enode)
elif( (len(self.childs)<2 or len(self.childs[1].childs)==0) and len(self.childs)+len(good_array)>2):
# create the union
self.my_union = union()
newunion = True
self.unionChilds = []
temp = None
if(len(self.childs)==0):
self.childs.append(good_array[minidx])
minidx+=1
if(len(self.childs)==1):
self.childs.append(self.my_union)
else:
self.unionChilds.append(self.childs[1])
self.childs[1] = self.my_union
self.childs.append(self.unionChilds[0])
for enode in good_array[minidx:] :
self.childs.append(enode)
self.unionChilds.append(enode)
else:
# just use them
if(len(self.childs)==0 and len(good_array)>0):
self.childs.append(good_array[minidx])
minidx+=1
if(len(self.childs)==1 and len(good_array)>0):
self.childs.append(good_array[minidx])
minidx+=1
if(newunion):
self.actions.append((self.create_childs,(self.my_union,)+tuple(good_array)))
else:
self.actions.append((self.create_childs,tuple(good_array)))
self.actions.append((self.layout_childs,tuple(good_array)))
return self
def layout_childs(self, *tupleOfNodes):
if(self.obj.Base == None and len(tupleOfNodes)>0):
self.obj.Base = tupleOfNodes[0].obj
if tupleOfNodes[0].obj.ViewObject is not None:
tupleOfNodes[0].obj.ViewObject.Visibility = False
tupleOfNodes = tupleOfNodes[1:]
if(self.my_union == None and self.obj.Tool == None and len(tupleOfNodes)>0):
self.obj.Tool = tupleOfNodes[0].obj
if tupleOfNodes[0].obj.ViewObject is not None:
tupleOfNodes[0].obj.ViewObject.Visibility = False
tupleOfNodes = tupleOfNodes[1:]
if(self.my_union != None and len(tupleOfNodes)>0):
self.obj.Tool = self.my_union.obj
self.my_union.layout_childs(*tupleOfNodes)
return self
def cut(name=None):
global _idx_EasyNode
node = EasyNodeDiff()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "cut_"+str(_idx_EasyNode)
else:
node.name = name
def createCut(node,name):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Cut", node.name)
node.obj.Base = None
node.obj.Tool = None
node.addAction(createCut, (node,name))
return node
def difference(name=None):
return cut(name)
# FIXME: The color doesn't apply to the chamfered/fillet section
class EasyNodeChamfer(EasyNode):
def __init__(self):
self.edges = [];
self.childs = []
self.actions = []
self.actions_after = []
self.simple = True
self.main_length = 0 #used as default if not provided
def layout_childs(self, *tupleOfNodes):
if(len(tupleOfNodes)>0):
if len(self.edges) == 0:
self.addAllEdges()
self.obj.Base = tupleOfNodes[0].obj
if self.obj.Base.ViewObject is not None:
self.obj.Base.ViewObject.Visibility = False
self.obj.Edges = self.edges
return self
def set(self, node):
self.add(node)
return self
def addEdge(self, length, *arrayOfEdges):
if isinstance(length, list):
if len(length) == 2:
self.addEdge2(length[0],length[1],*arrayOfEdges)
else:
self.addEdge2(length[0],length[0],*arrayOfEdges)
else:
self.addEdge2(length,length,*arrayOfEdges)
return self
def addEdge2(self, startLength, endLength, *arrayOfEdges):
for eidx in arrayOfEdges :
self.edges.append((eidx, startLength, endLength))
# self.obj.Edges = self.edges
return self
def setNb(self, length, nb, node):
self.set(node)
for eidx in range(1, nb+1) :
self.edges.append((eidx, length, length))
# self.obj.Edges = self.edges
return self
def addAllEdges(self):
if(len(self.childs) > 1):
print("Error, can only chamfer/fillet one object at a time")
return
if(len(self.childs) == 0):
return
eidx = 0
print("addAllEdges "+self.childs[0].obj.Name+" : " + str(len(self.childs[0].obj.Shape.Edges)))
for edge in self.childs[0].obj.Shape.Edges:
eidx += 1
self.edges.append((eidx, self.main_length, self.main_length))
def chamfer(name=None, l=_default_size/4):
global _idx_EasyNode
node = EasyNodeChamfer()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "chamfer_"+str(_idx_EasyNode)
else:
node.name = name
node.main_length = l
if(isinstance(name,(int, float))):
node.main_length = name
def createChamfer(node):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Chamfer", node.name)
node.addAction(createChamfer, (node,))
return node
def fillet(name=None, l=_default_size/4):
global _idx_EasyNode
node = EasyNodeChamfer()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "fillet_"+str(_idx_EasyNode)
else:
node.name = name
node.main_length = l
if(isinstance(name,(int, float))):
node.main_length = name
def createFillet(node):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Fillet", node.name)
node.addAction(createFillet, (node,))
return node
class EasyNodeMirror(EasyNodeColored):
def __init__(self):
self.edges = [];
self.childs = []
self.actions = []
self.actions_after = []
self.simple = True
def layout_childs(self, *tupleOfNodes):
if(len(tupleOfNodes)>0):
self.obj.Source = tupleOfNodes[0].obj
if self.obj.Source.ViewObject is not None:
self.obj.Source.ViewObject.Visibility = False
return self
def mirror(x=0.0,y=0.0,z=0.0,name=None):
(x,y,z) = _getTuple3(x,y,z,0.0)
global _idx_EasyNode
node = EasyNodeMirror()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "mirror_"+str(_idx_EasyNode)
else:
node.name = name
def createMirror(node,x,y,z):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Mirroring", node.name)
node.obj.Base = Base.Vector(0,0,0)
node.obj.Normal = Base.Vector(x,y,z).normalize()
node.addAction(createMirror, (node,x,y,z))
return node
def offset(length=_default_size,fillet=True,fusion=True,name=None):
global _idx_EasyNode
node = EasyNodeMirror()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "offset_"+str(_idx_EasyNode)
else:
node.name = name
def createOffset(node, length, fillet, fusion):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Offset", node.name)
node.obj.Value = length
node.obj.Mode = 0
node.obj.Join = 0 if(fillet) else 2
node.obj.Intersection = fusion
node.obj.SelfIntersection = False
node.addAction(createOffset, (node, length, fillet, fusion))
return node
def offset2D(length=_default_size,fillet=True,fusion=True,name=None):
global _idx_EasyNode
node = EasyNodeMirror()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "offset2D_"+str(_idx_EasyNode)
else:
node.name = name
def createOffset2D(node, length, fillet, fusion):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Offset2D", node.name)
node.obj.Value = length
node.obj.Mode = 0
node.obj.Join = 0 if(fillet) else 2
node.obj.Intersection = fusion
node.obj.SelfIntersection = False
node.addAction(createOffset2D, (node, length, fillet, fusion))
return node
# group: can't be modified, it's just for storage for cleaning the hierarchy
# move & rotate are passed to the childs.
#note: even less tested than the rest
class EasyNodeGroup(EasyNode):
def layout_childs(self, *tupleOfNodes):
arrayShape = self.obj.Group
for enode in tupleOfNodes :
if(isinstance(enode, EasyNode)):
arrayShape.append(enode.obj)
if enode.obj.ViewObject is not None:
enode.obj.ViewObject.Visibility = True
else:
print("error, trying to layout '"+str(enode)+"' into a union of EsayNode")
self.obj.Group = arrayShape
def move_action(self, x=0,y=0,z=0):
print("moveaction group:",x, y, z)
for enode in self.childs :
if(isinstance(enode, EasyNode)):
print(enode.name+" moveaction")
enode.move_action(x, y, z)
return self
def rotate_action(self, x=0,y=0,z=0):
for enode in self.childs :
if(isinstance(enode, EasyNode)):
enode.rotate_action(x, y, z)
return self
def group(name=None):
global _idx_EasyNode
node = EasyNodeGroup()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "group_"+str(_idx_EasyNode)
else:
node.name = name
def createGroup(node,name):
node.obj = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", node.name)
node.addAction(createGroup, (node,name))
return node
######### 3D objects #########
def cube(size=_default_size,y=0.0,z=0.0,center=None,x=0.0, name=None):
(size,y,z) = _getTuple3Abs(size,y,z,0.0)
if(x==0.0 and size != 0.0):
x = size
if(y==0.0 and z==0.0):
return box(x,x,x,center, name)
else:
return box(x,y if y!=0.0 else _default_size, z if z!=0.0 else _default_size,center, name)
def box(x=_default_size,y=_default_size,z=_default_size,center=None, name=None):
(x,y,z) = _getTuple3Abs(x,y,z,_default_size)
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "cube_"+str(_idx_EasyNode)
else:
node.name = name
def createBox(node, x,y,z,center):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Feature", node.name)
node.obj.Shape = Part.makeBox(x, y, z)
node.centerx = x/2.0
node.centery = y/2.0
node.centerz = z/2.0
useCenter(node, center)
node.addAction(createBox, (node, x,y,z,center))
return node
def triangle(x=_default_size,y=_default_size,z=_default_size,center=None, name=None):
(x,y,z) = _getTuple3Abs(x,y,z,_default_size)
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "triangle_"+str(_idx_EasyNode)
else:
node.name = name
def createTriRect(node, x,y,z,center):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Feature", node.name)
poly = Part.makePolygon([Base.Vector(0,0,0),Base.Vector(x,0,0),Base.Vector(0,y,0),Base.Vector(0,0,0)])
node.obj.Shape = Part.Face(poly).extrude(Base.Vector(0,0,z))
node.centerx = x/2.0
node.centery = y/2.0
node.centerz = z/2.0
useCenter(node, center)
node.addAction(createTriRect, (node, x,y,z,center))
return node
def cylinder(r=_default_size,h=_default_size,center=None,d=0.0,r1=0.0,r2=0.0,d1=0.0,d2=0.0,angle=360.0,fn=1,name=None):
r = abs(r)
h = abs(h)
d = abs(d)
if( (r == _default_size or r == 0) and d != 0.0):
r = d/2.0
if( d1!=0.0 and d1==d2 ):
r = d1/2.0
d1 = d2 = 0
if( r1!=0.0 and r1==r2 ):
r = r1
r1 = r2 = 0
if( (r == _default_size or r == 0) and (r1!=0.0 or r2!=0.0 or d1!=0.0 or d2!=0.0)):
return cone(r1,r2,h,center,d1,d2,fn)
if(fn>2):
return poly_ext(r=_default_size, nb=fn, h=h,center=center,d=0.0,name=name)
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "cylinder_"+str(_idx_EasyNode)
else:
node.name = name
def createCylinder(node, r,h,center,angle):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Feature", node.name)
if(int(angle) == 360):
node.obj.Shape = Part.makeCylinder(r,h)
else:
node.obj.Shape = Part.makeCylinder(r,h,Base.Vector(0,0,0),Base.Vector(0,0,1),angle)
node.centerx = -r
node.centery = -r
node.centerz = h/2.0
useCenter(node, center)
node.addAction(createCylinder, (node, r,h,center,angle))
return node
def cone(r1=_default_size*2,r2=_default_size,h=_default_size,center=None,d1=0.0,d2=0.0,fn=1,name=None):
r1 = abs(r1)
r2 = abs(r2)
h = abs(h)
d1 = abs(d1)
if( (r1 == _default_size*2 or r1 == 0) and d1 != 0.0):
r1 = d1/2.0
d2 = abs(d2)
if( (r2 == _default_size or r2 == 0) and d2 != 0.0):
r2 = d2/2.0
if(fn>2):
#compute extrusion angle FIXME: cone(r1=3,r2=1,h=4,fn=4) it amlost a pyramid!
angle = 0.0
if(r1>r2):
angle = -math.atan(float(r1-r2)/float(h))
else:
angle = math.atan(float(r2-r1)/float(h))
#print(str(r1)+" "+str(fn)+" "+str(angle))
return linear_extrude(height=h,taper=angle)(poly_reg(r1,fn,center=center))
# return poly_reg(r1,fn,center=center)
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "cone_"+str(_idx_EasyNode)
else:
node.name = name
def createCone(node, r1,r2,h,center):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Feature", node.name)
node.obj.Shape = Part.makeCone(r1,r2,h)
node.centerx = -r1 if r1>r2 else -r2
node.centery = -r1 if r1>r2 else -r2
node.centerz = h/2.0
useCenter(node, center)
node.addAction(createCone, (node, r1,r2,h,center))
return node
def sphere(r=_default_size,center=None,d=0.0,fn=1,name=None):
r = abs(r)
d = abs(d)
if( (r == _default_size or r == 0.0) and d != 0.0):
r = d/2.0
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "sphere_"+str(_idx_EasyNode)
else:
node.name = name
def createSphere(node, r,center):
node.obj = FreeCAD.ActiveDocument.addObject("Part::Feature", node.name)
node.obj.Shape = Part.makeSphere(r)
node.centerx = -r
node.centery = -r
node.centerz = -r
useCenter(node, center)
node.addAction(createSphere, (node, r,center))
return node
def torus(r1=_default_size, r2=0.1,center=None,d1=0.0,d2=0.0,name=None):
r1 = abs(r1)
r2 = abs(r2)
d1 = abs(d1)
if( (r1 == _default_size or r1 == 0.0) and d1 != 0.0):
r1 = d1/2.0
d2 = abs(d2)
if( (r2 == _default_size or r2 == 0.0) and d2 != 0.0):
r2 = d2/2.0
global _idx_EasyNode
node = EasyNodeLeaf()
_idx_EasyNode += 1
if(name == None or not isinstance(name, str)):
node.name = "torus_"+str(_idx_EasyNode)
else:
node.name = name
def createTorus(node, r1,r2,center):