forked from JeromeL63/Wood-Frame
/
WFUtils.py
281 lines (215 loc) · 8.82 KB
/
WFUtils.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
# -*- coding: utf-8 -*-
# FreeCAD init script of the Wood Frame module
# (c) 2019 Jerome Laverroux
# ***************************************************************************
# * (c) Jerome Laverroux (jerome.laverroux@free.fr) 2019 *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser 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. *
# * *
# * FreeCAD 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 Library General Public *
# * License along with FreeCAD; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# * Jerome Laverroux 2019 *
# ***************************************************************************/
__title__ = "FreeCAD Wood Frame API"
__author__ = "Jerome Laverroux"
__url__ = "http://www.freecadweb.org"
import FreeCAD
import Draft
from FreeCAD import Base
import os
from math import *
if FreeCAD.GuiUp:
import FreeCADGui,DraftTrackers,DraftVecUtils
__dir__ = os.path.dirname(__file__)
def QT_TRANSLATE_NOOP(ctx,txt): return txt # dummy function for the QT translator
def alignView():
#function to align view to the current WorkingPlane
c = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
r = FreeCAD.DraftWorkingPlane.getRotation().Rotation.Q
c.orientation.setValue(r)
def getTagList():
#function to retrieve tags in document
taglist = []
for obj in FreeCAD.ActiveDocument.Objects :
if hasattr(obj,"Tag"):
if not obj.Tag in taglist:
taglist.append(str(obj.Tag))
return taglist
def listFilter(items):
#Function to list selection by tag
doc = FreeCAD.ActiveDocument
objs = doc.Objects
objlist = []
for item in items :
if item == "Selection":
for obj in FreeCADGui.Selection.getSelection() :
objlist.append(obj)
taglist = getTagList()
for tag in taglist :
if item == tag :
for obj in objs:
if FreeCADGui.ActiveDocument.getObject(obj.Name).Visibility:
if hasattr(obj,"Proxy"):
if hasattr(obj.Proxy,"Type"):
if hasattr(obj,"Tag"):
if obj.Tag == item :
objlist.append(obj)
s = []
for i in objlist:
if i not in s:
s.append(i)
return s
class WFCopy():
"""WFrameAttributes"""
def GetResources(self):
return {'Pixmap': __dir__ + '/Resources/icons/WFCopy.svg',
'Accel': "6",
'MenuText': "Copy Object",
'ToolTip': "Copy Object"}
def Activated(self):
self.tracker = DraftTrackers.lineTracker()
self.tracker.on()
self.objList=FreeCADGui.Selection.getSelection()
FreeCADGui.Snapper.getPoint(callback=self.getBasePoint,title="Base Point")
def getBasePoint(self,point,obj=None):
if point == None:
self.tracker.finalize()
else:
self.basePoint=point
FreeCADGui.Snapper.getPoint(last=self.basePoint, callback=self.getPoint, movecallback=self.update,title="Translation vector")
def getPoint(self,point,obj=None):
if point == None:
self.tracker.finalize()
else:
copySelection(base=self.basePoint,end=point,objlist=self.objList)
def update(self, point, info):
pass
def IsActive(self):
"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
if FreeCADGui.ActiveDocument:
return len(FreeCADGui.Selection.getSelection()) > 0
else:
return False
def copySelection(base=FreeCAD.Vector(0, 0, 0), end=FreeCAD.Vector(0, 0, 0), objlist=None, number=1):
'''
Function to copy objects
:param base: base point of displacement vector
:param end: end point of displacement vector
:param objlist: List of objects
:param number: Number of copies
'''
vec = Base.Vector(0, 0, 0)
vec.x = end.x - base.x
vec.y = end.y - base.y
vec.z = end.z - base.z
if objlist:
print ("launch copies")
for i in range(0, number):
objlist = Draft.move(objlist, vec, copy=True)
FreeCAD.ActiveDocument.recompute()
FreeCADGui.addCommand('WF_Copy', WFCopy())
def offset(height,width,orientation=0,position=5):
'''
This function return an offset point calculated with height width and orientation
:param height: part's height
:param width: part's width
:param orientation: orientation between 0 to 2
:param position: from 1 to 9 equal numpad positions
:return: Base.Vector of the offset
'''
out=Base.Vector(0,0,0)
#TODO dashed and dashdot line style doesn't work properly
if position == 1:
if orientation == 2:
out=Base.Vector(-width / 2, -height / 2, 0)
else:
out=Base.Vector(0, -height / 2, -width / 2)
#self.beam.setSolid()
elif position == 2:
if orientation == 2:
out=Base.Vector(0, -height / 2, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, -height / 2, 0)
#self.beam.setDashDot()
elif position == 3:
if orientation == 2:
out=Base.Vector(width / 2, -height / 2, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, -height / 2, width / 2)
#self.beam.setDashed()
elif position == 4:
if orientation == 2:
out=Base.Vector(-width / 2, 0, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, 0, -width / 2)
#self.beam.setSolid()
elif position == 5:
if orientation == 2:
out=Base.Vector(0, 0, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, 0, 0)
#self.beam.setDashDot()
elif position == 6:
if orientation == 2:
out=Base.Vector(width / 2, 0, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, 0, width / 2)
#self.beam.setDashed()
elif position == 7:
if orientation == 2:
out=Base.Vector(-width / 2, height / 2, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, height / 2, -width / 2)
#self.beam.setSolid()
elif position == 8:
if orientation == 2:
out=Base.Vector(0, height / 2, 0)
#self.beam.setSolid()
else:
out=Base.Vector(0, height / 2, 0)
#self.beam.setDashDot()
elif position == 9:
if orientation == 2:
out=Base.Vector(width / 2, height / 2, 0)
else:
out=Base.Vector(0, height / 2, width / 2)
#self.beam.setDashed()
return out
def setRotations(structure=None,points=[Base.Vector(0,0,0),Base.Vector(0,0,0)],wplan=None):
'''Rotate structure by the given two points on the current workingplane wplan'''
import DraftVecUtils,Draft
vecAngle = FreeCAD.Vector(0, 0, 0)
vecAngle[0] = points[1][0] - points[0][0]
vecAngle[1] = points[1][1] - points[0][1]
vecAngle[2] = points[1][2] - points[0][2]
# along workingplane normal
angle = DraftVecUtils.angle(wplan.u, vecAngle, wplan.axis)
angle = degrees(angle)
# rotate in the current working plane
Draft.rotate(structure, angle, center=points[0], axis=wplan.getNormal(), copy=False)
return structure
def toNormalizedVector(vec,lg):
return FreeCAD.Vector(vec[0] / lg,vec[1] /lg,vec[2]/lg)
def normalizedToVector(vec,lg):
return FreeCAD.Vector(vec[0] * lg, vec[1] * lg, vec[2] * lg)