-
Notifications
You must be signed in to change notification settings - Fork 5
/
DefeaturingTools.py
2523 lines (2378 loc) · 908 KB
/
DefeaturingTools.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#****************************************************************************
#* *
#* Copyright (c) 2017 *
#* Maurice [email protected] *
#* *
# *
# Repair Defeaturing Macro *
# *
# (C) Maurice easyw-fc 2018 *
# This program is free software; you can redistribute it and/or modify *
# it under the terms of the GNU Library General Public License (LGPL) *
# as published by the Free Software Foundation; either version 2 of *
# the License, or (at your option) any later version. *
# for detail see the LICENCE text file. *
#****************************************************************************
import FreeCAD, FreeCADGui, Draft, Part
import re, os, sys
import OpenSCADCommands, OpenSCAD2Dgeom, OpenSCADFeatures
from PySide import QtCore, QtGui
import tempfile
try:
from PathScripts.PathUtils import horizontalEdgeLoop
from PathScripts.PathUtils import horizontalFaceLoop
from PathScripts.PathUtils import loopdetect
except:
FreeCAD.Console.PrintError('Path WB not found\n')
#int(re.search(r'\d+', string1).group())
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
global force_recompute, invert
__version__ = "v1.3.6"
## shape.sewShape(), shape.isClosed(), shape.isValid()
## shape.getTolerance(0), shape.fixTolerance(1e-4)
## shape.fixTolerance(1.e-4), shape.check(True)
from sys import platform as _platform
# window GUI dimensions parameters
wdsRHx=260;wdsRHy=534
pt_osx=False
if _platform == "linux" or _platform == "linux2":
# linux
sizeX=wdsRHx;sizeY=wdsRHy-22+34 #516 #536
else:
sizeX=wdsRHx;sizeY=wdsRHy-22 #482#502
if _platform == "darwin":
pt_osx=True
## # MAC OS X
##elif _platform == "win32":
## # Windows
btn_sizeX=28;btn_sizeY=28
invert = True
rh_edges = []
rh_edges_names = []
rh_edges_to_connect = []
rh_faces = []
rh_faces_names = []
rh_faces_indexes = []
created_faces = []
rh_obj = []
rh_obj_name = []
force_recompute = False #True
def mk_str(input):
if (sys.version_info > (3, 0)): #py3
if isinstance(input, str):
return input
else:
input = input.encode('utf-8')
return input
else: #py2
if type(input) == unicode:
input = input.encode('utf-8')
return input
else:
return input
##
def i_say(msg):
FreeCAD.Console.PrintMessage(msg)
FreeCAD.Console.PrintMessage('\n')
def i_sayw(msg):
FreeCAD.Console.PrintWarning(msg)
FreeCAD.Console.PrintWarning('\n')
def i_sayerr(msg):
FreeCAD.Console.PrintError(msg)
FreeCAD.Console.PrintWarning('\n')
##
def checkBOP(shape):
""" checking BOP errors of a shape
returns:
- True if Shape is Valid
- the Shape errors
"""
# enabling BOP check
paramGt = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Part/CheckGeometry")
paramGt.SetBool("RunBOPCheck",True)
try:
shape.check(True)
return True
except:
return sys.exc_info()[1] #ValueError #sys.exc_info() #False
##
def checking_BOP(o):
if hasattr(o,'Shape'):
chks=checkBOP(o.Shape)
if chks is not True:
i_sayerr('shape \''+o.Name+'\' \''+mk_str(o.Label)+'\' is INVALID!\n')
#print(str(chks))
i_sayw(str(chks))
#print (chks) #[0])
if 'No error' in str(chks):
if len (o.Shape.Shells) > 0:
for sh in o.Shape.Shells:
try:
sh.check(True)
except:
i_sayerr(mk_str(o.Label)+'.shell errors:')
i_sayw(mk_str(sys.exc_info()[1]))
else:
i_say('shape \''+o.Name+'\' \''+mk_str(o.Label)+'\' is valid\n')
##
def check_TypeId_RH():
if FreeCADGui.Selection.getSelection():
sel=FreeCADGui.Selection.getSelection()
if len(sel)<1:
msg="Select one or more object(s) to be checked!\n"
reply = QtGui.QMessageBox.information(None,"Warning", msg)
FreeCAD.Console.PrintWarning(msg)
else:
non_solids=''
solids=''
for o in sel:
if hasattr(o,"Shape"):
if '.[compsolid]' in o.Label or '.[solid]' in o.Label or '.[shell]' in o.Label\
or '.[compound]' in o.Label or '.[face]' in o.Label or '.[edge]' in o.Label or '.[wire]' in o.Label\
or '.[vertex]' in o.Label:
o.Label=mk_str(o.Label).replace('.[solid]','').replace('.[shell]','').replace('.[compsolid]','').replace('.[compound]','')\
.replace('.[face]','').replace('.[wire]','').replace('.[edge]','').replace('.[vertex]','')
else:
len_shapes = len(o.Shape.Solids)+len(o.Shape.Shells)+len(o.Shape.Compounds)+len(o.Shape.CompSolids)+\
len(o.Shape.Faces)+len(o.Shape.Edges)+len(o.Shape.Wires)+len(o.Shape.Vertexes)
lbl = mk_str (o.Label)
i_say('\n'+lbl + '-> Shape Content: '+str(len_shapes)+' shapes -------------------------------')
if len(o.Shape.Solids)==0:
i_sayerr(mk_str(o.Label)+' object is a NON Solid')
if len(o.Shape.CompSolids)>0:
i_say(mk_str(o.Label)+' CompSolids object(s) NBR : '+str(len(o.Shape.CompSolids)))
if '.[compsolid]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label\
and '.[compound]' not in o.Label and '.[face]' not in o.Label and '.[edge]' not in o.Label:
o.Label=mk_str(o.Label)+'.[compsolid]'
if len(o.Shape.Compounds)>0:
i_say(mk_str(o.Label)+' Compound object(s) NBR : '+str(len(o.Shape.Compounds)))
if '.[compound]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label:
o.Label=mk_str(o.Label)+'.[compound]'
if len(o.Shape.Solids)>0:
i_say(mk_str(o.Label)+' Solid object(s) NBR : '+str(len(o.Shape.Solids)))
solids+=mk_str(o.Label)+'<br>'
if '.[solid]' not in o.Label and '.[compsolid]' not in o.Label and '.[compound]' not in o.Label:
o.Label=mk_str(o.Label)+'.[solid]'
else:
#i_sayerr(mk_str(o.Label)+' object is a NON Solid')
non_solids+=mk_str(o.Label)+'<br>'
if len(o.Shape.Shells)>0:
i_say(mk_str(o.Label)+' Shell object(s) NBR : '+str(len(o.Shape.Shells)))
if '.[shell]' not in o.Label and '.[solid]' not in o.Label and '.[compsolid]' not in o.Label and '.[compound]' not in o.Label:
o.Label=mk_str(o.Label)+'.[shell]'
if len(o.Shape.Faces)>0:
i_say(mk_str(o.Label)+' Face object(s) NBR : '+str(len(o.Shape.Faces)))
if '.[compsolid]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label\
and '.[compound]' not in o.Label and '.[face]' not in o.Label and '.[edge]' not in o.Label:
o.Label=mk_str(o.Label)+'.[face]'
if len(o.Shape.Wires)>0:
i_say(mk_str(o.Label)+' Wire object(s) NBR : '+str(len(o.Shape.Wires)))
if '.[compsolid]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label\
and '.[compound]' not in o.Label and '.[face]' not in o.Label and '.[wire]' not in o.Label:
o.Label=mk_str(o.Label)+'.[wire]'
if len(o.Shape.Edges)>0:
i_say(mk_str(o.Label)+' Edge object(s) NBR : '+str(len(o.Shape.Edges)))
if '.[compsolid]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label\
and '.[compound]' not in o.Label and '.[face]' not in o.Label and '.[edge]' not in o.Label and '.[wire]' not in o.Label:
o.Label=mk_str(o.Label)+'.[edge]'
if len(o.Shape.Vertexes)>0:
i_say(mk_str(o.Label)+' Vertex object(s) NBR : '+str(len(o.Shape.Vertexes)))
if '.[compsolid]' not in o.Label and '.[solid]' not in o.Label and '.[shell]' not in o.Label\
and '.[compound]' not in o.Label and '.[face]' not in o.Label and '.[edge]' not in o.Label\
and '.[wire]' not in o.Label and '.[vertex]' not in o.Label:
o.Label=mk_str(o.Label)+'.[vertex]'
else:
FreeCAD.Console.PrintWarning("Select object with a \"Shape\" to be checked!\n")
# if len (non_solids)>0:
# reply = QtGui.QMessageBox.information(None,"Warning", 'List of <b>NON Solid</b> object(s):<br>'+non_solids)
# if len (solids)>0:
# reply = QtGui.QMessageBox.information(None,"Info", 'List of <b>Solid</b> object(s):<br>'+solids)
else:
#FreeCAD.Console.PrintError("Select elements from dxf imported file\n")
reply = QtGui.QMessageBox.information(None,"Warning", "Select one or more object(s) to be checked!")
FreeCAD.Console.PrintWarning("Select one or more object(s) to be checked!\n")
def clear_all_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
rh_edges = []
rh_edges_names = []
rh_edges_to_connect = []
created_faces = []
RHDockWidget.ui.TE_Edges.setPlainText("")
rh_faces = []
rh_faces_names = []
rh_faces_indexes = []
created_faces = []
RHDockWidget.ui.TE_Faces.setPlainText("")
rh_obj = []
rh_obj_name = []
RHDockWidget.ui.Edge_Nbr.setText("0")
RHDockWidget.ui.Face_Nbr.setText("0")
RHDockWidget.ui.Obj_Nbr.setText("0")
RHDockWidget.ui.Obj_Nbr_2.setText("0")
##
def refine_parametric_RH():
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelectionEx()
if len (sel) > 0:
for selobj in sel:
if hasattr(selobj.Object,"Shape"):
newobj=selobj.Document.addObject("Part::FeaturePython",'refined')
OpenSCADFeatures.RefineShape(newobj,selobj.Object)
OpenSCADFeatures.ViewProviderTree(newobj.ViewObject)
## to do: see if it is possible to conserve colors in refining
ao = FreeCAD.ActiveDocument.ActiveObject
docG.ActiveObject.ShapeColor=docG.getObject(selobj.Object.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(selobj.Object.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(selobj.Object.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(selobj.Object.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(selobj.Object.Name).Transparency
#newobj.Label='r_%s' % selobj.Object.Label
newobj.Label=selobj.Object.Label
selobj.Object.ViewObject.hide()
doc.recompute()
##
def refine_RH():
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel):
for o in sel:
if hasattr(o,"Shape"):
doc.addObject('Part::Feature','refined').Shape=o.Shape.removeSplitter()
doc.ActiveObject.Label=o.Label
docG.getObject(o.Name).hide()
docG.ActiveObject.ShapeColor=docG.getObject(o.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(o.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(o.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(o.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(o.Name).Transparency
doc.recompute()
##
def edges_clear_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
rh_edges = []
rh_edges_names = []
rh_edges_to_connect = []
created_faces = []
RHDockWidget.ui.TE_Edges.setPlainText("")
rh_faces = []
rh_faces_names = []
rh_faces_indexes = []
created_faces = []
RHDockWidget.ui.TE_Faces.setPlainText("")
rh_obj = []
rh_obj_name = []
RHDockWidget.ui.Edge_Nbr.setText("0")
RHDockWidget.ui.Face_Nbr.setText("0")
##
def faces_clear_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
rh_faces = []
rh_faces_names = []
rh_faces_indexes = []
created_faces = []
RHDockWidget.ui.TE_Faces.setPlainText("")
rh_edges = []
rh_edges_names = []
rh_edges_to_connect = []
created_faces = []
rh_obj = []
rh_obj_name = []
RHDockWidget.ui.TE_Edges.setPlainText("")
RHDockWidget.ui.Edge_Nbr.setText("0")
RHDockWidget.ui.Face_Nbr.setText("0")
##
def close_RH():
"""closing dialog"""
RHDockWidget.deleteLater()
def merge_selected_faces_RH():
"""merging Faces of selected shapes"""
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
#af_faces = []
af_faces = rh_faces
#print rh_faces
#faces = []
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
#rh_edges = []; rh_edges_names = []; created_faces = []
#rh_edges_to_connect = []
# af_faces = [];af_faces_names = []
# selEx=FreeCADGui.Selection.getSelectionEx()
# if len (selEx):
# for selFace in selEx:
# for i,f in enumerate(selFace.SubObjects):
# #for e in selEdge.SubObjects
# if 'Face' in selFace.SubElementNames[i]:
# af_faces.append(f)
# af_faces_names.append(selFace.SubElementNames[i])
# print(selFace.SubElementNames[i])
if len(af_faces) > 0:
#print af_faces
try:
_ = Part.Shell(af_faces)
if _.isNull():
#raise RuntimeError('Failed to create shell')
FreeCAD.Console.PrintWarning('Failed to create shell\n')
except:
FreeCAD.Console.PrintWarning('Failed to create shell\n')
for f in af_faces:
Part.show(f)
doc.ActiveObject.Label="face"
stop
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
if _.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
_=Part.Solid(_)
if _.isNull(): raise RuntimeError('Failed to create solid')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
if RHDockWidget.ui.checkBox_Refine.isChecked():
doc.addObject('Part::Feature','SolidRefined').Shape=_.removeSplitter()
else:
doc.addObject('Part::Feature','Solid').Shape=_
#App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.mysolid.Label
mysolidr = doc.ActiveObject
#original_label = rh_obj.Label
#if RHDockWidget.ui.checkBox_keep_original.isChecked():
# docG.getObject(rh_obj.Name).Visibility=False
#else:
# doc.removeObject(rh_obj.Name)
#if RHDockWidget.ui.checkBox_Refine.isChecked():
# mysolidr.Label = original_label + "_refined"
##
def checkShape():
"""checking Shape"""
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel) == 1:
o = sel[0]
checking_BOP(o)
else:
msg="Select one or more object(s) to be checked!\n"
reply = QtGui.QMessageBox.information(None,"Warning", msg)
FreeCAD.Console.PrintWarning(msg)
def sewShape():
"""checking Shape"""
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel) == 1:
o = sel[0]
if hasattr(o,'Shape'):
sh = o.Shape.copy()
sh.sewShape()
sl = Part.Solid(sh)
docG.getObject(o.Name).Visibility = False
Part.show(sl)
ao = FreeCAD.ActiveDocument.ActiveObject
ao.Label = 'Solid'
docG.ActiveObject.ShapeColor=docG.getObject(o.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(o.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(o.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(o.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(o.Name).Transparency
else:
msg="Select one or more object(s) to be checked!\n"
reply = QtGui.QMessageBox.information(None,"Warning", msg)
FreeCAD.Console.PrintWarning(msg)
def getTolerance():
"""getting Tolerance"""
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel) == 1:
o = sel[0]
if hasattr(o,'Shape'):
tol = o.Shape.getTolerance(0)
i_say(mk_str(o.Label)+' tolerance = '+str(tol))
else:
msg="Select one or more object(s) to be checked!\n"
reply = QtGui.QMessageBox.information(None,"Warning", msg)
FreeCAD.Console.PrintWarning(msg)
##
def setTolerance():
"""getting Tolerance"""
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel) == 1:
o = sel[0]
if hasattr(o,'Shape'):
ns = o.Shape.copy()
i_say (mk_str(o.Label)+' tolerance = '+str(ns.getTolerance(0)))
new_tol = float(RHDockWidget.ui.tolerance_value.text())
ns.fixTolerance(new_tol) #1e-4)
docG.getObject(o.Name).Visibility = False
Part.show(ns)
ao = FreeCAD.ActiveDocument.ActiveObject
docG.ActiveObject.ShapeColor=docG.getObject(o.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(o.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(o.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(o.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(o.Name).Transparency
ao.Label = 'Solid'
i_say (mk_str(ao.Label)+' tolerance = '+str(ao.Shape.getTolerance(0)))
else:
msg="Select one or more object(s) to be checked!\n"
reply = QtGui.QMessageBox.information(None,"Warning", msg)
FreeCAD.Console.PrintWarning(msg)
##
def merge_faces_from_selected_objects_RH(refobj=None):
"""merging Faces of selected shapes"""
faces = []
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
sel=FreeCADGui.Selection.getSelection()
if len (sel):
for o in sel:
for f in o.Shape.Faces:
if f.Area > 0:
faces.append(f)
#print faces
try:
_ = Part.Shell(faces)
if _.isNull():
#raise RuntimeError('Failed to create shell')
FreeCAD.Console.PrintWarning('Failed to create shell\n')
except:
FreeCAD.Console.PrintWarning('Failed to create shell\n')
for f in faces:
Part.show(f)
doc.ActiveObject.Label="face"
stop
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
if _.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
_=Part.Solid(_)
if _.isNull(): raise RuntimeError('Failed to create solid')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
if RHDockWidget.ui.checkBox_Refine.isChecked():
doc.addObject('Part::Feature','SolidRefined').Shape=_.removeSplitter()
else:
doc.addObject('Part::Feature','Solid').Shape=_
#App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.mysolid.Label
mysolidr = doc.ActiveObject
#original_label = rh_obj.Label
if refobj is not None and hasattr(refobj,'Name'):
docG.ActiveObject.ShapeColor=docG.getObject(refobj.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(refobj.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(refobj.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(refobj.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(refobj.Name).Transparency
else:
docG.ActiveObject.ShapeColor=docG.getObject(sel[0].Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(sel[0].Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(sel[0].Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(sel[0].Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(sel[0].Name).Transparency
if RHDockWidget.ui.checkBox_keep_original.isChecked():
for o in sel:
docG.getObject(o.Name).Visibility=False
else:
for o in sel:
doc.removeObject(o.Name)
#if RHDockWidget.ui.checkBox_Refine.isChecked():
# mysolidr.Label = original_label + "_refined"
##
def edges_confirmed_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
#close_RH()
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
#rh_edges = []; rh_edges_names = []; created_faces = []
#rh_edges_to_connect = []
en = None
selEx=FreeCADGui.Selection.getSelectionEx()
if len (selEx):
for selEdge in selEx:
for i,e in enumerate(selEdge.SubObjects):
#for e in selEdge.SubObjects
if 'Edge' in selEdge.SubElementNames[i]:
edge_in_list = False
for en in rh_edges_names:
if en == selEdge.ObjectName+'.'+selEdge.SubElementNames[i]:
edge_in_list =True
if not edge_in_list:
rh_edges.append(e)
rh_edges_names.append(selEdge.ObjectName+'.'+selEdge.SubElementNames[i])
rh_obj.append(selEdge.Object)
rh_obj_name.append(selEdge.ObjectName)
if (e.isClosed()):
cf=(Part.Face(Part.Wire(e)))
created_faces.append(cf)
i_say('face created from closed edge')
if RHDockWidget.ui.checkBox_keep_faces.isChecked():
# _ = Part.Solid(Part.Shell([cf]))
# doc.addObject('Part::Feature','Face_Solid').Shape = _
# doc.ActiveObject.Label = 'Face_Solid'
Part.show(cf)
doc.ActiveObject.Label = 'Face'
docG.ActiveObject.Visibility=False
else:
#cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges))
#if _.isNull(): raise RuntimeError('Failed to create face')
# App.ActiveDocument.addObject('Part::Feature','Face').Shape=_
#del _
#w1 = Part.Wire(e)
#try:
#cf=(Part.Face(w1))
#created_faces.append(cf)
#if RHDockWidget.ui.checkBox_keep_faces.isChecked():
# Part.show(cf)
# docG.ActiveObject.Visibility=False
#except:
rh_edges_to_connect.append(e)
#i_say(re.search(r'\d+', selEdge.SubElementNames[i]).group())
i_say(selEdge.ObjectName)
if len (rh_edges_to_connect) >0:
try:
#cf=Part.makeFilledFace(Part.Wire(Part.__sortEdges__(rh_edges_to_connect)))
cf=Part.Face(Part.Wire(Part.__sortEdges__(rh_edges_to_connect)))
created_faces.append(cf)
i_say('face created from open edges')
if RHDockWidget.ui.checkBox_keep_faces.isChecked():
Part.show(cf)
doc.ActiveObject.Label = 'Face'
docG.ActiveObject.Visibility=False
rh_edges_to_connect = []
except:
i_sayerr("make Face failed")
#rh_obj_name.append(selx.ObjectName)
#rh_obj.append(selx.Object)
#for e in rh_edges: # selx.SubObjects:
# if (e.isClosed()):
# cf=(Part.Face(Part.Wire(e)))
# created_faces.append(cf)
# else:
# rh_edges_to_connect.append(e)
#eh_edges_grouped = []
#for e in rh_edges_to_connect:
#nw_edges=sum((e for e in rh_edges_to_connect),[])
#print rh_edges_to_connect
#if len(rh_edges_to_connect) > 0:
# f = OpenSCAD2Dgeom.edgestofaces(rh_edges_to_connect)
# created_faces.append(f)
#Part.show(f)
#sn = doc.ActiveObject
#fn = sn.Shape.Faces[0]
#created_faces.append(fn)
#doc.removeObject(sn.Name)
if 0:
try:
cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges_to_connect))
#if _.isNull(): raise RuntimeError('Failed to create face')
# App.ActiveDocument.addObject('Part::Feature','Face').Shape=_
#del _
#w1 = Part.Wire(e)
#try:
#cf=(Part.Face(w1))
created_faces.append(cf)
if RHDockWidget.ui.checkBox_keep_faces.isChecked():
Part.show(Part.makeSolid(Part.makeShell(cf)))
docG.ActiveObject.Visibility=False
rh_edges_to_connect = []
except:
print('edge outline not closed')
print ('To Do: collect connected edges to create a Wire')
e_list=""
for e in rh_edges_names:
e_list=e_list+str(e)+'\n'
RHDockWidget.ui.TE_Edges.setPlainText(e_list)
RHDockWidget.ui.Edge_Nbr.setText(str(len(rh_edges)))
unique_obj = set(rh_obj)
unique_obj_count = len(unique_obj)
RHDockWidget.ui.Obj_Nbr.setText(str(unique_obj_count))
for ob in FreeCAD.ActiveDocument.Objects:
FreeCADGui.Selection.removeSelection(ob)
##
def faces_confirmed_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
doc=FreeCAD.ActiveDocument
#rh_faces = []; rh_faces_names = []
selEx=FreeCADGui.Selection.getSelectionEx()
#fn = None
if len (selEx):
for selFace in selEx:
for i,f in enumerate(selFace.SubObjects):
#for e in selEdge.SubObjects
if 'Face' in selFace.SubElementNames[i]:
face_in_list = False
for fn in rh_faces_names:
if fn == selFace.ObjectName+'.'+selFace.SubElementNames[i]:
face_in_list =True
#if len (rh_faces_names) == 0:
# fn = selFace.ObjectName+'.'+selFace.SubElementNames[i]
if not face_in_list:
rh_faces.append(f)
rh_faces_indexes.append (re.search(r'\d+',selFace.SubElementNames[i]).group())
rh_faces_names.append(selFace.ObjectName+'.'+selFace.SubElementNames[i])
rh_obj.append(selFace.Object)
rh_obj_name.append(selFace.ObjectName)
#af_faces.append(f)
#af_faces_names.append(selFace.Object+'.'+selFace.SubElementNames[i])
print(selFace.ObjectName+'.'+selFace.SubElementNames[i])
f_list=""
for f in rh_faces_names:
f_list=f_list+str(f)+'\n'
RHDockWidget.ui.TE_Faces.setPlainText(f_list)
#print(selx.ObjectName)
#if selx.ObjectName != rh_obj_name:
# #raise RuntimeError('ERROR object changed. Please repeat process from the start')
# FreeCAD.Console.PrintWarning('object changed\n')
# rh_obj = selx.Object
RHDockWidget.ui.Face_Nbr.setText(str(len(rh_faces)))
unique_obj = set(rh_obj)
unique_obj_count = len(unique_obj)
RHDockWidget.ui.Obj_Nbr_2.setText(str(unique_obj_count))
for ob in FreeCAD.ActiveDocument.Objects:
FreeCADGui.Selection.removeSelection(ob)
##
def removeHoles_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
global force_recompute, invert
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
print('Removing Holes')
#print rh_edges; print rh_faces; print (rh_obj)
unique_obj = set(rh_obj)
unique_obj_count = len(unique_obj)
if unique_obj_count == 1:
RHDockWidget.ui.TE_Edges.setPlainText("")
RHDockWidget.ui.TE_Faces.setPlainText("")
myshape = rh_obj[0]
i = 0
faces = []
for f in myshape.Shape.Faces:
i+=1
idx_found = False
for j in rh_faces_indexes:
if int(j) == i:
idx_found = True
print('index found '+str(j))
if not idx_found:
faces.append(f)
if len(rh_edges_to_connect) > 0:
if not invert:
try:
print("try to create a Face w/ OpenSCAD2Dgeom")
cf = OpenSCAD2Dgeom.edgestofaces(Part.__sortEdges__(rh_edges_to_connect))
except:
print("OpenSCAD2Dgeom failed\ntry to makeFilledFace")
cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges_to_connect))
else:
try:
print("try to makeFilledFace")
cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges_to_connect))
except:
print("makeFilledFace failed\ntry to create a Face w/ OpenSCAD2Dgeom")
cf = OpenSCAD2Dgeom.edgestofaces(Part.__sortEdges__(rh_edges_to_connect))
created_faces.append(cf)
if RHDockWidget.ui.checkBox_keep_faces.isChecked():
#_ = Part.Solid(Part.Shell([cf]))
#doc.addObject('Part::Feature','Face_Solid').Shape = _
#doc.ActiveObject.Label = 'Face_Solid'
Part.show(cf)
doc.ActiveObject.Label = 'Face'
docG.ActiveObject.Visibility=False
rh_edges_to_connect = []
if 0:
for f in created_faces:
faces.append(f)
res_faces = []
_ = Part.Shell(faces)
if _.isNull(): raise RuntimeError('Failed to create shell')
_ = Part.Shell(faces)
if _.isNull(): raise RuntimeError('Failed to create shell')
_=Part.Solid(_)
if _.isNull(): raise RuntimeError('Failed to create solid')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
for f in created_faces:
new_faces = []
for nf in _.Faces:
new_faces.append(nf)
new_faces.append(f)
del _
_ = Part.Shell(new_faces)
i_sayw('added 1 face')
if _.isNull(): raise RuntimeError('Failed to create shell')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
if _.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
_=Part.Solid(_)
if _.isNull(): raise RuntimeError('Failed to create solid')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
#doc.recompute()
#for f in created_faces:
# new_faces.append(f)
#del _
#_ = Part.Shell(new_faces)
#if _.isNull(): raise RuntimeError('Failed to create shell')
#App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_
#if RHDockWidget.ui.checkBox_Refine.isChecked():
# try:
# _.removeSplitter()
# except:
# print ('not refined')
#myshell = doc.ActiveObject
#del _
#if myshell.Shape.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
# if _.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
# _=Part.Solid(_)
# if _.isNull(): raise RuntimeError('Failed to create solid')
# if RHDockWidget.ui.checkBox_Refine.isChecked():
# try:
# _.removeSplitter()
# except:
# print ('not refined')
#App.ActiveDocument.addObject('Part::Feature','Solid').Shape=_
#mysolid = doc.ActiveObject
#del _
#doc.removeObject(myshell.Name)
#docG.mysolid.Visibility=True
#doc.addObject('Part::Feature','SolidRefined').Shape=mysolid.Shape.removeSplitter()
#doc.removeObject(mysolid.Name)
if RHDockWidget.ui.checkBox_Refine.isChecked():
doc.addObject('Part::Feature','SolidRefined').Shape=_.removeSplitter()
else:
doc.addObject('Part::Feature','Solid').Shape=_
# doc.addObject('Part::Feature','Solid').Shape=_
#App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.mysolid.Label
mysolidr = doc.ActiveObject
original_label = myshape.Label
docG.ActiveObject.ShapeColor=docG.getObject(myshape.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(myshape.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(myshape.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(myshape.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(myshape.Name).Transparency
if RHDockWidget.ui.checkBox_keep_original.isChecked():
docG.getObject(myshape.Name).Visibility=False
else:
doc.removeObject(myshape.Name)
if RHDockWidget.ui.checkBox_Refine.isChecked():
mysolidr.Label = original_label # + "_refined"
else:
mysolidr.Label = original_label
#mysolidr.hide()
clear_all_RH()
if force_recompute:
for obj in FreeCAD.ActiveDocument.Objects:
obj.touch()
doc.recompute()
print('ToDo Apply colors to corresponding faces')
else:
i_sayerr('select only one object')
##
##
def removeFaces_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
global force_recompute
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
print('Removing Holes')
#print rh_edges; print rh_faces; print (rh_obj)
#ui.TE_Edges.setPlainText("")
#ui.TE_Faces.setPlainText("")
unique_obj = set(rh_obj)
unique_obj_count = len(unique_obj)
if unique_obj_count == 1: #ToDo manage multi objs faces selection
#for myshape in unique_obj:
myshape = rh_obj[0]
i = 0
faces = []
for f in myshape.Shape.Faces:
i+=1
idx_found = False
for j in rh_faces_indexes:
if int(j) == i:
idx_found = True
print('index found '+str(j))
if not idx_found:
faces.append(f)
if 1:
try:
_ = Part.Shell(faces)
if _.isNull():
#raise RuntimeError('Failed to create shell')
FreeCAD.Console.PrintWarning('Failed to create shell\n')
except:
FreeCAD.Console.PrintWarning('Failed to create shell\n')
if RHDockWidget.ui.checkBox_keep_faces.isChecked():
for f in faces:
Part.show(f)
doc.ActiveObject.Label="face"
stop
#App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
#myshell = doc.ActiveObject
#del _
#if myshell.Shape.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
if _.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')
_=Part.Solid(_)
if _.isNull(): raise RuntimeError('Failed to create solid')
if RHDockWidget.ui.checkBox_Refine.isChecked():
try:
_.removeSplitter()
except:
print ('not refined')
#App.ActiveDocument.addObject('Part::Feature','Solid').Shape=_
#mysolid = doc.ActiveObject
#del _
#doc.removeObject(myshell.Name)
#docG.mysolid.Visibility=True
#doc.addObject('Part::Feature','SolidRefined').Shape=mysolid.Shape.removeSplitter()
#doc.removeObject(mysolid.Name)
if RHDockWidget.ui.checkBox_Refine.isChecked():
doc.addObject('Part::Feature','SolidRefined').Shape=_.removeSplitter()
else:
doc.addObject('Part::Feature','Solid').Shape=_
#App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.mysolid.Label
mysolidr = doc.ActiveObject
original_label = myshape.Label
docG.ActiveObject.ShapeColor=docG.getObject(myshape.Name).ShapeColor
docG.ActiveObject.LineColor=docG.getObject(myshape.Name).LineColor
docG.ActiveObject.PointColor=docG.getObject(myshape.Name).PointColor
docG.ActiveObject.DiffuseColor=docG.getObject(myshape.Name).DiffuseColor
docG.ActiveObject.Transparency=docG.getObject(myshape.Name).Transparency
if RHDockWidget.ui.checkBox_keep_original.isChecked():
docG.getObject(myshape.Name).Visibility=False
else:
doc.removeObject(myshape.Name)
if RHDockWidget.ui.checkBox_Refine.isChecked():
mysolidr.Label = original_label # + "_refined"
else:
mysolidr.Label = original_label
#mysolidr.hide()
#docG.getObject(mysolidr.Name).Visibility=False
#rh_edges = []; rh_edges_names = []; created_faces = []
#rh_edges_to_connect = []
#rh_faces = []; rh_faces_names = []
#rh_faces_indexes = []
#ui.Edge_Nbr.setText("0")
#ui.Face_Nbr.setText("0")
#ui.TE_Edges.setPlainText("")
#ui.TE_Faces.setPlainText("")
clear_all_RH()
if force_recompute:
for obj in FreeCAD.ActiveDocument.Objects:
obj.touch()
doc.recompute()
print('ToDo Apply colors to corresponding faces')
##
def addFaces_RH():
global rh_edges, rh_faces, rh_obj
global rh_edges_names, rh_faces_names, rh_obj_name
global created_faces, rh_faces_indexes, rh_edges_to_connect
global force_recompute, invert
doc=FreeCAD.ActiveDocument
docG = FreeCADGui.ActiveDocument
if len(rh_edges) > 0:
#try:
# print("try to makeFilledFace")
# cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges))
#except:
# print("makeFilledFace failed\ntry to create a Face w/ OpenSCAD2Dgeom")
# cf = OpenSCAD2Dgeom.edgestofaces(rh_edges)
if not invert:
try:
print("try to create a Face w/ OpenSCAD2Dgeom")
cf = OpenSCAD2Dgeom.edgestofaces(rh_edges)
except:
print("OpenSCAD2Dgeom failed\ntry to makeFilledFace")
cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges))
else:
try:
print("try to makeFilledFace")
cf=Part.makeFilledFace(Part.__sortEdges__(rh_edges))
except:
print("makeFilledFace failed\ntry to create a Face w/ OpenSCAD2Dgeom")
cf = OpenSCAD2Dgeom.edgestofaces(rh_edges)
#created_faces.append(cf)
Part.show(cf)