Below is my script that reads a step file in Fusion Autodesk 360, changes some of ites properties, and copies it as a f3d file. This script is run with Fusion's Design Automation API, and is able to create a f3d file successfully.
The problem is when I open the new f3d file in Fusion, its design history cannot be deleted since it thinks its linked to the original step file. Is there a better way to convert the file with this script? Am I missing any steps?
Here is a link to an image with a warning about the original step file. https://i.sstatic.net/f5sTDUc6.png
import { adsk } from '@adsk/fas';
function run() {
const scriptParams = JSON.parse(adsk.parameters);
if (!scriptParams) throw Error('Invalid parameters provided.');
const app = adsk.core.Application.get();
if (!app) throw Error('No adsk.core.Application.');
adsk.log(`Running script with params: ${JSON.stringify(scriptParams)}`);
const fileId = scriptParams.fileId;
const fileName = scriptParams.fileName;
const dataFile = app.data.findFileById(fileId)!;
const document = app.documents.open(dataFile, false);
if (document && document.products && document.products.count > 0) {
const design = document.products.item(0) as adsk.fusion.Design;
if (design.unitsManager) {
design.unitsManager.formatUnits('mm');
adsk.log('Units set to millimeters successfully.');
}
const rootComp = design.rootComponent;
if (rootComp && rootComp.bRepBodies) {
for (let i = 0; i < rootComp.bRepBodies.count; i++) {
const body = rootComp.bRepBodies.item(i);
if (body) {
body.name = 'ORIGINAL PART';
adsk.log('Body renamed to "ORIGINAL PART"');
break;
}
}
}
} else {
adsk.log('Could not access design from document.');
}
adsk.log(document?.name + ' - ' + document?.isSaved);
adsk.log(dataFile?.parentFolder.name + ' - ' + dataFile?.parentFolder.name);
document?.saveAs(fileName, dataFile.parentFolder, '', '');
dataFile.deleteMe();
}
run();```
When you open a uploaded step file into fusion it creates a new document with that step file as a reference. And when you save file in fusion and if you try to delete the step file you will not be allowed to do it sicne the reference is with the new file you have to break the reference. this is what happen in the UI workflow.
but the script workflow doc.saveas is an async so it does not wait till the save completes so it excutes the next line which is dataFile.deleteMe(); at that point the data is not used anywhere so it deletes the file. and this causes the issues.
but unluckly we don't have a way to break the reference at the moment via script. so the workaround will be you have create a new document and insert the step file as non reference
import { adsk } from '@adsk/fas';
function run() {
const scriptParams = JSON.parse(adsk.parameters);
if (!scriptParams) throw Error('Invalid parameters provided.');
const app = adsk.core.Application.get();
if (!app) throw Error('No adsk.core.Application.');
adsk.log(`Running script with params: ${JSON.stringify(scriptParams)}`);
const fileId = scriptParams.fileId;
const fileName = scriptParams.fileName;
const dataFile = app.data.findFileById(fileId)!;
// create a new document
const document = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType);
if (!document) throw Error('Could not create a new document.');
let design = document.products.itemByProductType("DesignProductType") as adsk.fusion.Design;
if (!design) {
adsk.log('No design product found in the document.');
return;
}
design.rootComponent.occurrences.addByInsert(dataFile, adsk.core.Matrix3D.create(), false);
// const document = app.documents.open(dataFile, false);
if (document && document.products && document.products.count > 0) {
const design = document.products.item(0) as adsk.fusion.Design;
if (design.unitsManager) {
design.unitsManager.formatUnits('mm');
adsk.log('Units set to millimeters successfully.');
}
const rootComp = design.rootComponent;
if (rootComp && rootComp.bRepBodies) {
for (let i = 0; i < rootComp.bRepBodies.count; i++) {
const body = rootComp.bRepBodies.item(i);
if (body) {
body.name = 'ORIGINAL PART';
adsk.log('Body renamed to "ORIGINAL PART"');
break;
}
}
}
} else {
adsk.log('Could not access design from document.');
}
adsk.log(document?.name + ' - ' + document?.isSaved);
adsk.log(dataFile?.parentFolder.name + ' - ' + dataFile?.parentFolder.name);
document?.saveAs(fileName, dataFile.parentFolder, '', '');
dataFile.deleteMe();
}
run();