From cd4a931002bcb765815f369f668a789cc7ad1110 Mon Sep 17 00:00:00 2001 From: Jroc Date: Fri, 3 Jun 2016 14:13:54 -0400 Subject: [PATCH] Simplified datapoints in a chunk to avoid Shapely error - shapely.geos.TopologicalError: The operation 'GEOSDifference_r' produced a null geometry. Likely cause is invalidity of the geometry - should fix problems like this: https://github.com/vilemnovak/blendercam/issues/24 --- .gitignore | 1 + scripts/addons/cam/chunk.py | 122 +++++++++++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 05c75fe8..004b49e7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ config/bookmarks.txt config/recent-files.txt scripts/addons_contrib/add_mesh_rocks/add_mesh_rocks.xml scripts/addons_contrib/online_mat_lib/material-library/bundled/cycles/wood/rough_pine.bcm +acp diff --git a/scripts/addons/cam/chunk.py b/scripts/addons/cam/chunk.py index 4ece61cd..cba9278c 100644 --- a/scripts/addons/cam/chunk.py +++ b/scripts/addons/cam/chunk.py @@ -740,8 +740,125 @@ def chunksToShapely(chunks):#this does more cleve chunks to Poly with hierarchie print('addparent') #polygon_utils_cam.shapelyToCurve('crust',ch.parents[0].poly,0) #polygon_utils_cam.shapelyToCurve('hole',ch.poly,0) - ch.parents[0].poly = ch.parents[0].poly.difference(ch.poly)#sgeometry.Polygon( ch.parents[0].poly, ch.poly) - + try: + ch.parents[0].poly = ch.parents[0].poly.difference(ch.poly)#sgeometry.Polygon( ch.parents[0].poly, ch.poly) + except: + + + print('chunksToShapely oops!') + + lastPt = False + tolerance = 0.0000003 + newPoints = [] + + for pt in ch.points: + toleranceXok = True + toleranceYok = True + #print( '{0:.9f}, {1:.9f}, {2:.9f}'.format(pt[0], pt[1], pt[2]) ) + #print(pt) + if lastPt: + #print( 'Distance Vector: {0:.9f}, {1:.9f}'.format(abs(pt[0] - lastPt[0]), abs(pt[1] - lastPt[1])) ) + #print( 'pt[0] is {} tolerance'.format( 'OUT of' if abs( pt[0] - lastPt[0] ) < tolerance else 'IN' ) ) + #print( 'pt[1] is {} tolerance'.format( 'OUT of' if abs( pt[1] - lastPt[1] ) < tolerance else 'IN' ) ) + if ( abs( pt[0] - lastPt[0] ) < tolerance ): + toleranceXok = False + if ( abs( pt[1] - lastPt[1] ) < tolerance ): + toleranceYok = False + + if ( toleranceXok or toleranceYok ): + #print('point ok, including') + #ch.points.remove( pt ) + newPoints.append( pt ) + lastPt = pt + else: + newPoints.append( pt ) + lastPt = pt + + toleranceXok = True + toleranceYok = True + if ( abs( newPoints[0][0] - lastPt[0] ) < tolerance ): + toleranceXok = False + if ( abs( newPoints[0][1] - lastPt[1] ) < tolerance ): + toleranceYok = False + + if ( not toleranceXok and not toleranceYok ): + newPoints.pop() + #print('starting and ending points too close, removing ending point') + + #if ( abs( lastPt[0] - ch.points[0][0] ) < tolerance ): + #if ( abs( lastPt[1] - ch.points[0][1] ) < tolerance ): + #newPoints.pop() + #print('starting and ending points too close, removing ending point') + + ch.points = newPoints + ch.poly = sgeometry.Polygon(ch.points) + + try: + ch.parents[0].poly = ch.parents[0].poly.difference(ch.poly)#sgeometry.Polygon( ch.parents[0].poly, ch.poly) + + except: + + #print('chunksToShapely double oops!') + + lastPt = False + tolerance = 0.0000003 + newPoints = [] + + for pt in ch.parents[0].points: + toleranceXok = True + toleranceYok = True + #print( '{0:.9f}, {0:.9f}, {0:.9f}'.format(pt[0], pt[1], pt[2]) ) + #print(pt) + if lastPt: + #print( 'Distance Vector: {0:.9f}, {0:.9f}'.format((pt[0] - lastPt[0]), (pt[1] - lastPt[1])) ) + if ( abs( pt[0] - lastPt[0] ) < tolerance ): + toleranceXok = False + if ( abs( pt[1] - lastPt[1] ) < tolerance ): + toleranceYok = False + + if ( toleranceXok or toleranceYok ): + #print('point ok, including') + #ch.points.remove( pt ) + newPoints.append( pt ) + lastPt = pt + else: + newPoints.append( pt ) + lastPt = pt + + + toleranceXok = True + toleranceYok = True + if ( abs( newPoints[0][0] - lastPt[0] ) < tolerance ): + toleranceXok = False + if ( abs( newPoints[0][1] - lastPt[1] ) < tolerance ): + toleranceYok = False + + if ( not toleranceXok and not toleranceYok ): + newPoints.pop() + #print('starting and ending points too close, removing ending point') + + #if ( abs( lastPt[0] - ch.parents[0].points[0][0] ) < tolerance ): + #if ( abs( lastPt[1] - ch.parents[0].points[0][1] ) < tolerance ): + #newPoints.pop() + #print('starting and ending points too close, removing ending point') + + ch.parents[0].points = newPoints + ch.parents[0].poly = sgeometry.Polygon(ch.parents[0].points) + + ch.parents[0].poly = ch.parents[0].poly.difference(ch.poly)#sgeometry.Polygon( ch.parents[0].poly, ch.poly) + + #p=spolygon.Polygon(ch.points) + #p.simplify(1.0, preserve_topology=False) + #sc=shapelyToChunks(p,0) + + #pParent=spolygon.Polygon(ch.parents[0].points) + #pParent.simplify(1.0, preserve_topology=False) + #scParent=shapelyToChunks(pParent,0) + + + #ch.parents[0].poly = scParent[0].poly.difference(sc[0].poly) + + #print('points dumped.!') returnpolys=[] @@ -759,6 +876,7 @@ def chunksToShapely(chunks):#this does more cleve chunks to Poly with hierarchie return returnpolys + def meshFromCurveToChunk(object):