Metadata-Version: 2.4
Name: cad3d-ir-fusion
Version: 0.1.2
Summary: Autodesk Fusion design (F3D / F3Z) importer for cad3d-ir through buildf3d and cq-acis
Keywords: 3d-cad,assembly,brep,f3d,f3z,fusion,gltf,step
Author: neka-nat
Author-email: neka-nat <nekanat.stock@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Dist: cad3d-ir>=0.3,<0.4
Requires-Dist: cad3d-ir-step>=0.4.3,<0.5
Requires-Dist: buildf3d>=0.4,<0.5 ; python_full_version >= '3.11'
Requires-Dist: cq-acis[occt]>=0.4.5,<0.5 ; python_full_version >= '3.11'
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/monozukuri-ai/cad3d-ir
Project-URL: Issues, https://github.com/monozukuri-ai/cad3d-ir/issues
Project-URL: Repository, https://github.com/monozukuri-ai/cad3d-ir/tree/main/packages/cad3d-ir-fusion
Description-Content-Type: text/markdown

# cad3d-ir-fusion

`cad3d-ir-fusion` imports Autodesk Fusion designs (`.f3d`) and design packages
(`.f3z`) into `cad3d-ir` through
[buildf3d](https://github.com/monozukuri-ai/buildf3d) and cq-acis. buildf3d
reads the component tree and places every occurrence, with the linked designs
of an F3Z expanded in place. cq-acis converts the ASM bodies of the shown
components into exact OCCT solids. The adapter writes one glTF binary with the
same conventions as the STEP adapter, and because the solids are exact B-Rep,
the same design can be written as a STEP assembly (`export_step`).

A Fusion design stores lengths in centimetres; the package is in millimetres.

Colours follow the appearances of the design. A placed body takes the
appearance applied to the body, else the one applied to the nearest
occurrence above it, else the one of its physical material. A face with an
appearance of its own keeps it. The GLB and the STEP file carry these colours
(`FUSION_APPEARANCE_COLOURS`). In the STEP file every colour sits on a solid
or a face: a placement that an occurrence appearance colours differently from
the other placements of its body is written as a shape of its own.

Canvases, the images a design places on a plane of a component (the artwork
on the two sides of a board, a reference picture), are kept as pictures: the
image files are resources of the package and `metadata.pictures` lists where
they lie (`FUSION_CANVASES_SHOWN`). They are not geometry. The GLB of the
package, the BREP resources and the STEP file hold the bodies only;
`cad3d_ir_fusion.pictures.show_pictures` returns the GLB with the pictures
for display:

```python
from cad3d_ir_fusion import show_pictures

glb = read(package.visualizations[0].resource_id)
shown, count = show_pictures(glb, package, read)  # read(resource_id) -> bytes
```

What the adapter does not do:

- It reads the bodies the design stores for its current state. Nothing is
  recomputed from the timeline.
- Of an appearance it reads the colour only: no textures, no transparency and
  no other material properties.
- Whether a canvas is hidden is not read: a canvas of a shown component is
  listed.
- Hidden occurrences and hidden bodies are left out of the visualization
  (`FUSION_HIDDEN`). A hidden occurrence stays in the package with
  `visible: false`.
- Bodies whose faces do not close a volume (surface bodies) are left out
  (`FUSION_BODY_SHEET`), and so is a body with geometry the converter does not
  build (`FUSION_BODY_UNSUPPORTED`, `FUSION_BODY_FAILED`).
- Components of linked designs that are not in the file, as in an F3D saved
  without its references, are left out (`FUSION_LINK_UNRESOLVED`).
- A record stored in a layout buildf3d does not read is skipped and reported
  (`FUSION_DESIGN_PARTIAL`).
- Mesh bodies, sketches, joints and the timeline are not read.

The distribution depends on `cad3d-ir`, `cad3d-ir-step` (tessellation and the
STEP writer), `buildf3d` and `cq-acis[occt]`; the core distribution does not
depend on this adapter. buildf3d and cq-acis need Python 3.11 or later: on
Python 3.10 the package installs without them and reading raises
`FusionImportError`.

## Usage

```python
from pathlib import Path

from cad3d_ir import DirectoryResourceWriter
from cad3d_ir.importers import ImportOptions
from cad3d_ir_fusion import FusionImporter, export_step

result = FusionImporter().import_file(
    Path("assembly.f3z"),
    options=ImportOptions(
        strict=False, include_visualization_mesh=True, include_brep=False
    ),
    resources=DirectoryResourceWriter("converted"),
)
print(result.statistics["bodies_converted"], result.statistics["triangles"])
for item in result.diagnostics:
    print(item.code, item.message)

export_step(Path("assembly.f3z"), Path("assembly.step"), schema="AP214")
```

```sh
cad3d-ir-fusion assembly.f3z converted --step assembly.step
```

`read_source` returns the design and its converted bodies once, so one read
can feed both `map_source` and `export_step`.

## Package layout

- One `ProductDefinition` per component that is placed, `part`, `assembly` or
  `hybrid` (bodies and occurrences). The root product is named after the root
  component, or after the file when the design was never saved.
- One `Occurrence` per child of a component configuration, with the placement
  relative to its parent. Instances of one component share a `Configuration`
  when their children sit at the same places; a path pose that places the
  children of two instances differently gives each its own configuration. A
  hidden occurrence has `visible: false` and an empty configuration.
- One `Body` per converted body, named as in the design, with a `brep`
  representation when `include_brep` is set (one OCCT BREP resource per body,
  in the frame of its component; `approximate` when stored fits stand in).
- One package `Visualization` (GLB, millimetres, Z up): one glTF node per
  shown occurrence, nested like the occurrences, and one node per placed
  body. Placements of a body share one mesh; a placement that an occurrence
  appearance colours differently has a mesh of its own. Node `extras.id`
  names the occurrence or the body.
- The preview image the design stores, when it has one, as a resource with
  `properties.role == "preview-image"`; `metadata.preview_resource_id` names
  it. It is written even when no body converts, so a caller can show it.
- The image of each canvas that a shown component places, as a resource with
  `properties.role == "picture-image"` (PNG or JPEG, with `width`, `height`
  and `alpha`). `metadata.pictures` lists each place: `name`, `resource_id`,
  `occurrence_path` (the occurrence IDs from the root design to the
  component, empty for the root component), `corners` (lower left, lower
  right, upper left and upper right corner of the image in the frame of the
  component, millimetres) and `opacity`. `show_pictures` adds them to a copy
  of the GLB as nodes with `extras.picture`, each a rectangle with the image
  as an unlit texture, 0.01 mm in front of its plane.
- For an F3Z, one `SourceDocument` for the package and one per design in it
  that is placed.

## Diagnostics

| Code | Meaning |
| --- | --- |
| `FUSION_UNIT_CM` | Lengths are converted from centimetres to millimetres (info) |
| `FUSION_EXACT_BREP` | Geometry is the B-Rep the design stores, converted to OCCT (info) |
| `FUSION_APPEARANCE_COLOURS` | Bodies and faces carry the colour of their appearance, with counts (info) |
| `FUSION_APPEARANCE_NOT_READ` | No appearance colour was read; default colour (info) |
| `FUSION_CANVASES_SHOWN` | Canvas images are listed as pictures of the visualization, with counts (info) |
| `FUSION_CANVAS_SKIPPED` | Canvas images that are missing, of another kind or too large, with reasons |
| `FUSION_HIDDEN` | Hidden occurrences and bodies are left out of the visualization (info) |
| `FUSION_APPROXIMATED_GEOMETRY` | Stored B-spline fits stand in for procedural curves and surfaces (info) |
| `FUSION_BODY_SHEET` | Bodies whose faces do not close a volume are left out (info) |
| `FUSION_DESIGN_PARTIAL` | Part of the design is in a layout buildf3d does not read |
| `FUSION_LINK_UNRESOLVED` | Components of linked designs that are not in the file are left out |
| `FUSION_BODY_UNSUPPORTED` | Bodies with geometry that is not converted, with reasons |
| `FUSION_BODY_FAILED` | Bodies that could not be built, with reasons |
| `FUSION_BODY_MISSING` | Bodies without a readable ASM body in the file |
| `FUSION_MESH_FAILED` | Bodies that could not be tessellated |
| `FUSION_PREVIEW_ONLY` | No shape can be displayed; the stored preview image is available |
| `FUSION_NO_GEOMETRY` | No body yielded a displayable shape (error) |
| `FUSION_DESIGN_UNSUPPORTED` | The component tree could not be read (error) |
| `FUSION_KERNEL_MISSING` | cq-acis's occt extra is not installed (error) |
| `FUSION_LIMIT` | buildf3d stopped at a resource limit (error) |
| `FUSION_READ_FAILED` | The archive could not be read (error) |

`statistics` carries the source format (`f3d` or `f3z`), buildf3d's status of
the design, counts of documents, components, shown instances and hidden
occurrences, body counts by outcome (`bodies_converted`, `bodies_open_shell`,
`bodies_unsupported`, `bodies_failed`, `bodies_missing`), the reasons by
converter code, the counts of approximated curves and surfaces with the
largest fit tolerance, the coloured placements and faces
(`coloured_placements` of `body_placements`, `coloured_faces`), the canvases
(`canvases`, `canvases_skipped`, and `pictures` for the places listed), triangle
counts and the read and conversion times, so a caller can show what was and
was not read.

## License

MIT, like `cad3d-ir`, buildf3d and cq-acis. Autodesk and Fusion are trademarks
of Autodesk, Inc.; this adapter reads the saved files and ships no Autodesk
software.
