(solved) Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
(solved) Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
does anyone have an idea how a macro could look like that all files in a folder and sub-folders open / import at once (fcstd stp stl iges dae obj ifc)?
that would be very useful for assemblies (main assembly with subassemblies)
Greetings Thomas
that would be very useful for assemblies (main assembly with subassemblies)
Greetings Thomas
Last edited by thomas-neemann on Mon Sep 20, 2021 5:27 pm, edited 1 time in total.
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
I took the feedback from https://forum.freecadweb.org/viewtopic.php?t=3332 and updated it be Python3 aware just for Step files in one folder:
I'm sure you can take it from there to expand for the other formats and you just need to Google for Python recursive folder search.
OS: Linux Mint 19.3 (X-Cinnamon/cinnamon)
Word size of FreeCAD: 64-bit
Version: 0.20.25773 (Git)
Build type: Release
Branch: master
Hash: b8a4d1e07e76dd313a8f81a51b567567acdd15f0
Python version: 3.6.9
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
Code: Select all
import FreeCAD,Part,os
folder = "/home/john/Data/FreeCAD"
# first we check for filenames that contain .stp or .step
stepfiles = []
for f in os.listdir(folder):
if (".STP" in f.upper()) or (".STEP" in f.upper()):
stepfiles.append(folder+os.sep+f)
if not stepfiles:
print ("No step files found in the given folder")
os.exit()
# we create a doc, read all step files and make a freecad object from each of them
doc = FreeCAD.newDocument()
for f in stepfiles:
s = Part.read(f)
doc.addObject("Part::Feature","Part").Shape = s
# always recompute after changing the doc
doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
OS: Linux Mint 19.3 (X-Cinnamon/cinnamon)
Word size of FreeCAD: 64-bit
Version: 0.20.25773 (Git)
Build type: Release
Branch: master
Hash: b8a4d1e07e76dd313a8f81a51b567567acdd15f0
Python version: 3.6.9
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
Thank you for your prompt reply. that is very helpful. I've already tried recursion in a shell script and built it myself as a list of all file names that were processed. i have no idea about python yet. how does such a recursion look like?
Last edited by thomas-neemann on Mon Sep 20, 2021 12:46 pm, edited 1 time in total.
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
Here's a recursive import of just STEP files:
Code: Select all
import FreeCAD,Part,os
folder = "/home/john/Data/FreeCAD"
# Recursive search for STEP files
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(folder) for f in filenames if os.path.splitext(f.upper())[1] == '.STEP']
if not result:
print ("No step files found in the given folder")
os.exit()
# we create a doc, read all step files and make a freecad object from each of them
doc = FreeCAD.newDocument()
for f in result:
s = Part.read(f)
doc.addObject("Part::Feature","Part").Shape = s
# always recompute after changing the doc
doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
It is easier than I thought, only fcstd files need to be opened / merged, because positioned step iges dae etc. objects can be saved as fcstd without any additional effort.
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
the python "battery included" glob makes the whole thing an easy one-liner to recurse a file-pattern.
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
because merge does not create any nesting in the parts tree, it is sufficient to load only one directory. This directory, which is the current assembly directory, can contain positioned individual parts and also positioned assemblies as an FCSTD file. it can be former step iges or mesh files that are positioned as fcstd and saved. I will shoot a video to explain it and link it here.
here the macro
fc-folder-assembly
here the macro
fc-folder-assembly
Code: Select all
#fc-folder-assembly
import FreeCAD,Part,os
folder = "/home/pc50/Data/FreeCAD"
for f in os.listdir(folder):
if (".FCSTD" in f.upper()):
Gui.ActiveDocument.mergeProject(folder+os.sep+f)
- thomas-neemann
- Veteran
- Posts: 11958
- Joined: Wed Jan 22, 2020 6:03 pm
- Location: Osnabrück DE 🇩🇪
- Contact:
Re: (solved) Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?
here is the video, the more parts there are, the more meaningful it becomes. this means that you can work in assemblies of any size. very fast with all formats
https://www.youtube.com/watch?v=HLIMqS8b-Uo
https://www.youtube.com/watch?v=HLIMqS8b-Uo