What's new

Function Release: AnalyzeSketch

NateLiquidGravity

Alibre Super User
This will Analyze the Sketch passed to it.
The Verbose setting is optional. Setting that to True that will print the results automatically.
The Heal setting is optional. Setting that to True and also setting HealingTolerance will attempt to heal a sketch.
The function will return either 0 (False indicating no problems to report) or it will return the integer quantities for each type of problem in a list in the order [DegenerateFigures, DisjointEnds, Intersections, OpenLoops, OverLaps]

Code:
def AnalyzeSketch(ThisSketch, Verbose = 0, Heal = 0, HealingTolerance = 0):
    # V2
    # This will Analyze the Sketch passed to it.
    # The Verbose setting is optional. Setting that to True that will print the results automatically.
    # The Heal setting is optional. Setting that to True and also setting HealingTolerance will attempt to heal a sketch.
    # It will return either 0 (False indicating no problems to report)
    # or it will return the integer quantities for each type of problem in a list
    # in the order [DegenerateFigures, DisjointEnds, Intersections, OpenLoops, OverLaps]
    try:
        if not ThisSketch.Figures:
            print "No Figures found in sketch!"
            return 1
        else:
            #myfigures = ThisSketch.Figures
            #objSketch = myfigures[0].FigureObject().Sketch
            objSketch = ThisSketch._Sketch
            try:
                objSketch.BeginChange()
            except:
                print "Unexpected error:", sys.exc_info()[0]
                pass
            results = objSketch.Analyze(1,1,1,1,1,Heal,HealingTolerance)
            try:
                objSketch.EndChange()
            except:
                print "Unexpected error:", sys.exc_info()[0]
                pass
            if results.DegenerateFigures.Count or results.DisjointEnds.Count or results.Intersections.Count or results.OpenLoops.Count or results.OverLaps.Count:
                if Verbose:
                    print('\nAnalyzing the sketch named "' + str(ThisSketch.Name) + '"')
                    if results.DegenerateFigures.Count > 0:
                        print('Found ' +str(results.DegenerateFigures.Count) + ' DegenerateFigure(s)')
                    if results.DisjointEnds.Count > 0:
                        print('Found ' +str(results.DisjointEnds.Count) + ' DisjointEnd(s)')
                    if results.Intersections.Count > 0:
                        print('Found ' +str(results.Intersections.Count) + ' Intersection(s)')
                    if results.OpenLoops.Count > 0:
                        print('Found ' +str(results.OpenLoops.Count) + ' OpenLoop(s)')
                    if results.OverLaps.Count > 0:
                        print('Found ' +str(results.OverLaps.Count) + ' OverLap(s)')
                    print('')
                return [int(results.DegenerateFigures.Count), int(results.DisjointEnds.Count), int(results.Intersections.Count), int(results.OpenLoops.Count), int(results.OverLaps.Count)]
            else:
                return 0
    except:
        return 0
Here is an example usage.
Code:
# example code
prt = CurrentPart()
mysketch = prt.GetSketch('Sketch<1>')
results = AnalyzeSketch(mysketch, True, True, 0.0001) # Verbose and heal settings are optional. Here I set them to True.
if results:
    print('Oh No! We found something wrong when analyzing the sketch!')
    # do something
else:
    print('The sketch looks good!')
    # do something

As always comments are appreciated.
 
Last edited:
Top