Source code for pyvcad_attribute_resolver.modules.j750_shore_hardness

"""J750 shore-hardness to volume-fractions conversion registrations."""

import pyvcad as pv

from pyvcad_attribute_resolver.registry import register_conversion

from .data import J750_SHORE_HARDNESS_VOLUME_FRACTIONS


def _first_material_id_with_prefix(material_defs, prefix):
    for material_id in range(material_defs.num_materials() + 1):
        try:
            name = material_defs.name(material_id)
        except Exception:
            continue

        if name.startswith(prefix):
            return material_id

    raise ValueError("No material found with prefix '{}'".format(prefix))


def _resolve_material_ids(material_defs, agilus_material, vero_material):
    if agilus_material is None:
        agilus_id = _first_material_id_with_prefix(material_defs, "Agilus30")
    else:
        agilus_id = material_defs.id(agilus_material)

    if vero_material is None:
        vero_id = _first_material_id_with_prefix(material_defs, "Vero")
    else:
        vero_id = material_defs.id(vero_material)

    return agilus_id, vero_id


def _lut_factory(agilus_id, vero_id):
    entries = []
    for shore_hardness, agilus_fraction, vero_fraction in J750_SHORE_HARDNESS_VOLUME_FRACTIONS:
        entries.append(
            pv.LookupTableEntry(
                shore_hardness,
                {
                    agilus_id: agilus_fraction,
                    vero_id: vero_fraction,
                },
            )
        )

    def _factory():
        return pv.LookupTableConverter(
            [pv.DefaultAttributes.SHORE_HARDNESS],
            [pv.DefaultAttributes.VOLUME_FRACTIONS],
            entries,
            pv.InterpolationMode.LINEAR,
        )

    return _factory


[docs] def register_j750_shore_hardness_conversions( material_defs=None, agilus_material=None, vero_material=None, ): """Register the J750 shore-hardness to volume-fractions conversion. By default this resolves the first J750 materials whose names begin with ``Agilus30`` and ``Vero``. Callers can override the representative material names when they want a specific color variant. """ if material_defs is None: material_defs = pv.j750_materials agilus_id, vero_id = _resolve_material_ids( material_defs, agilus_material, vero_material, ) register_conversion( source="shore_hardness", target="volume_fractions", converter_factory=_lut_factory(agilus_id, vero_id), name="j750_shore_hardness_to_volume_fractions", priority=0, tags=["j750_shore_hardness"], )