Tutorial on creating a CAD Client-Server infrastructure with pythonOCC and THREE.js

client web page

This tutorial will show you how to set up a CAD server for processing models submitted via a client web page. The client page will display the 3D model submitted. Note that only free software libraries are used. If you ever wanted to set up your own CAD system in the cloud, this is the tutorial for you!

A server written in Python is using pythonOCC to process CAD models and commands and return a mesh triangulation. The client web page displays the 3D model using THREE.js

If you don’t have it set up, or are unfamiliar with pythonOCC, the Python OpenCASCADE wrapper, please go our tutorial here first to to set it up alongside Anaconda platform.  

We covered THREE.js in this blog post.

We used the Spyder IDE for Python for coding and debugging this project. If you don’t have it, please install it using Anaconda.Likewise, if any Python packages mentioned in this tutorial are missing, please install them from the Anaconda command prompt.

Set up a project folder with three Python files: CADprocessor.py, main.py and myserver.py. We will cover each file separately. You can download the tutorial files below.



CADProcessor.py is the class that handles CAD file processing and triangulation. Only IGES files supported in this tutorial. You should be familiar with the basics of pythonOCC from our tutorial linked earlier in this article.

# -*- coding: utf-8 -*-
@author: Mihai Pruna
use at own risk, no warranty implied or provided

#load needed libraries
from OCC.IGESControl import IGESControl_Reader
from OCC.StlAPI import StlAPI_Writer
from OCC.BRepMesh import BRepMesh_IncrementalMesh
#this class holds functions for manipulating CAD files with PythonOCC
class CADProcessorClass():
#saves STL file
def SaveTriangulation(self,filename):
#create new IGES reader class
reader = IGESControl_Reader()
#read file, make sure you update path. Note in Windows slash needs to be used
#no idea what this does but without it the shape won't be created 🙂
#generate Shape
shape = reader.Shape()
#mesh shape
mesh = BRepMesh_IncrementalMesh(shape, 0.5, True, 0.5, True)
#save STL file on server
stl_writer = StlAPI_Writer()
return "tris.stl"

myserver.py is the class that handles POST requests from the client web page. Requests contain the file name, the file content, and a command string. Only command supported in this tutorial is Render, which generates STL triangulation of the CAD file (only IGES supported) and returns it to the client. 

from http.server import BaseHTTPRequestHandler
# -*- coding: utf-8 -*-
@author: Mihai Pruna
use at own risk, no warranty implied or provided
#server class to handle requests
import cgi
from CADprocessor import CADProcessorClass

class FServer(BaseHTTPRequestHandler):

def do_HEAD(self):

def do_GET(self):
#this function will respond to file upload via POST
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers['content-type'])
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
postvars = cgi.parse_multipart(self.rfile, pdict)
#save uploaded file locally
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Content-Type', 'text/html')
#generate response depending on command
self.wfile.write(bytes(theresponse, 'UTF-8'));

def handle_http(self, status, content_type):
self.send_header('Content-type', content_type)
return bytes('Hello World', 'UTF-8')

def respond(self):
content = self.handle_http(200, 'text/html')

def showFileStats(self):
return bytes(str(self.linecnt), 'UTF-8')

def finish(self):
if not self.wfile.closed:

def writeBinaryData(self,binaryData,filename):
f = open(filename, "wb")

#only Render which uses Python OCC to generate triangulation as string
def ProcessFormData(self,theCommand,theFilename):
if theCommand=="Render":
with open(newfile, 'r') as file:
commandresponse = file.read().replace('\n', '')
return commandresponse


main.py is the entry point. It starts the server locally. You can run this one from the Anaconda prompt in the same folder where the file is located with python main.py or from Spyder.

# -*- coding: utf-8 -*-
@author: Mihai Pruna
use at own risk, no warranty implied or provided
#!/usr/bin/env python3

#entry file, starts server
import time
from http.server import HTTPServer
from myserver import FServer
#run locally
HOST_NAME = 'localhost'

if __name__ == '__main__':
httpd = HTTPServer((HOST_NAME, PORT_NUMBER), FServer)
print(time.asctime(), 'Server UP - %s:%s' % (HOST_NAME, PORT_NUMBER))
except KeyboardInterrupt:
print(time.asctime(), 'Server DOWN - %s:%s' % (HOST_NAME, PORT_NUMBER))


The file client.html is a web page with a mix of HTML and Javascript. Rather than the entire THREE.js, only the libraries needed are referenced (and included in the download package at the bottom of this page). When the server is running, you can upload an IGES file (one included in download package) and type Render in the text field. The file should be processed by the server and displayed on the page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--Author: Mihai Pruna. use at own risk, no warranty implied or provided-->
<html xmlns="http://progmodcon.com">
<!--Load libraries-->
<script type="text/javascript" src="sources/jquery-3.1.1.js"> </script>
<script type="text/javascript" src="sources/jquery-3.1.1.min.js"> </script>
<script type="text/javascript" src="sources/three.min.js"></script>
<script type="text/javascript", src="sources/STLLoader.js"></script>
<script src="sources/TrackballControls.js"></script>
<!--Create form for upload of file and command-->
<form id="myformid" enctype="multipart/form-data" method="post" action="http://localhost:8000/">
<input id="myfileid" name="file" type="file"/>
<p><label>Command field: <input name="command" type="text" id="command-id" /></label></p>
<input type="submit" id="submitid" value="Upload"/>
//retrieve elements from document
var form = document.getElementById('myformid');
var fileSelect = document.getElementById('myfileid');
var uploadButton = document.getElementById('submitid');
var commandstring=document.getElementById('command-id');
//gets called when upload button pushed
form.onsubmit = function(event) {
// Update button text.
uploadButton.innerHTML = 'Uploading...';
// Get the selected files from the input.
var files = fileSelect.files;
// Create a new FormData object.
var formData = new FormData();
//append data in dictionary format
formData.append('dafile', files[0], files[0].name);
// Set up the request.
var xhr = new XMLHttpRequest();
//function called when state changes
xhr.onreadystatechange = function() {
//if request completed successfully and response received
if (xhr.readyState === 4 && xhr.status === 200) {
if (commandstring.value=="Render")
var stlmesh=MakeSTLMesh(xhr.responseText);
parent = new THREE.Object3D();
// Open the connection.
xhr.open('POST', 'http://localhost:8000/', true);
// Send the Data.
//3D window with THREE.js
//scene storage container
var parent;
var container, stats;
var camera, scene, renderer;
var cube, plane;
var startTime = Date.now();
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
//this gets called once
function init() {
// create the camera
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 350;
camera.position.z = 350;
camera.lookAt(new THREE.Vector3(0,0,0));
// create the Scene
scene = new THREE.Scene();
//add some lights
light1 = new THREE.DirectionalLight('white', 1.0);
light1.name = 'Back light';
light2 = new THREE.DirectionalLight('white', 1.0);
light2.name = 'Back light';
light3 = new THREE.DirectionalLight('white', 1.0);
light3.name = 'Back light';
light4 = new THREE.DirectionalLight('white', 1.0);
light4.name = 'Back light';
// create the container element
container = document.createElement( 'div' );
document.body.appendChild( container );
// init the WebGL renderer and append it to the DOM
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//add mouse controls to rotate camera around object
controls = new THREE.TrackballControls(camera, renderer.domElement);
controls.minDistance = 100;
controls.maxDistance = 50000;
//3d object will be stored here
parent = new THREE.Object3D();
* animate and display the scene
function animate() {
// render the 3D scene
// relaunch the 'timer'
requestAnimationFrame( animate );
* Render the 3D scene
function render() {
// actually display the scene in the Dom element
renderer.render( scene, camera );
//make mesh from string triangulation sent back from server
function MakeSTLMesh(mySTLString)
var aloader= new THREE.STLLoader();
var color1=new THREE.Color(0.99, 0.99, 0.99);
var mygeom=aloader.parse(mySTLString);
var material1 = new THREE.MeshLambertMaterial({
ambient: color1,
color: color1,
transparent: true
var mymesh = new THREE.Mesh(mygeom, material1);
return mymesh;

<!-- this is where the 3D view will be-->
<!--<div id="viewport"></div>-->

We hope you find this tutorial useful. Contact us if you would like to hire us to create a CAD server for you.

Leave a Reply

Scroll Up