cad3d-ir-step 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cad3d_ir_step-0.2.0/LICENSE +21 -0
- cad3d_ir_step-0.2.0/PKG-INFO +119 -0
- cad3d_ir_step-0.2.0/README.md +91 -0
- cad3d_ir_step-0.2.0/pyproject.toml +75 -0
- cad3d_ir_step-0.2.0/scripts/generate_reference_fixture.py +256 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/__init__.py +24 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/cli.py +124 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/errors.py +17 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/header.py +45 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/iges.py +258 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/importer.py +226 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/py.typed +1 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/tessellate.py +234 -0
- cad3d_ir_step-0.2.0/src/cad3d_ir_step/xde.py +1111 -0
- cad3d_ir_step-0.2.0/tests/conftest.py +8 -0
- cad3d_ir_step-0.2.0/tests/fixtures/README.md +22 -0
- cad3d_ir_step-0.2.0/tests/fixtures/expected-semantics.json +126 -0
- cad3d_ir_step-0.2.0/tests/fixtures/native-reference.json +69 -0
- cad3d_ir_step-0.2.0/tests/fixtures/reference-assembly-ap214.step +651 -0
- cad3d_ir_step-0.2.0/tests/fixtures/reference-assembly.iges +364 -0
- cad3d_ir_step-0.2.0/tests/fixtures/reference-assembly.step +652 -0
- cad3d_ir_step-0.2.0/tests/native_fixture.py +290 -0
- cad3d_ir_step-0.2.0/tests/semantic.py +105 -0
- cad3d_ir_step-0.2.0/tests/test_abstraction.py +37 -0
- cad3d_ir_step-0.2.0/tests/test_cli.py +82 -0
- cad3d_ir_step-0.2.0/tests/test_fixture_integrity.py +34 -0
- cad3d_ir_step-0.2.0/tests/test_iges.py +91 -0
- cad3d_ir_step-0.2.0/tests/test_importer.py +256 -0
- cad3d_ir_step-0.2.0/tests/test_package_metadata.py +24 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 monozukuri-ai contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cad3d-ir-step
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: STEP and IGES importers for cad3d-ir through Open CASCADE XDE
|
|
5
|
+
Keywords: 3d-cad,ap242,gltf,iges,open-cascade,step,xde
|
|
6
|
+
Author: neka-nat
|
|
7
|
+
Author-email: neka-nat <nekanat.stock@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Dist: cad3d-ir>=0.2,<0.3
|
|
22
|
+
Requires-Dist: cadquery-ocp-novtk>=7.9.3.1,<7.10
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Project-URL: Homepage, https://github.com/monozukuri-ai/cad3d-ir
|
|
25
|
+
Project-URL: Issues, https://github.com/monozukuri-ai/cad3d-ir/issues
|
|
26
|
+
Project-URL: Repository, https://github.com/monozukuri-ai/cad3d-ir/tree/main/packages/cad3d-ir-step
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# cad3d-ir-step
|
|
30
|
+
|
|
31
|
+
`cad3d-ir-step` imports STEP (AP203, AP214, AP242) and IGES files into
|
|
32
|
+
`cad3d-ir` through Open CASCADE XDE. It preserves product definitions,
|
|
33
|
+
assembly occurrences, local placements, names and surface colours, exports
|
|
34
|
+
each reusable leaf shape as an external OCCT B-Rep resource and, on request,
|
|
35
|
+
tessellates everything into glTF binary display meshes.
|
|
36
|
+
|
|
37
|
+
The runtime uses the official `cadquery-ocp-novtk` binding because conversion
|
|
38
|
+
does not require VTK or a visualization stack.
|
|
39
|
+
|
|
40
|
+
The distribution intentionally depends on `cad3d-ir`; the core distribution
|
|
41
|
+
does not depend on this adapter or on Open CASCADE.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from pathlib import Path
|
|
47
|
+
|
|
48
|
+
from cad3d_ir import DirectoryResourceWriter
|
|
49
|
+
from cad3d_ir.importers import ImportOptions
|
|
50
|
+
from cad3d_ir_step import IgesImporter, StepImporter
|
|
51
|
+
|
|
52
|
+
output = Path("converted")
|
|
53
|
+
result = StepImporter().import_file(
|
|
54
|
+
Path("assembly.step"),
|
|
55
|
+
options=ImportOptions(include_visualization_mesh=True),
|
|
56
|
+
resources=DirectoryResourceWriter(output),
|
|
57
|
+
)
|
|
58
|
+
preview = result.package.visualizations[0] # preview/<package>.glb
|
|
59
|
+
IgesImporter().import_file(Path("part.igs"), options=ImportOptions(), resources=...)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The command-line entry point picks the importer from the file content and
|
|
63
|
+
writes `manifest.json` plus geometry resources:
|
|
64
|
+
|
|
65
|
+
```console
|
|
66
|
+
cad3d-ir-step assembly.step converted/ # B-Rep resources only
|
|
67
|
+
cad3d-ir-step assembly.step converted/ --mesh # + per-body GLB + package GLB
|
|
68
|
+
cad3d-ir-step robot.stp converted/ --mesh --no-brep --deflection-ratio 0.002
|
|
69
|
+
cad3d-ir-step part.igs converted/ --mesh
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Options
|
|
73
|
+
|
|
74
|
+
`ImportOptions` (core):
|
|
75
|
+
|
|
76
|
+
- `include_visualization_mesh` – write one `mesh` representation per body and
|
|
77
|
+
one package-level `Visualization` (GLB). Off by default.
|
|
78
|
+
- `include_brep` – write exact OCCT B-Rep ASCII resources. On by default.
|
|
79
|
+
- `mesh_linear_deflection_ratio` – linear deflection as a ratio of the root
|
|
80
|
+
bounding-box diagonal (default `0.001`); `mesh_angular_deflection` in
|
|
81
|
+
radians (default `0.3`); `mesh_include_edges` for edge polylines.
|
|
82
|
+
|
|
83
|
+
`StepImporter(require_ap242=False, body_mesh_resources=True)`:
|
|
84
|
+
|
|
85
|
+
- `require_ap242=True` restores the 0.1 behaviour of rejecting other schemas
|
|
86
|
+
before any geometry is transferred. By default AP203/AP214 files are read
|
|
87
|
+
and an informational `STEP_SCHEMA_NOT_AP242` diagnostic is emitted.
|
|
88
|
+
- `body_mesh_resources=False` keeps only the package GLB when meshes are
|
|
89
|
+
requested (what a viewer needs; halves the written bytes).
|
|
90
|
+
|
|
91
|
+
## Current boundary
|
|
92
|
+
|
|
93
|
+
- STEP Part 21 files with any AP schema and IGES 5.x files are accepted; the
|
|
94
|
+
mapping is the same XDE product graph for both.
|
|
95
|
+
- XDE product structure, names, local placements, visibility and definition
|
|
96
|
+
surface colours are mapped. IGES carries little structure, so its packages
|
|
97
|
+
are usually one part per shape.
|
|
98
|
+
- Leaf definition shapes are stored as OCCT B-Rep ASCII v1 resources
|
|
99
|
+
(`include_brep`), as glTF binary meshes (`include_visualization_mesh`), or
|
|
100
|
+
both.
|
|
101
|
+
- One resolved `Default` configuration is synthesized for every product.
|
|
102
|
+
- PMI/GD&T, layers, physical materials and external STEP references are not
|
|
103
|
+
mapped; they produce `*_NOT_MAPPED` diagnostics instead of being dropped
|
|
104
|
+
silently. Faces that OCCT cannot triangulate are counted in
|
|
105
|
+
`*_MESH_INCOMPLETE`.
|
|
106
|
+
- Representation bounds come from `BRepBndLib::Add` (surface control points),
|
|
107
|
+
which is conservative; the visualization bounds are exact mesh bounds.
|
|
108
|
+
|
|
109
|
+
Measured on a 47 MB industrial robot STEP (12,817 faces): read 28 s, meshing
|
|
110
|
+
4 s, extraction and GLB 5 s, peak RSS about 730 MB, 27.6 MB GLB with 700k
|
|
111
|
+
triangles.
|
|
112
|
+
|
|
113
|
+
## Fixtures
|
|
114
|
+
|
|
115
|
+
`tests/fixtures/reference-assembly.step` (AP242), `reference-assembly-ap214.step`
|
|
116
|
+
and `reference-assembly.iges` are generated from primitive boxes and cylinders
|
|
117
|
+
by `scripts/generate_reference_fixture.py --schema {ap242,ap214,iges}` with
|
|
118
|
+
normalized dates and author fields. The abstraction test compares the AP242
|
|
119
|
+
assembly with an independent typed synthetic native-CAD source fixture.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# cad3d-ir-step
|
|
2
|
+
|
|
3
|
+
`cad3d-ir-step` imports STEP (AP203, AP214, AP242) and IGES files into
|
|
4
|
+
`cad3d-ir` through Open CASCADE XDE. It preserves product definitions,
|
|
5
|
+
assembly occurrences, local placements, names and surface colours, exports
|
|
6
|
+
each reusable leaf shape as an external OCCT B-Rep resource and, on request,
|
|
7
|
+
tessellates everything into glTF binary display meshes.
|
|
8
|
+
|
|
9
|
+
The runtime uses the official `cadquery-ocp-novtk` binding because conversion
|
|
10
|
+
does not require VTK or a visualization stack.
|
|
11
|
+
|
|
12
|
+
The distribution intentionally depends on `cad3d-ir`; the core distribution
|
|
13
|
+
does not depend on this adapter or on Open CASCADE.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from cad3d_ir import DirectoryResourceWriter
|
|
21
|
+
from cad3d_ir.importers import ImportOptions
|
|
22
|
+
from cad3d_ir_step import IgesImporter, StepImporter
|
|
23
|
+
|
|
24
|
+
output = Path("converted")
|
|
25
|
+
result = StepImporter().import_file(
|
|
26
|
+
Path("assembly.step"),
|
|
27
|
+
options=ImportOptions(include_visualization_mesh=True),
|
|
28
|
+
resources=DirectoryResourceWriter(output),
|
|
29
|
+
)
|
|
30
|
+
preview = result.package.visualizations[0] # preview/<package>.glb
|
|
31
|
+
IgesImporter().import_file(Path("part.igs"), options=ImportOptions(), resources=...)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The command-line entry point picks the importer from the file content and
|
|
35
|
+
writes `manifest.json` plus geometry resources:
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
cad3d-ir-step assembly.step converted/ # B-Rep resources only
|
|
39
|
+
cad3d-ir-step assembly.step converted/ --mesh # + per-body GLB + package GLB
|
|
40
|
+
cad3d-ir-step robot.stp converted/ --mesh --no-brep --deflection-ratio 0.002
|
|
41
|
+
cad3d-ir-step part.igs converted/ --mesh
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Options
|
|
45
|
+
|
|
46
|
+
`ImportOptions` (core):
|
|
47
|
+
|
|
48
|
+
- `include_visualization_mesh` – write one `mesh` representation per body and
|
|
49
|
+
one package-level `Visualization` (GLB). Off by default.
|
|
50
|
+
- `include_brep` – write exact OCCT B-Rep ASCII resources. On by default.
|
|
51
|
+
- `mesh_linear_deflection_ratio` – linear deflection as a ratio of the root
|
|
52
|
+
bounding-box diagonal (default `0.001`); `mesh_angular_deflection` in
|
|
53
|
+
radians (default `0.3`); `mesh_include_edges` for edge polylines.
|
|
54
|
+
|
|
55
|
+
`StepImporter(require_ap242=False, body_mesh_resources=True)`:
|
|
56
|
+
|
|
57
|
+
- `require_ap242=True` restores the 0.1 behaviour of rejecting other schemas
|
|
58
|
+
before any geometry is transferred. By default AP203/AP214 files are read
|
|
59
|
+
and an informational `STEP_SCHEMA_NOT_AP242` diagnostic is emitted.
|
|
60
|
+
- `body_mesh_resources=False` keeps only the package GLB when meshes are
|
|
61
|
+
requested (what a viewer needs; halves the written bytes).
|
|
62
|
+
|
|
63
|
+
## Current boundary
|
|
64
|
+
|
|
65
|
+
- STEP Part 21 files with any AP schema and IGES 5.x files are accepted; the
|
|
66
|
+
mapping is the same XDE product graph for both.
|
|
67
|
+
- XDE product structure, names, local placements, visibility and definition
|
|
68
|
+
surface colours are mapped. IGES carries little structure, so its packages
|
|
69
|
+
are usually one part per shape.
|
|
70
|
+
- Leaf definition shapes are stored as OCCT B-Rep ASCII v1 resources
|
|
71
|
+
(`include_brep`), as glTF binary meshes (`include_visualization_mesh`), or
|
|
72
|
+
both.
|
|
73
|
+
- One resolved `Default` configuration is synthesized for every product.
|
|
74
|
+
- PMI/GD&T, layers, physical materials and external STEP references are not
|
|
75
|
+
mapped; they produce `*_NOT_MAPPED` diagnostics instead of being dropped
|
|
76
|
+
silently. Faces that OCCT cannot triangulate are counted in
|
|
77
|
+
`*_MESH_INCOMPLETE`.
|
|
78
|
+
- Representation bounds come from `BRepBndLib::Add` (surface control points),
|
|
79
|
+
which is conservative; the visualization bounds are exact mesh bounds.
|
|
80
|
+
|
|
81
|
+
Measured on a 47 MB industrial robot STEP (12,817 faces): read 28 s, meshing
|
|
82
|
+
4 s, extraction and GLB 5 s, peak RSS about 730 MB, 27.6 MB GLB with 700k
|
|
83
|
+
triangles.
|
|
84
|
+
|
|
85
|
+
## Fixtures
|
|
86
|
+
|
|
87
|
+
`tests/fixtures/reference-assembly.step` (AP242), `reference-assembly-ap214.step`
|
|
88
|
+
and `reference-assembly.iges` are generated from primitive boxes and cylinders
|
|
89
|
+
by `scripts/generate_reference_fixture.py --schema {ap242,ap214,iges}` with
|
|
90
|
+
normalized dates and author fields. The abstraction test compares the AP242
|
|
91
|
+
assembly with an independent typed synthetic native-CAD source fixture.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "cad3d-ir-step"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "STEP and IGES importers for cad3d-ir through Open CASCADE XDE"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "neka-nat", email = "nekanat.stock@gmail.com" },
|
|
11
|
+
]
|
|
12
|
+
keywords = [
|
|
13
|
+
"3d-cad",
|
|
14
|
+
"ap242",
|
|
15
|
+
"gltf",
|
|
16
|
+
"iges",
|
|
17
|
+
"open-cascade",
|
|
18
|
+
"step",
|
|
19
|
+
"xde",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 3 - Alpha",
|
|
23
|
+
"Intended Audience :: Developers",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Programming Language :: Python :: 3.13",
|
|
30
|
+
"Programming Language :: Python :: 3.14",
|
|
31
|
+
"Topic :: Scientific/Engineering",
|
|
32
|
+
"Typing :: Typed",
|
|
33
|
+
]
|
|
34
|
+
dependencies = [
|
|
35
|
+
"cad3d-ir>=0.2,<0.3",
|
|
36
|
+
"cadquery-ocp-novtk>=7.9.3.1,<7.10",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/monozukuri-ai/cad3d-ir"
|
|
41
|
+
Repository = "https://github.com/monozukuri-ai/cad3d-ir/tree/main/packages/cad3d-ir-step"
|
|
42
|
+
Issues = "https://github.com/monozukuri-ai/cad3d-ir/issues"
|
|
43
|
+
|
|
44
|
+
[project.scripts]
|
|
45
|
+
cad3d-ir-step = "cad3d_ir_step.cli:main"
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["uv_build>=0.8.15,<0.9.0"]
|
|
49
|
+
build-backend = "uv_build"
|
|
50
|
+
|
|
51
|
+
[dependency-groups]
|
|
52
|
+
dev = [
|
|
53
|
+
"pytest>=8.4",
|
|
54
|
+
"ruff>=0.8",
|
|
55
|
+
"tomli>=2.0; python_version < '3.11'",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[tool.uv.sources]
|
|
59
|
+
cad3d-ir = { workspace = true }
|
|
60
|
+
|
|
61
|
+
[tool.uv.build-backend]
|
|
62
|
+
source-include = [
|
|
63
|
+
"scripts/**",
|
|
64
|
+
"tests/**",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.pytest.ini_options]
|
|
68
|
+
testpaths = ["tests"]
|
|
69
|
+
|
|
70
|
+
[tool.ruff]
|
|
71
|
+
target-version = "py310"
|
|
72
|
+
line-length = 88
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint]
|
|
75
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""Generate the repository-owned XDE fixtures (STEP AP242, STEP AP214, IGES)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCylinder
|
|
11
|
+
from OCP.gp import gp_Trsf, gp_Vec
|
|
12
|
+
from OCP.IFSelect import IFSelect_RetDone
|
|
13
|
+
from OCP.IGESCAFControl import IGESCAFControl_Writer
|
|
14
|
+
from OCP.IGESControl import IGESControl_Controller
|
|
15
|
+
from OCP.Interface import Interface_Static
|
|
16
|
+
from OCP.Quantity import Quantity_Color, Quantity_TOC_RGB
|
|
17
|
+
from OCP.STEPCAFControl import STEPCAFControl_Writer
|
|
18
|
+
from OCP.STEPControl import STEPControl_Controller, STEPControl_StepModelType
|
|
19
|
+
from OCP.TCollection import TCollection_ExtendedString
|
|
20
|
+
from OCP.TDataStd import TDataStd_Name
|
|
21
|
+
from OCP.TDocStd import TDocStd_Document
|
|
22
|
+
from OCP.TopLoc import TopLoc_Location
|
|
23
|
+
from OCP.XCAFApp import XCAFApp_Application
|
|
24
|
+
from OCP.XCAFDoc import XCAFDoc_ColorType, XCAFDoc_DocumentTool
|
|
25
|
+
|
|
26
|
+
_FIXED_TIMESTAMP = "2000-01-01T00:00:00"
|
|
27
|
+
_FILE_NAME_TIMESTAMP = re.compile(
|
|
28
|
+
r"(FILE_NAME\s*\(\s*'(?:''|[^'])*'\s*,\s*)'(?:''|[^'])*'",
|
|
29
|
+
re.IGNORECASE,
|
|
30
|
+
)
|
|
31
|
+
_SCHEMAS = {"ap242": "AP242DIS", "ap214": "AP214CD"}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _set_name(label: object, name: str) -> None:
|
|
35
|
+
TDataStd_Name.Set_s(label, TCollection_ExtendedString(name))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _location(x: float, y: float, z: float) -> TopLoc_Location:
|
|
39
|
+
transform = gp_Trsf()
|
|
40
|
+
transform.SetTranslationPart(gp_Vec(x, y, z))
|
|
41
|
+
return TopLoc_Location(transform)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _build_document() -> TDocStd_Document:
|
|
45
|
+
"""Build the reference assembly as an XDE document."""
|
|
46
|
+
application = XCAFApp_Application.GetApplication_s()
|
|
47
|
+
document = TDocStd_Document(TCollection_ExtendedString("XmlOcaf"))
|
|
48
|
+
application.InitDocument(document)
|
|
49
|
+
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(document.Main())
|
|
50
|
+
shape_tool.SetAutoNaming_s(False)
|
|
51
|
+
color_tool = XCAFDoc_DocumentTool.ColorTool_s(document.Main())
|
|
52
|
+
|
|
53
|
+
assembly_label = shape_tool.NewShape()
|
|
54
|
+
_set_name(assembly_label, "Reference Assembly")
|
|
55
|
+
|
|
56
|
+
plate_label = shape_tool.AddShape(
|
|
57
|
+
BRepPrimAPI_MakeBox(40.0, 20.0, 2.0).Shape(),
|
|
58
|
+
False,
|
|
59
|
+
)
|
|
60
|
+
_set_name(plate_label, "Base Plate")
|
|
61
|
+
color_tool.SetColor(
|
|
62
|
+
plate_label,
|
|
63
|
+
Quantity_Color(0.2, 0.4, 0.8, Quantity_TOC_RGB),
|
|
64
|
+
XCAFDoc_ColorType.XCAFDoc_ColorGen,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
pin_label = shape_tool.AddShape(
|
|
68
|
+
BRepPrimAPI_MakeCylinder(2.0, 10.0).Shape(),
|
|
69
|
+
False,
|
|
70
|
+
)
|
|
71
|
+
_set_name(pin_label, "Pin")
|
|
72
|
+
color_tool.SetColor(
|
|
73
|
+
pin_label,
|
|
74
|
+
Quantity_Color(0.8, 0.7, 0.2, Quantity_TOC_RGB),
|
|
75
|
+
XCAFDoc_ColorType.XCAFDoc_ColorGen,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
pin_pair_label = shape_tool.NewShape()
|
|
79
|
+
_set_name(pin_pair_label, "Pin Pair")
|
|
80
|
+
pin_components = (
|
|
81
|
+
(pin_label, "Pin:1", (0.0, 0.0, 0.0)),
|
|
82
|
+
(pin_label, "Pin:2", (20.0, 0.0, 0.0)),
|
|
83
|
+
)
|
|
84
|
+
for definition, name, position in pin_components:
|
|
85
|
+
component = shape_tool.AddComponent(
|
|
86
|
+
pin_pair_label,
|
|
87
|
+
definition,
|
|
88
|
+
_location(*position),
|
|
89
|
+
)
|
|
90
|
+
_set_name(component, name)
|
|
91
|
+
|
|
92
|
+
root_components = (
|
|
93
|
+
(plate_label, "Base Plate:1", (0.0, 0.0, 0.0)),
|
|
94
|
+
(pin_pair_label, "Pin Pair:1", (10.0, 10.0, 2.0)),
|
|
95
|
+
)
|
|
96
|
+
for definition, name, position in root_components:
|
|
97
|
+
component = shape_tool.AddComponent(
|
|
98
|
+
assembly_label,
|
|
99
|
+
definition,
|
|
100
|
+
_location(*position),
|
|
101
|
+
)
|
|
102
|
+
_set_name(component, name)
|
|
103
|
+
shape_tool.UpdateAssemblies()
|
|
104
|
+
return document
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def generate(output: Path, *, schema: str = "ap242") -> None:
|
|
108
|
+
"""Write the reference assembly as STEP (``ap242``/``ap214``) or IGES."""
|
|
109
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
110
|
+
if schema == "iges":
|
|
111
|
+
_generate_iges(output)
|
|
112
|
+
return
|
|
113
|
+
STEPControl_Controller.Init_s()
|
|
114
|
+
if not Interface_Static.SetCVal_s("write.step.schema", _SCHEMAS[schema]):
|
|
115
|
+
raise RuntimeError(f"OCCT rejected the {_SCHEMAS[schema]} schema setting")
|
|
116
|
+
document = _build_document()
|
|
117
|
+
|
|
118
|
+
writer = STEPCAFControl_Writer()
|
|
119
|
+
writer.SetNameMode(True)
|
|
120
|
+
writer.SetColorMode(True)
|
|
121
|
+
writer.SetLayerMode(True)
|
|
122
|
+
writer.SetPropsMode(True)
|
|
123
|
+
if not writer.Transfer(document, STEPControl_StepModelType.STEPControl_AsIs):
|
|
124
|
+
raise RuntimeError("OCCT failed to transfer the reference XDE document")
|
|
125
|
+
if writer.Write(str(output)) != IFSelect_RetDone:
|
|
126
|
+
raise RuntimeError(f"OCCT failed to write {output}")
|
|
127
|
+
|
|
128
|
+
text = output.read_text(encoding="utf-8")
|
|
129
|
+
normalized, replacements = _FILE_NAME_TIMESTAMP.subn(
|
|
130
|
+
rf"\g<1>'{_FIXED_TIMESTAMP}'",
|
|
131
|
+
text,
|
|
132
|
+
count=1,
|
|
133
|
+
)
|
|
134
|
+
if replacements != 1:
|
|
135
|
+
raise RuntimeError("could not normalize the STEP FILE_NAME timestamp")
|
|
136
|
+
output.write_text(normalized, encoding="utf-8", newline="\n")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _generate_iges(output: Path) -> None:
|
|
140
|
+
IGESControl_Controller.Init_s()
|
|
141
|
+
if not Interface_Static.SetCVal_s("write.iges.unit", "MM"):
|
|
142
|
+
raise RuntimeError("OCCT rejected the IGES unit setting")
|
|
143
|
+
if not Interface_Static.SetIVal_s("write.iges.brep.mode", 1):
|
|
144
|
+
raise RuntimeError("OCCT rejected the IGES B-Rep mode setting")
|
|
145
|
+
document = _build_document()
|
|
146
|
+
writer = IGESCAFControl_Writer()
|
|
147
|
+
writer.SetNameMode(True)
|
|
148
|
+
writer.SetColorMode(True)
|
|
149
|
+
writer.SetLayerMode(True)
|
|
150
|
+
if not writer.Transfer(document):
|
|
151
|
+
raise RuntimeError("OCCT failed to transfer the reference XDE document")
|
|
152
|
+
if not writer.Write(str(output)):
|
|
153
|
+
raise RuntimeError(f"OCCT failed to write {output}")
|
|
154
|
+
text = output.read_text(encoding="latin-1")
|
|
155
|
+
output.write_text(_normalize_iges(text), encoding="latin-1", newline="\n")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _hollerith(value: str) -> str:
|
|
159
|
+
return f"{len(value)}H{value}"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _split_global_fields(text: str) -> list[str]:
|
|
163
|
+
"""Split a global section into raw fields, keeping Hollerith prefixes."""
|
|
164
|
+
fields: list[str] = []
|
|
165
|
+
index = 0
|
|
166
|
+
current = ""
|
|
167
|
+
while index < len(text):
|
|
168
|
+
digits_end = index
|
|
169
|
+
while digits_end < len(text) and text[digits_end].isdigit():
|
|
170
|
+
digits_end += 1
|
|
171
|
+
if digits_end > index and digits_end < len(text) and text[digits_end] == "H":
|
|
172
|
+
length = int(text[index:digits_end])
|
|
173
|
+
end = digits_end + 1 + length
|
|
174
|
+
current += text[index:end]
|
|
175
|
+
index = end
|
|
176
|
+
continue
|
|
177
|
+
char = text[index]
|
|
178
|
+
if char == ",":
|
|
179
|
+
fields.append(current)
|
|
180
|
+
current = ""
|
|
181
|
+
elif char == ";":
|
|
182
|
+
fields.append(current)
|
|
183
|
+
current = ""
|
|
184
|
+
break
|
|
185
|
+
elif not char.isspace():
|
|
186
|
+
current += char
|
|
187
|
+
index += 1
|
|
188
|
+
return fields
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _normalize_iges(text: str) -> str:
|
|
192
|
+
"""Fix dates, author and company so the fixture is byte-stable everywhere."""
|
|
193
|
+
lines = text.splitlines()
|
|
194
|
+
global_lines = [line for line in lines if len(line) >= 73 and line[72] == "G"]
|
|
195
|
+
fields = _split_global_fields("".join(line[:72] for line in global_lines))
|
|
196
|
+
fixed_date = _hollerith(
|
|
197
|
+
_FIXED_TIMESTAMP.replace("-", "").replace("T", ".").replace(":", "")
|
|
198
|
+
)
|
|
199
|
+
# IGES global parameters (1-based): 18 = file date, 21 = author,
|
|
200
|
+
# 22 = organisation, 25 = modification date.
|
|
201
|
+
fields[17] = fixed_date
|
|
202
|
+
fields[20] = _hollerith("cad3d-ir")
|
|
203
|
+
fields[21] = _hollerith("monozukuri-ai")
|
|
204
|
+
fields[24] = fixed_date
|
|
205
|
+
body = ",".join(fields) + ";"
|
|
206
|
+
new_global = [body[index : index + 72] for index in range(0, len(body), 72)]
|
|
207
|
+
rebuilt: list[str] = []
|
|
208
|
+
sequence = 0
|
|
209
|
+
inserted = False
|
|
210
|
+
for line in lines:
|
|
211
|
+
if len(line) >= 73 and line[72] == "G":
|
|
212
|
+
if not inserted:
|
|
213
|
+
for chunk in new_global:
|
|
214
|
+
sequence += 1
|
|
215
|
+
rebuilt.append(f"{chunk:<72}G{sequence:07d}")
|
|
216
|
+
inserted = True
|
|
217
|
+
continue
|
|
218
|
+
if len(line) >= 73 and line[72] == "T":
|
|
219
|
+
counts = {"S": 0, "G": len(new_global), "D": 0, "P": 0}
|
|
220
|
+
for section in ("S", "D", "P"):
|
|
221
|
+
counts[section] = sum(
|
|
222
|
+
1 for item in rebuilt if len(item) >= 73 and item[72] == section
|
|
223
|
+
)
|
|
224
|
+
terminate = "".join(f"{key}{counts[key]:7d}" for key in "SGDP")
|
|
225
|
+
rebuilt.append(f"{terminate:<72}T{line[73:]}")
|
|
226
|
+
continue
|
|
227
|
+
rebuilt.append(line)
|
|
228
|
+
return "\n".join(rebuilt) + "\n"
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
232
|
+
parser = argparse.ArgumentParser()
|
|
233
|
+
parser.add_argument(
|
|
234
|
+
"output",
|
|
235
|
+
type=Path,
|
|
236
|
+
nargs="?",
|
|
237
|
+
default=(
|
|
238
|
+
Path(__file__).resolve().parents[1]
|
|
239
|
+
/ "tests"
|
|
240
|
+
/ "fixtures"
|
|
241
|
+
/ "reference-assembly.step"
|
|
242
|
+
),
|
|
243
|
+
)
|
|
244
|
+
parser.add_argument(
|
|
245
|
+
"--schema",
|
|
246
|
+
choices=sorted(_SCHEMAS) + ["iges"],
|
|
247
|
+
default="ap242",
|
|
248
|
+
help="STEP schema to write, or 'iges' for an IGES file",
|
|
249
|
+
)
|
|
250
|
+
args = parser.parse_args(argv)
|
|
251
|
+
generate(args.output, schema=args.schema)
|
|
252
|
+
return 0
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
if __name__ == "__main__":
|
|
256
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""STEP and IGES importers for cad3d-ir through Open CASCADE XDE."""
|
|
2
|
+
|
|
3
|
+
from cad3d_ir_step.errors import (
|
|
4
|
+
StepImportError,
|
|
5
|
+
StepReadError,
|
|
6
|
+
UnsupportedIRVersionError,
|
|
7
|
+
UnsupportedStepSchemaError,
|
|
8
|
+
)
|
|
9
|
+
from cad3d_ir_step.iges import IgesHeader, IgesImporter, inspect_iges_header
|
|
10
|
+
from cad3d_ir_step.importer import StepImporter
|
|
11
|
+
|
|
12
|
+
__version__ = "0.2.0"
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"IgesHeader",
|
|
16
|
+
"IgesImporter",
|
|
17
|
+
"StepImportError",
|
|
18
|
+
"StepImporter",
|
|
19
|
+
"StepReadError",
|
|
20
|
+
"UnsupportedIRVersionError",
|
|
21
|
+
"UnsupportedStepSchemaError",
|
|
22
|
+
"__version__",
|
|
23
|
+
"inspect_iges_header",
|
|
24
|
+
]
|