- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all, I think this is my first post in the API forum and I have exhausted the search function but nothing seems to answer my question. I am hoping it is simple for you all with lots of experience.
The problem (short): SaveAs after a model change isn't actually saving anywhere and I am not sure why.
The problem (long): I have a script that opens a CSV file that contains Length,Width values. Running the script will update the user parameters in the design of the same name. That all works fine. Where it seems to fall apart is on the Save As. The intent is to SaveAs with a common name "Door -" and pull in the values from the csv into the file name. Where things are not seeming to work is after it runs through the first line, updates the parameters/model and should save the design just has an * showing it in an unsaved state. I have tried using the current folder the active design is in as well as the root project folder with no success. The script will run through all the lines in the CSV if I don't require the save, but that is kind of the whole point of the script 🙂 Also there are a few things in the code that aren't being used in its current state like folder = adsk.core.DataFolder, and also note i removed the file location from the code, but that isn't how it is coded to run.
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)
document = adsk.core.Document.cast(app.activeDocument)
folder = adsk.core.DataFolder
#CSV File with the format of length,width
file = open('...Test2.csv')
#for loop that will run for each line in the CSV file
#loop will pull out each piece and assign it to Length and Width
for line in file:
pieces = line.split(',')
Length = pieces[0]
Width = pieces[1]
ui.messageBox('New Length = ' + Length + ' and New Width = ' + Width)
#Set up the user parameters by calling them by name and setting the expression
#To Length and Width from the CSV
csvParams = design.userParameters
csvParams.itemByName('Length').expression = Length
csvParams.itemByName('Width').expression = Width
#Get the Path and Root from the parent document and tell the user what they are
path = design.parentDocument.dataFile.parentFolder
#pathRoot = design.parentDocument.dataFile.parentProject
ui.messageBox('Path is ' + path.name + ' ' + path.id)
#Create a new file name using Cabinet plus the values of length and width from the CSV
newFileName = 'Cabinet - ' + Length + ' x ' + Width
#Save As using the new file name, path from the current active document
document.saveAs(newFileName,path,'','')
if app.activeDocument.isSaved:
ui.messageBox('Success')
return
else:
ui.messageBox('Fail')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
So the first time through it will send a messageBox "fail" stating the isSaved is coming back False. then when it runs through the second line of the CSV the file is in an unsaved state and throws a warning that I haven't coded to account for. If I toss in a Save, it will want me to manually use the saveAs dialog. So it is behaving as if a SaveAs was invoked but I feel like it just might be missing something.
All the samples I have come across doing the same or similar don't seem to have anything else supporting the SaveAs so I can't seem to figure out why the save is failing. Some of the samples were from 2015 or so and I am hoping there is just a missing piece.
Thoughts?
Thanks for your time!
Solved! Go to Solution.