Fusion 360 encapsulates JavaScript and Python API that allow developers to extend the abilities of Fusion. The API manual introduces how to write and debug the script.
As a developer of JavaScript, you would be wondering where the source script is and whether it could load the 3rd library.
In [Script and Add-Ins] dialog, click the script, Details>>Full Path tells where the script is.
Generally, the built-in sample scripts reside in
C:\Users\kh\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\JavaScript\Samples
While the custom scripts locate in
C:\Users\kh\AppData\Roaming\Autodesk\Autodesk Fusion 360\MyJS\Scripts\
you can set the Folder Location of your script when creating it:
In the older version of Fusion, you can find each ‘script’ is actually consist of two companion files:
- *.HTML file
- *.js file
The HTML is the skeleton like what we develop a web program. The *.js file is what you created in Fusion. e.g. the below is a snapshot of a script named Bottle.
This is the HTML file of this sample:
Its HTML loads the necessary libraries of Fusion (in the relative path of ../src/adsk/Fusion, and Bottle.js file of this sample. When we run the sample within Fusion, it will turn to the HTML to load the libraries.
In the latest version, Fusion provides the mechanism that a *.manifest file defines the profile of the sample such as the libraries. So 3 companion files now:
- *.HTML file
- *.js file
- *.manifest file
The *.manifest file configures in a format of Json. The libraries to be loaded are defined at the node of autodeskLibraries.
{
"autodeskProduct": "Fusion360",
"type": "script",
"author": "Autodesk",
"description":
{
"": "This is a test"
},
"supportedOS": "windows|mac",
"autodeskLibraries":
["application",
"dashboard",
"geometry",
"materials",
"userInterface",
"utilities",
"bRep",
"components",
"construction",
"features",
"fusion",
"meshBody",
"meshData",
"sketch",
"tSpline"]
}
now, the *.HTML is much simpler. We just need to load the *.js file only.
Well, let us come back to the question of this blog title: how to load the 3rd library. The answer is very straightforward. It is like what you did in a common web programming, linking it through a relative path or the path on internet.
However currently the libraries for web UI have not been supported in Fusion 360. In another word, we cannot create the web page. While we can still link to some libraries that are algorism related.
I played with a math library Numeric. It allows you to get the result of linear algebra, complex numbers, splines, ODE solver and so on. I downloaded the mini library numeric-1.2.6.min.js to the same folder of my sample NewScript1. NewScript1.html loads the library. This sample gets the spline values from Numeric and draws a spline in one sketch.
[NewScript1.html] [<!DOCTYPEhtml> <html> <scripttype="text/javascript"src="../adsk/core/application.js"></script> <scripttype="text/javascript"src="../adsk/core/geometry.js"></script> <scripttype="text/javascript"src="../adsk/core/userInterface.js"></script> <scripttype="text/javascript"src="../adsk/utilities.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/bRep.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/components.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/construction.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/features.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/fusion.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/meshBody.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/meshData.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/sketch.js"></script> <scripttype="text/javascript"src="../adsk/Fusion/tSpline.js"></script> <scripttype="text/javascript"src="numeric-1.2.6.min.js"></script> <scripttype="text/javascript"src="NewScript1.js"></script> <body> </body>
|
[NewScript1.js] //Author- //Description- /*globals adsk*/ (function () {
"use strict";
if (adsk.debug === true) { /*jslint debug: true*/ debugger; /*jslint debug: false*/ }
var ui; try { var app = adsk.core.Application.get(); ui = app.userInterface;
// Get the active design. var product = app.activeProduct; var design = adsk.fusion.Design(product); if (!design) { ui.messageBox('No active Fusion design'); adsk.terminate(); return; }
// Get the root component. var rootComp = design.rootComponent;
// Create a new sketch on the xy plane. var sketch = (rootComp.xYConstructionPlane);
// Create an object collection for the points. var points =
// This is the spline demo. // We start by taking 30 evenly spaced points in the interval [0,3] var x = numeric.linspace(0, 3, 30);
// We define a function f(x) = sin(x*x*x) var f = function (x) {
// We sample the function at the 30 points we chose. var y = f(x);
// Now we create the spline with the given control points. var s = numeric.spline(x, y);
var x0 = numeric.linspace(0, 3, 100);
for (var xx in x0) { var x_cor = Number(xx); var y_cor = Number(s.at(Number(x0[xx]))); var z_cor = 0; points.add(adsk.core.Point3D.create( y_cor, } // Create the spline. sketch.sketchCurves.
} catch (e) { if (ui) { ui.messageBox('Failed : ' + } }
adsk.terminate(); }()); |