Automated Process to Convert STL Files to Other Types Via FreeCAD

Introduction: Automated Process to Convert STL Files to Other Types Via FreeCAD

Finally I found a way to do conversions between different 3D files without wasting time!

Step 1: My Goal

I use Tinkercad to realize my 3D projects, so I make intensive use of STL files, but I was asked many times to render my STL in STEP format.

That's really a tedious work, due to large number of items to process.

To convert STL to other file type I use FreeCAD, whose flexibility let me make several types of conversion between formats, but it's a time consuming operation, of course.

I decided to face the problem one time for all and write down some script to automatize the conversions.

Step 2: The VBS Script

' Created by Francesco Francescangeli
' 2021-03-17

Set Arg = Wscript.Arguments

If wscript.arguments.count < 3 Then
  wscript.echo "Missing parameters!"
End If

FILEtype = Arg(0)
INdir = Arg(1)
INdir = Replace(INdir, "\", "/")
OUTdir = Arg(2)
OUTdir = Replace(OUTdir, "\", "/")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objPopup = CreateObject("WScript.Shell")

Dim Arg, INdir, OUTdir, nameFile, nameFileMod, numFiles, numTot, numDone, msg, MsgLine
Const ForReading = 1
Const ForWriting = 2
const DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0, WaitUntilFinished = true

Function ControlName(FileName)
  If IsNumeric(Mid(FileName,1,1)) Then
    ControlName = "_" & Mid(FileName,2)
    ControlName = FileName
  End If
End Function

Function sendIN(str)
  MsgLine = Trim(str)
  x = objPopup.Popup(MsgLine, 30, "Work in progress" , 64)
End Function

Function sendEND(str)
  MsgLine = Trim(str)
  x = objPopup.Popup(MsgLine, 30, "Job completed!" , 64)
End Function

objStartFolder = INdir
Set objFolder = objFSO.GetFolder(objStartFolder)
Set allFiles = objFolder.Files

numFiles = 0
For each item In allFiles
  numFiles = numFiles + 1

numTot = 0
numDone = 0
c = "C:\Users\ff\AppData\Roaming\FreeCAD\Macro\"
d = "C:\Users\ff\AppData\Roaming\FreeCAD\Macro\"
For Each objFile in allFiles
  numTot = numTot + 1
  nameFile = ControlName(objFSO.GetBaseName(objFile.Name))
  If UCase(objFSO.GetExtensionName(objFile.Name)) = "STL" Then
    sendIN("Please wait..." & vbCrLf & vbCrLf & "Converting:" & vbCrLf & objFSO.GetBaseName(objFile.Name))
    Set objFileOrg = objFSO.OpenTextFile(c, ForReading)
    strText = objFileOrg.ReadAll
    strNewText = Replace(strText, "!X!", INdir & "/" & objFile.Name)
    strNewText2 = Replace(strNewText, "!Y!", OUTdir & "/" & objFSO.GetBaseName(objFile.Name) & "." & FILEtype)
    strNewText3 = Replace(strNewText2, "!Z!", nameFile)
    Set objFileDest = objFSO.CreateTextFile(d, ForWriting)
    objFileDest.WriteLine strNewText3
    objShell.Run """C:\Program Files\FreeCAD 0.18\bin\freecadcmd""" & d, DontShowWindow, WaitUntilFinished
    numDone = numDone + 1
    If numDone = numFiles Then
      sendEND(numDone & " files processed.")
    End If
  End If
If objFSO.FileExists(d) Then
End If

Step 3: VBS Arguments

**FILEtype = Arg(0)**

The first argument to pass to script is the file type to convert to (any FreeCAD compatible type)

**INdir = Arg(1)**

The second argument to pass to script is the STL source path in the form


If any "/" is present inside it'll be converted to "\" automatically

**OUTdir = Arg(2)**

The second argument to pass to script is the destination path in the form


It's ininfluent that inside the source dir reside other types of files, they're filtered out by the script.

Step 4: The Stl2step PY Script

import FreeCAD
import Mesh
import Part
import Import
import sys"!X!")
del __shape__

del __objs__

Step 5: Stl2step PY Script Comments

You'll note that unobvious !X!, !Y! and !Z!...

They are the places where stl2step.vbs will place respectively:

!X! here will be placed the full STL source path

!Y! here will be placed the full STEP destination path

!Z! here will be placed the base name of the STL loaded in FreeCAD

N.B. Due to an FreeCAD idiosyncrasy to file name beginning with a digit, I was forced to temporarily convert that names, in order to let FreeCAD do its job.

The function for that is present in stl2step.vbs:

Function ControlName(FileName)
  If IsNumeric(Mid(FileName,1,1)) Then   
    ControlName = "_" & Mid(FileName,2)
    ControlName = FileName
  End If 
End Function

Ex: 2020foo.stl becomes _020foo.stl inside FreeCAD, but will be processed as output 2020foo.stl

Step 6: How It Works

Let's say we have:

- FreeCAD installed

- C:\Users\username\AppData\Roaming\FreeCAD\Macro **with inside my**

- C:\convert with inside my stl2step.vbs and my PopUpMsg.vbs

- C:\MyDesign\stlfiles with stl to convert inside

- C:\MyDesign\stepfiles as destination folder


1) Open Command Prompt (cmd.exe)

2) execute:

**cd \convert**

then type (in example step is used):

**stl2custom.vbs step C:\MyDesign\stlfiles C:\MyDesign\stepfiles**

3) a number of info windows will popup, informing you about work prosecution

4) on completion a new popup will appear showing how many files have been processed.

That's all!

Be the First to Share


    • Tinkercad Student Design Contest

      Tinkercad Student Design Contest
    • Space Contest

      Space Contest
    • Micro:bit Contest

      Micro:bit Contest