1
# example 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
8
Part.makeBox(100, 100, 100)
9
.cut(Part.makeCylinder(80, 100))
10
.cut(Part.makeBox(90, 40, 100))
11
.cut(Part.makeBox(20, 85, 100))
14
# direct projection. The G0 means hard edge, the G1 is tangend continues.
15
Shape = App.ActiveDocument.Shape.Shape
16
[visiblyG0, visiblyG1, hiddenG0, hiddenG1] = Drawing.project(Shape)
17
print("visible edges:", len(visiblyG0.Edges))
18
print("hidden edges:", len(hiddenG0.Edges))
19
# all was projected on the Z-plane:
22
Shape.BoundBox.XLength,
24
Shape.BoundBox.YLength,
26
Shape.BoundBox.ZLength,
29
"Bnd Box project: X=",
30
visiblyG0.BoundBox.XLength,
32
visiblyG0.BoundBox.YLength,
34
visiblyG0.BoundBox.ZLength,
37
# different projection vector
38
[visiblyG0, visiblyG1, hiddenG0, hiddenG1] = Drawing.project(Shape, Base.Vector(1, 1, 1))
41
resultSVG = Drawing.projectToSVG(Shape, App.Vector(1, 1, 1))
44
# And now the parametric way
46
# insert a Page object and assign a template
47
App.activeDocument().addObject("Drawing::FeaturePage", "Page")
48
App.activeDocument().Page.Template = (
49
App.ConfigGet("AppHomePath") + "Mod/Drawing/Templates/A3_Landscape.svg"
52
# create a view on the "Shape" object, define the position and scale and assign it to a Page
53
App.activeDocument().addObject("Drawing::FeatureViewPart", "View")
54
App.activeDocument().View.Source = App.activeDocument().Shape
55
App.activeDocument().View.Direction = (0.0, 0.0, 1.0)
56
App.activeDocument().View.X = 10.0
57
App.activeDocument().View.Y = 10.0
58
App.activeDocument().Page.addObject(App.activeDocument().View)
60
# create a second view on the same object but the view is
62
App.activeDocument().addObject("Drawing::FeatureViewPart", "ViewRot")
63
App.activeDocument().ViewRot.Source = App.activeDocument().Shape
64
App.activeDocument().ViewRot.Direction = (0.0, 0.0, 1.0)
65
App.activeDocument().ViewRot.X = 290.0
66
App.activeDocument().ViewRot.Y = 30.0
67
App.activeDocument().ViewRot.Scale = 1.0
68
App.activeDocument().ViewRot.Rotation = 90.0
69
App.activeDocument().Page.addObject(App.activeDocument().ViewRot)
71
# create a third view on the same object but with an isometric
72
# view direction. Also the hidden lines are activated.
74
App.activeDocument().addObject("Drawing::FeatureViewPart", "ViewIso")
75
App.activeDocument().ViewIso.Source = App.activeDocument().Shape
76
App.activeDocument().ViewIso.Direction = (1.0, 1.0, 1.0)
77
App.activeDocument().ViewIso.X = 335.0
78
App.activeDocument().ViewIso.Y = 140.0
79
App.activeDocument().ViewIso.ShowHiddenLines = True
80
App.activeDocument().Page.addObject(App.activeDocument().ViewIso)
82
# change something and update.
83
# The update process change the view and the page
84
App.activeDocument().View.X = 30.0
85
App.activeDocument().View.Y = 30.0
86
App.activeDocument().View.Scale = 1.5
87
App.activeDocument().recompute()
89
# Accessing the bits and peaces:
90
# get the SVG fragment of a single view
91
ViewSVG = App.activeDocument().View.ViewResult
94
# get the hole result page (its a file in the document temp dir, only read allowed)
95
print("Resulting SVG document: ", App.activeDocument().Page.PageResult)
96
file = open(App.activeDocument().Page.PageResult, "r")
97
print("Result page is ", len(file.readlines()), " lines long")
98
# important, give free the file!
101
# insert a view with your own content:
102
App.activeDocument().addObject("Drawing::FeatureView", "ViewSelf")
103
App.activeDocument().ViewSelf.ViewResult = """<g id="ViewSelf"
104
stroke="rgb(0, 0, 0)"
106
stroke-linecap="butt"
107
stroke-linejoin="miter"
108
transform="translate(30,30)"
112
<ellipse cx="40" cy="40" rx="30" ry="15"/>
115
App.activeDocument().Page.addObject(App.activeDocument().ViewSelf)
117
App.activeDocument().recompute()
119
del Shape, ViewSVG, resultSVG