To investigate the ability how Fusion 360 API connects to IoT, I started to pick some relevant technologies to practice. I was imagining a scenario that a sensor produces data and transfers to Fusion 360, the data drives a specific parameter of a model. This is a video on what I have achieved.
Although I have not yet got a sensor, it proves a possibility of such scenario. The ‘sensor’ is simulated by a client on web or mobile. An Android app is also created, thus we can touch the screen to send the data. The source code and demo usage can be found at
https://github.com/xiaodongliang/Fusion360-SocketIO-Drive-Parameter
From technical perspective, it is not a problem to drive parameter of Fusion model by API. While the first is how we transfer the data. I was attracted by Web Socket and Socket.IO. I am brand new with socket. This is some quotation from this blog:
'Socket.IO is a WebSocket API created by Guillermo Rauch, CTO of LearnBoost and lead scientist of LearnBoost Labs. Socket.IO will use feature detection to decide if the connection will be established with WebSocket, AJAX long polling, Flash, etc., making creating realtime apps that work everywhere a snap. Socket.IO also provides an API for Node.js which looks very much like the client side API.'
The official site of Socket.IO provides nice tutorial to get started with. By the guideline, I created my server of Node.js quickly. The scenario assumes the server is listening to the data from client with the specific tag, say ‘'fusion360'’. It will achieve the a user name and the updated data value. Then it will emit the message to other listeners. Since Fusion 360 provides API to know who logged, the emitting will send the data with the user name. Thus only the Fusion instance of which current logged user is the specific account can respond to the data to drive the parameter. To simulate the data producing, I also created an client, an HTML page.
Node.js server
var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(process.env.PORT || 3003); // listen to the data from client io.on('connection', function(socket){ socket.on('fusion360', function(msg){ console.log('message: ' + msg.user +' ' + msg.newv); //convert string to number var recievedV = msg.newv * 1.0; //emit the value to other listeners io.emit(msg.user, recievedV); }); console.log('connected'); }); // simulating the data producing on client app.get('/driveparam', function (req, res) { res.sendfile(__dirname + '/Fusion360/driveparam/index.html'); });
Then, I deployed the sever to Heroku: http://adnxdsocket.herokuapp.com/.
Client to simulate data producing (some key lines)
//import JS library of Socket.IO < script src="https://cdn.socket.io/socket.io-1.0.0.js">< / script> //initialize socket object. Input the sever URL which hosts socket transferring. var socket = io('http://adnxdsocket.herokuapp.com/'); //When the client produces the updated data, the code builds a json to emit to the server. //user name var user = $('#txtUser').val(); //new data var newv = targetRad/iniRad; //emit to server socket.emit('fusion360', {user:user,newv:newv}); //The verify the sending and listening, the client also set a listener and shows the data which is emited from the sever . socket.on($('#txtUser').val(), function(msg){ $('#txtValue').val(msg); });
Now, the workflow of emitting and listening are ready. We can verify by playing the client to see the updating value in the textbox. Next, we just need to copy the workflow of client to fusion 360.
As we know, Fusion 360 provides Javascript API, with which, we can migrate the code pretty easily.
- Create an Add-in of JS.
- Import JS library of Socket.IO at *.html
< script type="text/javascript" charset="UTF-8" src="RemoteDriveParam.js">< / script>
3. In *.js , define the a dialog which contains some controls.
Combox: parameters list
Checkebox: enable or diable listening
Textbox: display current value of parameter
4. In the Input changed handler, define the logic when listening is enabled or not.
if(isToggleSocket){ whichParamInput.isEnabled = false; initialV = currentParamValueInput.value; if(socketObj == null) socketObj = io(socketServerURL); if(socketObj){ socketObj.off(userName); //watch the data that is emitted with the message of this specific user. socketObj.on(userName, function(msg){ //update the parameter with the data as a percentage of the initial data. currentP.value = initialV * msg; args.isValidResult = true; //refresh the screen to see the update var app = adsk.core.Application.get(); app.activeViewport.refresh(); //looks there is an issue when updating the input in socket //currentParamValueInput.value = currentP.value ; }); } } else{ //disable listening whichParamInput.isEnabled = true; args.isValidResult = true; if(socketObj) socketObj.off(userName); }
Issues:
There is one issue at Fusion API side. In the event of Socket, the parameter is updated, but Fusion will freeze if updating other control of the dialog (say that textbox). I think this might be a problem of message pump. I am working on this issue.
Comments