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