Cut a B-Rep body with multiple profiles at once

Cut a B-Rep body with multiple profiles at once

JeromeBriot
Mentor Mentor
414 Views
1 Reply
Message 1 of 2

Cut a B-Rep body with multiple profiles at once

JeromeBriot
Mentor
Mentor

Hello,

 

I can't figure out how to cut a B-Rep body with multiple profiles at once:

#Author-
#Description-

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

def run(context):
    ui = None
    try:

        allProfiles = False

        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)

        rootComponent = design.rootComponent

        sketches = rootComponent.sketches
        sketchesPlane = rootComponent.xYConstructionPlane

        sketch1 = sketches.add(sketchesPlane)
        sketch1.name = 'Square'

        lines = sketch1.sketchCurves.sketchLines;

        lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(4, 4, 0))

        sketch2 = sketches.add(sketchesPlane)
        sketch2.name = 'Hole'

        circles = sketch2.sketchCurves.sketchCircles

        circles.addByCenterRadius(adsk.core.Point3D.create(1, 1, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(1, 3, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(3, 3, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(3, 1, 0), 0.5)

        extrudes = rootComponent.features.extrudeFeatures

        extrudeInput = extrudes.createInput(sketch1.profiles.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        distance = adsk.core.ValueInput.createByReal(1.0)
        extrudeInput.setSymmetricExtent(distance, True)
        extrudes.add(extrudeInput)

        if allProfiles:

            extrudeInput = extrudes.createInput(sketch2.profiles, adsk.fusion.FeatureOperations.CutFeatureOperation)
            distance = adsk.core.ValueInput.createByReal(1.0)
            extrudeInput.setSymmetricExtent(distance, True)
            extrudes.add(extrudeInput)

        else:

            for profile in sketch2.profiles:
                extrudeInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.CutFeatureOperation)
                distance = adsk.core.ValueInput.createByReal(1.0)
                extrudeInput.setSymmetricExtent(distance, True)
                extrudes.add(extrudeInput)

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

The above code failed when allProfiles is set to True.

 

Thanks.

0 Likes
Accepted solutions (1)
415 Views
1 Reply
Reply (1)
Message 2 of 2

JeromeBriot
Mentor
Mentor
Accepted solution

OK, I found the answer there: Extruding profile collection in single operation? 

#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)

        rootComponent = design.rootComponent

        sketches = rootComponent.sketches
        sketchesPlane = rootComponent.xYConstructionPlane

        sketch1 = sketches.add(sketchesPlane)
        sketch1.name = 'Square'

        lines = sketch1.sketchCurves.sketchLines;

        lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(4, 4, 0))

        sketch2 = sketches.add(sketchesPlane)
        sketch2.name = 'Hole'

        circles = sketch2.sketchCurves.sketchCircles

        circles.addByCenterRadius(adsk.core.Point3D.create(1, 1, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(1, 3, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(3, 3, 0), 0.5)
        circles.addByCenterRadius(adsk.core.Point3D.create(3, 1, 0), 0.5)

        extrudes = rootComponent.features.extrudeFeatures

        extrudeInput = extrudes.createInput(sketch1.profiles.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        distance = adsk.core.ValueInput.createByReal(1.0)
        extrudeInput.setSymmetricExtent(distance, True)
        extrudes.add(extrudeInput)

        profs = adsk.core.ObjectCollection.create()
        for prof in sketch2.profiles:
            profs.add(prof)

        extrudeInput = extrudes.createInput(profs, adsk.fusion.FeatureOperations.CutFeatureOperation)
        distance = adsk.core.ValueInput.createByReal(1.0)
        extrudeInput.setSymmetricExtent(distance, True)
        extrudes.add(extrudeInput)

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

I tried to create an object collection before asking for help here. But I made a mistake trying to populate the collection with all the profiles in one line:

profs = adsk.core.ObjectCollection.create()
profs.add(sketch2.profiles) # This line is wrong

 

0 Likes