-
Notifications
You must be signed in to change notification settings - Fork 14
/
Speckle.py
229 lines (178 loc) · 7.83 KB
/
Speckle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#***************************************************************************
#* *
#* Copyright (c) 2018 Yorik van Havre <[email protected]> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import os, requests, xml.sax, uuid
### Generic Speckle interface (doesn't depend on FreeCAD)
def register(email,name,surname,company,password):
data = {"email":email,"name":name,"surname":surname,"company":company,"password":password}
r = requests.post(url='http://localhost:3000/api/accounts/register',data=data)
if r.json()["success"] == True:
return r.json()["token"]
else:
print(r.json()["message"])
return False
def login(email,password):
data = {"email":email,"password":password}
r = requests.post(url='http://localhost:3000/api/accounts/login',data=data)
if r.json()["success"] == True:
#print r.json()["apiToken"]
return r.json()["token"]
else:
print(r.json()["message"])
return False
def getStreams(token):
r = requests.get(url='http://localhost:3000/api/accounts/streams',headers={"Authorization":token})
if r.json()["success"] == True:
return r.json()["streams"]
else:
print(r.json()["message"])
return False
def getSharedStreams(token):
r = requests.get(url='http://localhost:3000/api/accounts/streams',headers={"Authorization":token})
if r.json()["success"] == True:
return r.json()["sharedStreams"]
else:
print(r.json()["message"])
return False
def createStream(token,name="test"):
headers = {"Authorization":token}
data = {"_id":"59ce300e7056224ff5eb06ae","name":name,"objects":[],"layers":[]}
data={"_id": "59ce300e7056224ff5eb06ae",
"owner": "string",
"private": True,
"anonymousComments": True,
"canRead": ["string"],
"canWrite": ["string"],
"comments": ["string"],
"deleted": False,
"streamId": "string",
"name": "string",
"objects": [ {"_id": "string",
"owner": "string",
"private": True,
"anonymousComments": True,
"canRead": ["string"],
"canWrite": ["string"],
"comments": ["string"],
"deleted": False,
"type": "Null",
"hash": "hash",
"geometryHash": "Type.hash",
"applicationId": "GUID",
"properties": {},
"parent": "string",
"children": ["string"],
"ancestors": ["string"] } ],
"layers": [ { "name": "string",
"guid": "string",
"orderIndex": 0,
"startIndex": 0,
"objectCount": 0,
"topology": "string" } ],
"baseProperties": {},
"globalMeasures": {},
"isComputedResult": False,
"viewerLayers": [ {} ],
"parent": "string",
"children": ["string"],
"ancestors": ["string"]}
r = requests.post(url='http://localhost:3000/api/streams',data=data,headers=headers)
return r
if r.json()["success"] == True:
return r.json()["streams"]
else:
print(r.json()["message"])
return False
### FreeCAD <-> json conversion tools
def jsonify(obj):
result = {"typeid":obj.TypeId}
if hasattr(obj,"Name"):
result["name"] = obj.Name
reader = FCObjectReader(obj)
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
parser.setContentHandler(reader)
parser.parse(obj.Content)
result["extensions"] = reader.extensions
result["properties"] = reader.properties
return result
### FreeCAD-specific interface
class FCObjectReader(xml.sax.ContentHandler):
def __init__(self,obj):
self.obj = obj
self.extensions = []
self.properties = []
self.property = None
self.enums = []
def startElement(self, tag, attributes):
if tag == "Extension":
self.extensions.append(attributes)
elif tag == "Property":
self.property = attributes
if attributes["type"] == "Part::PropertyPartShape":
shape = getattr(self.obj,attributes["name"])
if shape:
self.property["brep"] = shape.exportBrepToString()
elif tag == "Enum":
self.enums.append(attributes["value"])
else:
self.property.update(attributes)
def endElement(self, tag):
if tag == "Property":
if self.property:
self.properties.append(self.property)
elif tag == "CustomEnumList":
self.property["enumValues"] = self.enums
self.enums = []
class CommandSpeckle:
"the WebTools_Speckle command definition"
def GetResources(self):
return {'Pixmap' : os.path.join(os.path.dirname(__file__),"icons",'speckle.svg'),
'MenuText': QT_TRANSLATE_NOOP("WebTools_Speckle","Speckle.Works"),
'ToolTip': QT_TRANSLATE_NOOP("WebTools_Speckle","Connects and interacts with a Specke.Works server instance")}
def Activated(self):
import FreeCAD
try:
import requests
except:
FreeCAD.Console.PrintError(translate("WebTools","requests python module not found, aborting. Please install python-requests\n"))
return
try:
import json
except:
FreeCAD.Console.PrintError(translate("WebTools","json python module not found, aborting. Please install python-json\n"))
else:
FreeCADGui.Control.showDialog(SpeckleTaskPanel())
class SpeckleTaskPanel:
def __init__(self):
import FreeCAD, FreeCADGui
from PySide import QtCore, QtGui
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(os.path.dirname(__file__),"dialogSpeckle.ui"))
self.form.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__),"icons","Speckle.svg")))
self.prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/WebTools")
try:
import FreeCAD
except:
pass
else:
if FreeCAD.GuiUp:
import FreeCADGui
FreeCADGui.addCommand('WebTools_Speckle',CommandSpeckle())