FreeCAD crashes when batch producing three view drawing

Need help, or want to share a macro? Post here!
Post Reply
siyuan
Posts: 2
Joined: Thu Jan 16, 2020 3:13 am

FreeCAD crashes when batch producing three view drawing

Post by siyuan » Thu Jan 16, 2020 3:44 am

Hello all,

I am using FreeCAD 0.18.3 with python scripting to batch producing three-view line drawings (front, right, and top view) for 10,000 STEP files. The script runs well. However, when the FreeCAD python console runs for long time, like several hours, the FreeCAD will crash and quit suddenly. I have no idea since python does not require release memory manually, and I am not sure where I might make a mistake. For your convenience, I pose the core part of my code as follows:

Code: Select all

import FreeCAD, Part, Draft, Drawing, DrawingGui, TechDraw, TechDrawGui

def export_three_view(x,filepath):
    a=FreeCAD.getDocument("Unnamed").getObject(x)
    TechDrawGui.exportPageAsSvg(a,filepath)
    del a

for file in dirs:
    ImportGui.open(file)
    App.setActiveDocument("Unnamed")
    App.ActiveDocument=App.getDocument("Unnamed")
    Gui.ActiveDocument=Gui.getDocument("Unnamed")
    Gui.SendMsgToActiveView("ViewFit")

    Gui.Selection.clearSelection()

    #compound all the parts to a whole object...

    App.activeDocument().addObject('TechDraw::DrawPage','Page')
    App.activeDocument().addObject('TechDraw::DrawSVGTemplate','Template')
    App.activeDocument().Template.Template = "my/path/A3_Landscape_blank.svg"
    App.activeDocument().Page.Template = App.activeDocument().Template
    App.activeDocument().addObject('TechDraw::DrawProjGroup','ProjGroup')
    App.activeDocument().Page.addView(App.activeDocument().ProjGroup)

    #Front
    #TODO: set the front perspective...

    export_three_view("Page", MY_PATH)

    break
Notice the indent cannot be shown here.
Thanks so much if you have any suggestions!
User avatar
microelly2
Posts: 4560
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: FreeCAD crashes when batch producing three view drawing

Post by microelly2 » Thu Jan 16, 2020 7:02 am

I have added the code tag to your post, so the source is displayed with intents.
User avatar
wandererfan
Posts: 3484
Joined: Tue Nov 06, 2012 5:42 pm

Re: FreeCAD crashes when batch producing three view drawing

Post by wandererfan » Thu Jan 16, 2020 12:49 pm

siyuan wrote:
Thu Jan 16, 2020 3:44 am
I have no idea since python does not require release memory manually, ...
The python code is supported by lots of C++ in the background. It might have nothing to do with your Python code.

Will have to set up a test for this.
User avatar
wandererfan
Posts: 3484
Joined: Tue Nov 06, 2012 5:42 pm

Re: FreeCAD crashes when batch producing three view drawing

Post by wandererfan » Thu Jan 16, 2020 8:50 pm

siyuan wrote:
Thu Jan 16, 2020 3:44 am
I am using FreeCAD 0.18.3 with python scripting to batch producing three-view line drawings (front, right, and top view) for 10,000 STEP files. The script runs well. However, when the FreeCAD python console runs for long time, like several hours, the FreeCAD will crash and quit suddenly.
I'm still investigating, but if I understand your code correctly, at some point you will have 1000's of documents open. Not sure anybody has tried that. The first change I would suggest is to close the document created by "ImportGui.open()" at the end of each iteration.
siyuan
Posts: 2
Joined: Thu Jan 16, 2020 3:13 am

Re: FreeCAD crashes when batch producing three view drawing

Post by siyuan » Fri Jan 17, 2020 7:02 pm

wandererfan wrote:
Thu Jan 16, 2020 8:50 pm
siyuan wrote:
Thu Jan 16, 2020 3:44 am
I am using FreeCAD 0.18.3 with python scripting to batch producing three-view line drawings (front, right, and top view) for 10,000 STEP files. The script runs well. However, when the FreeCAD python console runs for long time, like several hours, the FreeCAD will crash and quit suddenly.
I'm still investigating, but if I understand your code correctly, at some point you will have 1000's of documents open. Not sure anybody has tried that. The first change I would suggest is to close the document created by "ImportGui.open()" at the end of each iteration.
Thanks so much for your investigation! Do you have any suggestions that which code I need to use to close it? I found several codes online, but not sure. I will report the result as soon as I re-run.
User avatar
wandererfan
Posts: 3484
Joined: Tue Nov 06, 2012 5:42 pm

Re: FreeCAD crashes when batch producing three view drawing

Post by wandererfan » Fri Jan 17, 2020 7:17 pm

siyuan wrote:
Fri Jan 17, 2020 7:02 pm
Thanks so much for your investigation! Do you have any suggestions that which code I need to use to close it? I found several codes online, but not sure. I will report the result as soon as I re-run.
This is the code I've been testing with. It is a mix of your code and the test script for ProjectionGroups (I didn't have 10000 step files handy!). It has been up to 1000 iterations so far - it slows down sometimes (~300 iterations?) then speeds back up.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# test script for STEP export macro crash
# see: https://www.forum.freecadweb.org/viewtopic.php?f=22&t=42475#p360848

import FreeCAD, Part, TechDraw, TechDrawGui, Import, ImportGui

def export_three_view(x,filepath):
    a=App.activeDocument().getObject(x)
    TechDrawGui.exportPageAsSvg(a,filepath)

templateSpec = '/usr/share/freecad/Mod/TechDraw/Templates/A4_LandscapeTD.svg'
exportSpec = '/home/huxster/Documents/CAD/TDFiles/pageExport'
maxPages = 1000
infile = '/home/huxster/Documents/CAD/TDFiles/pageImport/box.step'

#doc = FreeCAD.newDocument()

for x in range(0,maxPages):
    #get a step file
    ImportGui.open(infile)
    doc = App.ActiveDocument
    docName = doc.Name
#    Import.insert(infile, doc.Name)
    cube = doc.getObject("Solid")    #not sure if this is always the objectName?

    print("making a page: {}".format(x))
    page = doc.addObject('TechDraw::DrawPage','Page')
    template = doc.addObject('TechDraw::DrawSVGTemplate','Template')
    template.Template = templateSpec
    page.Template = template

    print("making a projection group")
    doc.openTransaction("Create Proj Group")
    pGroup = doc.addObject('TechDraw::DrawProjGroup','ProjGroup')
    page.addView(pGroup)
    pGroup.Source = [cube]
    front_view = pGroup.addProjection("Front")
    pGroup.Anchor.recompute()
    doc.commitTransaction()
    print("projection group created")

    #add some more views
    left_view = pGroup.addProjection("Left")
    top_view = pGroup.addProjection("Top")

    pageName = page.Name
    export_three_view(pageName, exportSpec + "/expFile" + str(x) + ".svg")

    print("removing page")
    doc.removeObject(page.Name)

    print("removing step object")
    doc.removeObject(cube.Name)

    App.closeDocument(docName)
    print("end of iteration: {}".format(x))
vocx
Posts: 2841
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD crashes when batch producing three view drawing

Post by vocx » Fri Jan 17, 2020 11:19 pm

siyuan wrote:
Fri Jan 17, 2020 7:02 pm
...Do you have any suggestions that which code I need to use to close it?...
Wandererfan just says that you have to close the active document. When you are iterating, you are opening a document in each iteration. So, just close this file at the end of the cycle; maybe that helps avoid consuming all memory in the system.

Code: Select all

ImportGui.open(file)
...
App.closeDocument(App.ActiveDocument.Name)
If this still triggers a crash, maybe there is a memory leak somewhere.
To support the documentation effort, and code development, your donation is appreciated: paypal.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests