Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Javascript from web service

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
asinghal
607 Views, 13 Replies

Javascript from web service

Hi

 

I am looking to send some parameter values to a web service and get back Javascript code.

 

This javascript needs to be executed in the Fusion 360 app to generate geometry.

 

Kindly provide a simple sample to get started.

 

Best Regards

Abhishek

13 REPLIES 13
Message 2 of 14
ShirleyShi
in reply to: asinghal

Hi Abhishek,

 

The solution depends on the architecture of your web service. Can you provide a bit more information about your web service?

  

Regards,

Shirley

Shirley

Developer for Fusion Electronics

Autodesk, Inc.

Message 3 of 14
asinghal
in reply to: ShirleyShi

Hi Shirley

 

The web service is ASP .NET / MVC secured by Bearer Authentication.

 

Best Regards

Abhishek

 

 

Message 4 of 14
ShirleyShi
in reply to: asinghal

Since it depends on the collabration with a server, it's not easy to give a simple sample. I just add a few comments here: If you simply want to send a few parameters to your web services, you could add some forms in the html file of the add-in and let the forms send the data to the server. You can also call Ajax to send your requests to the server. The whole process requires authorization, geometry processing etc, I think it might be easier to use C++/Python in this case to complete your work. If the service is only for yourself, the other option could be, using your knowledge of ASP.net to design the web page, and then embed it into the html of the add-in. But for security reasons, you have to set the X-Frame-Options properly.

Shirley

Developer for Fusion Electronics

Autodesk, Inc.

Message 5 of 14
asinghal
in reply to: ShirleyShi

Hi Shirley

 

OK - lets break this problem down into two issues.

We'll handle the security issue later.

 

For now, kindly provide a sample where some parameters are sent to web service, javascript is downloaded as response and that javascript is executed in the document to produce some geometry. The add-in language can be python if that makes this task simpler.

 

Best Regards

Abhishek

Message 6 of 14
marshaltu
in reply to: asinghal

Hello,

 

A simplest sample is as below. You could post parameters to your web server and get response from the server. Then you could generate geometries according to the response in Fusion 360.

 

Thanks,

Marshal

 

//Author-
//Description-

function run(context) {

    "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;
 
        var posturl = 'http://yoururl/?' + 'parameter1=' + 'value1' + 'parameter2=' + 'value2';
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', posturl, false);
        xmlhttp.send();
        
        // Parse results and create geometry in Fusion 360
        // ....
    } 
    catch (e) {
        if (ui) {
            ui.messageBox('Failed : ' + (e.description ? e.description : e));
        }
    }

    adsk.terminate(); 
}


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 7 of 14
asinghal
in reply to: marshaltu

Hi Marshal

 

Thanks for the code.

 

I was hoping the web service could itself generate some javascript that contains code to create geometry.

 

Client side add-in should be able to just run the script obtained from service.

 

Best Regards

Abhishek

Message 8 of 14
ShirleyShi
in reply to: asinghal

Hi Abhishek,

 

What you need to do is to let the server code generate the script and response to Fusion. Then in the Fusion API, you can call eval to run the script. 

 

For example, if your web service wants to generate some code to create a circle in Fusion with specified radius value, in the server side, you need some code like this:

public string GetCircleScript(double radius){
 return "varapp=adsk.core.Application.get();"
   + "ui=app.userInterface;"
   + "var doc=app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType);"
   + "var product=app.activeProduct;"
   + "var design=adsk.fusion.Design(product);"
   + "var rootComp=design.rootComponent;"
   + "var sketches=rootComp.sketches;"
   + "var xyPlane=rootComp.xYConstructionPlane;"
   + "var sketch=sketches.add(xyPlane);"
   + "var circles=sketch.sketchCurves.sketchCircles;"
   + "var circle=circles.addByCenterRadius(adsk.core.Point3D.create(0,0,0)," + radius.toString() + ");";
}

 

In Fusion side, you will get the respsonse string as follows if you send the request with radius=2:

 

var responseText="var app = adsk.core.Application.get();ui = app.userInterface;var doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType);var product = app.activeProduct;var design = adsk.fusion.Design(product);var rootComp = design.rootComponent;var sketches = rootComp.sketches;var xyPlane = rootComp.xYConstructionPlane;var sketch = sketches.add(xyPlane);var circles = sketch.sketchCurves.sketchCircles;var circle = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2); ";

 

Then you can simply call the following code to run the script to create the circle in Fusion:

 

eval(responseText);

 

 

Shirley

Developer for Fusion Electronics

Autodesk, Inc.

Message 9 of 14
asinghal
in reply to: ShirleyShi

Hi

 

Looks good.

 

Can the client side language be Python ?

 

The server would still return Javascript.

 

Best Regards

Abhishek

Message 10 of 14
ShirleyShi
in reply to: asinghal

 

Yes you can definitely use Python in the client side. Actually I recommend to use Python because you can grain more power to process numeric data and if your web services is not restful, it will be easier for you to access your web services than Javascript. However, if you use Python, you can let the server return Python too  - there is no extra work for you since the script is supposed to run in Fusion.

 

Sample code segment in Python:
      #Suppose the server returns a piece of script you want to run in Fusion, and you save the script into the variable returnedScript:
      #returnedScript="doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)\ndesign = app.activeProduct\nrootComp = design.rootComponent\nsketches = rootComp.sketches\nxyPlane = rootComp.xYConstructionPlane\nsketch = sketches.add(xyPlane)\ncircles = sketch.sketchCurves.sketchCircles\ncircle = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 20)"

      serviceScript=returnedScript.decode('utf-8')
      exec(serviceScript)

 

 

Shirley

Developer for Fusion Electronics

Autodesk, Inc.

Message 11 of 14
asinghal
in reply to: ShirleyShi

Hi Shirley

 

Agreed regarding use of Python on client side.

 

From script generation point of view, I find that Javascript tends to be more precise.

 

Is it possible to have the client side Python execute a script obtained from server in Javascript ?

 

Best Regards

Abhishek

Message 12 of 14
marshaltu
in reply to: asinghal

Hello,

 

Unfortunately there was no way to do that so far. I would like to recommend you log the idea to our ideastation.

 

http://forums.autodesk.com/t5/ideastation-request-a-feature-or/idb-p/125

 

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 13 of 14
ShirleyShi
in reply to: asinghal

Can you explain more about "From script generation point of view, I find that JavaScript tends to be more precise"? I think it's much easier to fix this issue than calling Fusion JS in Python.

Shirley

Developer for Fusion Electronics

Autodesk, Inc.

Message 14 of 14
asinghal
in reply to: ShirleyShi

Hi Shirley

 

That will take us into philosophical domain 🙂

 

Best Regards

Abhishek

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report