Help on Cleaning up a Sketch from duplicated Bsplines

Need help, or want to share a macro? Post here!
User avatar
easyw-fc
Posts: 2663
Joined: Thu Jul 09, 2015 9:34 am

Help on Cleaning up a Sketch from duplicated Bsplines

Postby easyw-fc » Tue Apr 30, 2019 10:52 am

Hi,
Following this thread,
I'm trying to tidy up a Sketch obtained from a projection of a 3D model.
I've noticed in the generated sketch, sometimes there are duplicated or even more splines...
I've tried to clean up the Sketch, removing the extra splines with the following macro:

Code: Select all

def sanitizeSkBsp(s_name, knot_tolerance):
    s=FreeCAD.ActiveDocument.getObject(s_name)
    FreeCAD.Console.PrintWarning('check to sanitize\n')
    if 'Sketcher' in s.TypeId:
        FreeCAD.ActiveDocument.openTransaction('Sanitizing')
        idx_to_del=[]
        geo_to_del = []
        # check for duplicates in splines
        for i,g in enumerate (s.Geometry):
            if 'BSplineCurve object' in str(g):
                j=i+1
                for bg in s.Geometry[(i + 1):]:
                    if 'BSplineCurve object' in str(bg):
                        if (len(g.KnotSequence) == len(bg.KnotSequence)):
                            eqp = True
                            for k,kn in enumerate (bg.KnotSequence):
                                if abs(kn-g.KnotSequence[k]) > knot_tolerance:
                                    eqp = False
                            if (eqp):
                                print ('identical splines',g,bg)
                                if j not in idx_to_del:
                                    idx_to_del.append(j)
                    j+=1
        j=0
        print(idx_to_del)
        if len(idx_to_del) >0:
            FreeCAD.Console.PrintMessage(u'sanitizing '+s.Label)
            FreeCAD.Console.PrintMessage('\n')
            idx_to_del.sort() 
            print(idx_to_del)
            idx_to_del.reverse() 
            print(idx_to_del)
            #stop
            for i, e in enumerate(idx_to_del):
                print('to delete ',s.Geometry[(e)],e)
                s.delGeometry(e)
        FreeCAD.ActiveDocument.commitTransaction()
        return s.Geometry
    else:
        return None
##
sname = 'Sketch'
knot_tolerance = 0.0001
uGeo = sanitizeSkBsp(sname,knot_tolerance)
FreeCAD.ActiveDocument.recompute()
ATM what I obtain is to delete some wanted geometries and not to delete all redundancies...

Could someone have a look at?
I'm attaching also the Sketch to be cleaned.
sketch-spline-to-sanitize.FCStd
(37.75 KiB) Downloaded 13 times
Thx in advance
Maurice