API Extrude all Profiles in a sketch layer help

API Extrude all Profiles in a sketch layer help

Anonymous
Not applicable
928 Views
1 Reply
Message 1 of 2

API Extrude all Profiles in a sketch layer help

Anonymous
Not applicable

Hi there

 

A quick bit if background, I'm not a coder, I've never done any sort of coding untill 12 weeks ago when I got furloughed. I'm now trying to learn a bit more about it, as it can be very time effecient by automating my usual every day tasks.

 

What I want my code to do is:

1 - Import in a dxf, using a dialog box

 

2 - Extrude every single profile in a sketch layer and extrude it to a height

This is where I'm struggling

 

3 - Lastly, I want to save the fusion file as the name of the imported dxf

 

 

For Step 1:

Importing the dxf works fine, and so does the dialog box, it's only when I don't select anything from the dialog box my code dosen't work.

 

Also the dxf that I will be importing will have multiple sketch layers, and multiple profiles in each sketch. I will link the dxf that I have been using, it's very basic with as it's just a square with three circles, and four rectangles inside it. I want to get this basic one to work first before I attempt anything more complex, like what I would normally be dealing with.

 

For Step 2:

This is where my issues have been occuring, and I'm looking for some help, as I can extrude a single profile from each sketch layer fine, but it's when I attempt to group all the profiles together and extrude all of them at once, I start to have issues.

 

For Step 3:

I haven't attempted this part yet as I haven't finished step 2

 

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback
#import os.path

def run(context):
    ui = None
    try:

        #allProfiles = False

        app = adsk.core.Application.get()
        ui  = app.userInterface
    

        ##  ------------------------------------------------------------------- ##
        ##        This create a new document when you run the code              ##
        ##     Copied from Extrude Feature API Sample on Fusion Website         ##
        ##                            IT WORKS                                  ##  
        
        #TypeOfDoc = adsk.core.DocumentTypes.FusionDesignDocumentType
        #doc = app.documents.add(TypeOfDoc)

        ##    End of Copied from Extrude Feature API Sample on Fusion Website   ##
        ##  ------------------------------------------------------------------- ##

        ###  --------------------------------------------------------------------------- ###
        ###                              Importing the dxf                               ###
        ###           Copied from Import Manager, importing a dxf fusion sample          ###
        ###                              The import works                                ###


        #     -----------------------------------------------------------------------------       #
        #           Using a dialog box to select the dxf file I want to import                    # 
        #                                   IT WORKS BUT                                          #
        #     I can't get the dialog to go away without any issues if I don't select a file       #

        # This allows me to select a file from the location I want
        # Set styles of file dialog
        fileToPick = ui.createFileDialog()
        fileToPick.title = 'Please select a dxf to import'
        fileToPick.filter = '*.dxf*'
        fileToPick.isMultiSelectEnabled = False

        # An attempt to get the dialog box to close if nothing was selected, it didn't work
        # Show file open dialog
        #pickedFileResults = fileToPick.showOpen()
        #if pickedFileResults == adsk.core.DialogResults.DialogOK:
        #    msg += '\nopen files:'
        #    for filename in fileToPick.filenames:
        #        msg += '\n\t{}'.format(filename)

        if adsk.core.DialogResults.DialogOK == fileToPick.showOpen():
                dxfFile = fileToPick.filename
        
        # Another failed attempt to get the dialog box to close if nothing was selected
        #        if cancelled
        #        return

        #if adsk.core.DialogResults.DialogCancel:
        #    break

        #   End of I can't get the dialog to go away without any issues if I don't select a file  #
        #     -------------------------------------------------------------------------------     #

        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # Get root component
        rootComp = design.rootComponent

        # Get import manager
        importManager = app.importManager

        # Get dxf import options
        dxfOptions = importManager.createDXF2DImportOptions(dxfFile,rootComp.xZConstructionPlane)
        dxfOptions.isViewFit = False
        
        # Import dxf file to root component
        # Want to find the difference between importToTarget and importToTarget2
        #importManager.importToTarget2(dxfOptions, rootComp)
        importManager.importToTarget(dxfOptions, rootComp) 

        ###    End of Copy of Import Manager, importing a dxf fusion sample stops here   ###
        ###  --------------------------------------------------------------------------- ###


        ##  ------------------------------------------------------------------- ##
        ##          Copy of Extrude Feature API SAmple - Example 1              ##


        #   -----------------------------------------------------------------------------------------------------------------     #
        #     I want to extrude more than just one profile on a sketch layer, want a profile collection or something like that    #      

        # Define Sketches 
        sketches = rootComp.sketches

        # When I define a sketch it creates an extra sketch layer, something I should look into later
        # For now I'm just trying to group all the profiles in the sketch layer before extruding, so not an issue right now
        # Define sketch 
        sketch = rootComp.sketches.add(rootComp.xZConstructionPlane)


        # Here I have attempted to create an object collection to use an input for the extrude
        # I was looking to see if you could get a profile collection instead of an object collection
        #profiles1 = adsk.core.ObjectCollection.create()

        # Add all of the profiles to the collection
        #for prof in sketch1.profiles:
        #    profiles1.add(prof)

        #prof = adsk.fusion.Profile.cast(None)
        #for prof in sketch.profiles:
        #    loop = prof.profileLoops.item(0)
        #    if loop.profileCurves.count <=5:
        #        extProfiles.add(prof)
        
        # Here I choose a profile from each sketch layer to extrude
        # If I wanted to I could just do this for every profile in the sketch layer but I woudld prefer to them grouped together
        profiles1 = sketches.item(0).profiles.item(0)
        profiles2 = sketches.item(1).profiles.item(1)
        profiles3 = sketches.item(2).profiles.item(3)

        # End of I want to extrude more than just one profile on a sketch layer, want a profile collection or something like that #    
        #   -----------------------------------------------------------------------------------------------------------------     #

        # Get extrude features
        extrudes = rootComp.features.extrudeFeatures

        # Extrude Sample 1: A simple way of creating typical extrusions (extrusion that goes from the profile plane the specified distance).
        # Define a distance extent of 5 cm

        # Simplying the extrude 
        operation = adsk.fusion.FeatureOperations.NewBodyFeatureOperation

        # An attempt to loop through each sketch layer and extrude all the profiles in the layer, it didn't work
        # Put up at top of code
        #allProfiles = False

        #if allProfiles:

        #    extrude1 = extrudes.addSimple(profiles1, distance1, operation)
        #    distance1 = adsk.core.ValueInput.createByReal(1)

        #else:

        #    for profile in sketch.item(0).profiles:
        #        extrude1 = extrudes.addSimple(profile, distancce1, operation)
        #        distance1 = adsk.core.ValueInput.createByReal(1)
        
        distance = adsk.core.ValueInput.createByReal(1)
        extrude1 = extrudes.addSimple(profiles1, distance, operation)      

        distance = adsk.core.ValueInput.createByReal(3)
        extrude2 = extrudes.addSimple(profiles2, distance, operation) 
    
        distance = adsk.core.ValueInput.createByReal(5)
        extrude3 = extrudes.addSimple(profiles3, distance, operation) 

        # Get the extrusion body
        #body1 = extrudetest.profile
        body1 = extrude1.bodies.item(0)
        body1.name = "Square"

        # Get the extrusion body
        body2 = extrude2.bodies.item(0)
        body2.name = "Circle"

        # Get the extrusion body
        body3 = extrude3.bodies.item(0)
        body3.name = "Rectangle"

        # Get the state of the extrusion
        health = extrude1.healthState
        if health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState or health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState:
            message = extrude1.errorOrWarningMessage
        
        # Get the state of timeline object
        timeline = design.timeline
        timelineObj = timeline.item(timeline.count - 1)
        health = timelineObj.healthState
        message = timelineObj.errorOrWarningMessage

        ## End of Copy of Extrude Feature API SAmple - Example 1                ##
        ##  ------------------------------------------------------------------- ##


        ###  ---------------------------------------------------------------------- ### 
        ###     Saving the fusion file as the name of the imported dxf              ###




        ###     End of saving the fusion file as the name of the imported dxf       ###        
        ###  ---------------------------------------------------------------------- ###        


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

 

 

 

 

 

 

 

 

 

 

 

0 Likes
929 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

Hi there

Quick update on my code

 

I pretty much finished what I set out to do

 

The only part that I'm still working my way through is the saving of the fusion file as the name of the imported dxf, but if I can't get it, it's not the end of the world

 

I'm wondering if there is anyone out there how could give a suggestion as to how I could tidy up my code and full prof it a little more. As for example, if I import a dxf that doesn't have exact 1 square, 3 circles and 4 rectnagles then my code fails. Is there a way that I can loop through all the profiles on each sketch layer instead of indiviually going through them?  Any thoughts would be helpful

 

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

def run(context):
    ui = None
    try:


        app = adsk.core.Application.get()
        ui  = app.userInterface
    

        ##  ------------------------------------------------------------------- ##
        ##        This create a new document when you run the code              ##
        ##     Copied from Extrude Feature API Sample on Fusion Website         ##
        ##                            IT WORKS                                  ##  
        
        TypeOfDoc = adsk.core.DocumentTypes.FusionDesignDocumentType
        doc = app.documents.add(TypeOfDoc)

        ##    End of Copied from Extrude Feature API Sample on Fusion Website   ##
        ##  ------------------------------------------------------------------- ##



        ###  --------------------------------------------------------------------------- ###
        ###                           This Imports the dxf                               ###
        ###           Copied from Import Manager, importing a dxf fusion sample          ###
        ###                              The import works                                ###


        #     -----------------------------------------------------------------------------       #
        #           Using a dialog box to select the dxf file I want to import                    # 
        #                                   IT WORKS BUT                                          #
        #     I can't get the dialog to go away without any issues if I don't select a file       #

        # This allows me to select a file from the location I want
        # Set styles of file dialog
        fileToPick = ui.createFileDialog()
        fileToPick.title = 'Please select a dxf to import'
        fileToPick.filter = '*.dxf*'
        fileToPick.isMultiSelectEnabled = False

        if adsk.core.DialogResults.DialogOK == fileToPick.showOpen():
                dxfFile = fileToPick.filename

        #   End of I can't get the dialog to go away without any issues if I don't select a file  #
        #     -------------------------------------------------------------------------------     #


        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # Get root component
        rootComp = design.rootComponent

        # Get import manager
        importManager = app.importManager

        # Get dxf import options
        dxfOptions = importManager.createDXF2DImportOptions(dxfFile,rootComp.xZConstructionPlane)
        dxfOptions.isViewFit = False
        
        # Import dxf file to root component
        # Want to find the difference between importToTarget and importToTarget2
        #importManager.importToTarget2(dxfOptions, rootComp)
        importManager.importToTarget(dxfOptions, rootComp) 

        ###    End of Copy of Import Manager, importing a dxf fusion sample stops here   ###
        ###  --------------------------------------------------------------------------- ###





        ##  ------------------------------------------------------------------- ##
        ##          Copy of Extrude Feature API SAmple - Example 1              ##

        #   -----------------------------------------------------------------------------------------------------------------     #
        #     I want to extrude more than just one profile on a sketch layer, want a profile collection or something like that    #      

        # Define Sketches 
        sketches = rootComp.sketches

        # Here I choose a profile from each sketch layer to extrude
        # If I wanted to I could just do this for every profile in the sketch layer but I woudld prefer to them grouped together
        profiles1 = sketches.item(0).profiles.item(0)

        profiles2_1 = sketches.item(1).profiles.item(0)
        profiles2_2 = sketches.item(1).profiles.item(1)
        profiles2_3 = sketches.item(1).profiles.item(2)
        profiles2_4 = sketches.item(1).profiles.item(3)

        profiles3_1 = sketches.item(2).profiles.item(0)
        profiles3_2 = sketches.item(2).profiles.item(1)
        profiles3_3 = sketches.item(2).profiles.item(2)
        profiles3_4 = sketches.item(2).profiles.item(3)

        # End of I want to extrude more than just one profile on a sketch layer, want a profile collection or something like that #    
        #   -----------------------------------------------------------------------------------------------------------------     #



        # Get extrude features
        extrudes = rootComp.features.extrudeFeatures

        # Simplying the extrude 
        operation = adsk.fusion.FeatureOperations.NewBodyFeatureOperation 
        
        extrude1distance = adsk.core.ValueInput.createByReal(1)
        extrude1 = extrudes.addSimple(profiles1, extrude1distance, operation)      

        extrude2distance = adsk.core.ValueInput.createByReal(3)
        operation2 = adsk.fusion.FeatureOperations.JoinFeatureOperation
        extrude2_1 = extrudes.addSimple(profiles2_1, extrude2distance, operation2) 
        extrude2_2 = extrudes.addSimple(profiles2_2, extrude2distance, operation2) 
        extrude2_3 = extrudes.addSimple(profiles2_3, extrude2distance, operation2) 
        extrude2_4 = extrudes.addSimple(profiles2_4, extrude2distance, operation2) 

        extrude3distance = adsk.core.ValueInput.createByReal(5)
        extrude3_1 = extrudes.addSimple(profiles3_1, extrude3distance, operation2) 
        extrude3_2 = extrudes.addSimple(profiles3_2, extrude3distance, operation2) 
        extrude3_3 = extrudes.addSimple(profiles3_3, extrude3distance, operation2) 
        extrude3_4 = extrudes.addSimple(profiles3_4, extrude3distance, operation2) 

        # Get the extrusion body
        body1 = extrude1.bodies.item(0)
        #body1 = extrude1.parentComponent
        body1.name = "Whole Body"


        # Get the state of the extrusion
        health = extrude1.healthState
        if health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState or health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState:
            message = extrude1.errorOrWarningMessage
        
        # Get the state of timeline object
        timeline = design.timeline
        timelineObj = timeline.item(timeline.count - 1)
        health = timelineObj.healthState
        message = timelineObj.errorOrWarningMessage

        ## End of Copy of Extrude Feature API SAmple - Example 1                ##
        ##  ------------------------------------------------------------------- ##



        ###  ---------------------------------------------------------------------- ### 
        ###     Saving the fusion file as the name of the imported dxf              ###



        ###     End of saving the fusion file as the name of the imported dxf       ###        
        ###  ---------------------------------------------------------------------- ###        



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