(solved) Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
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!
Post Reply
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

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
Last edited by thomas-neemann on Mon Sep 20, 2021 5:27 pm, edited 1 time in total.
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?

Post by Syres »

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:

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")
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)
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

Syres wrote: Mon Sep 20, 2021 11:52 am
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.
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?

Post by Syres »

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")
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

Syres wrote: Mon Sep 20, 2021 12:22 pm
1000 thanks that is a great help, wonderful
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

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.
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Open / import all files in a folder and sub-folders at once (fcstd stp stl iges dae obj ifc)?

Post by heda »

the python "battery included" glob makes the whole thing an easy one-liner to recurse a file-pattern.
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

heda wrote: Mon Sep 20, 2021 1:23 pm
thank you, I like python very much right away
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

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


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)
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
User avatar
thomas-neemann
Veteran
Posts: 11903
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)?

Post by thomas-neemann »

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

phpBB [video]
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
Post Reply