Macro 3D Printer Workflow
Description |
---|
Macro that creates an stl file with perfect rounding, i.e. without visible facets, from selected parts. It also allows to launch programs of your choice. For example to automate the FreeCAD -> Slicer -> printing workflow. After the creation of the stl file you can, for example, open your slicer with the stl file, switch on your printer, heat the printer bed and, if necessary, trigger additional commands in your home automation. |
Author |
2cv001 |
Download |
None |
Links |
Macros recipes How to install macros How to customize toolbars |
Macro Version |
00.02 |
Date last modified |
2023-01-21 |
FreeCAD Version(s) |
All |
Default shortcut |
None |
See also |
None |
Description
This macro creates an stl file with perfect rounding, i.e. without visible facets, from selected parts. It also allows to launch programs of your choice. For example to automate the FreeCAD -> Slicer -> printing workflow.
After the creation of the stl file you can, for example, open your slicer with the stl file, switch on your printer, heat the printer bed and, if necessary, trigger additional commands in your home automation.
Principle of smoothing
With visible facets
Without visible facets
The macro modifies the deviation property of the solids before creating the stl file and restores the original values afterwards. Next it can launch the stl file which will open for example under cura if the stl extension has been associated with that program on your operating system.
Launching other programs or commands
The macro can launch additional programs or commands that you might type in a terminal:
- Turn on the printer and the light (requires a computer controlled socket).
- Connect Octoprint to the printer.
- Start the tray preheating.
- Save your FreeCAD and stl file somewhere.
Setup
Check that on your operating system, the stl extension is associated with your slicer. In other words, when you double-click an stl file, your slicer should open and load your stl file.
If you run the macro as it is, it will just create the stl without visible facets, but no others programs will be launched. For that you have to adapt the lines of code that are after commands=[
in the section Parameters that can be changed. Have a look at the comments in the Code.
Usage
- Select one or more solid objects to print.
- Start the macro.
- The dialog opens:
- Check the desired options in the left column.
- Enter the Accuracy (param deviation):
- 0.5 is the default value in FreeCAD.
- 0.05 allows a stronger smoothing.
- 0.01 is perfect from a smoothing point of view.
- Press the OK button.
The lower the deviation, the better the quality, but the larger the size of the stl file. The value must be between 0 and 1
Links
Credits
Thanks to openBrain for the help on the code. Very good teacher!
Thanks to Mario 52, David69 and Roy_043 for the help on this wiki.
Script
Code
ver 00.02 21/01/2023 by 2cv001
#!/usr/bin/env python # -*- coding: utf-8 -*- # ================================================================== # ================================================================== # == == # == Workflow to print with "stl" file with perfect rounding == # == and launch others programs == # == == # ================================================================== # ================================================================== # ABOUT # ================================================================== # version v00.02 """ Macro that creates an "stl" file with perfect rounding = without visible facets. It also allows to launch programs of your choice For example to automate the FreeCAD -> Slicer -> printing workflow Principle of smoothing : it modifies the deviation property of the solids before generation of the stl and then replace the old values At the end, it proposes to launch the stl file which will open for example under cura if the stl extension has been associated with cura in your operating system. Launching other programs or commands : You can ask it to chain any program or command that you might type in a terminal. To do this, you must change what is in the "parameters that can be changed" section. Examples of applications: - Turn on the printer and the light (requires for example a controlled socket) - Connect octoprint to the printer - Start the tray preheating - Save your FreeCAD and stl file .... Tested on windows and Linux """ import FreeCAD as App import Mesh from PySide2 import QtCore, QtWidgets import os, sys, subprocess import time import FreeCADGui as Gui #================================ # Parameters that can be changed #================================ # Smoothing management #-------------------- # deviation : default value that will be displayed in the dialog box. # 0.5 is the default value in FreeCad, 0.05 allows a stronger smoothing. # 0.01 is perfect from a smoothing point of view. # The lower the value, the better the quality, # but the larger the size of the stl file # the value must be between 0 and 1 deviation=0.01 doitLancerFichier=True # if True, it will be proposed to launch the stl file. # otherwise put False # Automatic program launch, # Automation of the production line #------------------------------------------ delaiMax=10 # if the user enters a delay greater than this value, # it will be reduced to delaiMax #To launch programs of your choice #Typically for example a home automation program, #turn on your printer or/and a light etc. #Use the following syntax by adding a line below, in commands=[....: # [ ['command to execute', 'param 1', param 2, ] ,'.extention','question to ask', waiting time], # # 'command to execute' : the command you want to execute. # 'param 1, param 2,...' : if your program needs parameters. # These parameters will be taken into account only if extention=''. # # 'extention': if not empty then the first parameter will be replaced by the name of # your FreeCAD file but with this extension. # 'question to ask': the question to display in the dialog box # time_wait : optional : nb of seconds to wait after execution # allows for example to wait for the printer to start before asking it to heat the plate # Note the double \ in place of \ (because the special characters # must be preceded by an \ in python). ditto for the apostrophe for example # the lines in question are to be inserted further down after commands=[... # # Examples of possible launches: """ [ ['calc.exe' ] ,'' ,'Launch the calculator ?'], [ ['C:\\WINDOWS\\system32\\mspaint.exe','dessin.jpg' ] ,'' ,'Launch Paint ?' ], [ ['C:\\Program Files\\Ultimaker Cura 4.8.0\\Cura.exe'] ,'.stl','Launch Cura ?' ], [ ['curl','http://pidomotique/connecte.php' ] , '' ,'Connect the printer ?'], [ ['C:\\Program Files\\Mozilla Firefox\\firefox.exe','https://octopi.local'],'','Launch Octoprint ?'], On linux: [ [ 'firefox','https://octopi.local' ] , '' ,'Launch Octoprint ?' ], Example of printer control via octoprint (if you have it...): temperature setting of the plate - Replace the XXX after X-Api-Key: by your API key (found in the octoprint parameters) - Replace http://octopi.local by the url with which you access octoprint - Change the value 050 in "M140 S050". S050 indicates that you need to heat to 50°. if you want 60°, replace S050 by S060 - 6 lines to copy after having adapted it: [ [ 'curl','-H', 'Content-Type: application/json','-H', 'X-Api-Key: XXXXXXXXXXXXXXXXXXXXX', '-X', 'POST', '-d {"command":"M140 S050"}','http://octopi.local/api/printer/command' ] , '','Warm up the printer plate ?' ], """ commandes=[ # insert your lines here. # [ ['calc.exe' ] ,'' ,'Lancer la calculatrice ?',2], # [ ['C:\\Program Files\\Ultimaker Cura 5.2.1\\Ultimaker-Cura.exe'] ,'.stl','Lancer Cura ?' ], # [ ['curl','http://pidomotique/connecte.php' ] , '' ,'Allumer et connecter l\'imprimante ?',4], # 2 jeedom command that I use. Put a # at the begining of the 2 lines. [ ['curl', 'https://pijeedom/core/api/jeeApi.php?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxx&type=cmd&id=4042','--insecure' ] , '' ,'Turn on and connect printer ?',10], [ ['curl','https://pijeedom/core/api/jeeApi.php?apikey=xxxxxxxxxxxxxxxxxxxxxxxx&type=cmd&id=4048','--insecure' ] , '' ,'Heat the bed ?'], [ ['C:\\Program Files\\Mozilla Firefox\\firefox.exe','https://octopi'],'','Launch Octoprint ?'], # an example for octopi (heat the bed). Replace XXXX with your octopi API key. See explanations above # and remove the # at the begin of the next 6 lines # [ # [ 'curl','-H', 'Content-Type: application/json','-H', 'X-Api-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxx', '-X', 'POST', # '-d {"command":"M140 S050"}','http://octopi.local/api/printer/command' # ] # , '','Warm up the printer plate ?' # ], ] #=============== #Initialisations #=============== dictionnaireOrigineDeviation={} mw = Gui.getMainWindow() indiceCommandeEtParam=0 indiceExtFileNameParamCommandeALancer=1 indiceTextAutreCommandeALancer=2 indiceDelai=3 # UI Class definitions class BoiteDialogueSTLGuiClass(QtWidgets.QDialog): def __init__(self, parent=None): super(BoiteDialogueSTLGuiClass, self).__init__(parent) self.initUI() def initUI(self): self.tabCheckboxTextCommandes=[] # table of checkbox commands to run self.setGeometry( 250, 250, 400, 150) self.setWindowTitle("Export and launch software") lay = QtWidgets.QGridLayout(self) # checkboxs self.checkboxGenererSTL = QtWidgets.QCheckBox("Generate STL ?", self) self.checkboxGenererSTL.setCheckState(QtCore.Qt.Checked) lay.addWidget(self.checkboxGenererSTL, 0, 0) if doitLancerFichier : self.checkboxLancerSTL = QtWidgets.QCheckBox("Launch the slicer ?", self) self.checkboxLancerSTL.setCheckState(QtCore.Qt.Checked) lay.addWidget(self.checkboxLancerSTL, 2, 0) for posLigne, cmd in enumerate(commandes, start=3): textCommande=cmd[indiceTextAutreCommandeALancer] self.checkboxTextCommande = QtWidgets.QCheckBox(textCommande, self) # storing this checkbox in a table : self.tabCheckboxTextCommandes.append(self.checkboxTextCommande) self.checkboxTextCommande.setCheckState(QtCore.Qt.Checked) lay.addWidget(self.checkboxTextCommande, posLigne, 0) # numeric input field self.label2 = QtWidgets.QLabel('Accuracy (param deviation)\nbetween 0.01 and 1\n' + '0.01 for high quality', self) lay.addWidget(self.label2, 0, 1) self.precision = QtWidgets.QDoubleSpinBox(self) self.precision.setValue(deviation) self.precision.setRange(0.01,1) self.precision.setSingleStep(0.01) self.precision.setToolTip('The larger the number, the larger the file size\n' + 'and the better the accuracy.\n' + 'See the deviation parameter in FreeCAD') lay.addWidget(self.precision, 0, 2) butBox = QtWidgets.QDialogButtonBox(self) butBox.setOrientation(QtCore.Qt.Horizontal) butBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel