danfalck

headless_freecad_to_svg.py

Nov 11th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4.  
  5. FREECADPATH = '/home/danfalck/cadcam/freecadcam/dancam6/freecad/lib'
  6. sys.path.append(FREECADPATH)
  7. import FreeCAD, Part, Drawing
  8.  
  9. #dv = 0.000000000001
  10.  
  11. box = Part.makeBox(100,100,100)
  12. box.translate(box.CenterOfMass.multiply(-1))
  13.  
  14. cylx = Part.makeCylinder(25, 100, FreeCAD.Base.Vector(0,1,0), FreeCAD.Base.Vector(0,90,0))
  15. cylx.translate(cylx.CenterOfMass.multiply(-1))
  16.  
  17. cyly = Part.makeCylinder(25, 100, FreeCAD.Base.Vector(0,1,0), FreeCAD.Base.Vector(90,0,0))
  18. cyly.translate(cyly.CenterOfMass.multiply(-1))
  19.  
  20. cylz = Part.makeCylinder(25, 100)
  21. cylz.translate(cylz.CenterOfMass.multiply(-1))
  22.  
  23. hole = cylx.fuse(cyly).fuse(cylz)
  24.  
  25. test = box.cut(hole).common(Part.makeSphere(50))
  26.  
  27. #with open('coin.iv', 'w') as f:
  28. #  f.write(test.writeInventor())
  29.  
  30. Part.show(test)
  31.  
  32. # FreeCAD.ActiveDocument.FileName = 'foo.fcstd'
  33. # FreeCAD.ActiveDocument.save()
  34.  
  35. Shape = FreeCAD.ActiveDocument.Shape.Shape
  36.  
  37. visibleG0,visibleG1,hiddenG0,hiddenG1 = Drawing.project(Shape,FreeCAD.Base.Vector(1,1,1))
  38. resultSVG = Drawing.projectToSVG(Shape,FreeCAD.Base.Vector(1,0.2,1))
  39. # if 0 is used instead of dv, the image is rendered incorrectly
  40. #resultSVG = Drawing.projectToSVG(Shape,FreeCAD.Base.Vector(0,dv,1))
  41.  
  42. with open('foo.svg', 'w') as f:
  43.   f.write('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-100 -100 200 200">')
  44.   f.write(resultSVG)
  45.   f.write('</svg>')
Add Comment
Please, Sign In to add comment