通用的方式调用CREOSON做CREO二次开发
这是调用CREOSON做CREO二次开发的最后一篇。CREOSON的架构是用采用web服务器的方式对jlink进行了二次开发,用户通过对CREOSON server发送Http请求,并返回相应的结果。所以不管什么开发语言,只要能够按照官方给的格式发送请求并读取返回结果,即可轻松实现CREO二次开发。
1.Curl命令测试
先用curl命令来手动实现前面打开当前目录下fin.prt添加参数后保存的代码。
1.1 创建会话
首先是打开Creo会话。按照CREOSON的文档,向CREOSON server发送请求:
1 | { |
打开命令提示符,输入以下命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"connection\",\"function\":\"start_creo\",\"data\":{\"start_dir\":\"D:\\\\mydoc\\\\Creoson_test\",\"start_command\":\"nitro_proe_remote.bat\",\"retries\":5,\"use_desktop\":false}}" |
运行正确会返回以下结果,同时打开了Creo:
1 | {"status":{"error":false},"data":null} |
1.2 连接会话
接下来是连接会话,获取SessionId,对应的请求是:
1 | { |
运行Curl命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"connection\",\"function\":\"connect\",\"data\":{\"timeout\":60000}}" |
正常的情况下返回结果如下,sessionId根据实际会有不同的值:
1 | {"status":{"error":false},"sessionId":"8332306140927056488","data":null} |
1.3 切换工作目录
对应的请求如下,注意sessionId应是上一步返回的结果:
1 | { |
运行Curl命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"creo\",\"function\":\"cd\",\"sessionId\":\"8332306140927056488\",\"data\":{\"dirname\":\"D:\\\\mydoc\\\\Creoson_test\"}}" |
正常的情况下返回结果如下:
1 | {"status":{"error":false},"data":{"dirname":"D:/mydoc/Creoson_test/"}} |
1.4 打开文件
对应的请求如下,注意sessionId:
1 | { |
运行Curl命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"file\",\"function\":\"open\",\"sessionId\":\"8332306140927056488\",\"data\":{\"file\":\"fin.prt\",\"display\":true,\"activate\":true}}" |
返回结果如下:
1 | {"status":{"error":false},"data":{"files":["FIN.prt"],"dirname":"D:/mydoc/Creoson_test/"}} |
1.5 添加修改参数
对应的请求如下,注意sessionId:
1 | { |
运行Curl命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"parameter\",\"function\":\"set\",\"sessionId\":\"8332306140927056488\",\"data\":{\"name\":\"test\",\"type\":\"STRING\",\"value\":\"Curl手动添加参数\",\"no_create\":false,\"designate\":true}}" |
返回结果如下:
1 | {"status":{"error":false},"data":null} |
1.6 保存文件
对应的请求如下,注意sessionId:
1 | { |
运行Curl命令:
1 | curl -X POST "http://localhost:9056/creoson" -H "Content-Type: application/json" --max-time 60 -k -d "{\"command\":\"file\",\"function\":\"save\",\"sessionId\":\"8332306140927056488\",\"data\":{\"file\":\"fin.prt\"}}" |
返回结果如下:
1 | {"status":{"error":false},"data":null} |
这样我们就用手动输入Curl命令实现了前面文章的CREO二次开发功能。
2.使用批处理实现
根据上面的内容,可以将Curl命令写入一个批处理文件中,然后运行该批处理文件即可实现CREO二次开发功能,唯一需要处理的就是获得第二步Connect后得到的SessionId并传入后续步骤。也没什么好说的,直接给出代码:
1 | @echo off |
3.PHP实现
按照前面的思路,给出php实现打开当前目录下fin.prt添加参数后保存的代码,也没什么好解释的,无非是发送对应的请求即可:
1 |
|
4.Python实现
同样的,Python也可以不使用CREOSON的库,使用HTTP请求实现:
1 | import requests |
其余如java、CSharp等语言均可按照这个思路实现,代码公开不一一赘述了,可在Github.com下载。