Converting Dimensions to SVG Format

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
tahimik_naman
Posts: 3
Joined: Thu Apr 27, 2017 5:37 am

Converting Dimensions to SVG Format

Post by tahimik_naman »

The python script below has been reduced to its essential lines to show that
a drawing's geometric entities, created using commands such as
makeLine and makeCircle from FreeCAD's Draft module, can be converted
to SVG format using getSVG. However, an error is generated by getSVG
if it encounters a dimension created using makeDimension.

Code: Select all

import sys
sys.path.append('/usr/lib/freecad/lib')

import FreeCAD
import FreeCADGui
import Draft

doc = FreeCAD.newDocument()

Draft.makeLine((0,0,0), (1,1,0))

Draft.makeDimension((2,0,0),(9,0,0),(2,1,0))

doc.recompute()

svgfile = open('script_output.svg', 'w') 
svgfile.write(r'<?svg version="1.1" encoding="iso-8859-1"?>')
svgfile.write(r'<!DOCTYPE svg>')
svgfile.write(r'<svg width="100" height="100">')

for obj in doc.Objects:
    svgfile.write(Draft.getSVG(obj))

svgfile.write(r'</svg>')
The error message is: -

Code: Select all

FreeCAD 0.15, Libs: 0.15R4671 (Git)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/adamlee/x_x/wip/freecad_script/script.py in <module>()
     20 
     21 for obj in doc.Objects:
---> 22     svgfile.write(Draft.getSVG(obj))
     23 
     24 svgfile.write(r'</svg>')

/usr/lib/freecad/Mod/Draft/Draft.pyc in getSVG(obj, scale, linewidth, fontsize, fillstyle, direction, linestyle, color)
   1966 
   1967     elif getType(obj) == "Dimension":
-> 1968         if obj.ViewObject.Proxy:
   1969             if hasattr(obj.ViewObject.Proxy,"p1"):
   1970                 prx = obj.ViewObject.Proxy

AttributeError: 'NoneType' object has no attribute 'Proxy'
My system configuration: -

OS: Ubuntu 16.04.2 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.15.4671 (Git)
Branch: releases/FreeCAD-0-15
Hash: 244b3aef360841646cbfe80a1b225c8b39c8380c
Python version: 2.7.11
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17

Any suggestions will be most gratefully received.
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Converting Dimensions to SVG Format

Post by Chris_G »

Hi,
Your FreeCAD version is too old.
Current stable is 0.16
Your script works for me, without errors, with 0.17-dev.
tahimik_naman
Posts: 3
Joined: Thu Apr 27, 2017 5:37 am

Re: Converting Dimensions to SVG Format

Post by tahimik_naman »

Chris_G,


Many thanks for looking into this issue. I took your suggestion and installed
daily build of FreeCAD, then modified the file path in the script. However, I
still have an error. The revised script and the error message are below.


The script is running in a stand-alone python session, not in the FreeCAD
interpreter. Any further ideas will be welcome.

Code: Select all

import sys
sys.path.append('/usr/lib/freecad-daily/lib')

import FreeCAD
import FreeCADGui
import Draft

doc = FreeCAD.newDocument()

Draft.makeLine((0,0,0), (1,1,0))

Draft.makeDimension((2,0,0),(9,0,0),(2,1,0))

doc.recompute()

svgfile = open('script_output.svg', 'w') 
svgfile.write(r'<?svg version="1.1" encoding="iso-8859-1"?>')
svgfile.write(r'<!DOCTYPE svg>')
svgfile.write(r'<svg width="100" height="100">')

for obj in doc.Objects:
    svgfile.write(Draft.getSVG(obj))

svgfile.write(r'</svg>')

Code: Select all

FreeCAD 0.17, Libs: 0.17R10950 (Git)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/adamlee/x_x/wip/freecad_script/script.py in <module>()
     25 
     26 for obj in doc.Objects:
---> 27     svgfile.write(Draft.getSVG(obj))
     28 
     29 svgfile.write(r'</svg>')

/usr/lib/freecad-daily/Mod/Draft/Draft.pyc in getSVG(obj, scale, linewidth, fontsize, fillstyle, direction, linestyle, color, linespacing, techdraw, rotation)
   2190 
   2191     elif getType(obj) == "Dimension":
-> 2192         if obj.ViewObject.Proxy:
   2193             if hasattr(obj.ViewObject.Proxy,"p1"):
   2194                 prx = obj.ViewObject.Proxy

AttributeError: 'NoneType' object has no attribute 'Proxy'
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Converting Dimensions to SVG Format

Post by Chris_G »

Hi,
Unfortunatly, I am not familiar with running FreeCAD python scripts without the Gui.
So, some more experienced people may give you better help.
I would say that Draft.getSVG tries to access the ViewObject of the input Object, but I think that ViewObjects are only available when the Gui is running. So Draft.getSVG only works with Gui.
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Converting Dimensions to SVG Format

Post by yorik »

okay.. the reason that the getSVG function fails is that it's not correctly made to work without the GUI. in git commit ead006131 I now fixed this so you won't get an error anymore, but some objects -like dimensions- currently require to be with the GUI open for them to be exported to SVG, so a workaround will still need to be implemented. At the moment they will simply be skipped.
tahimik_naman
Posts: 3
Joined: Thu Apr 27, 2017 5:37 am

Re: Converting Dimensions to SVG Format

Post by tahimik_naman »

yorik, Chris_G,

Thanks to both of you for for explaining this particular issue, and for all of the hours you give to the FreeCAD project.
Post Reply