pygcadwin
pygcadwin 是一个用于 Windows 上浩辰 CAD / GstarCAD 的 Python COM
自动化库。它通过 GstarCAD 注册的 GStarCAD.Application.* ProgID 连接
到正在运行的 GstarCAD,或按需启动新的 GstarCAD 实例,并提供更接近
Python 使用习惯的绘图、选择、布局、表格和视图截图 API。
这个项目的接口风格参考了
pyautocad,但目标应用是
GstarCAD,并且底层依赖 pywin32。
安装
运行环境:
- Windows
- Python 3.10+
- 已安装并注册 COM 的 GstarCAD
从 GitHub 安装:
pip install git+https://github.com/GstarCAD-AI/pygcadwin.git
本地开发安装:
git clone https://github.com/GstarCAD-AI/pygcadwin.git
cd pygcadwin
pip install -e .
如果需要 Excel / xlsx 表格导入导出功能:
pip install -e ".[tables]"
快速开始
import pygcadwin as gcad
cad = gcad.Gcad(create_if_missing=True, visible=True)
ctx = cad.context
ctx.ensure_layer("Walls")
ctx.create_segment((0, 0), (100, 0), layer="Walls", color=1)
ctx.create_circle((50, 50), 25)
ctx.zoom_extents()
cad.document.save_as(r"C:\tmp\demo.dwg")
cad.close()
也可以用上下文管理器自动释放 COM 引用:
from pygcadwin import Gcad
with Gcad(create_if_missing=True, visible=True) as cad:
cad.context.create_rect((0, 0), (100, 50), layer="Frame", color=3)
cad.context.zoom_extents()
主要功能
- 连接或启动 GstarCAD COM 应用。
- 访问 Application、ActiveDocument、ModelSpace、Layouts。
- 创建线段、圆、圆弧、椭圆、多段线、矩形、文字、填充、标注和表格。
- 选择集、对象遍历、对象查找和布局遍历。
Point2、Vector2、距离计算和 COM VARIANT 数组辅助函数。- CSV / JSON 表格导入导出,Excel 格式通过
tables可选依赖启用。 - 简单的工具调用分发接口,可用于 LLM agent 调用 CAD 操作。
- 视图快照和缩放辅助。
指定 GstarCAD 版本
默认会优先尝试已注册的版本化 ProgID,例如:
GStarCAD.Application.27GStarCAD.Application.26GStarCAD.Application
当同一台机器上安装了多个版本时,可以显式指定:
from pygcadwin import Gcad
cad = Gcad(prog_id="GStarCAD.Application.27")
Agent 工具调用
from pygcadwin import execute_tool
result = execute_tool(
"create_circle",
{"center": [0, 0], "radius": 10, "layer": "A-CIRC"},
)
print(result)
使用 tool_schemas() 可以获取可暴露给工具调用 agent 的操作 schema,
run_actions([...]) 可以顺序执行多步绘图动作。
当前状态
这是第一版公开发布,API 仍处于 alpha 阶段。建议先在测试图纸或临时 DWG 文件中验证脚本行为,再用于正式生产图纸。
许可证
MIT License。详见 LICENSE。
Metadata
Release files for pygcadwin 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygcadwin-0.1.0.tar.gz | 26.4 kB | With provenance | Details for pygcadwin-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygcadwin-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pygcadwin-0.1.0-py3-none-any.whl |
Total release size: 55.8 kB
Release files / pygcadwin-0.1.0.tar.gz
| Download URL | pygcadwin-0.1.0.tar.gz |
|---|---|
| Size | 26.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9208d1f365aea1c37f9d3a170d955a9c761f027eb6668187b0deebba2949e50
|
|
BLAKE2b-256 checksum How to use checksums |
9f9d07d2a2cde3c43152f7d5b358a90ca71e87663e0f3543b153d0d28075079a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.
Transparency logRelease files / pygcadwin-0.1.0-py3-none-any.whl
| Download URL | pygcadwin-0.1.0-py3-none-any.whl |
|---|---|
| Size | 29.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7caf09f2566c3cf49b7fbffcac4e85c560a08ff884b129ecfbe8ef13966e7d0d
|
|
BLAKE2b-256 checksum How to use checksums |
87b7d882dfc3a2f9687af4e41f4bf4439290529267a78d515027d5f5c1868877
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.
Transparency logSecurity for pygcadwin 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pygcadwin-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.