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()