1
# exampel how to use the scripting API of the drawing module
3
# first of all you need the Part and the Drawing module:
4
import FreeCAD, Part, Drawing
6
# create a small sample part
7
Part.show(Part.makeBox(100,100,100).cut(Part.makeCylinder(80,100)).cut(Part.makeBox(90,40,100)).cut(Part.makeBox(20,85,100)))
9
# direct projection. The G0 means hard edge, the G1 is tangend continues.
10
Shape = App.ActiveDocument.Shape.Shape
11
[visiblyG0,visiblyG1,hiddenG0,hiddenG1] = Drawing.project(Shape)
12
print "visible edges:", len(visiblyG0.Edges)
13
print "hidden edges:", len(hiddenG0.Edges)
14
# all was projected on the Z-plane:
15
print "Bnd Box shape: X=",Shape.BoundBox.XLength," Y=",Shape.BoundBox.YLength," Z=",Shape.BoundBox.ZLength
16
print "Bnd Box project: X=",visiblyG0.BoundBox.XLength," Y=",visiblyG0.BoundBox.YLength," Z=",visiblyG0.BoundBox.ZLength
18
# different projection vector
19
[visiblyG0,visiblyG1,hiddenG0,hiddenG1] = Drawing.project(Shape,Base.Vector(1,1,1))
22
resultSVG = Drawing.projectToSVG(Shape,App.Vector(1,1,1))
25
# And now the parametric way
27
# insert a Page object and assign a template
28
App.activeDocument().addObject('Drawing::FeaturePage','Page')
29
App.activeDocument().Page.Template = App.ConfigGet('AppHomePath')+'Mod/Drawing/Templates/A3_Landscape.svg'
31
# create a view on the "Shape" object, define the position and scale and assign it to a Page
32
App.activeDocument().addObject('Drawing::FeatureViewPart','View')
33
App.activeDocument().View.Source = App.activeDocument().Shape
34
App.activeDocument().View.Direction = (0.0,0.0,1.0)
35
App.activeDocument().View.X = 10.0
36
App.activeDocument().View.Y = 10.0
37
App.activeDocument().Page.addObject(App.activeDocument().View)
39
# create a second view on the same object but the view is
41
App.activeDocument().addObject('Drawing::FeatureViewPart','ViewRot')
42
App.activeDocument().ViewRot.Source = App.activeDocument().Shape
43
App.activeDocument().ViewRot.Direction = (0.0,0.0,1.0)
44
App.activeDocument().ViewRot.X = 290.0
45
App.activeDocument().ViewRot.Y = 30.0
46
App.activeDocument().ViewRot.Scale = 1.0
47
App.activeDocument().ViewRot.Rotation = 90.0
48
App.activeDocument().Page.addObject(App.activeDocument().ViewRot)
50
# create a third view on the same object but with an isometric
51
# view direction. Also the hidden lines are activated.
53
App.activeDocument().addObject('Drawing::FeatureViewPart','ViewIso')
54
App.activeDocument().ViewIso.Source = App.activeDocument().Shape
55
App.activeDocument().ViewIso.Direction = (1.0,1.0,1.0)
56
App.activeDocument().ViewIso.X = 335.0
57
App.activeDocument().ViewIso.Y = 140.0
58
App.activeDocument().ViewIso.ShowHiddenLines = True
59
App.activeDocument().Page.addObject(App.activeDocument().ViewIso)
61
# change something and update.
62
# The update process change the view and the page
63
App.activeDocument().View.X = 30.0
64
App.activeDocument().View.Y = 30.0
65
App.activeDocument().View.Scale = 1.5
66
App.activeDocument().recompute()
68
# Accessing the bits and peaces:
69
# get the SVG fragment of a single view
70
ViewSVG = App.activeDocument().View.ViewResult
73
# get the hole result page (its a file in the document temp dir, only read allowed)
74
print "Resulting SVG document: ",App.activeDocument().Page.PageResult
75
file = open(App.activeDocument().Page.PageResult,"r")
76
print "Result page is ",len(file.readlines())," lines long"
77
# important, give free the file!
80
# insert a view with your own content:
81
App.activeDocument().addObject('Drawing::FeatureView','ViewSelf')
82
App.activeDocument().ViewSelf.ViewResult = """<g id="ViewSelf"
86
stroke-linejoin="miter"
87
transform="translate(30,30)"
91
<ellipse cx="40" cy="40" rx="30" ry="15"/>
94
App.activeDocument().Page.addObject(App.activeDocument().ViewSelf)
96
App.activeDocument().recompute()
98
del Shape,ViewSVG, resultSVG