FreeCAD

Форк
0
/
DrawingExample.py 
119 строк · 4.0 Кб
1
# example 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(
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))
12
)
13

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:
20
print(
21
    "Bnd Box shape: X=",
22
    Shape.BoundBox.XLength,
23
    " Y=",
24
    Shape.BoundBox.YLength,
25
    " Z=",
26
    Shape.BoundBox.ZLength,
27
)
28
print(
29
    "Bnd Box project: X=",
30
    visiblyG0.BoundBox.XLength,
31
    " Y=",
32
    visiblyG0.BoundBox.YLength,
33
    " Z=",
34
    visiblyG0.BoundBox.ZLength,
35
)
36

37
# different projection vector
38
[visiblyG0, visiblyG1, hiddenG0, hiddenG1] = Drawing.project(Shape, Base.Vector(1, 1, 1))
39

40
# project to SVG
41
resultSVG = Drawing.projectToSVG(Shape, App.Vector(1, 1, 1))
42
print(resultSVG)
43

44
# And now the parametric way
45
#
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"
50
)
51

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

60
# create a second view on the same object but the view is
61
# rotatet 90 degrees.
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)
70

71
# create a third view on the same object but with an isometric
72
# view direction. Also the hidden lines are activated.
73

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

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

89
# Accessing the bits and peaces:
90
# get the SVG fragment of a single view
91
ViewSVG = App.activeDocument().View.ViewResult
92
print(ViewSVG)
93

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!
99
del file
100

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)"
105
  stroke-width="0.35"
106
  stroke-linecap="butt"
107
  stroke-linejoin="miter"
108
  transform="translate(30,30)"
109
  fill="#00cc00"
110
 >
111

112
<ellipse cx="40" cy="40" rx="30" ry="15"/>
113
</g>
114
"""
115
App.activeDocument().Page.addObject(App.activeDocument().ViewSelf)
116

117
App.activeDocument().recompute()
118

119
del Shape, ViewSVG, resultSVG
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.