[Solved] TechDraw : the programmatically way, part II

Discussions about the development of the TechDraw workbench
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
cristian.a73
Posts: 41
Joined: Wed Jul 18, 2018 4:15 pm

[Solved] TechDraw : the programmatically way, part II

Post by cristian.a73 »

Hi FreeCAD Forumer
In order to create 2D draft of a model (for mechanical components) I create a page by follows code :

Code: Select all

        page = doc.addObject('TechDraw::DrawPage', str(pageTitle))
        template = doc.addObject('TechDraw::DrawSVGTemplate', str(pageTitle))
        template.Template = SVGTemplate
        page.Template = template
is there a way to create a SVG file, from a page (<DrawPage object>) ?

importSVG.export() don't support <DrawPage object>
TechDraw.viewPartAsSvg() don't support <DrawPage object>
Draft.svg.getSVG(page) return empty string
TechDraw.exportPageAsSvg() is perfect but from command line is not available

What is needed is something like TechDraw.writeSVGPage(<DrawPage object>, outFileName)

Thank you
Cristian
Last edited by cristian.a73 on Sat Oct 12, 2019 11:20 pm, edited 1 time in total.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: TechDraw : the programmatically way, part II

Post by vocx »

cristian.a73 wrote: Fri Oct 11, 2019 9:51 pm ...
TechDraw.exportPageAsSvg() is perfect but from command line is not available
...
I don't understand your question. This is exactly a function that you can use from the Python console. What do you mean it's not available.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: TechDraw : the programmatically way, part II

Post by wandererfan »

vocx wrote: Fri Oct 11, 2019 10:03 pm I don't understand your question. This is exactly a function that you can use from the Python console. What do you mean it's not available.
It's actually TechDrawGui.exportPageAsSvg(). It depends on FreeCADGui (among other things) and FreeCADGui is hard (impossible?) to run from the command line. @ian.rees spent quite a bit of time on this, but IIRC, he never got it to work right.
cristian.a73 wrote: Fri Oct 11, 2019 9:51 pm
Somewhere there's a macro that implements something like:

Code: Select all

mySvgString = headerText
for view in page:
    viewSvg = TechDraw.viewPartAsSvg(view)
    mySvgString += viewSvg
mySvgString += trailerText
file =  open("outfile.svg","w")
file.write(mySvgString)
I don't remember who did it or if the template was included.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: TechDraw : the programmatically way, part II

Post by vocx »

wandererfan wrote: Sat Oct 12, 2019 12:49 am ...
It's actually TechDrawGui.exportPageAsSvg(). It depends on FreeCADGui (among other things) and FreeCADGui is hard (impossible?) to run from the command line. @ian.rees spent quite a bit of time on this, but IIRC, he never got it to work right.
...
Oh, you mean headless, without a GUI at all?

I remember a thread a few weeks ago, in the general help section or Python scripting, about running some scripts that used the GUI, but without showing the FreeCAD interface. The solution was to launch FreeCAD on GUI mode, then hide the main interface. That seemed to work okay. The FreeCADGui module was available and all, it's just that the main window was hidden.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: TechDraw : the programmatically way, part II

Post by wandererfan »

vocx wrote: Sat Oct 12, 2019 12:58 am Oh, you mean headless, without a GUI at all?

I remember a thread a few weeks ago, in the general help section or Python scripting, about running some scripts that used the GUI, but without showing the FreeCAD interface. The solution was to launch FreeCAD on GUI mode, then hide the main interface. That seemed to work okay. The FreeCADGui module was available and all, it's just that the main window was hidden.
I haven't looked at this for a while issue #3077. Maybe you have the solution.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: TechDraw : the programmatically way, part II

Post by vocx »

wandererfan wrote: Sat Oct 12, 2019 1:01 am I haven't looked at this for a while issue #3077. Maybe you have the solution.
[Solved] Running FreeCAD in headless mode
naxq0 wrote: Fri Sep 20, 2019 7:45 am Haven't played around much with the FreeCADGui classes, but I think for the most part you need to start up the GUI. For instance, the color info is not loaded when you're on headless mode.

That doesn't mean you can't automate it though, you can start up and hide the GUI by doing something like this:

Code: Select all

import FreeCAD, FreeCADGui
FreeCADGui.showMainWindow()
mw = FreeCADGui.getMainWindow()
mw.hide()
...
They also point to this resource, Embedding FreeCAD. This is also mentioned in the thread, Minimal script to export SVG from TechDraw, which is mentioned in issue #3077.

Of course, before importing FreeCAD and FreeCADGui, the paths to the appropriate directories need to be set.

Code: Select all

import sys
sys.path.append("/usr/lib/freecad/lib")
sys.path.append("/usr/share/freecad/Mod")

import FreeCAD, FreeCADGui
...
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
cristian.a73
Posts: 41
Joined: Wed Jul 18, 2018 4:15 pm

Re: TechDraw : the programmatically way, part II

Post by cristian.a73 »

Hi vocx, wondererfan
Thank you for reply
Unfortunally my server doesn't have x-display (graphic cad not installed), it's running in virtualized server, so FreeCADGui don't run
Before to swith to TechDraw, using Drawing module in headless mode (FreeCADCmd) the object page <Drawing::FeaturePage>, after called doc.recompute(), was exposing the field page.PageResult as the SVG content.
So I though a similar function exist in TechDraw

By the wondererfan tip, the best way is to use TechDraw.viewPartAsSvg(obj) an mix the result as nested SVGs, adding manualy the template too

Code: Select all

	allViewsSVG = '<svg version="1.1" width="1189mm" height="841mm">'
        for v in page.Views:
		allViewsSVG += '<svg x="'+str(v.X)+'" y="'+str(v.Y)+'" width="'+str(W)+'" height="'+str(H)+'">'
		try:
                	allViewsSVG += TechDraw.viewPartAsSvg(v)
               	except Exception as e:
                	print_exc()

	allViewsSVG += "</svg>"
You may need to set viewBox property of SVG in order to avoid unexpected clipping effect :
https://www.w3.org/TR/SVG/coords.html#I ... nateSystem
Post Reply