Intersection between two sketches

Intersection between two sketches

saikishoret99
Contributor Contributor
1,457 Views
2 Replies
Message 1 of 3

Intersection between two sketches

saikishoret99
Contributor
Contributor
Hello all. I am doing a project in which I use the Voronoi sketch generator to create random closed spline pattern, but the problem is basically that I don't need those splines that interests a particular random curved boundary(another new sketch) projected onto the voronoi sketches from a organic shape. Although i can select all those closed splines which are intersecting the projected boundary line and delete them or move them, but the problem is I will be dealing with many Voronoi sketches, which makes it difficult to select every closed interested splines in every Voronoi sketches and deleting them.I thought of creating a addon which will have two sketch inputs namely the projected boundary sketch and the second the Voronoi sketch, then it will check the intersections between boundary and voronoi loops and delete those loops which are intersecting the boundary and also for other voronoi sketches. So basically this is the idea.. and I have found a Intersect addon, but it does not calculate intersections between two sketch curves..How can I write a addon to find the intersected loops..Is there any feature to do this thing....I am somewhat new to API and am learning from a few days ..Any help on any issue will be of great help to me.. Please anyone help me on the intersection part of two different sketches.
Thank you
0 Likes
Accepted solutions (1)
1,458 Views
2 Replies
Replies (2)
Message 2 of 3

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

The following samples would demo how to find all splines(from voronoi sketch) which intersect with projected curves from another sketch(e.g. "sketch1"). 

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        design = adsk.fusion.Design.cast(app.activeProduct)
        sketch1 = design.rootComponent.sketches.item(0)
        voronoi = design.rootComponent.sketches.item(1)
        
        splines = adsk.core.ObjectCollection.create()
        for curve in voronoi.sketchCurves:
            splines.add(curve)
        
        projectedcurves = adsk.core.ObjectCollection.create()
        for curve in sketch1.sketchCurves:
            results = voronoi.project(curve)
            for resultcurve in results:
                projectedcurves.add(resultcurve)
        
        intersectedsplines = adsk.core.ObjectCollection.create()
        for curve in projectedcurves:
            result, intersectionresults, intersectionpts = curve.intersections(splines)
            if result:
                for intersectionresult in intersectionresults:
                    intersectedsplines.add(intersectionresult)
        
        ui.messageBox('intersection splines: {}'.format(intersectedsplines.count))        

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
Message 3 of 3

saikishoret99
Contributor
Contributor

Thank you very much for your reply. The code you posted had done all the work I needed. I just added some more code to ask the user to specify the sketches rather than automatically selecting. I also added some code to delete the intersected splines and also to delete the projected curves on the voronoi sketch. I just want to inform you that the code has an unwanted behavior when a spline intersects two or many separate entities(curves) of single projected sketch, it is being added as new one in the intersectionresults, actually it has been already added in the intersectionresults  but when the same spline intersects another curve, it is been adding as an new one. I have added some condition statement for this not to happen. I am posting the code with the modifications, If I am wrong any where, please correct me.

 

#Author-
#Description-
#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        design = adsk.fusion.Design.cast(app.activeProduct)
        s = ui.selectEntity('Select a Boundary sketch', 'Sketches')
        sketch1 = s.entity 
        v = ui.selectEntity('Select a Voronoi Sketch', 'Sketches')
        voronoi = v.entity
        
        splines = adsk.core.ObjectCollection.create()
        for curve in voronoi.sketchCurves:
            splines.add(curve)
        
        projectedcurves = adsk.core.ObjectCollection.create()
        for curve in sketch1.sketchCurves:
            results = voronoi.project(curve)
            for resultcurve in results:
                projectedcurves.add(resultcurve)
        
        intersectedsplines = adsk.core.ObjectCollection.create()
        for curve in projectedcurves:
            result, intersectionresults, intersectionpts = curve.intersections(splines)
            if result:
                for intersectionresult in intersectionresults:
                    if  intersectedsplines.find(intersectionresult) == -1 :
                        intersectedsplines.add(intersectionresult)
        
        ui.messageBox('intersection splines: {}'.format(intersectedsplines.count))   
        
        for intersectedspline in intersectedsplines:
            intersectedspline.deleteMe()
            
        for curve in projectedcurves:
            curve.deleteMe()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Thank you,

Kishore

0 Likes