Webserver (Django REST) + RhinoInside

Hi,

I have been working to build Web API with Django REST framework running Rhino Inside and other geometry libraries. The machine hosting the server would run the geometric calculations, output geometry to a new Rhino3dm file (possibly multiple geometry types). The request would be sent through UI in open Rhino instance, on user PC. I would like the new Rhino file to be passed to the user back, and ideally to import it with all its contents to an open Rhino Instance (on user side).

Now my question is what would be the best way to serialize / encode data, to pass it back to the user, and import to an open Rhino instance ? Hopefully as a file, rather that serialising/deserialising each geometry type.

Previously I successfully passed geometry (ex. Breps) from server side through:
Json = Json.dumps(Newtonsoft.Json.JsonConvert.SerializeObject(Brep))

which on the user side opens as:
Brep = Newtonsoft.Json.JsonConvert.DeserializeObject(Json, Rhino.Geometry.Brep)

Hope it makes sense,
Wojtek

1 Like

One option would be to have your web server return the whole raw file instead of encoded data into json. If it takes a long time to generate the file, you could also return a URL to the client that the client could later call to try and retrieve the file.

1 Like

Great. I’ve managed to pass URL to the client. I can download the file and save locally, but is there an easy way to directly pass the file to import to an open rhino document ? Or would I have to download file temporarily, import to ActiveDoc and delete?

Read URL received by Client:

# Receives file URL on webserver
    (...)
    file_address = urllib2.urlopen("http://" + file_path)
    content = file_address.read()
    return content

Save file locally:

#Saves file from url locally
def saveDataLocal(file_name, content):
    with open(current_dir + "/{}.3dm".format(file_name), "wb") as out_file:
        out_file.write(content)

At the moment I import files simply by:

doc = Rhino.RhinoDoc.ActiveDoc

def importFile(path):
    options = Rhino.FileIO.FileReadOptions()
    options.ImportMode = True
    result = doc.ReadFile(path, options)  
    return result

Appreciate your advice.
WK

That looks like a decent approach. Is it being problematic?

1 Like

Hi,

@stevebaer it works fine if I save a file locally, then import it into a document and finally delete it. I was just wondering if there is a way of opening a file directly from the server location such as:

image

or alternatively:

image

But none of this work. If not, I will settle on the working version. Just curiosity, trying to avoid unnecessary steps.

Best,
Wojciech

Ah I see; what you are doing is correct. We don’t currently support opening a file from a web address.

Sounds good. Appreciate your help.
WK