From e3dee63e68f52581d0d511b37709b29d1f6222f5 Mon Sep 17 00:00:00 2001 From: opensourcecnc Date: Sun, 22 Sep 2024 15:04:09 +0300 Subject: [PATCH] Fix for the Profile/On Line and Curve to Path strategies not working when the curve contains splines made of polylines that have 2 points --- scripts/addons/cam/simple.py | 24 ++++++++++++++++++++++++ scripts/addons/cam/strategy.py | 11 +++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/addons/cam/simple.py b/scripts/addons/cam/simple.py index e63ab8c0..b30750cc 100644 --- a/scripts/addons/cam/simple.py +++ b/scripts/addons/cam/simple.py @@ -841,3 +841,27 @@ def active_to_shapely_poly(): """ # convert coordinates to shapely Polygon datastructure return Polygon(active_to_coords()) + + +#checks for curve splines shorter than three points and subdivides if necessary +def subdivide_short_lines(co): + """Subdivide all polylines to have at least three points. + + This function iterates through the splines of a curve, checks if they are not bezier + and if they have less or equal to two points. If so, each spline is subdivided to get + at least three points. + + Args: + co (Object): A curve object to be analyzed and modified. + """ + if bpy.context.active_object.mode != 'EDIT': + bpy.ops.object.mode_set(mode="EDIT") + for sp in co.data.splines: + if len(sp.points) <= 2 and sp.type != 'BEZIER': + bpy.ops.curve.select_all(action='DESELECT') + for pt in sp.points: + pt.select = True + bpy.ops.curve.subdivide() + if bpy.context.active_object.mode == 'EDIT': + bpy.ops.object.editmode_toggle() + bpy.ops.object.select_all(action='SELECT') diff --git a/scripts/addons/cam/strategy.py b/scripts/addons/cam/strategy.py index eb7c6254..5c3eda7c 100644 --- a/scripts/addons/cam/strategy.py +++ b/scripts/addons/cam/strategy.py @@ -49,6 +49,7 @@ from .simple import ( join_multiple, progress, remove_multiple, + subdivide_short_lines, ) from .utils import ( Add_Pocket, @@ -121,14 +122,10 @@ async def cutout(o): if ob.data.splines[0].type == 'BEZIER': activate(ob) bpy.ops.object.curve_remove_doubles(merg_distance=0.0001, keep_bezier=True) - elif len(ob.data.splines[0].points) <= 2: - bpy.ops.object.curve_remove_doubles() - bpy.ops.object.editmode_toggle() - bpy.ops.curve.select_all(action='SELECT') - bpy.ops.curve.subdivide() - bpy.ops.object.editmode_toggle() else: bpy.ops.object.curve_remove_doubles() + #make sure all polylines are at least three points long + subdivide_short_lines(ob) if o.cut_type == 'ONLINE' and o.onlycurves: # is separate to allow open curves :) print('separate') @@ -289,6 +286,8 @@ async def curve(o): raise CamException("All Objects Must Be Curves for This Operation.") for ob in o.objects: + #make sure all polylines are at least three points long + subdivide_short_lines(ob) # make the chunks from curve here pathSamples.extend(curveToChunks(ob)) # sort before sampling