1
+ ---
2
+ jupytext:
3
+ text_representation:
4
+ extension: .mystnb
5
+ format_name: myst
6
+ format_version: 0.13
7
+ jupytext_version: 1.14.1
8
+ kernelspec:
9
+ display_name: Python 3 (ipykernel)
10
+ language: python
11
+ name: python3
12
+ ---
13
+ # Modeling: Visualization of the design tree on terminal
14
+
15
+ A user can visualize its model object tree easily by using the ``tree_print()`` method
16
+ available on the ``Design`` and ``Component`` objects. This method prints the tree
17
+ structure of the model in the terminal.
18
+
19
+ ## Perform required imports
20
+
21
+ For the following example, we need to import these modules:
22
+
23
+ ```{code-cell} ipython3
24
+ from pint import Quantity
25
+
26
+ from ansys.geometry.core import Modeler
27
+ from ansys.geometry.core.math.constants import UNITVECTOR3D_X, UNITVECTOR3D_Y
28
+ from ansys.geometry.core.math.point import Point2D, Point3D
29
+ from ansys.geometry.core.misc.units import UNITS
30
+ from ansys.geometry.core.sketch.sketch import Sketch
31
+ ```
32
+
33
+ ## Create a design
34
+
35
+ The following code creates a simple design for demonstration purposes. The design consists of
36
+ several cylinders extruded. The interesting part is visualizing the corresponding design tree.
37
+
38
+ ```{code-cell} ipython3
39
+ # Create a modeler object
40
+ modeler = Modeler()
41
+
42
+ # Create your design on the server side
43
+ design = modeler.create_design("TreePrintComponent")
44
+
45
+ # Create a Sketch object and draw a circle (all client side)
46
+ sketch = Sketch()
47
+ sketch.circle(Point2D([-30, -30]), 10 * UNITS.m)
48
+ distance = 30 * UNITS.m
49
+
50
+ # The following component hierarchy is made
51
+ #
52
+ # |---> comp_1 ---|---> nested_1_comp_1 ---> nested_1_nested_1_comp_1
53
+ # | |
54
+ # | |---> nested_2_comp_1
55
+ # |
56
+ # DESIGN ---|---> comp_2 -------> nested_1_comp_2
57
+ # |
58
+ # |
59
+ # |---> comp_3
60
+ #
61
+ #
62
+ # Now, only "comp_3", "nested_2_comp_1" and "nested_1_nested_1_comp_1"
63
+ # will have a body associated.
64
+ #
65
+
66
+ # Create the components
67
+ comp_1 = design.add_component("Component_1")
68
+ comp_2 = design.add_component("Component_2")
69
+ comp_3 = design.add_component("Component_3")
70
+ nested_1_comp_1 = comp_1.add_component("Nested_1_Component_1")
71
+ nested_1_nested_1_comp_1 = nested_1_comp_1.add_component("Nested_1_Nested_1_Component_1")
72
+ nested_2_comp_1 = comp_1.add_component("Nested_2_Component_1")
73
+ nested_1_comp_2 = comp_2.add_component("Nested_1_Component_2")
74
+
75
+ # Create the bodies
76
+ b1 = comp_3.extrude_sketch(name="comp_3_circle", sketch=sketch, distance=distance)
77
+ b2 = nested_2_comp_1.extrude_sketch(
78
+ name="nested_2_comp_1_circle", sketch=sketch, distance=distance
79
+ )
80
+ b2.translate(UNITVECTOR3D_X, 50)
81
+ b3 = nested_1_nested_1_comp_1.extrude_sketch(
82
+ name="nested_1_nested_1_comp_1_circle", sketch=sketch, distance=distance
83
+ )
84
+ b3.translate(UNITVECTOR3D_Y, 50)
85
+
86
+ # Create beams (in design)
87
+ circle_profile_1 = design.add_beam_circular_profile(
88
+ "CircleProfile1", Quantity(10, UNITS.mm), Point3D([0, 0, 0]), UNITVECTOR3D_X, UNITVECTOR3D_Y
89
+ )
90
+ beam_1 = nested_1_comp_2.create_beam(
91
+ Point3D([9, 99, 999], UNITS.mm), Point3D([8, 88, 888], UNITS.mm), circle_profile_1
92
+ )
93
+
94
+ design.plot()
95
+ ```
96
+
97
+ ## Visualize the design tree
98
+
99
+ Now, let's visualize the design tree using the ``tree_print()`` method. Let's start by
100
+ printing the tree structure of the design object with no extra arguments.
101
+
102
+ ```{code-cell} ipython3
103
+ design.tree_print()
104
+ ```
105
+
106
+ ### Controlling the depth of the tree
107
+
108
+ The ``tree_print()`` method accepts an optional argument ``depth`` to control the depth of the
109
+ tree to be printed. The default value is ``None``, which means the entire tree will be printed.
110
+
111
+ ```{code-cell} ipython3
112
+ design.tree_print(depth=1)
113
+ ```
114
+
115
+ In this case, only the first level of the tree is printed - that is, the three main
116
+ components.
117
+
118
+ ### Excluding bodies, components, or beams
119
+
120
+ By default, the ``tree_print()`` method prints all the bodies, components, and beams in the
121
+ design tree. However, you can exclude any of these by setting the corresponding argument to
122
+ ``False``.
123
+
124
+ ```{code-cell} ipython3
125
+ design.tree_print(consider_bodies=False, consider_beams=False)
126
+ ```
127
+
128
+ In this case, the bodies and beams are not be printed in the tree structure.
129
+
130
+ ```{code-cell} ipython3
131
+ design.tree_print(consider_comps=False)
132
+ ```
133
+
134
+ In this case, the components are not be printed in the tree structure - leaving only the
135
+ design object represented.
136
+
137
+ ### Sorting the tree
138
+
139
+ By default, the tree structure is sorted by the way the components, bodies, and beams were
140
+ created. However, you can sort the tree structure by setting the ``sort_keys`` argument to ``True``.
141
+ In that case, the tree is sorted alphabetically.
142
+
143
+ Let's add a new component to the design and print the tree structure by default.
144
+
145
+ ```{code-cell} ipython3
146
+ comp_4 = design.add_component("A_Component")
147
+ design.tree_print(depth=1)
148
+ ```
149
+
150
+ Now, let's print the tree structure with the components sorted alphabetically.
151
+
152
+ ```{code-cell} ipython3
153
+ design.tree_print(depth=1, sort_keys=True)
154
+ ```
155
+
156
+ ### Indenting the tree
157
+
158
+ By default, the tree structure is printed with an indentation level of 4. However, you
159
+ can indent the tree structure by setting the ``indent`` argument to the desired value.
160
+
161
+ ```{code-cell} ipython3
162
+ design.tree_print(depth=1, indent=8)
163
+ ```
164
+
165
+ In this case, the tree structure is printed with an indentation level of 8.
166
+
167
+ ### Printing the tree from a specific component
168
+
169
+ You can print the tree structure from a specific component by calling the ``tree_print()``
170
+ method on the component object.
171
+
172
+ ```{code-cell} ipython3
173
+ nested_1_comp_1.tree_print()
174
+ ```
0 commit comments