Availability of javascript standard libraries such as XML APIs

Availability of javascript standard libraries such as XML APIs

Anonymous
Not applicable
1,255 Views
3 Replies
Message 1 of 4

Availability of javascript standard libraries such as XML APIs

Anonymous
Not applicable

Hello,

I am playing around with the fusion 360 API.

 

I am basically want to create an assembly based from a local read XML file. I am getting so far so show a file dialog and let the user select the XML-File. Then I read the file using adsk.readFile(dialog.filename). But I have no success doing anything further with the buffer because two approaches to convert the ArrayBuffer into something sensible. Here is what I tried:

 

Approach 1:

 

var dataView = new DataView(contentBuffer);
var decoder = new TextDecoder(encoding);
var decodedString = decoder.decode(dataView);

 

Approach 2:

 

String.fromCharCode.apply(null, new Uint16Array(contentBuffer));

 

Both approaches fail because either DataView is not found or Uint16Array is not found. I found both approaches on the internet but as far as I see, these are standard approaches to process ArrayBuffers in current javascript environments.

 

I miss those "standard libraries". Can you comment on the availability of javascript standard libraries? 

0 Likes
Accepted solutions (1)
1,256 Views
3 Replies
Replies (3)
Message 2 of 4

KrisKaplan
Autodesk
Autodesk
Accepted solution

Hi,

 

Is this on Windows or Mac?  If a Mac, what version are you running on?  Can you include the output of 'navigator.userAgent' from the console of the debugger (or just output in a messageBox from a script).

 

Support for these types (ArrayBuffer, Uint8Array, Uint16Array, DataView, etc...) are browser specific as they are still part of the JavaScript 6 draft specification.  However, the version of the Chromium Web Browser we use on Windows, and the WebKit version used on Mac platforms tested on (I just verified it on 10.9.5 and 10.10) do support all of these types.

 

If you look at the implementation of adsk.readFile in utilities.js, you will notice that it uses Uint8Array (as well as ArrayBuffer) in it's implementation.  Uint8Array support should match Uint16Array support, and if support for Uint8Array was missing from your version, adsk.readFile should have thrown a type exception and not succeeded.

 

In utilities.js there is also the implementation of 'adsk.utf8ToString'.  This can be used to convert the bytes in an ArrayBuffer or a Uint8Array into a JavaScript String.  If your XML file is ASCII or UTF-8, you can just use this function to make the conversion.  If it is in some other encoding, you should be able to use DataView or any equivalent polyfill implementation to do the same, or use adsk.utf8ToString as a reference to create your own.  Otherwise, if you can post a more complete script and data file to read, I can try to see what's going wrong.

 

FYI: The ImportSplineCSV.js sample script demonstrates reading a text file (ASCII or UTF-8) into a JavaScript String with the following code.

 

        ...
var buffer = adsk.readFile(filename); ... var data = adsk.utf8ToString(buffer); data = data.split('\n');
...

 

 Kris



Kris Kaplan
0 Likes
Message 3 of 4

Anonymous
Not applicable

Hello KrisKaplan,

thank you for your reply. This is on a windows machine. navigator.userAgent says:

 

Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Neutron/2.0.1300 Chrome/31.0.1650 Safari/537.36

 

DataView is available, howewer the use of TextDecoder results in an error message: "Failed: ReferenceError: TextDecoder is not defined"

 

Using adsk.utf8ToString, I was able to do what I tried using DataView and TextDecoder. I am now a step further and get the root element of my XML file.

 

Thank you for your help. I will report back when I have something to show.

 

Axel

 

 

0 Likes
Message 4 of 4

KrisKaplan
Autodesk
Autodesk

OK.  TextDecoder/Encoder is still a spec feature, and support for it was added in Chrome version 38. From the userAgent string, you can see that the version of Chromium we are currently using is equivalent to Chrome version 31.  So it does not support this functionality yet.  But if you would like to use TextDecoder, there is a polyfill implementation that you can include into your script available at http://code.google.com/p/stringencoding/.

 

Kris



Kris Kaplan
0 Likes