~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Drawing/DrawingExample.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# exampel how to use the scripting API of the drawing module
 
2
 
3
# first of all you need the Part and the Drawing module:
 
4
import FreeCAD, Part, Drawing
 
5
 
 
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)))
 
8
 
 
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
 
17
 
 
18
# different projection vector
 
19
[visiblyG0,visiblyG1,hiddenG0,hiddenG1] = Drawing.project(Shape,Base.Vector(1,1,1))
 
20
 
 
21
# project to SVG
 
22
resultSVG = Drawing.projectToSVG(Shape,App.Vector(1,1,1))
 
23
print resultSVG
 
24
 
 
25
# And now the parametric way
 
26
 
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'
 
30
 
 
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)
 
38
 
 
39
# create a second view on the same object but the view is 
 
40
# rotatet 90 degrees.
 
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) 
 
49
 
 
50
# create a third view on the same object but with an isometric
 
51
# view direction. Also the hidden lines are activated.
 
52
 
 
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) 
 
60
 
 
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()
 
67
 
 
68
# Accessing the bits and peaces:
 
69
# get the SVG fragment of a single view
 
70
ViewSVG = App.activeDocument().View.ViewResult
 
71
print ViewSVG
 
72
 
 
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!
 
78
del file
 
79
 
 
80
# insert a view with your own content:
 
81
App.activeDocument().addObject('Drawing::FeatureView','ViewSelf')
 
82
App.activeDocument().ViewSelf.ViewResult = """<g id="ViewSelf"
 
83
  stroke="rgb(0, 0, 0)"
 
84
  stroke-width="0.35"
 
85
  stroke-linecap="butt"
 
86
  stroke-linejoin="miter"
 
87
  transform="translate(30,30)"
 
88
  fill="#00cc00"
 
89
 >
 
90
 
 
91
<ellipse cx="40" cy="40" rx="30" ry="15"/>
 
92
</g>
 
93
"""
 
94
App.activeDocument().Page.addObject(App.activeDocument().ViewSelf)
 
95
 
 
96
App.activeDocument().recompute()
 
97
 
 
98
del Shape,ViewSVG, resultSVG
 
99