-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDesign456_2DTools.py
856 lines (738 loc) · 35.1 KB
/
Design456_2DTools.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
#
# ***************************************************************************
# * *
# * This file is a part of the Open Source Design456 Workbench - FreeCAD. *
# * *
# * Copyright (C) 2025 *
# * *
# * *
# * This library is free software; you can redistribute it and/or *
# * modify it under the terms of the GNU Lesser General Public *
# * License as published by the Free Software Foundation; either *
# * version 2 of the License, or (at your option) any later version. *
# * *
# * This library is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with this library; if not, If not, see *
# * <http://www.gnu.org/licenses/>. *
# * *
# * Author : Mariwan Jalal [email protected] *
# ***************************************************************************
import os
import sys
import FreeCAD as App
import FreeCADGui as Gui
from PySide import QtGui, QtCore # https://www.freecadweb.org/wiki/PySide
import Draft as _draft
import math
import Part
import Design456Init
import FACE_D as faced
from draftutils.translate import translate # for translation
from Design456_2Ddrawing import Design456_SimplifiedFace
import Mesh
import MeshPart
from Design456_3DTools import Design456_SimplifyCompound
__updated__ = '2022-10-05 20:45:35'
class Design456_MeshCircleToCircle:
"""
[Create a new shape that is a replacement shape of the old mesh circle.]
"""
def Activated(self):
App.ActiveDocument.openTransaction(
translate("Design456", "MeshCircleToCircle"))
s=Gui.Selection.getSelectionEx()[0]
shp=s.Object.Shape
Bound=shp.BoundBox
r=(Bound.XMax-Bound.XMin)/2
newShp= (Part.makeCircle(r, App.Vector(0, 0, 0), App.Vector(0, 0, 1)))
sub2=Part.Face(Part.Wire(newShp))
sub2=Part.show(sub2)
sub2.Placement.Base=shp.CenterOfGravity
sub1= s.Object
face1 = sub1.Shape.Faces[0]
face2 = sub2.Shape.Faces[0]
norm1 = face1.normalAt(0.0,0.0)*(-1)
norm2 = face2.normalAt(0.0,0.0)*(-1)
ang= math.degrees(norm1.getAngle(norm2))
print(ang)
#sub2.Object.Placement.Base = face1.Surface.Position
ROTA= norm2.cross(norm1)
print(ROTA)
rotation = App.Rotation(ROTA,ang)
sub2.Placement=App.Placement(face1.Surface.Position,ROTA,ang)
sub1.Visibility=False;
App.ActiveDocument.recompute()
App.ActiveDocument.commitTransaction() # undo reg.de here
def GetResources(self):
return{
'Pixmap': Design456Init.ICON_PATH + 'MeshCircleToCircle.svg',
'MenuText': 'MeshCircleToCircle',
'ToolTip': 'Mesh Circle To Circle '
}
Gui.addCommand('Design456_MeshCircleToCircle', Design456_MeshCircleToCircle())
class Design456_CommonFace:
"""[Create a new shape that is the common between two other shapes.]
"""
def Activated(self):
App.ActiveDocument.openTransaction(
translate("Design456", "CommonFace"))
s=Gui.Selection.getSelectionEx()
newshape=None
for obj in s:
if obj.HasSubObjects:
sh1=obj.SubObjects[0]
else:
sh1=obj.Object.Shape
if newshape is not None:
newshape= newshape.common(sh1)
else:
newshape= sh1
App.ActiveDocument.recompute()
newobj = App.ActiveDocument.addObject("Part::Feature", "CommonFace")
newobj.Shape=newshape
App.ActiveDocument.recompute()
for obj in s:
App.ActiveDocument.removeObject(obj.Object.Name)
App.ActiveDocument.recompute()
App.ActiveDocument.commitTransaction() # undo reg.de here
def GetResources(self):
return{
'Pixmap': Design456Init.ICON_PATH + 'CommonFace.svg',
'MenuText': 'CommonFace',
'ToolTip': 'CommonFace between 2-2D Faces'
}
Gui.addCommand('Design456_CommonFace', Design456_CommonFace())
# Subtract faces
class Design456_SubtractFaces:
def Activated(self):
App.ActiveDocument.openTransaction(
translate("Design456", "SubtractFace"))
s=Gui.Selection.getSelectionEx()
newshape=None
for obj in s:
if obj.HasSubObjects:
sh1=obj.SubObjects[0]
else:
sh1=obj.Object.Shape
if newshape is not None:
newshape= newshape.cut(sh1)
else:
newshape= sh1
App.ActiveDocument.recompute()
newobj = App.ActiveDocument.addObject("Part::Feature", "SubtractFace")
newobj.Shape=newshape
App.ActiveDocument.recompute()
for obj in s:
App.ActiveDocument.removeObject(obj.Object.Name)
App.ActiveDocument.recompute()
App.ActiveDocument.commitTransaction() # undo reg.de here
def GetResources(self):
return{
'Pixmap': Design456Init.ICON_PATH + 'SubtractFace.svg',
'MenuText': 'Subtract Faces',
'ToolTip': 'Subtract 2-2D Faces'
}
Gui.addCommand('Design456_SubtractFaces', Design456_SubtractFaces())
# Combine two faces
class Design456_CombineFaces:
def Activated(self):
App.ActiveDocument.openTransaction(
translate("Design456", "CombineFaces"))
s=Gui.Selection.getSelectionEx()
newshape=None
for obj in s:
if obj.HasSubObjects:
sh1=obj.SubObjects[0]
else:
sh1=obj.Object.Shape
if newshape is not None:
newshape= newshape.fuse(sh1)
else:
newshape= sh1
App.ActiveDocument.recompute()
newobj = App.ActiveDocument.addObject("Part::Feature", "CombineFaces")
newobj.Shape=newshape
App.ActiveDocument.recompute()
for obj in s:
App.ActiveDocument.removeObject(obj.Object.Name)
App.ActiveDocument.recompute()
simp= Design456_SimplifiedFace()
Gui.Selection.clearSelection()
Gui.Selection.addSelection(App.ActiveDocument.Name,newobj.Name)
simp.Activated()
App.ActiveDocument.recompute()
App.ActiveDocument.commitTransaction() # undo reg.de here
def GetResources(self):
return{
'Pixmap': Design456Init.ICON_PATH + 'CombineFaces.svg',
'MenuText': 'Combine Face',
'ToolTip': 'Combine 2-2D Faces'
}
Gui.addCommand('Design456_CombineFaces', Design456_CombineFaces())
# Surface between two line
class Design456Part_Surface:
def Activated(self):
try:
s = Gui.Selection.getSelectionEx()
if (len(s) < 1 or len(s) > 2):
# Two object must be selected
errMessage = "Select two edges or two wire to make a face or "
faced.errorDialog(errMessage)
return
elementsName = None
subObj = None
obj1=None
obj2=None
App.ActiveDocument.openTransaction(translate("Design456", "Surface"))
if len(s)==1:
obj1shp= s[0].SubObjects[0].copy()
obj1=App.ActiveDocument.addObject('Part::Feature',"E1")
obj1.Shape=obj1shp
obj2shp= s[0].SubObjects[1].copy()
obj2=App.ActiveDocument.addObject('Part::Feature',"E2")
obj2.Shape=obj2shp
App.ActiveDocument.recompute()
elementsName=[obj1.Name, obj2.Name]
subObj = [obj1, obj2]
elif len(s)==2 :
obj1shp= s[0].SubObjects[0].copy()
obj1=App.ActiveDocument.addObject('Part::Feature',"E1")
obj1.Shape=obj1shp
obj2shp= s[1].SubObjects[0].copy()
obj2=App.ActiveDocument.addObject('Part::Feature',"E2")
obj2.Shape=obj2shp
elementsName=[obj1.Name, obj2.Name]
subObj = [obj1, obj2]
for ss in s:
word = ss.FullName
if(word.find('Vertex') != -1):
# Two lines or curves or wires must be selected
errMessage = "Select two edges or two wires not Vertex"
faced.errorDialog(errMessage)
return
newObj = App.ActiveDocument.addObject(
'Part::RuledSurface', 'tempSurface')
newObj.Curve1 = subObj[0]
newObj.Curve2 = subObj[1]
App.ActiveDocument.recompute()
# Make a simple copy of the object
newShape = Part.getShape(
newObj, '', needSubElement=False, refine=True)
tempNewObj = App.ActiveDocument.addObject(
'Part::Feature', 'Surface')
tempNewObj.Shape = newShape
App.ActiveDocument.ActiveObject.Label = 'Surface'
App.ActiveDocument.recompute()
if tempNewObj.isValid() is False:
App.ActiveDocument.removeObject(tempNewObj.Name)
# Shape != OK
errMessage = "Failed to create the face"
faced.errorDialog(errMessage)
else:
App.ActiveDocument.removeObject(newObj.Name)
App.ActiveDocument.commitTransaction() # undo reg.de here
App.ActiveDocument.recompute()
if (obj1 is not None and obj2 is not None):
App.ActiveDocument.removeObject(obj1.Name)
App.ActiveDocument.removeObject(obj2.Name)
except Exception as err:
App.Console.PrintError("'Part Surface' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def GetResources(self):
return {
'Pixmap': Design456Init.ICON_PATH + 'Part_Surface.svg',
'MenuText': 'Part_Surface',
'ToolTip': 'Part Surface'
}
Gui.addCommand('Design456Part_Surface', Design456Part_Surface())
#########################
class Design456_ArcFace6Points:
def Activated(self):
try:
App.ActiveDocument.openTransaction(
translate("Design456", "ArcFace6Points"))
selected = Gui.Selection.getSelectionEx()
'''
Kind of selections:
1-Three objects - 2 Vertexes per each
2-mixed objects with different vertexes (2 objects three vertex each, 4 vertex 2-1-3 ..etc)
3- Six vertexes (6 objects)
For each object we take first vertex as row 1, second as row 2 if we have more than 1 object
'''
firstRow=[]
secondRow=[]
allSelected = []
if len(selected)==1:
#We have one object with 6 vertexes selected - 1-3 must be first row, 4-6 must be second row
firstRow.append(selected[0].SubObjects[0].Point)
firstRow.append(selected[0].SubObjects[1].Point)
firstRow.append(selected[0].SubObjects[2].Point)
secondRow.append(selected[0].SubObjects[3].Point)
secondRow.append(selected[0].SubObjects[4].Point)
secondRow.append(selected[0].SubObjects[5].Point)
elif len(selected)==2:
# we have two objects, each object must have 3 vertexes which is a row
firstRow.append(selected[0].SubObjects[0].Point)
firstRow.append(selected[0].SubObjects[1].Point)
firstRow.append(selected[0].SubObjects[2].Point)
secondRow.append(selected[1].SubObjects[0].Point)
secondRow.append(selected[1].SubObjects[1].Point)
secondRow.append(selected[1].SubObjects[2].Point)
elif len(selected)==3:
#We have three objects. Must be with 2 vertex each .
# First are first row, second vertex for each are second row
for obj in selected:
if len(obj.SubObjects)>1:
firstRow.append (obj.SubObjects[0].Point)
secondRow.append(obj.SubObjects[1].Point)
else:
errMessage = "Please select 2 vertexes on each object"
faced.errorDialog(errMessage)
return
elif len(selected)==4 or len(selected)==5:
# this can be a combination of different things.
#Still we take first vertexes as first row, and second vertexes as second row.
first=0
second=0
if len(selected[0])>1:
firstRow.append(selected[0].SubObjects[0].Point)
secondRow.append(selected[0].SubObjects[1].Point)
first+=1
second+=1
else:
first+=1
firstRow.append(selected[0].SubObjects[0].Point)
if len(selected[1])>1:
firstRow.append(selected[1].SubObjects[0].Point)
secondRow.append(selected[1].SubObjects[0].Point)
first+=1
second+=1
else:
first+=1
firstRow.append(selected[0].SubObjects[0].Point)
if len(selected[2])>1:
firstRow.append(selected[1].SubObjects[0].Point)
secondRow.append(selected[1].SubObjects[0].Point)
first+=1
second+=1
else:
first+=1
firstRow.append(selected[0].SubObjects[0].Point)
if len(selected[3])>1:
if firstRow==3:
#First row is finished
secondRow.append(selected[3].SubObjects[0].Point)
secondRow.append(selected[3].SubObjects[1].Point)
else:
firstRow.append(selected[3].SubObjects[0].Point)
secondRow.append(selected[3].SubObjects[1].Point)
else:
if firstRow==3:
#First row is finished
secondRow.append(selected[3].SubObjects[0].Point)
else:
errMessage = "Selection error please try again"
faced.errorDialog(errMessage)
return
elif len(selected)==6:
if selected[0].HasSubObjects:
firstRow.append(selected[0].SubObjects[0].Point)
firstRow.append(selected[1].SubObjects[0].Point)
firstRow.append(selected[2].SubObjects[0].Point)
secondRow.append(selected[3].SubObjects[0].Point)
secondRow.append(selected[4].SubObjects[0].Point)
secondRow.append(selected[5].SubObjects[0].Point)
else:
firstRow.append(selected[0].Object.Shape.Vertexes[0].Point)
firstRow.append(selected[1].Object.Shape.Vertexes[0].Point)
firstRow.append(selected[2].Object.Shape.Vertexes[0].Point)
secondRow.append(selected[3].Object.Shape.Vertexes[0].Point)
secondRow.append(selected[4].Object.Shape.Vertexes[0].Point)
secondRow.append(selected[5].Object.Shape.Vertexes[0].Point)
allSelected=firstRow+secondRow
C1 = Part.Arc(App.Vector(allSelected[0]), App.Vector(
allSelected[1]), App.Vector(allSelected[2]))
C2 = Part.Arc(App.Vector(allSelected[3]), App.Vector(
allSelected[4]), App.Vector(allSelected[5]))
sel1 = C1.toShape()
sel2 = C2.toShape()
W1 = Part.Wire(sel1.Edges)
W2 = Part.Wire(sel2.Edges)
newObj = App.ActiveDocument.addObject(
'Part::RuledSurface', 'Test')
obj1=App.ActiveDocument.addObject('Part::Feature',"E1")
obj1.Shape=W1
obj2=App.ActiveDocument.addObject('Part::Feature',"E2")
obj2.Shape=W2
newObj.Curve1 = obj1
newObj.Curve2 = obj2
App.ActiveDocument.recompute()
finalObj = App.ActiveDocument.addObject(
'Part::Feature', 'ArcFace6Points')
finalObj.Shape=newObj.Shape.copy()
App.ActiveDocument.removeObject(newObj.Name)
App.ActiveDocument.removeObject(obj1.Name)
App.ActiveDocument.removeObject(obj2.Name)
App.ActiveDocument.recompute()
del allSelected[:]
App.ActiveDocument.commitTransaction() # undo
except Exception as err:
App.Console.PrintError("'ArcFace6Points' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def GetResources(self):
return {
'Pixmap': Design456Init.ICON_PATH + 'ArcFace6Points.svg',
'MenuText': 'ArcFace6Points',
'ToolTip': 'ArcFace 6 Points'
}
Gui.addCommand('Design456_ArcFace6Points', Design456_ArcFace6Points())
# Segment a face from 3D object or 2D object
class Design456_SegmentAFace:
def __init__(self):
self.sel=None
self.Segments=10
self.SingleFace=False
self.oldFace=None
def selectedOptions(self):
from DefeaturingWB.oDefeaturingTools import sewShape
self.Segments=self.inpINTSegments.value()
if self.radAll.isChecked():
self.SingleFace=False
elif self.radOneFace.isChecked():
self.SingleFace=True
self.frmMain.hide()
self.oldFace=None
try:
self.sel=Gui.Selection.getSelectionEx()[0]
if hasattr(self.sel,"HasSubObjects"):
if self.sel.HasSubObjects:
self.oldFace=self.sel.SubObjects
App.ActiveDocument.openTransaction(translate("Design456","SegmentAFace"))
self.sel.Object.Visibility= False
mesh = self.divideFace()
_solid = self.collectFace(mesh)
App.ActiveDocument.removeObject(mesh.Name)
#undo
newObj=App.ActiveDocument.addObject('Part::Feature', "SegmentAFace")
result=sewShape([_solid])
newObj.Shape=result[0].Shape.copy()
App.ActiveDocument.removeObject(_solid.Name)
App.ActiveDocument.removeObject(result[0].Name)
App.ActiveDocument.commitTransaction() #undo reg.
except Exception as err:
App.Console.PrintError("'Design456_SegmentAFace' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def hide(self):
self.frmMain.hide()
return
def createDialog(self):
self.frmMain=QtGui.QDialog()
self.frmMain.setObjectName("frmMain")
self.frmMain.resize(374, 200)
self.frmMain.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.Europe))
self.frmMain.setModal(True)
self.buttonBox = QtGui.QDialogButtonBox(self.frmMain)
self.buttonBox.setGeometry(QtCore.QRect(30, 150, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.buttonGroup = QtGui.QButtonGroup(self.frmMain)
self.buttonGroup.setObjectName("buttonGroup")
self.radOneFace = QtGui.QRadioButton(self.frmMain)
self.radOneFace.setGeometry(QtCore.QRect(20, 20, 130, 20))
self.radOneFace.setObjectName("radOneFace")
self.radAll = QtGui.QRadioButton(self.frmMain)
self.radAll.setGeometry(QtCore.QRect(20, 50, 89, 20))
self.radAll.setObjectName("radAll")
self.inpINTSegments = QtGui.QSpinBox(self.frmMain)
self.inpINTSegments.setGeometry(QtCore.QRect(210, 60, 130, 22))
self.inpINTSegments.setObjectName("inpINTSegments")
self.label = QtGui.QLabel(self.frmMain)
self.label.setGeometry(QtCore.QRect(210, 40, 130, 16))
self.label.setObjectName("label")
self.buttonGroup.addButton(self.radOneFace)
self.buttonGroup.addButton(self.radAll)
self.radAll.setChecked(True)
self.inpINTSegments.setValue(10)
self.inpINTSegments.setMaximum (10000)
self.inpINTSegments.setMinimum(1)
_translate = QtCore.QCoreApplication.translate
self.frmMain.setWindowTitle(_translate("frmMain", "Select Face and Segments size"))
self.radOneFace.setText(_translate("frmMain", "Only selected face"))
self.radAll.setText(_translate("frmMain", "All faces"))
self.label.setText(_translate("frmMain", "Segments"))
self.buttonBox.accepted.connect(self.selectedOptions)
self.buttonBox.rejected.connect(self.hide)
QtCore.QMetaObject.connectSlotsByName(self.frmMain)
self.frmMain.show()
def Activated(self):
self.createDialog()
def GetResources(self):
return {
'Pixmap': Design456Init.ICON_PATH + 'SegmentAFace.svg',
'MenuText': 'SegmentAFace',
'ToolTip': 'Segment A Face'
}
def divideFace(self):
try:
mesh = App.ActiveDocument.addObject("Mesh::Feature", "Mesh")
shp=None
if self.SingleFace is False:
shp = self.sel.Object.Shape
else:
shp = self.sel.SubObjects[0]
mesh.Mesh = MeshPart.meshFromShape(
Shape=shp,
LinearDeflection=5/self.Segments,
AngularDeflection=10/2.5,
Relative=False)
App.ActiveDocument.recompute()
return mesh
except Exception as err:
App.Console.PrintError("'devideFace' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def collectFace(self,obj,swe=0.1):
try:
#sel=Gui.Selection.getSelectionEx()[0]
simplify=Design456_SimplifyCompound()
mesh = obj.Mesh
shape = Part.Shape()
shape.makeShapeFromMesh(mesh.Topology, swe)
compShp=Part.makeCompound(shape)
newSolid = App.ActiveDocument.addObject('Part::Feature', "tempSegmentAFace")
if self.SingleFace is False:
newObj = App.ActiveDocument.addObject('Part::Feature', "collectFace")
newObj.Shape = shape
App.ActiveDocument.recompute()
temp=simplify.Activated(newObj)[0]
newSolid.Shape = temp.Shape.copy()
newSolid.Placement=temp.Placement
App.ActiveDocument.removeObject(temp.Name)
else:
tempShape=faced.EqualizeFaces(self.sel.Object,self.sel.SubObjects[0],compShp)
newSolid.Shape =tempShape.Shape.copy()
App.ActiveDocument.removeObject(tempShape.Name)
App.ActiveDocument.recompute()
return newSolid
except Exception as err:
App.Console.PrintError("'SegmentAFace' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
Gui.addCommand('Design456_SegmentAFace', Design456_SegmentAFace())
######################################################################
"""
Unfortunately, I couldn't find a solution and I don't want to waste more time at the moment
on this tool. I leave it for now, and comment the code. Maybe when I find a solution
I come back to it.
The problem is that there is no way to reset face's placement. It can have an Axis and a rotation axis
which means double rotation. Resetting that with the weird rotation mechanism in FreeCAD is not easy.
"""
# class Design456_EqualizeFaces:
# def __init__(self):
# self.dialog= None
# self.sel1=None
# self.sel2=None
# """[Use this tool to equalize two faces (copy first, replace second with the copied face).]
# """
# def Activated(self):
# try:
# from Design456_Alignment import Design456_ResetPlacements
# sel=Gui.Selection.getSelectionEx()
# newshape=None
# if len(sel)<2 or len(sel)>2 :
# #error message
# # Two object must be selected
# errMessage = "Select two faces to use the tool "
# faced.errorDialog(errMessage)
# return
# f1=sel[0].SubObjects[0]
# f2=sel[1].SubObjects[0]
# if type(f1)!=Part.Face or type(f2)!=Part.Face:
# errMessage = "Select two faces to use the tool "
# faced.errorDialog(errMessage)
# return
# self.dialog = self.getMainWindow()
# #We must reset placement for each object otherwise this tool is impossible
# App.ActiveDocument.openTransaction(translate("Design456", "EqualizeFaces"))
# restedObj=Design456_ResetPlacements([sel[0].Object,sel[1].Object]).Activated()
# self.sel1=restedObj[0]
# self.sel2=restedObj[1]
# except Exception as err:
# App.Console.PrintError("'EqualizeFaces' Failed. "
# "{err}\n".format(err=str(err)))
# exc_type, exc_obj, exc_tb = sys.exc_info()
# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
# print(exc_type, fname, exc_tb.tb_lineno)
# def getMainWindow(self):
# """[Create the tab for the tool]
# """
# try:
# toplevel = QtGui.QApplication.topLevelWidgets()
# self.mw = None
# for i in toplevel:
# if i.metaObject().className() == "Gui::MainWindow":
# self.mw = i
# if self.mw is None:
# raise Exception("No main window found")
# dw = self.mw.findChildren(QtGui.QDockWidget)
# for i in dw:
# if str(i.objectName()) == "Model":
# self.tab = i.findChild(QtGui.QTabWidget)
# elif str(i.objectName()) == "Python Console":
# self.tab = i.findChild(QtGui.QTabWidget)
# if self.tab is None:
# raise Exception("No tab widget found")
# oldsize = self.tab.count()
# self.dialog = QtGui.QDialog()
# self.tab.addTab(self.dialog, "Replace Face")
# self.dialog.resize(200, 450)
# self.lblReplace = QtGui.QLabel(self.dialog)
# self.lblReplace.setGeometry(QtCore.QRect(10, 0, 191, 61))
# font = QtGui.QFont()
# font.setPointSize(10)
# self.lblTitle = QtGui.QLabel(self.dialog)
# self.lblTitle.setGeometry(QtCore.QRect(10, 10, 300, 91))
# font = QtGui.QFont()
# font.setFamily("Times New Roman")
# font.setPointSize(10)
# self.lblTitle.setFont(font)
# self.lblTitle.setObjectName("lblTitle")
# _translate = QtCore.QCoreApplication.translate
# self.dialog.setWindowTitle(_translate("Dialog", "Replace Face"))
# self.lblTitle.setText(_translate("Dialog", "(Replace Face)\n"
# "Tweak an object by replacing faces or equalize them"))
# self.formLayoutWidget_2 = QtGui.QWidget(self.dialog)
# self.formLayoutWidget_2.setGeometry(QtCore.QRect(10, 100, 400, 300))
# self.formLayoutWidget_2.setObjectName("formLayoutWidget_2")
# self.radioXdir = QtGui.QRadioButton(self.formLayoutWidget_2)
# self.radioXdir.setObjectName("xDir")
# self.radioYdir = QtGui.QRadioButton(self.formLayoutWidget_2)
# self.radioYdir.setObjectName("yDir")
# self.radioZdir = QtGui.QRadioButton(self.formLayoutWidget_2)
# self.radioZdir.setObjectName("zDir")
# self.radioXdir.setGeometry(QtCore.QRect(10, 120, 100, 30))
# self.radioYdir.setGeometry(QtCore.QRect(10, 150, 100, 30))
# self.radioZdir.setGeometry(QtCore.QRect(10, 180, 100, 30))
# self.radioXdir.setText(_translate("Dialog", "X-Dir"))
# self.radioYdir.setText(_translate("Dialog", "Y-Dir"))
# self.radioZdir.setText(_translate("Dialog", "Z-Dir"))
# self.radioZdir.setChecked(1) # default
# font = QtGui.QFont()
# font.setPointSize(10)
# font.setBold(True)
# font.setWeight(75)
# self.btnOK = QtGui.QDialogButtonBox(self.dialog)
# self.btnOK.setGeometry(QtCore.QRect(200, 300, 190, 61))
# self.btnOK.setFont(font)
# self.btnOK.setObjectName("btnOK")
# self.btnOK.setStandardButtons(
# QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
# self.btnOK.accepted.connect(self.OK_cb)
# self.btnOK.rejected.connect(self.Cancel_cb)
# QtCore.QMetaObject.connectSlotsByName(self.dialog)
# self.tab.setCurrentWidget(self.dialog)
# return self.dialog
# except Exception as err:
# App.Console.PrintError("'Activated' Failed. "
# "{err}\n".format(err=str(err)))
# exc_type, exc_obj, exc_tb = sys.exc_info()
# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
# print(exc_type, fname, exc_tb.tb_lineno)
# def hide(self):
# try:
# self.dialog.hide()
# del self.dialog
# dw = self.mw.findChildren(QtGui.QDockWidget)
# newsize = self.tab.count()
# self.tab.removeTab(newsize-1) # it ==0,1,2,3 ..etc
# faced.showFirstTab()
# except Exception as err:
# App.Console.PrintError("'Hide' Failed. "
# "{err}\n".format(err=str(err)))
# exc_type, exc_obj, exc_tb = sys.exc_info()
# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
# print(exc_type, fname, exc_tb.tb_lineno)
# def EqualizeFaces(self):
# App.ActiveDocument.openTransaction(
# translate("Design456", "EqualizeFaces")) #Record undo
# newShape= App.ActiveDocument.addObject('Part::Feature',self.sel2.Name)
# newShape.Shape=self.sel1.Shape.copy()
# App.ActiveDocument.recompute()
# pl1=self.sel1.Placement
# # X1min=self.sel1.Shape.BoundBox.XMin
# # Y1min=self.sel1.Shape.BoundBox.YMin
# # Z1min=self.sel1.Shape.BoundBox.ZMin
# # X1max=self.sel1.Shape.BoundBox.XMax
# # Y1max=self.sel1.Shape.BoundBox.YMax
# # Z1max=self.sel1.Shape.BoundBox.ZMax
# # X2min=self.sel2.Shape.BoundBox.XMin
# # Y2min=self.sel2.Shape.BoundBox.YMin
# # Z2min=self.sel2.Shape.BoundBox.ZMin
# # X2max=self.sel2.Shape.BoundBox.XMax
# # Y2max=self.sel2.Shape.BoundBox.YMax
# # Z2max=self.sel2.Shape.BoundBox.ZMax
# newShape.Placement=self.sel2.Placement.copy()
# newShape.Placement.Rotation.Axis=self.Shape.CenterOfMass
# newShape.Placement.Rotation.Angle=self.Shape.SubShapes[0].Surface.Rotation.Angle
# App.ActiveDocument.removeObject(self.sel2.Name)
# App.ActiveDocument.recompute()
# App.ActiveDocument.commitTransaction() # undo reg.de here
# def OK_cb(self):
# self.EqualizeFaces()
# self.hide()
# def Cancel_cb(self):
# self.hide()
# def GetResources(self):
# return{
# 'Pixmap': Design456Init.ICON_PATH + 'EqualizeFaces.svg',
# 'MenuText': 'EqualizeFaces',
# 'ToolTip': 'EqualizeFaces between 2-2D Faces'
# }
# Gui.addCommand('Design456_EqualizeFaces', Design456_EqualizeFaces())
# ##############################
"""Design456 Part 2D Tools"""
class Design456_2DToolsGroup:
def __init__(self):
return
"""Gui command for the group of 2D tools."""
def GetCommands(self):
"""2D Face commands."""
return ("Design456Part_Surface",
"Design456_ArcFace6Points",
"Design456_CombineFaces",
"Design456_SubtractFaces",
"Design456_CommonFace",
"Design456_SegmentAFace",
"Design456_MeshCircleToCircle",
#"Design456_EqualizeFaces",
)
def GetResources(self):
import Design456Init
from PySide.QtCore import QT_TRANSLATE_NOOP
"""Set icon, menu and tooltip."""
_tooltip = ("Different Tools for modifying 2D Shapes")
return {'Pixmap': Design456Init.ICON_PATH + 'Design456_2DTools.svg',
'MenuText': QT_TRANSLATE_NOOP("Design456", "2Dtools"),
'ToolTip': QT_TRANSLATE_NOOP("Design456", _tooltip)}
Gui.addCommand("Design456_2DToolsGroup", Design456_2DToolsGroup())