yorikvanhavre / WebTools

A series of tools to communicate with web services from within FreeCAD
http://www.freecadweb.org
17 stars 13 forks source link

[Solved] Problems when exporting project to BIMServer #11

Open dcapeletti opened 4 years ago

dcapeletti commented 4 years ago

Hi, I've been trying to export my work to BIMServer and I've encountered some problems, I've fixed the problems in my python file version. I've attached the file so you can merge it into the main branch.

I have solved the following problems: To get the deserializers: ifcopenshell.file().schema

The ifc file was not really created on disk.

Greetings

OS: Debian GNU/Linux 9 (stretch) (GNOME/default) Word size of OS: 64-bit Word size of FreeCAD: 64-bit Version: 0.19.20639 (Git) AppImage Build type: Release Branch: master Hash: 45aef7b028a54c5420db49ff3284fc7377475e90 Python version: 3.8.2 Qt version: 5.12.5 Coin version: 4.0.0 OCC version: 7.4.0 Locale: Spanish/Argentina (es_AR)

def uploadFile(self):
        self.form.labelStatus.setText("")
        if (self.form.comboProjects.currentIndex() >= 0) and (len(self.Projects) > self.form.comboProjects.currentIndex()) and (self.form.comboRoot.currentIndex() >= 0):
            project = self.Projects[self.form.comboProjects.currentIndex()]
            import requests, json
            url,token = self.getPrefs()
            if url and token:
                url += "/json"
                deserializer = None
                FreeCAD.Console.PrintMessage(translate("WebTools","Saving file...\n"))
                self.form.labelStatus.setText(translate("WebTools","Checking available deserializers..."))
                import ifcopenshell
                schema = ifcopenshell.file().schema.lower()
                data = { "token": token, "request": { "interface": "PluginInterface",  "method": "getAllDeserializers", "parameters": { "onlyEnabled": "true" } } }
                resp = requests.post(url,data = json.dumps(data))
                if resp.ok:
                    try:
                        for d in resp.json()["response"]["result"]:
                            if schema in d["name"].lower():
                                deserializer = d
                                break
                    except:
                        pass
                if not deserializer:
                    FreeCAD.Console.PrintError(translate("WebTools","Unable to get a valid deserializer for the schema")+" "+schema+"\n")
                    return
                tf = QtGui.QFileDialog.getSaveFileName(QtGui.QApplication.activeWindow(), translate("WebTools","Save the IFC file before uploading?"), None, translate("WebTools","IFC files (*.ifc)"))
                comment = self.form.editComment.text()
                if tf and tf[0]:
                    tf = tf[0]
                    if not comment:
                        comment = os.path.basename(tf)
                else:
                    tf = tempfile.mktemp(suffix=".ifc")
                import exportIFC
                self.form.labelStatus.setText(translate("WebTools","Saving file..."))
                exportIFC.export([self.RootObjects[self.form.comboRoot.currentIndex()]],tf)
                f = open(tf,"rb")
                ifcdata = base64.b64encode(f.read()).decode("ascii")
                f.close()
                FreeCAD.Console.PrintMessage(translate("WebTools","Uploading file to Bimserver...\n"))
                self.form.labelStatus.setText(translate("WebTools","Uploading file..."))
                data = { "token": token, "request": { "interface": "ServiceInterface", "method": "checkinSync", "parameters": { "poid": project["oid"], "comment": comment, "deserializerOid": deserializer["oid"], "fileSize": os.path.getsize(tf), "fileName": os.path.basename(tf), "data": ifcdata, "merge": "false" } } }
                resp = requests.post(url,data = json.dumps(data))
                if resp.ok:
                    if "result" in resp.json()["response"]:
                        FreeCAD.Console.PrintMessage(translate("WebTools","File upload successful\n"))
                        self.getRevisions(self.form.comboProjects.currentIndex())
                    elif "exception" in resp.json()["response"]:
                        FreeCAD.Console.PrintError(translate("WebTools","File upload failed, caused by: ")+resp.json()["response"]["exception"]["message"]+"\n")
                    else:
                        FreeCAD.Console.PrintError(translate("WebTools","File upload failed\n"))
        self.form.labelStatus.setText("")