CST Studio Suite Python自动化学习笔记
前言
这CST Studio Suite 2026版给加了个Python自动化的教程,算是能学了。然而CST的底层全部基于VBA,Python的文档里的方法都写不全,还得去看VBA的文档,鉴定为不如PyAEDT。
基础调用
按照教程安装完成后,可以直接在VS Code里面调用cst库。
使用cst.interface.get_current_project()方法,可以直接获取到当前打开项目对应的Project类,不知道为什么教程把这个写在CST内部的Python解释器的一节中。
创建立方体
只能通过向History List中添加VBA命令来创建物体。文档中给了以下代码。
# define a String holding the VBA code for creating a Brick
vba_code = """
With Brick
.Reset
.Name "solid1"
.Component "component1"
.Material "PEC"
.Xrange "0", "3"
.Yrange "0", "4"
.Zrange "0", "5"
.Create
End With
"""
# add the VBA code to the History List
prj.model3d.add_to_history("create brick", vba_code)
修改其中的值,就能添加不同位置的立方体/矩形面片。
add_to_history方法的第一个参数是显示在History List中的名称,可以用中文。
删除物体
参考文档中的代码。
def get_components_including(name: str, prj: cst.interface.Project) -> Iterator[str]:
"""
Create iterator to iterate over all 3D Components, whose name includes `name`.
"""
for item in prj.model3d.get_tree_items():
# split the path
split_item = item.split("\\")
# make sure we are only deleting shapes or components
if "Components" == split_item[0]:
# search for our components (item-names are in last item of split_item)
if name in split_item[-1]:
# get components
components = split_item[1:]
# join components to the component path and append
component_path = "/".join(components)
# check if found item is a Component
if prj.model3d.Component.DoesExist(component_path):
yield component_path
to_be_removed = "undefined_"
for shape_path in get_shapes_including(to_be_removed, prj):
# define the VBA code_examples & add to the history list
delete_vba = f'Solid.Delete "{shape_path}"'
prj.model3d.add_to_history(f"delete shape: {shape_path}", delete_vba)
会在History List中添加一项删除。
看起来要彻底删除的话还是得研究一下VBA文档,只能先手动了。

浙公网安备 33010602011771号