Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to coalesce redundant edges #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions lasercut/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,47 @@ def get_matrix_transform(face):

return m

def normaliseEdge(edge):
sizeX = edge.BoundBox.XMax - edge.BoundBox.XMin
sizeY = edge.BoundBox.YMax - edge.BoundBox.YMin
sizeZ = edge.BoundBox.ZMax - edge.BoundBox.ZMin

return FreeCAD.Vector(sizeX / edge.Length, sizeY / edge.Length, sizeZ / edge.Length )

def get_local_axis(face):
list_edges = Part.__sortEdges__(face.Edges)

coalescedEdges = []
previousEdge = None
previousEdgeGradient = None
for edge in list_edges:
edgeGradient = normaliseEdge(edge)

if previousEdge is not None:
# If this edge is the same direction as the previous edge, then we can merge the two edges together.
if edgeGradient == previousEdgeGradient:
ls = Part.LineSegment( coalescedEdges[-1].Vertexes[0].Point, FreeCAD.Vector(edge.Vertexes[1].X, edge.Vertexes[1].Y, edge.Vertexes[1].Z) )
coalescedEdges[-1] = Part.Edge(ls)
else:
coalescedEdges.append(edge)
else:
coalescedEdges.append(edge)

previousEdgeGradient = edgeGradient
previousEdge = edge

# And check in case the last edge is an extension of the first.
lastGradient = normaliseEdge(coalescedEdges[-1])
firstGradient = normaliseEdge(coalescedEdges[0])

if lastGradient == firstGradient:
ls = Part.LineSegment( coalescedEdges[-1].Vertexes[0].Point, FreeCAD.Vector(coalescedEdges[0].Vertexes[1].X, coalescedEdges[0].Vertexes[1].Y, coalescedEdges[0].Vertexes[1].Z) )
coalescedEdges[-1] = Part.Edge(ls)

coalescedEdges.remove(coalescedEdges[0])

list_edges = coalescedEdges

list_points = sort_quad_vertex(list_edges, False)
if list_points is None:
list_points = sort_quad_vertex(list_edges, True)
Expand All @@ -182,9 +220,9 @@ def get_local_axis(face):
x_local = normal_face.normalize()
z_local_not_normalized = None
y_local_not_normalized = None
for x in range(0, 4):
vector1 = list_points[(x + 1) % 4] - list_points[x]
vector2 = list_points[(x - 1) % 4] - list_points[x]
for x in range(0, len(list_edges)):
vector1 = list_points[(x + 1) % len(list_edges)] - list_points[x]
vector2 = list_points[(x - 1) % len(list_edges)] - list_points[x]
y_local = None
z_local = None
if vector1.Length >= vector2.Length:
Expand All @@ -211,7 +249,6 @@ def get_local_axis(face):

return None, None, None


def get_local_axis_normalized(face):
x_local, y_local_not_normalized, z_local_not_normalized = get_local_axis(face)
y_local_not_normalized.normalize()
Expand Down