Test create svg grid line on TechDraw

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chakkree
Posts: 327
Joined: Tue Jun 30, 2015 12:58 am
Location: Bangkok Thailand

Test create svg grid line on TechDraw

Post by chakkree »

SVGTechDraw_GridLine.png
SVGTechDraw_GridLine.png (165.42 KiB) Viewed 2189 times

Code: Select all

"""
   Test on TechDraw
"""

from xml.etree import ElementTree as et

page = None
for o in FreeCAD.ActiveDocument.Objects:
	if o.isDerivedFrom('TechDraw::DrawPage'):
		page = o
		break

def VGrid(Label='A' , length = 100):
    w = 10
    h = 10
    r = 3
    gridSVG = et.Element('svg' ,width='%gmm'%w , height='%gmm'%h , viewBox='0 0 %g %g'%(w,h) )
    gridSVG.append(et.Element('circle' , cx='0' , cy='%g'%(-30-r), r='%g'%(r) , style='fill:white;stroke:black;stroke-width:0.25;'))
    gridSVG.append(et.Element('line' , x1='0' , y1='-20', x2='0' , y2='%g'%length , style='stroke:black;stroke-width:0.25;stroke-dasharray:5 2 1 2;'))
    text1 = et.Element('text' , x='0' , y='%g'%(-30-r+0.5*r) )
    text1.text = Label
    text1.set('style' , 'text-anchor:middle;font-size:4.5;' )
    gridSVG.append(text1)
    
    gridSVG.append(et.Element('line' , x1='-2' , y1='0', x2='2' , y2='0' , style='stroke:red;stroke-width:0.18;'))
    return gridSVG

def HGrid(Label='1' , length = 100):
    w = 10
    h = 10
    r = 3
    gridSVG = et.Element('svg' ,width='%gmm'%w , height='%gmm'%h , viewBox='0 0 %g %g'%(w,h) )
    gridSVG.append(et.Element('circle' , cy='0' , cx='%g'%(-30), r='%g'%(r) , style='fill:white;stroke:black;stroke-width:0.25;'))
    gridSVG.append(et.Element('line' , y1='0' , x1='-20', y2='0' , x2='%g'%length , style='stroke:black;stroke-width:0.25;stroke-dasharray:5 2 1 2;'))
    text1 = et.Element('text'  , x='%g'%(-30), y='%g'%(+0.5*r) )
    text1.text = Label
    text1.set('style' , 'text-anchor:middle;font-size:4.5;' )
    gridSVG.append(text1)
    
    #gridSVG.append(et.Element('line' , x1='-2' , y1='0', x2='2' , y2='0' , style='stroke:red;stroke-width:0.18;'))
    return gridSVG

x_org=50; y_org = 150
scale = 1/100.
GridX = [
  {'name':'A' , 'pos':0   },
  {'name':'B' , 'pos':4000   },
  {'name':'C' , 'pos':4000   },
]

GridY = [
  {'name':'1' , 'pos':0   },
  {'name':'2' , 'pos':3500   },
  {'name':'3' , 'pos':3500   },
]

posX = x_org
for iGrid in GridX:
    name = 'Grid' + iGrid['name']
    mySymbol = FreeCAD.ActiveDocument.addObject('TechDraw::DrawViewSymbol',name)
    page.addView(mySymbol)
    posX += iGrid['pos']*scale
    mySymbol.X = posX
    mySymbol.Y = y_org
    mySymbol.setExpression('Scale', u'1 / 3.5')
    
    mySymbol.Symbol=et.tostring(VGrid(iGrid['name']))

posY = y_org
for iGrid in GridY:
    name = 'Grid' + iGrid['name']
    mySymbol = FreeCAD.ActiveDocument.addObject('TechDraw::DrawViewSymbol',name)
    page.addView(mySymbol)
    posY -= iGrid['pos']*scale
    mySymbol.X = x_org 
    mySymbol.Y =  posY
    mySymbol.setExpression('Scale', u'1 / 3.5')
    
    mySymbol.Symbol=et.tostring(HGrid(iGrid['name']))   

FreeCAD.ActiveDocument.recompute()

Drawing Template
A4_Landscape_Plain.svg
(2.52 KiB) Downloaded 137 times
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Test create svg grid line on TechDraw

Post by microelly2 »

Thank you for sharing this example.
User avatar
chakkree
Posts: 327
Joined: Tue Jun 30, 2015 12:58 am
Location: Bangkok Thailand

Re: Test create svg grid line on TechDraw

Post by chakkree »

But TechDraw has some bug when export page to svg file, size of paper has change from width="297mm" height="210mm" to width="301.752mm" height="213.36mm"
checked...
301.752/297 = 1.016 and 213.36/210 = 1.016
this ratio is same value to 25.4/25 = 1.016
may be convertional factor from mm to in have not same value.

Header of output svg file.

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="301.752mm" height="213.36mm"
 viewBox="0 0 297 210"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<title>FreeCAD SVG Export</title>
<desc>Drawing page: Page exported from FreeCAD document: Unnamed</desc>
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Test create svg grid line on TechDraw

Post by Kunda1 »

chakkree wrote:But TechDraw has some bug when export page to svg file, size of paper has change from width="297mm" height="210mm" to width="301.752mm" height="213.36mm"
checked...
301.752/297 = 1.016 and 213.36/210 = 1.016
this ratio is same value to 25.4/25 = 1.016
may be convertional factor from mm to in have not same value.

Header of output svg file.

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="301.752mm" height="213.36mm"
 viewBox="0 0 297 210"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<title>FreeCAD SVG Export</title>
<desc>Drawing page: Page exported from FreeCAD document: Unnamed</desc>
Is this bug still reproducible? Andregistered on th tracker?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Test create svg grid line on TechDraw

Post by sgrogan »

Kunda1 wrote: Is this bug still reproducible? Andregistered on th tracker?
See here: https://www.forum.freecadweb.org/viewto ... 35&t=21597
"fight the good fight"
User avatar
chakkree
Posts: 327
Joined: Tue Jun 30, 2015 12:58 am
Location: Bangkok Thailand

Re: Test create svg grid line on TechDraw

Post by chakkree »

I exported SVG for check, Now TechDraw can export with not change a size of SVG's working area.

Code: Select all

<svg width="297mm" height="210mm"
 viewBox="0 0 2970 2100"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
--------------------
OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.11054 (Git)
Build type: Release
Branch: master
Hash: 36c60d3f8d924799e805ce56cb6e8fe6e49cf12b
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Post Reply