-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement_creation_demo.py
More file actions
187 lines (153 loc) · 5.67 KB
/
Copy pathelement_creation_demo.py
File metadata and controls
187 lines (153 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
Example demonstrating the element creation functionality using the builders API.
This demo shows how to create various types of elements using the modern builders pattern.
"""
import ducpy as duc
from ducpy.builders.style_builders import (create_fill_and_stroke_style,
create_simple_styles,
create_solid_content)
def demo_basic_elements():
"""Demo basic elements using the builders API."""
print("=== Basic Elements Demo ===")
rect = (duc.ElementBuilder()
.at_position(0, 0)
.with_size(100, 50)
.with_label("Sample Rectangle")
.with_styles(create_fill_and_stroke_style(
fill_content=create_solid_content("#FF6B6B"),
stroke_content=create_solid_content("#2C3E50"),
stroke_width=2.0,
roundness=5.0
))
.build_rectangle()
.build())
ellipse = (duc.ElementBuilder()
.at_position(120, 0)
.with_size(60, 40)
.with_label("Sample Ellipse")
.with_styles(create_fill_and_stroke_style(
fill_content=create_solid_content("#4ECDC4"),
stroke_content=create_solid_content("#34495E"),
stroke_width=1.5
))
.build_ellipse()
.build())
poly = (duc.ElementBuilder()
.at_position(200, 0)
.with_size(50, 50)
.with_label("Hexagon")
.with_styles(create_fill_and_stroke_style(
fill_content=create_solid_content("#45B7D1"),
stroke_content=create_solid_content("#2C3E50"),
stroke_width=1.0,
roundness=0.0
))
.build_polygon()
.with_sides(6)
.build())
print(f"Rectangle ID: {rect.element.base.id}")
print(f"Ellipse ID: {ellipse.element.base.id}")
print(f"Polygon sides: {poly.element.sides}")
# Demonstrate mutation with random versioning
duc.mutate_element(rect, x=10, label="Moved Rectangle")
return [rect, ellipse, poly]
def demo_linear_elements():
"""Demo linear and arrow elements with styles."""
print("\n=== Linear Elements Demo ===")
line_points = [(0, 0), (50, 25), (100, 0)]
line = (duc.ElementBuilder()
.with_label("Sample Line")
.with_styles(create_simple_styles(
strokes=[duc.create_stroke(duc.create_solid_content("#E74C3C"), width=3.0)]
))
.build_linear_element()
.with_points(line_points)
.build())
print(f"Line has {len(line.element.linear_base.points)} points")
arrow_points = [(0, 50), (75, 100)]
arrow = (duc.ElementBuilder()
.with_label("Sample Arrow")
.with_styles(create_simple_styles(
strokes=[duc.create_stroke(duc.create_solid_content("#8E44AD"), width=2.5)]
))
.build_arrow_element()
.with_points(arrow_points)
.build())
print(f"Arrow element type: {type(arrow.element).__name__}")
return [line, arrow]
def demo_text_elements():
"""Demo text elements with styles and document formatting."""
print("\n=== Text Elements Demo ===")
text = (duc.ElementBuilder()
.at_position(0, 100)
.with_size(150, 25)
.with_label("Sample Text")
.with_styles(create_simple_styles(opacity=0.9))
.build_text_element()
.with_text("Hello, DucPy!")
.build())
print(f"Text content: '{text.element.text}'")
return [text]
def demo_stack_elements():
"""Demo new stack-based elements with styles."""
print("\n=== Stack Elements Demo ===")
frame = (duc.ElementBuilder()
.at_position(0, 150)
.with_size(200, 100)
.with_label("Technical Frame")
.with_styles(create_fill_and_stroke_style(
fill_content=create_solid_content("#F8F9FA"),
stroke_content=create_solid_content("#495057"),
stroke_width=2.0,
roundness=3.0
))
.build_frame_element()
.build())
print(f"Frame stack label: {frame.element.stack_element_base.stack_base.label}")
plot = (duc.ElementBuilder()
.at_position(220, 150)
.with_size(180, 120)
.with_label("Engineering Plot")
.with_styles(create_fill_and_stroke_style(
fill_content=create_solid_content("#E9ECEF"),
stroke_content=create_solid_content("#6C757D"),
stroke_width=1.5
))
.build_plot_element()
.with_margins(duc.Margins(top=5, right=5, bottom=5, left=5))
.build())
return [frame, plot]
def demo_custom_stack_base():
"""Demo custom stack base creation."""
print("\n=== Custom Stack Base Demo ===")
custom_frame = (duc.ElementBuilder()
.at_position(50, 280)
.with_size(150, 80)
.with_label("Custom Container")
.build_frame_element()
.with_stack_base(duc.StateBuilder().build_stack_base()
.with_is_collapsed(False)
.with_styles(duc.DucStackLikeStyles(opacity=0.8))
.build())
.build())
return [custom_frame]
def main():
"""Run all element creation demos."""
print("DucPy Element Creation Demo")
print("=" * 40)
elements = []
elements.extend(demo_basic_elements())
elements.extend(demo_linear_elements())
elements.extend(demo_text_elements())
elements.extend(demo_stack_elements())
elements.extend(demo_custom_stack_base())
duc_bytes = duc.serialize_duc(
name="element_creation_example",
elements=elements,
)
print(f"\nCreated {len(elements)} elements → serialized {len(duc_bytes)} bytes.")
print("✅ Element creation demo complete!")
return duc_bytes
if __name__ == "__main__":
main()