-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRotate.py
136 lines (101 loc) · 4.74 KB
/
Rotate.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
# -*- coding: utf-8 -*-
__title__ = "Create Rotation"
__author__ = "Andrew Shkolik & Andrei Bezborodov"
__license__ = "LGPL 2.1"
__doc__ = "Rotate selected body."
__usage__ = """Select target body to rotate and activate tool."""
import FreeCAD
App=FreeCAD
import FreeCADGui
Gui=FreeCADGui
import FoamCutViewProviders
import FoamCutBase
import utilities
class Rotation(FoamCutBase.FoamCutBaseObject):
def __init__(self, obj, source, jobName):
super().__init__(obj, jobName)
obj.Type = "Rotation"
obj.addProperty("App::PropertyDistance", "OriginRotationX", "", "", 5)
obj.addProperty("App::PropertyLink", "Source", "Task", "Source object").Source = source
obj.addProperty("App::PropertyAngle", "Angle", "Task", "Rotate object by angle").Angle = 90
config = self.getConfigName(obj)
obj.setExpression(".OriginRotationX", u"<<{}>>.OriginRotationX".format(config))
obj.Proxy = self
self.execute(obj)
def execute(self, obj):
if obj.Source is None:
return
obj.Source.ViewObject.Visibility = False
# - Copy and rotate object
shape = obj.Source.Shape.copy()
shape.rotate(App.Vector(0.0, obj.OriginRotationX, 0.0), App.Vector(0,0,1), obj.Angle)
# - Assign new shape
obj.Shape = shape
obj.Placement = shape.Placement
class RotationVP(FoamCutViewProviders.FoamCutBaseViewProvider):
def getIcon(self):
return utilities.getIconPath("rotation.svg")
def claimChildren(self):
return [self.Object.Source]
def onDelete(self, obj, subelements):
try:
self.Object.Source.ViewObject.Visibility = True
except Exception as err:
App.Console.PrintError("Error in onDelete: {0} \n".format(err))
return True
class AddRotation():
"""Add Rotation"""
def GetResources(self):
return {"Pixmap" : utilities.getIconPath("rotation.svg"), # the name of a svg file available in the resources
'Accel' : "", # a default shortcut (optional)
"MenuText": "Rotate target",
"ToolTip" : "Rotate target object by given angle around rotation axis"}
def Activated(self):
group = Gui.ActiveDocument.ActiveView.getActiveObject("group")
setActive = False
# - if machine is not active, try to select first one in a document
if group is None or group.Type != "Job":
group = App.ActiveDocument.getObject("Job")
setActive = True
if group is not None and group.Type == "Job":
if setActive:
Gui.ActiveDocument.ActiveView.setActiveObject("group", group)
source = Gui.Selection.getSelectionEx()[0].Object
# - Create rotation object
rt = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Rotation")
Rotation(rt, source, group.Name)
RotationVP(rt.ViewObject)
group.addObject(rt)
Gui.Selection.clearSelection()
FreeCAD.ActiveDocument.recompute()
def IsActive(self):
if FreeCAD.ActiveDocument is None:
return False
else:
group = Gui.ActiveDocument.ActiveView.getActiveObject("group")
# - if machine is not active, try to select first one in a document
if group is None or group.Type != "Job":
group = App.ActiveDocument.getObject("Job")
if group is not None and group.Type == "Job":
config = group.getObject(group.ConfigName)
if not config.FiveAxisMachine:
return False
# - Get selecttion
selection = Gui.Selection.getSelectionEx()
# - nothing selected
if len(selection) == 0:
return False
# - Check object type
if selection[0] is None or selection[0].Object is None:
return False
obj = selection[0].Object
if not hasattr(obj, "Shape"):
return False
print(obj.TypeId)
if (hasattr(obj, "Type") and
(obj.Type == "Path" or obj.Type == "Enter" or obj.Type == "Job" or obj.Type == "Helper" or obj.Type == "Config"
or obj.Type == "Exit" or obj.Type == "Move" or obj.Type == "Join" or obj.Type == "Route" or obj.Type == "Projection")):
return False
return True
return False
Gui.addCommand("Rotate", AddRotation())