Macro 3d Printer Slicer: Difference between revisions

From FreeCAD Documentation
(Exports to Slicing or CAM software for 3D printers or CNC machines)
 
No edit summary
Line 1: Line 1:
{{Macro|Icon=Text-x-python|Name=Export to slicing software for 3D printers|Description=Exports stl file in the same directory as original design file, then opens it in slicing software.|Author=cae2100}}
{{Macro|Icon=Text-x-python|Name=Export to slicing software for 3D printers|Description=Exports stl file in the same directory as original design file, then opens it in slicing software.|Author=cae2100}}

This code, when run, will export the currently open design to STL file, and open it in the slicing software that you use. This example is for KISSlicer, but can be modified to use Slic3r, cura, or any other 3d printer software. It can also be modified slightly to open up CAM software for CNC machines.

The SLICER varable can be changed to any slicing software of your choosing, just make sure to change it before you try running it or it'll flag an error.

[code] import FreeCAD
import Mesh
import sys
import math
import os
import subprocess
# some fuctions
def getPlacement(quat,vect,obj):
if quat[3] > -1 and quat[3] < 1:
delta = math.acos(quat[3])*2.0
scale = math.sin(delta/2)
rx = quat[0]/scale
ry = quat[1]/scale
rz = quat[2]/scale
else:
delta = 0
rx = 0
ry = 0
rz = 1
info0 = "translation "+str(vect.x)+" "+str(vect.y)+" "+str(vect.z)
info1 = "rotation "+str(rx)+" "+str(ry)+" "+str(rz)+" "+str(delta)
return info0+" "+info1
# some definitions
placement = App.Placement(App.Vector(0,0,0),App.Rotation(0,0,0,1))
# user need to set this directory where STL files will be placed
OutDir = FreeCAD.ActiveDocument.FileName.replace(FreeCAD.ActiveDocument.Label + ".fcstd", "")
visible_objs = []
SLICER = "/kisslicer location/" # Put your Slicer program location here
os.chdir(SLICER)
# Get Objects in document
doc = App.ActiveDocument
objs = doc.Objects
# hide all
for obj in objs:
if obj.ViewObject.isVisible():
visible_objs.append(obj)
print "number of volumes "+str(len(visible_objs))
for obj in visible_objs:
# {
# get volume
volume = obj.Shape.Volume
# get Rotation and translation of volume
quat = obj.Placement.Rotation.Q
vect = obj.Placement.Base
pinfo = getPlacement(quat,vect,obj)
# reset placement, export it and set at original placement
oldPlacement = obj.Placement
obj.Placement = placement
obj.Placement = oldPlacement
stlFile = OutDir+str(doc.Label)+".stl"
Mesh.export(visible_objs,stlFile)
subprocess.Popen([SLICER + "KISSlicer", stlFile])[/code]

Revision as of 19:54, 9 October 2013

File:Text-x-python Export to slicing software for 3D printers

Description
Exports stl file in the same directory as original design file, then opens it in slicing software.

Author: cae2100
Author
cae2100
Download
None
Links
Macro Version
1.0
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

This code, when run, will export the currently open design to STL file, and open it in the slicing software that you use. This example is for KISSlicer, but can be modified to use Slic3r, cura, or any other 3d printer software. It can also be modified slightly to open up CAM software for CNC machines.

The SLICER varable can be changed to any slicing software of your choosing, just make sure to change it before you try running it or it'll flag an error.

[code] import FreeCAD

   import Mesh
   import sys
   import math
   import os
   import subprocess
   # some fuctions
   def getPlacement(quat,vect,obj):
     if quat[3] > -1  and quat[3] < 1:
       delta = math.acos(quat[3])*2.0
       scale = math.sin(delta/2)
       rx = quat[0]/scale
       ry = quat[1]/scale
       rz = quat[2]/scale
     else:
       delta = 0
       rx = 0
       ry = 0
       rz = 1
     info0 = "translation "+str(vect.x)+" "+str(vect.y)+" "+str(vect.z)
     info1 = "rotation "+str(rx)+" "+str(ry)+" "+str(rz)+" "+str(delta)
     return info0+" "+info1
   # some definitions
   placement = App.Placement(App.Vector(0,0,0),App.Rotation(0,0,0,1))
   # user need to set this directory where STL files will be placed
   OutDir = FreeCAD.ActiveDocument.FileName.replace(FreeCAD.ActiveDocument.Label + ".fcstd", "")
   visible_objs = []
   SLICER = "/kisslicer location/"                          # Put your Slicer program location here
   os.chdir(SLICER)
   # Get Objects in document
   doc = App.ActiveDocument
   objs = doc.Objects
   # hide all
   for obj in objs:
      if obj.ViewObject.isVisible():
         visible_objs.append(obj)
   print "number of volumes "+str(len(visible_objs))
   for obj in visible_objs:
   # {
     # get volume
     volume = obj.Shape.Volume
     # get Rotation and translation of volume
     quat = obj.Placement.Rotation.Q
     vect = obj.Placement.Base
     pinfo = getPlacement(quat,vect,obj)
     # reset placement, export it and set at original placement
     oldPlacement = obj.Placement
     obj.Placement = placement
     obj.Placement = oldPlacement   
   stlFile = OutDir+str(doc.Label)+".stl"
   Mesh.export(visible_objs,stlFile)
   subprocess.Popen([SLICER + "KISSlicer", stlFile])[/code]