-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDesign456_Magnet.py
92 lines (86 loc) · 4.32 KB
/
Design456_Magnet.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
# -*- 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
import Draft
import Part
import Design456Init
import FACE_D as faced
from draftutils.translate import translate #for translate
import math
__updated__ = '2022-07-29 09:46:24'
# Move an object to the location of the mouse click on another surface
class Design456_Magnet:
""" Magnet tool.
Use this tool to move any object to any face.
Select face1 (destination)
Select face2 (Source - Moved object)
Click the icon .. Done
"""
def Activated(self):
try:
s = Gui.Selection.getSelectionEx()
if (len(s) < 2):
# Two object must be selected
errMessage = "Select two or more objects to use Magnet Tool"
faced.errorDialog(errMessage)
return
App.ActiveDocument.openTransaction(translate("Design456", "Magnet"))
sub1 = s[0]
sub2 = s[1]
face1 = faced.getObjectFromFaceName(sub1, sub1.SubElementNames[0])
face2 = faced.getObjectFromFaceName(sub2, sub2.SubElementNames[0])
norm1 = face1.normalAt(0.0,0.0)*(-1)
norm2 = face2.normalAt(0.0,0.0)*(-1)
ang= math.degrees(norm1.getAngle(norm2))
sub2.Object.Placement.Base = face1.Surface.Position
ROTA= norm2.cross(norm1)
rotation = App.Rotation(ROTA,ang)
sub2.Object.Placement=App.Placement(face1.Surface.Position,ROTA,ang)
App.ActiveDocument.commitTransaction() # undo
App.ActiveDocument.recompute()
except Exception as err:
App.Console.PrintError("'Magnet' 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_Magnet.svg',
'MenuText': 'Part_Magnet',
'ToolTip': 'Part Magnet'
}
Gui.addCommand('Design456_Magnet', Design456_Magnet())
Design456_Magnet.__doc__ = """Magnet tool:
Use this tool for moving any object by
Selecting face1 of obj1 (destination)
Selecting face2 of obj2 (Source - Moved object)
Click the 'Magnet' icon .. Done
"""