Skip to content
Gains Summary
Main Navigation 首页 / Home
C++ 编程 / C++ Programming
系统与高性能 / Systems & Performance
Web 开发 / Web Development
人工智能 / Artificial Intelligence
工业软件 / Industrial Software
其他内容 / Other Topics
C++ 编程 / C++系统与性能 / SystemsWeb 开发 / Web人工智能 / AI工业软件 / Industrial

外观

Sidebar Navigation

← 工业软件 / Industrial Software

SAM 平台开发 / SAM Platform Development

1. SAM 二次开发总览与证据分级 / SAM Secondary Development Overview and Evidence Levels

2. 从 P0 到 P5:SAM 插件界面开发流程 / SAM Plugin Interface Development from P0 to P5

3. PYD 注册、Model Fragment 与 Part Fragment / PYD Registration, Model Fragments, and Part Fragments

4. Python 2 SAM API:已验证接口与失败清单 / Verified Python 2 SAM APIs and Known Failures

5. C++ SAM SDK:模型、网格与 ODB 接口 / C++ SAM SDK APIs for Models, Meshes, and ODB Data

6. SAM 参数化构件建模:从二维轮廓到三维网格 / SAM Parametric Component Modeling from 2D Profiles to 3D Meshes

7. Agent 接入 SAM:插件架构、工具注册与安全边界 / Integrating AI Agents into SAM with Plugins, Tools, and Safety Boundaries

8. SAM API 验证工作流:禁止猜测与七级验收门 / A Seven-Gate SAM API Validation Workflow Without Guesswork

9. 真实案例、当前边界与后续路线 / Validated Cases, Current Boundaries, and the Development Roadmap

10. SAM 二次开发指南:架构总览与系列索引 / SAM Secondary Development Architecture and Series Index

11. SAM GUI、Toolset 与交互类 API / SAM GUI, Toolset, and Interaction APIs

12. SAM 命令桥、Python 与 PYD 扩展 API / SAM Command Bridge, Python, and PYD Extension APIs

13. SAM 数据模型、Part 与 Mesh API / SAM Data Models, Parts, and Mesh APIs

14. SAM 场景、显示表示与渲染 API / SAM Scene, Display Representation, and Rendering APIs

15. SAM 插件编译、部署与故障排查 / Building, Deploying, and Troubleshooting SAM Plugins

16. SAM 二次开发旧入口 / Legacy Entry for SAM Secondary Development

17. SAM 二次开发知识体系 / SAM Secondary Development Knowledge System

本页目录

SAM 命令桥、Python 与 PYD 扩展 API / SAM Command Bridge, Python, and PYD Extension APIs ​

本篇解释 GUI 如何将操作交给 SAM Kernel,以及 PYD 如何把 C++ 能力暴露给内嵌 Python。

1. 三层调用模型 ​

text
GUI DLL
  `-- SAMGuiMode/SAMForm 生成 Python 命令字符串
        `-- cmdGCommandDeliveryRole
              `-- SAM Python/Kernel
                    +-- mdb 用户级 API
                    `-- import 业务 PYD
                          `-- C++ SDK、模型和网格算法
1
2
3
4
5
6
7

GUI DLL 和 PYD 不会因为放在同一目录就自动通信。二者之间通常以 Python 命令、方法参数、返回值和模型更新作为显式协议。

2. 命令投递类 ​

2.1 cmdGCommandDeliveryRole ​

GUI 到 Kernel/Python 的核心命令桥,采用单例角色设计。

cpp
void SendCommand(const QString& command,
                 cmdGCommandDoneCB* callback = nullptr,
                 bool writeToReplay = true,
                 bool writeToJournal = false,
                 bool waitForReply = true);

cmdCReply* EvalCommand(const QString&);
void SendCliCommand(const QString&,
                    cmdGCommandDoneCB* = nullptr);
void SyncSendCommand(const QString&,
                     bool replay = true,
                     bool journal = false);
void SoftInterrupt();
int OkToSend();
QString CurrentModel() const;
sesGCurrentContext CurrentModelContext() const;
bool HasReply() const;
const ipcReply& GetCmdReply() const;
omuPrimitive* getMethodValue(const omuPrimPath*,
                             const QString& methodName,
                             omuArguments& args);
Q_INVOKABLE void onCommand(const QString&,
                           bool replay,
                           bool journal);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  • SendCommand():普通命令发送,可附带完成回调。
  • EvalCommand():计算表达式并取得返回对象。
  • SyncSendCommand():同步等待结果,使用不当会阻塞 GUI。
  • writeToReplay:是否记录到 Replay。
  • writeToJournal:是否记录到 Journal。

2.2 cmdGCommandDoneCB ​

命令完成回调基类:

cpp
cmdGCommandDoneCB(cmdGCommandDoneCB* decorator = 0);
virtual void cmdContinue();
void SendCommand(const QString&,
                 bool replay = true,
                 bool journal = false);
bool HasReply() const;
ipcReplyCOW GetReply();
1
2
3
4
5
6
7

派生类可重写 cmdContinue(),读取上一条 Reply 后继续业务流程。

2.3 cmdGCommandDeliveryQueue ​

维护 CLI 命令顺序:

cpp
cmdGCommandDeliveryQueue();
void QueueCommand(const QString&,
                  cmdGCommandDoneCB* = nullptr);
1
2
3

3. Python 用户层的责任 ​

SAM 内嵌 Python 2.7。Python 层适合:

  • 使用 mdb.models[...]、parts[...] 等对象;
  • 编排可回放、可批处理的业务流程;
  • 调用 PYD 提供的 C++ 算法;
  • 处理适合脚本表达的参数和循环;
  • 形成 CLI、宏和自动化入口。

典型命令:

python
import YourModule
result = YourModule.calculate(...)
1
2

在 SAM 命令窗口输入时,不要复制 >>> 提示符,也不要在顶层命令前添加空格。

4. PYD 是什么 ​

PYD 是 Windows 上的 CPython 原生扩展模块。它在文件格式上类似 DLL,但由 Python import 加载,而不是由 Qt Toolset 插件扫描器加载。

PYD 适合:

  • 访问 SAM C++ 模型和网格对象;
  • 执行大量节点、单元和几何计算;
  • 包装已有 C++ 库;
  • 将算法注册为 Python 函数、类型或 Fragment;
  • 返回 Python 可识别的数值、字符串、元组和接口对象。

本机常用部署位置:

text
D:\Program Files\SAM\Release\YourModule.pyd
1

5. Python 绑定值体系 ​

5.1 omuPrimitive ​

所有绑定值的抽象根类型:

cpp
virtual QString TypeString() const;
virtual QString AsString(int depth = 0) const = 0;
virtual QString AsRepr() const;
virtual void Accept(omuPrimVisitor*) = 0;
virtual omuPrimitive* Copy() const = 0;
bool IsType(char) const;
bool IsA(typTypeTag) const;
1
2
3
4
5
6
7

5.2 omuInterfaceObj ​

把具有方法和成员的 C++ 对象暴露给 Python。

cpp
typedef omuPrimitive*
    (omuInterfaceObj::*methodFunc)(omuArguments&) const;

void DescribeType(const char*,
                  const methodTable[] = 0,
                  const memberTable[] = 0);
virtual bool IsMethod(const char*) const;
virtual omuPrimitive* CallMethod(const char* path,
                                 const char* method,
                                 omuArguments& args) const;
virtual omuPrimitive* GetMember(const char*) const;
virtual omuPrimitive* SetMember(const char*,
                                omuArguments&) const;
virtual cowListString MemberList() const;
virtual cowListString MethodList() const;
bool IsA(const char* type) const;
virtual bool IsStale() const;
virtual void SetStale(bool);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

5.3 omuArguments ​

继承 omuPrimTuple,解析位置参数和关键字参数。

cpp
void Begin();
void End(QString nesting = "");

void Get(int&);
void Get(double&);
void Get(QString&);
void Get(int&, const QString& keyword);
void Get(double&, const QString& keyword);
void Get(QString&, const QString& keyword);

void Put(int);
void Put(double);
void Put(const QString&);
void Put(omuPrimitive*);     // 接管 Primitive 指针
void Put(omuInterfaceObj*);  // 借用接口对象指针

bool Error() const;
bool Alternative();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

所有权差异十分重要。跨 DLL/PYD 错误释放对象可能直接导致崩溃。

5.4 omuMethodCall ​

构造 Python 方法调用表达式:

cpp
omuMethodCall(const QString& object,
              const QString& method,
              const omuArguments& args);
operator QString() const;
1
2
3
4

5.5 omuPrimNumber ​

数值 Primitive 基类,提供字符串表示、复制、Visitor 和数据库序列化接口。

5.6 omuPrimString ​

cpp
omuPrimString(const QString&);
omuPrimString(int);
omuPrimString(uint);
omuPrimString(double);
const QString& AsCharPtr() const;
omuPrimString* Slice(int i, int j) const;
void Concat(const omuPrimString&);
int Length() const;
1
2
3
4
5
6
7
8

5.7 omuPrimTuple ​

序列/元组包装:

cpp
omuPrimTuple(int size, char type = '(');
const omuPrimitive* Index(int i) const;
omuPrimSequence* Slice(int i, int j) const;
void Put(int/double/QString/...);
void Get(int/double/QString/...);
void Begin();
void End(QString nesting = "");
void Optional();
cowListInt AsListInt();
cowListDouble AsListDouble();
cowListString AsListString();
1
2
3
4
5
6
7
8
9
10
11

6. PYD 模块注册 ​

6.1 pyoModule ​

表示一个 C++ 实现的 Python 模块。

cpp
pyoModule(const char* name,
          omuInterfaceObj::methodTable* methods,
          ImportEnm autoImport = NO_IMPORT);
void DefineVariable(const char*, int/double/const char*/...);
void DefineConstant(const omuPrimEnumBase&);
virtual void DefineConstants() = 0;
void DefineType(const char* name);
void Import(const char* module);
void ImportFrom(const char* module, const char* variable);
void ImportAs(const char* module, const char* variable);
void DeleteVar(const char* variable);
1
2
3
4
5
6
7
8
9
10
11

6.2 iniPythonModuleRegistrar ​

注册模块初始化和结束函数:

cpp
void Register(inifunction initialize,
              inifunction finalize);
1
2

6.3 ptsKModelFragment ​

在现有 Python Model 对象上增加 Part 构造方法。

cpp
omuPrimitive* PartConstructor(omuArguments&);
omuPrimitive* PartFromGeometryFile(omuArguments&);
omuPrimitive* PartFromAcis(omuArguments&);
omuPrimitive* PartFromODB(omuArguments&);
omuPrimitive* PartFromExtrude2DMesh(omuArguments&);
omuPrimitive* PartFromMeshMirror(omuArguments&);
omuPrimitive* PartMeshToGeometry(omuArguments&);
omuPrimitive* OrphanMeshPart(omuArguments&);
omuPrimitive* PartFromIges(omuArguments&);
omuPrimitive* PartFromStep(omuArguments&);
omuPrimitive* PartFromSTL(omuArguments&);
1
2
3
4
5
6
7
8
9
10
11

6.4 ptsKPartFragment ​

在现有 Python Part 对象上增加方法:

cpp
omuPrimitive* Copy() const;
omuPrimitive* CallMethod(const char* path,
                         const char* method,
                         omuArguments& args) const;
1
2
3
4

Model Fragment 和 Part Fragment 的挂载层级不同。前者用于 model.method(),后者用于 part.method()。

7. 一个完整业务调用的推荐形式 ​

GUI 槽函数不直接遍历网格,而是发送稳定的 Python 命令:

text
用户点击
  -> Toolset 槽函数
  -> 激活 Form/Dialog
  -> 收集 Model、Part 和参数
  -> 生成 YourModule.operation(...)
  -> cmdGCommandDeliveryRole::SendCommand
  -> Python import YourModule
  -> PYD 访问 Part/Mesh
  -> 返回结果或更新模型
  -> 回调/Message Area/视口刷新
1
2
3
4
5
6
7
8
9
10

8. 常见问题 ​

import 失败 ​

检查 PYD 文件名、导出模块名、Python 2.7、x64、MSVC Runtime、依赖 DLL 和 sys.path。

import 成功但没有方法 ​

重点检查 methodTable、DefineType()、Fragment 注册和初始化顺序。

GUI 能找到 PYD,但调用崩溃 ​

重点检查 ABI、对象所有权、C++ 异常是否越过模块边界,以及 SAM 对象是否已经失效。

Python 修改了数据但 GUI 没刷新 ​

模型操作和显示更新是两个阶段。需要触发模型通知、Part 场景更新或当前 Scene 刷新。

最后更新于:

Pager
上一篇11. SAM GUI、Toolset 与交互类 API / SAM GUI, Toolset, and Interaction APIs
下一篇13. SAM 数据模型、Part 与 Mesh API / SAM Data Models, Parts, and Mesh APIs

持续记录,持续成长

Copyright © Tidenflow