Extruding 3D sketch by using API

Extruding 3D sketch by using API

shutov441-vuz
Contributor Contributor
4,836 Views
14 Replies
Message 1 of 15

Extruding 3D sketch by using API

shutov441-vuz
Contributor
Contributor

Hello! I have one strange task. I need to extrude sketch (fig. 1) to get something similar to fig. 2 by using API. I am using Python. I figured out how to extrude sketch lying in one plane, but how to extrude a three-dimensional sketch, I don't understand, tell me please in which direction to think. Also i need to use profile from current project.

...

sketch = rootComp.sketches.itemByName('sketchName') - it is right?
...

 

 

Fig.1Fig.1Fig.2Fig.2

0 Likes
4,837 Views
14 Replies
Replies (14)
Message 2 of 15

chris.midgley
Enthusiast
Enthusiast

Hi - it appears you are trying to extrude a surface and not a solid.  That makes it just slightly harder, as extruding a simple planar sketch into a solid is an easy call to ExtrudeFeatures.addSimple.  In your case, you will have to use the more advanced ExtrudeFeatures.add method which requires a bit more setup.

 

Here is a sample I threw together that extrudes a surface using the first profile in the sketch Sketch1, and a positive direction of 10mm:

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent
        extrudes = rootComp.features.extrudeFeatures

        # locate our sketch 
        sketch = rootComp.sketches.itemByName('Sketch1')
        if not sketch:
            ui.messageBox("Unable to locate sketch")
            return

        # set extrude distance to 10mm
        mm10 = adsk.core.ValueInput.createByString("10 mm")
        distance = adsk.fusion.DistanceExtentDefinition.create(mm10) 

        # set the extrude to do the first profile in the sketch, for the 10mm distance from above
        extrudeInput = extrudes.createInput(sketch.profiles[0], adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extrudeInput.setOneSideExtent(distance, adsk.fusion.ExtentDirections.PositiveExtentDirection)

        # indiciate that the extrude should be a surface, not a solid 
        extrudeInput.isSolid = False

        # extrude the profile into a surface
        extrudes.add(extrudeInput)

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

 

0 Likes
Message 3 of 15

shutov441-vuz
Contributor
Contributor

Hi, thank you for your code. It is works well with flat sketches, but sketch i need to extrude is in three planes, so there are problems with choosing a profile. I have attached a picture with a description of the error. I don't understand how to get a profile from a 3D sketch, when I do it manually, there are no problems, there is only one profile. You are right, i need to extrude a surface.

Error.PNG

0 Likes
Message 4 of 15

chris.midgley
Enthusiast
Enthusiast

It's pretty easy - you just need to add all profiles of the sketch that you want to extrude into an ObjectCollection, and use that to extrude.

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent
        extrudes = rootComp.features.extrudeFeatures

        # locate our sketch 
        sketch = rootComp.sketches.itemByName('Sketch1')
        if not sketch:
            ui.messageBox("Unable to locate sketch")
            return

        # set extrude distance to 10mm
        mm10 = adsk.core.ValueInput.createByString("10 mm")
        distance = adsk.fusion.DistanceExtentDefinition.create(mm10) 

        # build up an ObjectCollection of all profiles to extrude
        profileCollection = adsk.core.ObjectCollection.create()
        for profile in sketch.profiles:
            profileCollection.add(profile)

        # extrude surfaces according to the profileColleciton
        extrudeInput = extrudes.createInput(profileCollection, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extrudeInput.setOneSideExtent(distance, adsk.fusion.ExtentDirections.PositiveExtentDirection)

        # indiciate that the extrude should be a surface, not a solid 
        extrudeInput.isSolid = False

        # extrude the profile into a surface
        extrudes.add(extrudeInput)

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

 

Hopefully this does what you want.  If not, perhaps upload your model with the sketch so I can try it against that.

0 Likes
Message 5 of 15

shutov441-vuz
Contributor
Contributor

Sadly, this doesn't work either. Attached a file with a sketch. Thank you very much for the help!

0 Likes
Message 6 of 15

chris.midgley
Enthusiast
Enthusiast

I've now spent the last three hours on this ... I've tried using sketch curves, paths, open profiles, and more yet nothing seems to work.  They all complain in the end that the curves (or profiles, depending on which API is used) are not planar. 

 

I also took your sketch and extruded it, and then in the debugger looked at what Fusion creates, and it is an extrusion feature as expected, and no indication of unusual properties that be a clue as to what we need to do.  I search the web a bunch and found nothing that helped with a solution (extruding 3D sketches as surfaces using the API is not a common discussion!)

 

My gut is this is an API bug, where they are trying to ensure the planar use of extrude even though there is a valid use case where it should work.  But just as easily it could be incorrect API usage, as the documentation is weak in these areas.

 

Here is one of my latest attempts, in case somebody else wants to take the model above and take a whack at the code.  The error occurs on the "extrudes.add" line near the end.  

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent
        extrudes = rootComp.features.extrudeFeatures

        # locate our sketch 
        sketch = rootComp.sketches.itemByName('Sketch1')
        if not sketch:
            ui.messageBox("Unable to locate sketch")
            return

        # set extrude distance to 10mm
        mm10 = adsk.core.ValueInput.createByString("10 mm")
        distance = adsk.fusion.DistanceExtentDefinition.create(mm10) 

        # define a path from the sketch curves
        path = rootComp.features.createPath(sketch.sketchCurves[0])

        # set parameters to extrude surfaces according to the profileCollection
        extrudeInput = extrudes.createInput(path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extrudeInput.setOneSideExtent(distance, adsk.fusion.ExtentDirections.PositiveExtentDirection)

        # indiciate that the extrude should be a surface, not a solid 
        extrudeInput.isSolid = False

        # extrude the profile into a surface (*** this generates the error "input curves are not on the same plane" ***)
        extrudes.add(extrudeInput)

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

 

If you, or anyone else, have any suggestions, I'm glad to plug away at this some more.

 

Chris

0 Likes
Message 7 of 15

BrianEkins
Mentor
Mentor

I tested this and agree with your result; that the API is being to picky about the input.  I'll see that this gets logged as a bug.  Thanks for your time investigating it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 8 of 15

kandennti
Mentor
Mentor

Hi @shutov441-vuz .

 

It's not a good idea, but it works fine.

import adsk.core, adsk.fusion, traceback

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

        # locate our sketch 
        sketch = rootComp.sketches.itemByName('Sketch1')
        if not sketch:
            ui.messageBox("Unable to locate sketch")
            return

        txtCmds = [
            u'Commands.Start SurfaceExtrude',
            u'Commands.SetDouble AlongExtrudeDistance 1.0',
            u'NuCommands.CommitCmd'
        ]

        for cmd in txtCmds:
            app.executeTextCommand(cmd)

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

 

See here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/m-p/9645688 

Message 9 of 15

shutov441-vuz
Contributor
Contributor

I am very grateful to you. I'm sorry you spent so much time, I hope this error will be fixed.

 
0 Likes
Message 10 of 15

shutov441-vuz
Contributor
Contributor

It really works right. Didn't know about text commands, thanks for the solution and hint.

0 Likes
Message 11 of 15

chris.midgley
Enthusiast
Enthusiast

@kandennti - I was also unaware of this ExecuteTextCommand method.  while imperfect, it is a great workaround until the API is updated!  How do you discover the supported commands and parameters?  Is there documentation, or perhaps another trick to figure it out?

 

0 Likes
Message 12 of 15

haik.vasil.3
Participant
Participant

Thanks for the great insights @chris.midgley  

Do you guys know if I could extrude a 3D curve like this using Pipe command solid?

0 Likes
Message 13 of 15

Joshua.mursic
Advocate
Advocate

@shutov441-vuz did you ever find a solution to this issue?

@BrianEkins Is creating a surface from a 3D sketch possible using the API? It is easily done when using the extrude interface in fusion.

 

Joshuamursic_0-1692049739839.png

 

 

Message 14 of 15

shutov441-vuz
Contributor
Contributor

Hi! Yes, I found a solution, but it was a long time ago and I lost the final version. you can look here https://forums.autodesk.com/t5/fusion-360-api-and-scripts/loft-wrong-orientation/m-p/10079230#M12679 function create_surface

0 Likes
Message 15 of 15

BrianEkins
Mentor
Mentor

Unfortunately, this is still a problem. However, they have been doing a lot of work on the API so I'm hopeful this will be addressed sooner than later.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes