Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

how to intersect a new extrusion to a specific body through api?

2022282210229
Participant Participant
467 Views
4 Replies
Message 1 of 5

how to intersect a new extrusion to a specific body through api?

2022282210229
Participant
Participant

hello,i want to build a csg tree model,but when i try to create a new intersection, it intersect with all existing models.

 

here is my code:

def generate_primitive(rootComp, control_points, point_weights, height, three_points, flag):
    
    sketches = rootComp.sketches
    xyPlane = rootComp.xYConstructionPlane
    planes = rootComp.constructionPlanes
    extrudes = rootComp.features.extrudeFeatures
    firstSketch = sketches.add(xyPlane)
    pos_1 = adsk.core.Point3D.create(three_points[0][0], three_points[0][1], three_points[0][2])
    pos_2 = adsk.core.Point3D.create(three_points[1][0], three_points[1][1], three_points[1][2])
    pos_3 = adsk.core.Point3D.create(three_points[2][0], three_points[2][1], three_points[2][2])
    p_1 = firstSketch.sketchPoints.add(pos_1)
    p_2 = firstSketch.sketchPoints.add(pos_2)
    p_3 = firstSketch.sketchPoints.add(pos_3)
    planeInput = planes.createInput()
    planeInput.setByThreePoints(p_1, p_2, p_3)
    threePointsPlane = planes.add(planeInput)
    #threePointsPlane.name = 'Three-points plane'
    sketch = sketches.add(threePointsPlane)
   
    degree = 3
    knots = [0, 0, 0, 0, 1, 1, 1, 1]
    splines = sketch.sketchCurves.sketchFixedSplines
    splines.isFixed = False
    for index, p in enumerate(control_points):
        weights = [1, 1, 1, 1]
        weights[1], weights[2] = point_weights[index][0], point_weights[index][1]
        point1 = adsk.core.Point3D.create (p[0][0],p[0][1],0)
        point2 = adsk.core.Point3D.create (p[1][0],p[1][1],0)
        point3 = adsk.core.Point3D.create (p[2][0],p[2][1],0)
        point4 = adsk.core.Point3D.create (p[3][0],p[3][1],0)
        points = [point1, point2, point3, point4]
        nurbs = adsk.core.NurbsCurve3D.createRational(points, degree, knots, weights, False)
        fixed_splines = splines.addByNurbsCurve(nurbs)
        fixed_splines.isReference = False
    distance = adsk.core.ValueInput.createByReal(height)
    try:
        body = extrudes.addSimple(sketch.profiles.item(0), distance, adsk.fusion.FeatureOperations.IntersectFeatureOperation)
    except:
        body = extrudes.addSimple(sketch.profiles.item(0), distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    return body


def generate_model(rootComp, json_data, ui):
    #print(json_data)
    control_points = json_data["param"]["points"]
    weights = json_data["param"]["weights"]
    heights = json_data["param"]["heights"]
    planes = json_data["param"]["planes"]
    intersections = json_data["intersection"]
    union = json_data["union"]
    #ui.messageBox(str(weights[0][0]))
    for c in range(len(intersections[0])):
        if union[c]!=1:
            continue
        newCom = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        revComp = newCom.component
        for k in range(len(intersections)):
            if intersections[k][c] == 1:
                ext = generate_primitive(revComp, control_points[k], weights[k], heights[k], planes[k], 0)
        break

def parse_json(rootComp, category,start_id, n, ui):
    dir = "C:/Users/86454/Desktop/jsons/"+category
    count = 0
    #ui.messageBox(os.getcwd())
    for index, file in enumerate(os.listdir(dir)):
        if index < start_id:
            continue
        with open("{}/{}".format(dir, file), "r") as fp:
            generate_model(rootComp, json.load(fp), ui)
        count += 1
        if count >= n:
            break

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent
        category = "bench"
        id = 0
        num = 1
        parse_json(rootComp, category, id, num, ui)
    
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

and here is what i want to achieve:

20221116111937.png

0 Likes
Accepted solutions (2)
468 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor
Accepted solution

Hi @2022282210229 .

 

I think the desired processing can be done by using the createInput method instead of the addSimple method.

Here is a sample.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-5a1236c0-b0ae-11e6-8b8e-989096c62968 

0 Likes
Message 3 of 5

BrianEkins
Mentor
Mentor
Accepted solution

I think there are two basic approaches to what you want. The first is to perform the CSG boolean operation when you create the extrusion. This is done by specifying the operation for the boolean, which for the first primitive will be to create a new body and for subsequent primitives will be to either add or subtract. Using the ExtrudeFeatureInput you can use the participantBodies properties to control which bodies the new extrusion will affect.

 

The other approach is to create the primitive as a new body and then use the Combine feature to combine them in different ways where you can specify if they should be unioned, subtracted, or intersected.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like
Message 4 of 5

2022282210229
Participant
Participant
thansk a lot!!! I'll have a try.
0 Likes
Message 5 of 5

2022282210229
Participant
Participant
thanks a lot!!! I'll have a try.
0 Likes