Troubleshooting Large FreeCAD File Sizes

This article will show you how to find out exactly which layer is causing your FreeCAD file to balloon in size, by getting a granular list of all of the layers in your document tree, sorted by size.

Troubleshooting Large File Sizes in FreeCAD

Context

Recently, we had a volunteer who made some very helpful contributions to the Eco-Libre Life-Line project — an open-source design for raw water intake, filtration, and storage system for sustainable communities.

The original file was 812 KB. After the changes, the file was 17 MB (compressed). And — worse — manipulating the objects in FreeCAD was now very slow and choppy (spoiler: the uncompressed size was 114 MB).

Before merging the updated objects into our GitHub, I figured that some optimizations could be made to reduce the file size — to make opening the file in FreeCAD much smoother. But which object should I focus on optimizing?

Sorting layers by size

You can see the size of each layer by pasting a small python snippet into FreeCAD’s python console.

As far as I know, there is no way to get a granular view of the size of each layer in the FreeCAD tree from within the UI. Fortunately, FreeCAD is a very robust software that exposes the “Python console” to the user, where you can paste custom code to interact with the objects. The snippet below will:

  1. Iterate through every layer in the FreeCAD Document’s Tree,
  2. Get the size (MemSize) of each layer,
  3. Sort the list of layers by their size, and
  4. Print the list of layers (sorted by size)

To use this, you first need to open the Python Console in FreeCAD. Do this by clicking to View -> Panels -> Python Console. Then paste the following snippet into the Python Console. And press enter.

def printMem():
    objs = list(FreeCAD.ActiveDocument.Objects)
    objs.append(FreeCAD.ActiveDocument)              # add doc to list
    objs.sort(reverse=True, key=lambda x: x.MemSize) # max mem is first

    hdr = "MemSize (bytes) | Object Label\n"
    hLine = "-"*len(hdr) + "\n"
    linesList = ["\n", hLine, hdr, hLine]
    for obj in objs:
        linesList.append("{:>15,d} | {}\n".format(obj.MemSize, obj.Label))
    linesList.append(hLine)
    s = "".join(linesList)
    print(s)

printMem();

Note that it may take several seconds to finish the calculation.

Example

In our case, the file we want to troubleshoot is intake.FCStd.

We get the file by cloning the repository in git and open the file in FreeCAD.

user@disp1268:~$ git clone https://github.com/Matterhorn777/life-line.git
Cloning into 'life-line'...
remote: Enumerating objects: 432, done.
remote: Counting objects: 100% (149/149), done.
remote: Compressing objects: 100% (78/78), done.
remote: Total 432 (delta 83), reused 125 (delta 61), pack-reused 283 (from 1)
Receiving objects: 100% (432/432), 181.97 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (173/173), done.
Updating files: 100% (41/41), done.
user@disp1268:~$ 

user@disp1268:~$ freecad life-line/intake.FCStd 
FreeCAD 0.20.2, Libs: 0.20.2R
© Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2022
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

Next, we open the Python Console in the FreeCAD UI

Finally, paste the code snippet (from above) into the Python console

Screenshot of FreeCAD v0.20.2, showing the python console executing a call to "printMem();" and a table with a list of objects and their MemSize (bytes). The top item is "intake" with "120,263,898 bytes" followed by "expandedMetalMesh001" with "108,638,853 byes"
Clearly “expandedMetalMesh001” is the largest object in the tree (besides the document’s root itself)

The table output printed in the Python console shows that (besides the root of the document tree “intake”), the largest layer in this FreeCAD file is “expandedMetalMesh001” at 120,263,898 bytes. That’s about 114 MB uncompressed (120,263,898/1,024/1,024), and it’s responsible for about 90% of the total size of this FreeCAD file (108,638,853/120,263,898). Wow!

Now that we’ve very clearly identified which object in our FreeCAD file is responsible for bloating the file size, we can begin to try to optimize it.

Attribution

Thank you to cadsimple for the code snippet above. For more information, please see the following thread on the FreeCAD forums:

Who is Eco-Libre?

Eco-Libre is a volunteer-run project that designs libre technology for sustainable communities.

Eco-Libre’s mission is to research, develop, document, teach, build, and distribute open-source technology that sustainably enfranchises communities’ human rights.

– Eco-Libre’s mission statement

We aim to provide clear documentation to build low-cost machines, tools, and infrastructure for people all over the world who wish to live in sustainable communities with others.

If you’d like to help Eco-Libre reach our mission to enfranchise sustainable communities’ human rights with libre hardware, please contact us to get involved 🙂

Please enter your email address below for updates from the Eco-Libre Team.

Leave a Reply

Your email address will not be published. Required fields are marked *