Skip to content

Instantly share code, notes, and snippets.

@NaPs
Created May 31, 2020 10:55
Show Gist options
  • Save NaPs/3629abb4154f5dedee262a7db1642725 to your computer and use it in GitHub Desktop.
Save NaPs/3629abb4154f5dedee262a7db1642725 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Located in ~/.FreeCAD/Macro/export.FCMacro
import os
import FreeCAD
import Mesh
import MeshPart
import PartDesign
from PySide import QtGui
base_dir = os.path.dirname(FreeCAD.ActiveDocument.FileName)
stls = {}
selection = FreeCADGui.Selection.getSelection()
if selection:
meshes = []
for item in selection:
shape = item.Shape.copy(False)
shape.Placement = item.getGlobalPlacement()
mesh = FreeCAD.ActiveDocument.addObject('Mesh::Feature', 'Mesh')
mesh.Mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.01, AngularDeflection=0.0872665, Relative=False)
meshes.append(mesh)
stls[FreeCAD.ActiveDocument.Name] = meshes
else:
bodies = [x for x in FreeCAD.ActiveDocument.RootObjects if x.TypeId == 'PartDesign::Body']
for body in bodies:
shape = body.Shape.copy(False)
shape.Placement = body.getGlobalPlacement()
mesh = FreeCAD.ActiveDocument.addObject('Mesh::Feature', 'Mesh')
mesh.Mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.01, AngularDeflection=0.0872665, Relative=False)
stls['%s_%s' % (FreeCAD.ActiveDocument.Name, body.Name)] = [mesh]
for name, meshes in stls.items():
Mesh.export(meshes, '%s.stl' % os.path.join(base_dir, name))
# Cleanup
for meshes in stls.values():
for mesh in meshes:
FreeCAD.ActiveDocument.removeObject(mesh.Name)
QtGui.QMessageBox.information(None, 'Export', '%s stl files exported' % len(stls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment