-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathHelperFace.py
More file actions
489 lines (394 loc) · 16.3 KB
/
Copy pathPathHelperFace.py
File metadata and controls
489 lines (394 loc) · 16.3 KB
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
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2020 Daniel Wood <s.d.wood.82@googlemail.com> *
# * *
# * 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. *
# * *
# * This program 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD, FreeCADGui
import Part
import os, math
from PySide.QtGui import QMessageBox
import PathScripts.PathUtils as PathUtils
import PathHelperFaceGui
__dir__ = os.path.dirname(__file__)
iconPath = os.path.join( __dir__, 'Icons' )
class HelperEdge:
def __init__(self, edge, obj, fixedEdge = False):
self._edge = edge
self._obj = obj
self._extendable = False
self._fixedEdge = fixedEdge
def _getEdge(self):
return self._edge
def _getMidParam(self):
midparam = self._edge.FirstParameter + 0.5 * (self._edge.LastParameter - self._edge.FirstParameter)
return midparam
def _getMidPnt(self):
''' get the mid point '''
midpnt = self._edge.valueAt(self._getMidParam())
return midpnt
def _rotate(self, vec, angle):
''' rotate point by angle in radians '''
x = vec.x * math.cos(angle) - vec.y * math.sin(angle)
y = vec.x * math.sin(angle) + vec.y * math.cos(angle)
return FreeCAD.Vector(x, y, vec.z)
def _getPerpNormal(self):
''' get edge perpendicular normal at the mid point in the open direction'''
midpnt = self._getMidPnt()
axialNorm = self._edge.tangentAt(self._getMidParam())
perpNormal = self._rotate(axialNorm, 1.5708)
poffPlus = midpnt + 0.01 * perpNormal
poffMinus = midpnt - 0.01 * perpNormal
if self._obj.Shape.isInside(poffPlus, 0.005, False):
perpNormal = perpNormal.negative()
if self._obj.Shape.isInside(poffMinus, 0.005, False):
pass
return perpNormal
def _isExtendable(self):
''' check if the edge is extendable i.e. constrained by a connected face'''
if self._fixedEdge:
return False
extendable = True
midpnt = self._getMidPnt()
perpNormal = self._getPerpNormal()
poffPlus = midpnt + 0.01 * perpNormal
poffMinus = midpnt - 0.01 * perpNormal
if self._obj.Shape.isInside(poffPlus, 0.005, False):
extendable = False
if self._obj.Shape.isInside(poffMinus, 0.005, False):
extendable = False
return extendable
class HelperFace:
def __init__(self, obj, baseFace, toolController=None):
obj.addProperty('App::PropertyLinkSub', 'BaseFace', 'Base', 'faceName').BaseFace = baseFace
obj.addProperty('App::PropertyFloat', 'ExtraDist', 'Base', 'Additional Offset')
obj.addProperty('App::PropertyIntegerList', 'CheckedEdges', 'Base', 'list of edges to extend')
obj.addProperty('App::PropertyIntegerList', 'ExtendableEdges', 'Base', 'list of edges that can be extended')
obj.addProperty("App::PropertyLink", "ToolController", "Base", "The tool controller that will be used to calculate the path")
if toolController:
obj.ToolController = toolController
obj.Proxy = self
obj.setEditorMode('CheckedEdges', 2)
obj.setEditorMode('ExtendableEdges', 2)
def onChanged(self, obj, prop):
'''Do something when a property has changed'''
#FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
pass
def onDocumentRestored(self, obj):
'''Do something when a document is restored'''
pass
def execute(self, obj):
""" Called on document recompute """
edgeManager = HelperEdgeManager()
helperEdges = edgeManager.getEdges(obj.BaseFace)
if len(helperEdges) < 3:
FreeCAD.Console.PrintError('Helper Face Generation Failed\n')
return
edges = []
extendableEdges = []
for idx, helperEdge in enumerate(helperEdges):
if helperEdge._isExtendable():
extendableEdges.append(idx+1)
edge = helperEdge._getEdge()
edges.append(edge)
extendDist = 0
if obj.ToolController:
toolRad = obj.ToolController.Tool.Diameter * 0.5
extendDist = toolRad
extendDist += obj.ExtraDist
obj.ExtendableEdges = extendableEdges
newFace = edgeManager.createFace(edges)
obj.Shape = edgeManager.extendFace(edges, obj.CheckedEdges, newFace, extendDist)
class ViewProviderHelperFace:
def __init__(self, obj):
"""
Set this object to the proxy object of the actual view provider
"""
obj.Proxy = self
def attach(self, obj):
"""
Setup the scene sub-graph of the view provider, this method is mandatory
"""
return
def setEdit(self, vobj, mode=0):
# pylint: disable=unused-argument
panel = PathHelperFaceGui.PathHelperPanel(vobj.Object)
FreeCADGui.Control.showDialog(panel)
return False
def unsetEdit(self, vobj, mode=0):
# pylint: disable=unused-argument
return False
def getIcon(self):
return os.path.join( iconPath , 'Path_HelperFace.svg')
class HelperEdgeManager:
def __init__(self):
self.helperEdges = []
def showEdge(self, edges):
Wire = Part.Wire(edges)
edgeName = 'testEdge'
Part.show(Wire, edgeName)
FreeCAD.ActiveDocument.recompute()
def getEndPoints(self):
points = []
for helperEdge in self.helperEdges:
edge = helperEdge._getEdge()
for v in edge.Vertexes:
points.append(v.Point)
### Find the two points that are not connected ###
endPoints = []
for p in points:
if points.count(p) == 1:
endPoints.append(Part.Vertex(p))
return endPoints
def getEdges(self, baseFace):
model = baseFace[0]
face = model.Shape.getElement(baseFace[1][0])
wire = face.OuterWire
job = PathUtils.findParentJob(model)
stock = job.Stock
bb = stock.Shape.BoundBox
for edge in wire.Edges:
newEdge = HelperEdge(edge, model)
if not newEdge._isExtendable():
self.helperEdges.append(newEdge)
###### get boundingbox edges at face z height ######
bbEdges=[]
bbz = face.BoundBox.ZMax
bbEdges.append(Part.Edge(Part.LineSegment(FreeCAD.Vector(bb.XMin,bb.YMin,bbz), FreeCAD.Vector(bb.XMin,bb.YMax,bbz))))
bbEdges.append(Part.Edge(Part.LineSegment(FreeCAD.Vector(bb.XMin,bb.YMax,bbz), FreeCAD.Vector(bb.XMax,bb.YMax,bbz))))
bbEdges.append(Part.Edge(Part.LineSegment(FreeCAD.Vector(bb.XMax,bb.YMax,bbz), FreeCAD.Vector(bb.XMax,bb.YMin,bbz))))
bbEdges.append(Part.Edge(Part.LineSegment(FreeCAD.Vector(bb.XMax,bb.YMin,bbz), FreeCAD.Vector(bb.XMin,bb.YMin,bbz))))
if not len(self.helperEdges):
FreeCAD.Console.PrintWarning('Open Face Selected')
objBBz = model.Shape.BoundBox.ZMax
if round(bbz, 5) == round(objBBz, 5):
FreeCAD.Console.PrintWarning('Top face of object selected')
for edge in bbEdges:
newEdge = HelperEdge(edge, model)
self.helperEdges.append(newEdge)
# Check if the helper edges are available and return
if len(self.helperEdges):
return self.helperEdges
endPoints = self.getEndPoints()
if not endPoints:
## No end points generated, face generation failed, return the list of helper edges and exit cleanly.
return self.helperEdges
else:
## if a single fixed edge cannot be generated return all the edges from the selected face
if len(endPoints) > 2:
self.helperEdges = []
for edge in wire.Edges:
newEdge = HelperEdge(edge, model)
self.helperEdges.append(newEdge)
return self.helperEdges
## get edge edges ##
stockIntersectPoints = []
bbStockEdges = []
bbConnEdges = []
tempEdges = self.helperEdges.copy()
for ep in endPoints:
intersections = []
for i, newEdge in enumerate(tempEdges):
edge = newEdge._getEdge()
for v in edge.Vertexes:
if self.isSamePoint(ep.Point, v.Point):
for bbedge in bbEdges:
## get the intersection point with the stock edge
if Part.Circle == type(edge.Curve):
## get the direction of the end points
tangent = edge.tangentAt(edge.FirstParameter).negative()
if self.isSamePoint(ep.Point, edge.lastVertex().Point):
tangent = edge.tangentAt(edge.LastParameter)
normal = tangent
# ## If the normal generates a point inside the model rotate the normal by 90 deg
testPoint = ep.Point + 0.01 * tangent
## if the test point is inside the model take the normal from the circle centre to the end point.
if model.Shape.isInside(testPoint, 0.005, True):
normal = ep.Point.sub(edge.Curve.Location).normalize()
endPoint = ep.Point + 5 * normal
tempEdge = Part.Edge(Part.LineSegment(ep.Point, FreeCAD.Vector(endPoint.x, endPoint.y, endPoint.z)))
intersectPts = tempEdge.Curve.intersectCC(bbedge.Curve)
else:
## edge is a line
## get the normal if the element is a line
normal = edge.lastVertex().Point.sub(edge.firstVertex().Point)
if self.isSamePoint(ep.Point, edge.firstVertex().Point):
normal = edge.firstVertex().Point.sub(edge.lastVertex().Point)
normal = normal.normalize()
intersectPts = edge.Curve.intersectCC(bbedge.Curve)
if len(intersectPts):
for pnt in intersectPts:
tempPnt = FreeCAD.Vector(pnt.X, pnt.Y, pnt.Z)
posOnEdge = bbedge.Curve.parameter(tempPnt)
if not posOnEdge < 0 and not posOnEdge > bbedge.LastParameter:
intersections.append(pnt)
bbStockEdges.append(bbedge)
vectorCompare = tempPnt.sub(v.Point).normalize()
dist = vectorCompare.sub(normal).Length
if dist < 0.01:
extEdge = Part.Edge(Part.LineSegment(ep.Point, tempPnt))
newEdge = HelperEdge(extEdge, model, True)
self.helperEdges.append(newEdge)
bbConnEdges.append(bbedge)
stockIntersectPoints.append(tempPnt)
###### check if the bb edges are the same edge ######
if len(bbConnEdges) == 2:
if bbConnEdges[0] == bbConnEdges[1]:
###### Create new connecting edge and add to the list of edges ######
closeEdge = Part.Edge(Part.LineSegment(stockIntersectPoints[0], stockIntersectPoints[1]))
newEdge = HelperEdge(closeEdge, model)
self.helperEdges.append(newEdge)
else:
###### check if the bb edges are connected ######
bbEdgesConnected = False
for v1 in bbConnEdges[0].Vertexes:
for v2 in bbConnEdges[1].Vertexes:
if self.isSamePoint(v1.Point, v2.Point):
bbEdgesConnected = True
###### Create new edges to the stock and add to the list of edges ######
for pnt in stockIntersectPoints:
edge = Part.Edge(Part.LineSegment(pnt, v1.Point))
newEdge = HelperEdge(edge, model)
self.helperEdges.append(newEdge)
if not bbEdgesConnected:
offsetDir = FreeCAD.Vector()
edgeNormals = []
for helperEdge in self.helperEdges:
edgeNormals.append(helperEdge._getPerpNormal())
for edgeVec in edgeNormals:
offsetDir.x += edgeVec.x
offsetDir.y += edgeVec.y
offsetDir.z += edgeVec.z
offsetDir = offsetDir.normalize()
closingPts = []
for i, pnt in enumerate(stockIntersectPoints):
for v in bbConnEdges[i].Vertexes:
vectorCompare = v.Point.sub(pnt).normalize()
dist = vectorCompare.sub(offsetDir).Length
#if self.isSamePoint(offsetDir, vectorCompare):
if dist < 0.5: #TODO: Is checking the dist robust?
closingPts.append(v.Point)
edge = Part.Edge(Part.LineSegment(pnt, v.Point))
newEdge = HelperEdge(edge, model)
self.helperEdges.append(newEdge)
if len(closingPts) == 2:
edge = Part.Edge(Part.LineSegment(closingPts[0], closingPts[1]))
newEdge = HelperEdge(edge, model)
self.helperEdges.append(newEdge)
if len(self.helperEdges):
self.sortEdges()
return self.helperEdges
return face.Edges
def sortEdges(self):
''' sort the helper edges into a continous loop '''
fcEdges = []
for helperEdge in self.helperEdges:
edge = helperEdge._getEdge()
fcEdges.append(edge)
sortedEdges = Part.__sortEdges__(fcEdges)
sortedHelperEdges = []
for edge in sortedEdges:
for helperEdge in self.helperEdges:
v1_1 = edge.firstVertex()
v1_2 = edge.lastVertex()
v2_1 = helperEdge._getEdge().firstVertex()
v2_2 = helperEdge._getEdge().lastVertex()
if self.isSamePoint(v1_1.Point, v2_1.Point) and self.isSamePoint(v1_2.Point, v2_2.Point) \
or self.isSamePoint(v1_1.Point, v2_2.Point) and self.isSamePoint(v1_2.Point, v2_1.Point):
sortedHelperEdges.append(helperEdge)
self.helperEdges = sortedHelperEdges
def createFace(self, edges):
''' create a new face using from the supplied edges'''
finalWire = Part.Wire(edges)
if not finalWire.isClosed():
FreeCAD.Console.PrintError('Face Creation failed - wire not closed')
self.showEdge(edges)
return None
else:
nface = Part.Face(finalWire, "Part::FaceMakerBullseye")
return nface
def isSamePoint(self, pt1, pt2):
''' Checks if two points share the same coordinates '''
if round(pt1.x, 5) == round(pt2.x, 5):
if round(pt1.y, 5) == round(pt2.y, 5):
if round(pt1.z, 5) == round(pt2.z, 5):
return True
return False
def extendFace(self, edges, checkedEdges, face, extendDist=0):
''' extend the selected edges '''
newEdges = edges
if len(checkedEdges):
for e in checkedEdges:
origEdge = newEdges[int(e) -1]
if origEdge:
p1 = origEdge.Vertexes[0].Point
p2 = origEdge.Vertexes[1].Point
## work out the direction the edge needs to be extended
vec = p2.sub(p1)
offsetDir = self.rotate(vec, 1.5708)
offsetDir = offsetDir.normalize()
cen = face.BoundBox.Center
if cen.distanceToPoint(p1.add(offsetDir)) < cen.distanceToPoint(p1):
offsetDir = offsetDir.negative()
## the direction is a normalised vector. multiply that by the distance required
offset = offsetDir.multiply(extendDist)
for i, repEdge in enumerate(newEdges):
rev = repEdge.Vertexes
for j, v in enumerate(rev):
jOpp = 1 - j
if self.isSamePoint(v.Point, p1) or self.isSamePoint(v.Point, p2):
tempVerts = newEdges[i].Vertexes
newEdge = Part.Edge(Part.LineSegment(tempVerts[j].Point.add(offset), tempVerts[jOpp].Point))
newEdges.pop(i)
newEdges.insert(i, newEdge)
## clear the selection to ensure no weird graphics
FreeCADGui.Selection.clearSelection()
newFace = self.createFace(newEdges)
if not newFace:
FreeCAD.Console.PrintError('Face Extension Failed')
else:
return newFace
def rotate(self, vec, angle):
''' rotate the vector by the supplied angle in radians '''
x = vec.x * math.cos(angle) - vec.y * math.sin(angle)
y = vec.x * math.sin(angle) + vec.y * math.cos(angle)
return FreeCAD.Vector(x, y, vec.z)
def create(baseFace):
model = baseFace[0]
job = PathUtils.findParentJob(model)
doc = model.Document
if job is None:
#msgBox = QMessageBox()
#msgBox.setText("Select Face from Job Model")
#msgBox.exec_()
QMessageBox.warning(None, "Invalid Model", "Select Face from a model within the job object")
return None
helperGrpName = job.Name + '_HelperGeometry'
helperGrp = doc.getObject(helperGrpName)
if not helperGrp:
helperGrp = doc.addObject("App::DocumentObjectGroup", helperGrpName)
objName = model.Name
faceName = baseFace[1]
modelFaceName = objName + '.' + faceName
helperFaceName = modelFaceName + '_Helper'
obj = helperGrp.newObject('Part::FeaturePython', helperFaceName)
HelperFace(obj, baseFace)
ViewProviderHelperFace(obj.ViewObject)
FreeCAD.ActiveDocument.recompute()
return obj