Skip to content

mo_unit_mode

mo_unit_mode

Wrapper for set unit mode token.

UnitMode

Bases: ExtendedCommandToken

Wrapper for set unit mode token.

Sets the unit to mm or inch.

Source code in src/pygerber/gerberx3/tokenizer/tokens/mo_unit_mode.py
class UnitMode(ExtendedCommandToken):
    """Wrapper for set unit mode token.

    Sets the unit to mm or inch.
    """

    def __init__(self, string: str, location: int, unit: Unit) -> None:
        super().__init__(string, location)
        self.unit = unit

    @classmethod
    def new(cls, string: str, location: int, tokens: ParseResults) -> Self:
        """Create instance of this class.

        Created to be used as callback in `ParserElement.set_parse_action()`.
        """
        unit: Unit = Unit(tokens["unit"])
        return cls(string=string, location=location, unit=unit)

    def update_drawing_state(
        self,
        state: State,
        _backend: Backend,
    ) -> Tuple[State, Iterable[DrawCommand]]:
        """Update drawing state."""
        if self.unit == Unit.Inches:
            logging.warning(
                "Detected use of imperial units. Using metric units is recommended. "
                "Imperial units will be deprecated in future. "
                "(See 4.2.1 in Gerber Layer Format Specification)",
            )
        if state.draw_units is not None:
            logging.warning(
                "Overriding coordinate format is illegal. "
                "(See 4.2.1 in Gerber Layer Format Specification)",
            )
        return (
            state.model_copy(
                update={
                    "draw_units": self.unit,
                },
            ),
            (),
        )

    def parser2_visit_token(self, context: Parser2Context) -> None:
        """Perform actions on the context implicated by this token."""
        context.get_hooks().unit_mode.pre_parser_visit_token(self, context)
        context.get_hooks().unit_mode.on_parser_visit_token(self, context)
        context.get_hooks().unit_mode.post_parser_visit_token(self, context)

    def get_gerber_code(
        self,
        indent: str = "",  # noqa: ARG002
        endline: str = "\n",  # noqa: ARG002
    ) -> str:
        """Get gerber code represented by this token."""
        return f"MO{self.unit.value}"

new classmethod

new(
    string: str, location: int, tokens: ParseResults
) -> Self

Create instance of this class.

Created to be used as callback in ParserElement.set_parse_action().

Source code in src/pygerber/gerberx3/tokenizer/tokens/mo_unit_mode.py
@classmethod
def new(cls, string: str, location: int, tokens: ParseResults) -> Self:
    """Create instance of this class.

    Created to be used as callback in `ParserElement.set_parse_action()`.
    """
    unit: Unit = Unit(tokens["unit"])
    return cls(string=string, location=location, unit=unit)

update_drawing_state

update_drawing_state(
    state: State, _backend: Backend
) -> Tuple[State, Iterable[DrawCommand]]

Update drawing state.

Source code in src/pygerber/gerberx3/tokenizer/tokens/mo_unit_mode.py
def update_drawing_state(
    self,
    state: State,
    _backend: Backend,
) -> Tuple[State, Iterable[DrawCommand]]:
    """Update drawing state."""
    if self.unit == Unit.Inches:
        logging.warning(
            "Detected use of imperial units. Using metric units is recommended. "
            "Imperial units will be deprecated in future. "
            "(See 4.2.1 in Gerber Layer Format Specification)",
        )
    if state.draw_units is not None:
        logging.warning(
            "Overriding coordinate format is illegal. "
            "(See 4.2.1 in Gerber Layer Format Specification)",
        )
    return (
        state.model_copy(
            update={
                "draw_units": self.unit,
            },
        ),
        (),
    )

parser2_visit_token

parser2_visit_token(context: Parser2Context) -> None

Perform actions on the context implicated by this token.

Source code in src/pygerber/gerberx3/tokenizer/tokens/mo_unit_mode.py
def parser2_visit_token(self, context: Parser2Context) -> None:
    """Perform actions on the context implicated by this token."""
    context.get_hooks().unit_mode.pre_parser_visit_token(self, context)
    context.get_hooks().unit_mode.on_parser_visit_token(self, context)
    context.get_hooks().unit_mode.post_parser_visit_token(self, context)

get_gerber_code

get_gerber_code(
    indent: str = "", endline: str = "\n"
) -> str

Get gerber code represented by this token.

Source code in src/pygerber/gerberx3/tokenizer/tokens/mo_unit_mode.py
def get_gerber_code(
    self,
    indent: str = "",  # noqa: ARG002
    endline: str = "\n",  # noqa: ARG002
) -> str:
    """Get gerber code represented by this token."""
    return f"MO{self.unit.value}"