-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_script.py
More file actions
313 lines (283 loc) · 9.21 KB
/
example_script.py
File metadata and controls
313 lines (283 loc) · 9.21 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/C:/Users/Avinash/cst_env/Scripts/python.exe
"""
CST External Scripting Example
Demonstrates how to create a patch antenna using the cst.interface API
This example shows:
1. Creating a DesignEnvironment
2. Building geometry with History List commands
3. Configuring simulation settings
4. Saving and running the simulation
"""
#py "C:\Users\Avinash\Documents\myProject_Stuff\cst_linker\example_script.py" --output "C:\Users\Avinash\Documents\myProject_Stuff\cst_linker\external"
import os
#import sys
# Import CST API
import cst.interface
# try:
# import cst.interface
# except ImportError:
# print("ERROR: CST Python API not found!")
# print("Install with: pip install --no-index --find-links '<CST_PATH>/Library/Python/repo/simple' cst-studio-suite-link")
# sys.exit(1)
def create_patch_antenna_external(
#substrate_material: str,
patch_length_mm: float,
patch_width_mm: float,
substrate_thickness_mm: float,
port_type: str = "Waveguide",
excitation_mode: str = "H01",
output_path: str = ".",
) -> None:
"""
Create a patch antenna using external scripting.
This function demonstrates the CST interface methodology:
- DesignEnvironment for connection
- MWS project for project management
- History List for geometry creation
"""
print("=" * 60)
print("CST External Scripting Example")
print("=" * 60)
print()
# Step 1: Initialize DesignEnvironment
design_env = cst.interface.DesignEnvironment()
# Step 2: Create and save MWS project
# Create new MWS (Microwave Studio) project
project = design_env.new_mws()
# Save project (must be absolute path)
project_name = "example_patch_antenna.cstprj"
if os.path.isabs(output_path):
save_path = os.path.join(output_path, project_name)
else:
save_path = os.path.abspath(os.path.join(output_path, project_name))
project.save(save_path, allow_overwrite=True)
print(f" Project saved to: {save_path}")
print()
# Set the units
unit_history = f"""
With Units
.SetUnit "Length", "mm"
.SetUnit "Frequency", "GHz"
.SetUnit "Voltage", "V"
.SetUnit "Resistance", "Ohm"
.SetUnit "Inductance", "nH"
.SetUnit "Temperature", "degC"
.SetUnit "Time", "ns"
.SetUnit "Current", "A"
.SetUnit "Conductance", "S"
.SetUnit "Capacitance", "pF"
End With
"""
project.model3d.add_to_history("Set units", unit_history)
# Step 3: Create substrate
substrate_thickness = substrate_thickness_mm
# Build history command for substrate
substrate_history = f"""
With Material
.Reset
.Name "FR-4 (lossy)"
.Folder ""
.FrqType "all"
.Type "Normal"
.SetMaterialUnit "GHz", "mm"
.Epsilon "4.3"
.Mu "1.0"
.Kappa "0.0"
.TanD "0.025"
.TanDFreq "10.0"
.TanDGiven "True"
.TanDModel "ConstTanD"
.KappaM "0.0"
.TanDM "0.0"
.TanDMFreq "0.0"
.TanDMGiven "False"
.TanDMModel "ConstKappa"
.DispModelEps "None"
.DispModelMu "None"
.DispersiveFittingSchemeEps "General 1st"
.DispersiveFittingSchemeMu "General 1st"
.UseGeneralDispersionEps "False"
.UseGeneralDispersionMu "False"
.Rho "0.0"
.ThermalType "Normal"
.ThermalConductivity "0.3"
.SetActiveMaterial "all"
.Colour "0.94", "0.82", "0.76"
.Wireframe "False"
.Transparency "0"
.Create
End With
"""
project.model3d.add_to_history("Create substrate material", substrate_history)
substrate_material = "FR-4 (lossy)"
substrate_width = 2 * patch_width_mm
substrate_length = 2 * patch_length_mm
substrate_history = f"""
With Brick
.Reset
.Name "substrate"
.Component "component1"
.Material "{substrate_material}"
.Xrange "- {substrate_width/2}", " {substrate_width/2}"
.Yrange "- {substrate_length/2}", " {substrate_length/2}"
.Zrange " {0.035}", " {0.035+substrate_thickness}"
.Create
End With
"""
# Add to history list
project.model3d.add_to_history("Create substrate", substrate_history)
# Step 4: Create ground plane
ground_thickness = 0.035
ground_history = f"""
With Brick
.Reset
.Name "ground_plane"
.Component "component1"
.Material "Copper (annealed)"
.Xrange "- {substrate_width/2}", " {substrate_width/2}"
.Yrange "- {substrate_length/2}", " {substrate_length/2}"
.Zrange " {0}", " {ground_thickness}"
.Create
End With
"""
project.model3d.add_to_history("Create ground plane", ground_history)
# Step 5: Create dielectric patch
patch_history = f"""
With Brick
.Reset
.Name "patch"
.Component "component1"
.Material "Copper (annealed)"
.Xrange "- {patch_width_mm/2}", " {patch_width_mm/2}"
.Yrange "- {patch_length_mm/2}", " {patch_length_mm/2}"
.Zrange " {0.035 + substrate_thickness}", " {0.035 + 0.035 + substrate_thickness}"
.Create
End With
"""
project.model3d.add_to_history("Create patch", patch_history)
# Step 6: Create Microstrip
MW = 2.86
microstrip_history = f"""
With Brick
.Reset
.Name "microstrip"
.Component "component1"
.Material "Copper (annealed)"
.Xrange "- {2.86/2}", " {2.86/2}"
.Yrange "- {patch_length_mm/2}", "- {substrate_length/2}"
.Zrange " {0.035 + substrate_thickness}", " {0.035 + 0.035 + substrate_thickness}"
.Create
End With
"""
project.model3d.add_to_history("Create microstrip", microstrip_history)
# Step 7: Create Insets
inset_history = f"""
With Material
.Reset
.Name "Vacuum"
.Folder ""
.FrqType "all"
.Type "Normal"
.SetMaterialUnit "Hz", "mm"
.Epsilon "1.0"
.Mu "1.0"
.Kappa "0"
.TanD "0.0"
.TanDFreq "0.0"
.TanDGiven "False"
.TanDModel "ConstKappa"
.KappaM "0"
.TanDM "0.0"
.TanDMFreq "0.0"
.TanDMGiven "False"
.TanDMModel "ConstKappa"
.DispModelEps "None"
.DispModelMu "None"
.DispersiveFittingSchemeEps "General 1st"
.DispersiveFittingSchemeMu "General 1st"
.UseGeneralDispersionEps "False"
.UseGeneralDispersionMu "False"
.Rho "0"
.ThermalConductivity "0"
.SetActiveMaterial "all"
.Colour "0.5", "0.8", "1"
.Wireframe "False"
.Transparency "0"
.Create
End With
"""
project.model3d.add_to_history("Create inset material", inset_history)
InL = 9
InW = 0.74
inset_history = f"""
With Brick
.Reset
.Name "inset1"
.Component "component1"
.Material "Vacuum"
.Xrange "- {MW/2}", "- {MW/2 + InW}"
.Yrange "- {patch_length_mm/2 - InL}", "- {patch_length_mm / 2}"
.Zrange " {0.035 + substrate_thickness}", " {0.035 + 0.035 + substrate_thickness}"
.Create
End With
"""
project.model3d.add_to_history("Create inset1", inset_history)
boolean_history = f""" Solid.Subtract "component1:patch", "component1:inset1" """
project.model3d.add_to_history("Create subtract inset1", boolean_history)
inset_history = f"""
With Brick
.Reset
.Name "inset2"
.Component "component1"
.Material "Vacuum"
.Xrange " {MW/2}", " {MW/2 + InW}"
.Yrange "- {patch_length_mm/2 - InL}", "- {patch_length_mm / 2}"
.Zrange " {0.035 + substrate_thickness}", " {0.035 + 0.035 + substrate_thickness}"
.Create
End With
"""
project.model3d.add_to_history("Create inset2", inset_history)
boolean_history = f""" Solid.Subtract "component1:patch", "component1:inset2" """
project.model3d.add_to_history("Create subtract inset2", boolean_history)
# Summary
print("=" * 60)
print("Antenna Creation Complete!")
print("=" * 60)
print()
print(f"Project file: {save_path}")
print()
print("Next steps:")
print(" 1. Open the .cstprj file in CST Studio")
print(" 2. Check geometry in 3D viewer")
print(" 3. Click 'Compute' to run simulation")
print(" 4. Or run: cst run '" + save_path + "'")
print()
def main():
"""Main entry point for examples."""
import argparse
parser = argparse.ArgumentParser(
description="CST External Scripting Examples"
)
parser.add_argument(
"--output", "-o",
default=".",
help="Output path for CST project files"
)
parser.add_argument(
"--parametric", "-p",
action="store_true",
help="Run parametric study"
)
args = parser.parse_args()
print("=" * 60)
print("Enter parametric values")
patch_length_mm = float(input("Enter patch length in mm:"))
substrate_thickness_mm = float(input("Enter substrate height in mm:"))
create_patch_antenna_external(
output_path=args.output,
patch_length_mm=patch_length_mm,
patch_width_mm=38.0,
substrate_thickness_mm=substrate_thickness_mm,
)
if __name__ == "__main__":
main()