Headless export of Drawing to svg/pdf

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
sankey
Posts: 10
Joined: Sun Mar 12, 2017 1:56 am

Headless export of Drawing to svg/pdf

Post by sankey »

I have a fcstd file (attached) with a single sketch and a bunch of drawings made with TechDraw. Each drawing includes one instance of the sketch, along with some Dimensions. I basically want to run a script on a headless server which writes a bunch of SVG files (or PDF) of each drawing, when given the .fcstd file. When I tried to search around on the forums:

search results: search.php?keywords=svg+|+pdf+|+drawing ... mit=Search

I couldn't find exactly what I was looking for. The first result seemed promising, providing a simple 3 line macro to export an SVG of a drawing, but when I run it I get an error:

Code: Select all

>>> __objs__=[]
>>> __objs__.append(FreeCAD.getDocument("cargo_bike").getObject("Page001"))
>>> import DrawingGui
>>> DrawingGui.export(__objs__,"/home/user/workspace/cad/output/test.svg")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: Export of this object type is not supported by Drawing module
The other search results were not exactly related.

OS: Ubuntu 16.04.1 LTS
Word size of OS: 32-bit
Word size of FreeCAD: 32-bit
Version: 0.17.10476 (Git)
Build type: None
Branch: master
Hash: b6150097e9cf4d7b0f5ad71f2f8750f2e7aac0d9
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Attachments
cargo_bike.fcstd
(57.21 KiB) Downloaded 64 times
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Headless export of Drawing to svg/pdf

Post by sgrogan »

sankey wrote:TypeError: Export of this object type is not supported by Drawing module
DrawingGui export function doesn't work with a TechDraw object. It looks like TechDraw export function may not be exposed to python?
pdf works though

Code: Select all

import FreeCAD
import FreeCADGui

__objs__=[]
__objs__.append(FreeCAD.getDocument("test").getObject("Page"))
FreeCADGui.export(__objs__,u"C:/Users/chris/Downloads/test.pdf")

del __objs__
Maybe this in combination with 2. from here https://forum.freecadweb.org/viewtopic. ... 52#p131124 is a work around?
"fight the good fight"
User avatar
yorik
Founder
Posts: 13660
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Headless export of Drawing to svg/pdf

Post by yorik »

If you work with Drawing only, the SVG output of a page is always available in its PageResult property, so it's just a matter of using python file open -> write.

But indeed I don't think this is implemented in TechDraw yet
sankey
Posts: 10
Joined: Sun Mar 12, 2017 1:56 am

Re: Headless export of Drawing to svg/pdf

Post by sankey »

sgrogan wrote:DrawingGui export function doesn't work with a TechDraw object. It looks like TechDraw export function may not be exposed to python?
pdf works though

Code: Select all

import FreeCAD
import FreeCADGui

__objs__=[]
__objs__.append(FreeCAD.getDocument("test").getObject("Page"))
FreeCADGui.export(__objs__,u"C:/Users/chris/Downloads/test.pdf")

del __objs__
Cool, that doesn't raise an error and it almost works! The output document isn't landscape like the default A4 template, so parts of the drawing is cropped off, but this is the closest I've gotten so far.
sgrogan wrote: Maybe this in combination with 2. from here https://forum.freecadweb.org/viewtopic. ... 52#p131124 is a work around?
Ultimately I need PDFs, but since I was having problems with the above macro, I decided to give inscape a try:

Code: Select all

inkscape --export-area-drawing --export-text-to-path --without-gui --export-pdf=test_fixed.pdf test.pdf
Using Inkscape to convert a PDF to a PDF using --export-area-drawing successfully re-cropped the file appropriately! See the test.pdf and test_fixed.pdf files attached.

I'll put in my TODO list to report a bug regarding TechDraw pdf export, but until then Inkscape gets added to my automation workflow.

EDIT: Totally uploaded the wrong files the first time. Fixed now.
Attachments
test_fixed.pdf
(266.17 KiB) Downloaded 75 times
test.pdf
(430.13 KiB) Downloaded 80 times
sankey
Posts: 10
Joined: Sun Mar 12, 2017 1:56 am

Re: Headless export of Drawing to svg/pdf

Post by sankey »

Unfortunately, TechDraw pages cannot be exported unless they are open and in the foreground. I reported issue #0002972.

So I tried to work around this issue by closing all TechDraw pages manually and running

Code: Select all

# __objs__ contains all the TechDraw pages
for obj in __objs__:
    obj.ViewObject.show()
    FreeCADGui.export([obj], u"/path/to/output/{}.pdf".format(obj.Label))
to force the page to become open in the foreground before exporting. It works! But then I realized that whenever I open the document, all TechDraw pages in the document automatically open, so show() does not work (it does not change focus if the ViewObject is already open). This is a bug, and it's already reported: issue #0002967.

So, my workaround for that is to simply close all the pages first, in Python:

Code: Select all

# __objs__ contains all the TechDraw pages
for obj in __objs__:
    obj.ViewObject.hide()
This works for all but one page: the one that's currently in the foreground. If you try to hide() the current foreground TechDraw page, FreeCAD segfaults. I reported issue #0002971.

In summary, I tried to workaround issue #0002972 but encountered issue #0002967, and tried to workaround that but encountered issue #0002971, so I'm stuck now! The fact that I cannot export all TechDraw pages programmatically undermines my whole drawing build automation workflow. Can anybody think of any more workarounds to export all TechDraw pages?
User avatar
wandererfan
Veteran
Posts: 6315
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Headless export of Drawing to svg/pdf

Post by wandererfan »

sankey wrote:Unfortunately, TechDraw pages cannot be exported unless they are open and in the foreground. I reported issue #0002972.
git commit 5a0f53d
Post Reply