Message 1 of 4
Open file that was just created in folder.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I'm working on code to have the user select a body to make a copy of in the parent directory, which works, and then have it open the file. Problem is there seems to be a delay necessary to do this? Is there a way of refreshing the directory first before you try to open the copied file? See inserted code below. The code works up until the line: adsk.core.Application.get().documents.open(targetDatafile). The line crashes because targetDataFile is None. The file exists but it doesn't come up in the dataFiles loop.
If I run just the function openDataFile a second time by manually passing in the name it will find the file and open it.
Eric
#Author-Bunn
#Description-Select Entities on screen and return the filtered selection. Does one at a time only.
import adsk.core, adsk.fusion, adsk.cam, traceback
def selectEnt(msg: str,filtterStr: str) -> adsk.core.Selection:
try:
app = adsk.core.Application.get()
ui = app.userInterface
sel = ui.selectEntity(msg, filtterStr)
#get the BRep from the selection (Programming Interface Fusion 360 API User's Manual Selection Filters)
if filtterStr == 'Faces':
face = adsk.fusion.BRepFace.cast(sel.entity)
return face
elif filtterStr == 'Bodies':
body = adsk.fusion.BRepBody.cast(sel.entity)
return body
elif filtterStr == 'SolidBodies':
body = adsk.fusion.BRepBody.cast(sel.entity)
return body
elif filtterStr == 'Edges':
body = adsk.fusion.BRepEdge.cast(sel.entity)
return body
else:
return sel
except:
return None
def getDataFolder(app):
# get active datafile
actDataFile: adsk.core.DataFile = app.activeDocument.dataFile
if actDataFile:
# get the unique data file
dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
return dataFolder
else:
return None
def SaveCopyAs():
ui = None
app = adsk.core.Application.get()
ui = app.userInterface
root = app.activeProduct.rootComponent
parentName = root.name
ui.messageBox('Select Body')
targetBody: adsk.fusion.BRepFace = selectEnt('Select Body','SolidBodies')
comp = targetBody.parentComponent
name = comp.name
#get dataFolder
dataFolder = getDataFolder(app)
if not dataFolder:
ui.messageBox('Save file first.')
else:
newfile = comp.saveCopyAs(parentName + " " + name + "Tray Design",dataFolder,"","")
return parentName + " " + name + "Tray Design"
def openDataFile(file):
app = adsk.core.Application.get()
#Open File
# get datafile ID
actId = app.activeDocument.dataFile.id
# get the unique data file
dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
targetDatafile: adsk.core.DataFile = None
#this loads an array with all the files in the parent folder. This could be used to populate a list box for user selection.
dataFiles = dataFolder.dataFiles.asArray()
for df in dataFiles:
if df.id != actId and df.fileExtension == 'f3d':
print(df.name)
for df in dataFiles:
if df.id != actId and df.fileExtension == 'f3d' and df.name == file:
targetDatafile = df
break
adsk.core.Application.get().documents.open(targetDatafile)
copyName = SaveCopyAs()
# file = 'Nesting Example v4 EndCapTray Design'
openDataFile(copyName)