Javascript from web service

Javascript from web service

Anonymous
Not applicable
1,726 Views
13 Replies
Message 1 of 14

Javascript from web service

Anonymous
Not applicable

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

0 Likes
Accepted solutions (1)
1,727 Views
13 Replies
Replies (13)
Message 2 of 14

ShirleyShi
Alumni
Alumni

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.

0 Likes
Message 3 of 14

Anonymous
Not applicable

Hi Shirley

 

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

 

Best Regards

Abhishek

 

 

0 Likes
Message 4 of 14

ShirleyShi
Alumni
Alumni
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.

0 Likes
Message 5 of 14

Anonymous
Not applicable

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

0 Likes
Message 6 of 14

marshaltu
Autodesk
Autodesk

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 Developer
>
0 Likes
Message 7 of 14

Anonymous
Not applicable

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

0 Likes
Message 8 of 14

ShirleyShi
Alumni
Alumni
Accepted solution

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

Anonymous
Not applicable

Hi

 

Looks good.

 

Can the client side language be Python ?

 

The server would still return Javascript.

 

Best Regards

Abhishek

0 Likes
Message 10 of 14

ShirleyShi
Alumni
Alumni

 

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.

0 Likes
Message 11 of 14

Anonymous
Not applicable

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

0 Likes
Message 12 of 14

marshaltu
Autodesk
Autodesk

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 Developer
>
0 Likes
Message 13 of 14

ShirleyShi
Alumni
Alumni
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.

0 Likes
Message 14 of 14

Anonymous
Not applicable

Hi Shirley

 

That will take us into philosophical domain 🙂

 

Best Regards

Abhishek

0 Likes