/src/examples/Level2/XMLRPC/geometry_server.py

http://pythonocc.googlecode.com/ · Python · 76 lines · 44 code · 12 blank · 20 comment · 1 complexity · 1400ee81dfb52d98358f273a784d3c3a MD5 · raw file

  1. #!/usr/bin/env python
  2. ##Copyright 2009-2011 Thomas Paviot (tpaviot@gmail.com)
  3. ##
  4. ##This file is part of pythonOCC.
  5. ##
  6. ##pythonOCC is free software: you can redistribute it and/or modify
  7. ##it under the terms of the GNU Lesser General Public License as published by
  8. ##the Free Software Foundation, either version 3 of the License, or
  9. ##(at your option) any later version.
  10. ##
  11. ##pythonOCC is distributed in the hope that it will be useful,
  12. ##but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ##GNU Lesser General Public License for more details.
  15. ##
  16. ##You should have received a copy of the GNU Lesser General Public License
  17. ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
  18. import SimpleXMLRPCServer
  19. from OCC.BRepPrimAPI import *
  20. from OCC.TopoDS import *
  21. from OCC.gp import *
  22. from OCC.Geom import *
  23. from OCC.GeomAPI import *
  24. from OCC.TColgp import *
  25. from OCC.BRepBuilderAPI import *
  26. import pickle
  27. # Creating XMLRPC server
  28. server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888))
  29. def point_list_to_TColgp_Array1OfPnt(li):
  30. pts = TColgp_Array1OfPnt(0, len(li)-1)
  31. for n,i in enumerate(li):
  32. pts.SetValue(n,i)
  33. return pts
  34. def make_face(shape):
  35. face = BRepBuilderAPI_MakeFace(shape)
  36. face.Build()
  37. return face.Shape()
  38. class ShapeFactory(object):
  39. def getBox(self, x,y,z):
  40. print 'Box creation (%f,%f,%f)'%(x,y,z)
  41. shape = BRepPrimAPI_MakeBox(x,y,z).Shape()
  42. return pickle.dumps(shape)
  43. def getSphere(self, radius):
  44. print 'Sphere creation of radius %f'%radius
  45. shape = BRepPrimAPI_MakeSphere(radius).Shape()
  46. return pickle.dumps(shape)
  47. def getSurfaceFromRevolution(self):
  48. print 'Surface of revolution created'
  49. array = []
  50. array.append(gp_Pnt(0,0,1))
  51. array.append(gp_Pnt(1,2,2))
  52. array.append(gp_Pnt(2,3,3))
  53. array.append(gp_Pnt(4,3,4))
  54. array.append(gp_Pnt(5,5,5))
  55. aCurve = GeomAPI_PointsToBSpline(point_list_to_TColgp_Array1OfPnt(array)).Curve()
  56. SOR =Geom_SurfaceOfRevolution(aCurve, gp.gp().OX())
  57. shape = make_face(SOR.GetHandle())
  58. return pickle.dumps(shape)
  59. # Creating object to share
  60. shape_factory = ShapeFactory()
  61. server.register_instance(shape_factory)
  62. #Go into the main listener loop
  63. print "Listening on port 8888"
  64. server.serve_forever()