My FreeCAD addon

Ive been working on a FreeCAD addon since I use kisslicer to slice up my stl files for my 3d objects I design, so eventually, I got tired of constantly having to search for the stl files, and designed this code. The code will automatically convert only the visible parts that you want, group them together, and export it as an stl file next to the original design file, with the same filename except with stl extension. Then it’ll open the STL file automatically for you ready to slice and to create gcode from your settings.

I designed it on linux, but it should easily be converted to be used on windows or any other system. Just create a macro in freecad, and paste this into the macro, then save it. Then you can go to tools/customize and set up the macro to a button on the toolbar, so you can just click the button and it converts everything for you.

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 = []
KISSlicer = "/kisslicer location/"     #put the location of your kisslicer directory here
os.chdir(KISSlicer)

# 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([KISSlicer + "KISSlicer", stlFile])

You’ll have to change the values of the location of KISSlicer, or modify it for whatever slicer software you use, and it should work fine.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: