STP 格式解析
本文不讨论 STP (Standard for the Exchange of Product)
整个庞大的标准体系,只看日常能结构工程师手里拿到 .stp 文件。
.stp 是纯文本的 3D 结构交换文件。
与各路专有 CAD 格式不同,stp 格式是 ISO 标准化的格式。
stp 专注于 3D 数模交换,因此专有软件中的制图细节
导出为 stp 格式后会部分细节丢失。可以类比为 docx 文档导出为
pdf 失去了 docx 文档的细节
(与 pdf 不同的是 stp 仍可以导入 CAD 中编辑)。
stp 格式可以称之为结构工程师的 pdf.
STP 格式一瞥
以为手头的一个 3D 数模为例:
ISO-10303-21;
HEADER;
FILE_DESCRIPTION(
('This file contains a STEP AP42 implementation'
,'as created by ZW3D STEP Interface translator.')
,'2;1'
);
FILE_NAME(
'demo.stp'
,'26 312.112656',
(''),
('ZWCAD Software Co.'),
'Version 1.0',
'ZW3D to STEP translator',
''
);
FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));
ENDSEC;
开头是文件格式标准 ISO-10303-21
紧接着是 stp 的元数据描述 HEADER;
最后一行 ENDSEC 表示 “end of section”, 标志整个头部部分结束。
紧接着是 stp 的主体部分:
DATA;
/**File Base Unit**/
#1 = (LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
#2 = (NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));
...
/**product_definition_shape**/
#6=APPLICATION_CONTEXT('mechanical parts and assemblies' );
#7=DESIGN_CONTEXT('NONE',#6,'design');
...
/**geometry information**/
#14=CARTESIAN_POINT('',(0.,0.,0.));
#15=DIRECTION('',(0.,0.,1.));
#16=DIRECTION('',(1.,0.,0.));
#17=AXIS2_PLACEMENT_3D('',#14,#15,#16);
...
ENDSEC;
END-ISO-10303-21;
DATA 部分跟头部一样,由 ENDSEC 表示 DATA 部分的结束。 DATA 组成部分由:单位设置、产品信息、图像信息三部分组成。 简洁明了。
以 #15 为例:
#15=DIRECTION('',(0.,0.,1.));
DATA 部分的内容称之为 Entity instances
形如 #15 的编号称之为实例名称 (entity instance name)
DIRECTION 是用 EXPRESS 语言(表达式) 定义的实体(类比理解为面向对象的 class)
用
grep -e '^#\d\+=[A-Z]\+' demo.stp | sed 's/^#.*=//' | sed 's/(.*//' | sort | uniq
看下 stp 文件有多少 Entity,demo.stp 到文件结束实例编号来到了 #34486,
wc -l 统计只有五十八个 Entity,其中还包含了定义产品信息的 Entity。
实际上用来画图描述结构的 Entity 看起来比较精简。 读者可以通过文章末尾的引用参考下载 NIST 提供 Sample stp 文件验证。 同时 NIST 还开源了一个 step file anayzer 实现。
<!--
(资本主义确实有值得学习的地方.jpg)
(我们的的社会主义过家家会变成甚么样子.jpg)
-->
标准中画图有关的 Entity
这部分是我最感兴趣的部分。 不过我买不起 ISO 正式出版的文稿,各个影子图书馆也没有激情泄漏版。 可惜没有标准文档,很难准确知道每个 Entity 具体怎么绘制。
原本以为 stp 文件的绘图指令比较简单,准备自己实现个简单的 stp viewer. 一番折腾发现事情并没有想象中的简单,先暂时搁置。
EXAMPLE Entity definition in EXPRESS:
ENTITY widget;
i1: INTEGER; -----------> A
i2: INTEGER; -----------> B
s1: STRING(3); -----------> C
s2: STRING; -----------> D
l : LOGICAL; -----------> E
b : BOOLEAN; -----------> F
r1: REAL(4); -----------> G
r2: REAL; -----------> H
r3: REAL; -----------> L
r4: REAL; -----------> M
END_ENTITY;
Sample instance in the data section:
#2 = WIDGET(99, 99999, 'ABC', 'ABCDEFG', .T., .F., 9., 1.2345, @10, @PI);
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | |
| | | | | | | | | |
A B C D E F G H L M
A: i1 has a value of 99 in this entity instance.
B: i2 has a value of 99999 in this entity instance.
C: s1 has a value of 'ABC' in this entity instance. This value falls within the range (3 characters) specified for this attribute.
D: s2 has a value of 'ABCDEFG' in this entity instance.
E: l has a value of TRUE in this entity instance.
F: b has a value of FALSE in this entity instance.
G: r1 has the value of 9. in this entity instance. The precision specification does not affect the encoding.
H: r2 has a value of 1.2345 in this entity instance.
L: r3 has a value defined in the reference section.
M: r4 has a value defined in the EXPRESS schema.
引用参考
1: ISO TC 184/SC4/WG11 N304
https://www.steptools.com/stds/step/IS_final_p21e3.html
2: Stptool arm.html https://steptools.com/stds/stp_expg/arm.html
3: Wikipedia
https://en.wikipedia.org/wiki/ISO_10303-21
4: stepcode
https://github.com/stepcode/stepcode
5: Fundamentals of STEP Implementation https://steptools.com/stds/step/fundimpl.pdf
6: step file analyzer:
https://www.nist.gov/services-resources/software/step-file-analyzer-and-viewer