Skip to content

init

compiler

compiler module contains internals of Gerber X3 to RVMC compiler.

Compiler

Bases: StateTrackingVisitor

Compiler for transforming transforming Gerber (AST) to PyGerber rendering VM commands (RVMC).

Source code in src/pygerber/gerberx3/compiler/compiler.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
class Compiler(StateTrackingVisitor):
    """Compiler for transforming transforming Gerber (AST) to PyGerber rendering VM
    commands (RVMC).
    """

    MAIN_BUFFER_ID: ClassVar[str] = "%main%"

    def __init__(self, *, ignore_program_stop: bool = False) -> None:
        super().__init__(ignore_program_stop=ignore_program_stop)
        self._buffers: dict[str, CommandBuffer] = {}
        self._buffer_stack: list[str] = []
        self._contour_buffer: Optional[list[ShapeSegment]] = []
        self._create_main_buffer()

    def _set_buffer(self, buffer: CommandBuffer) -> None:
        """Register buffer."""
        self._buffers[buffer.id_str] = buffer

    def _get_buffer(self, id_: str) -> CommandBuffer:
        """Get buffer by id."""
        return self._buffers[id_]

    def _del_buffer(self, id_: str) -> None:
        """Delete buffer by id."""
        del self._buffers[id_]

    def _get_buffer_opt(self, id_: str) -> Optional[CommandBuffer]:
        """Get buffer by id."""
        return self._buffers.get(id_)

    def _get_current_buffer(self) -> CommandBuffer:
        return self._get_buffer(self._buffer_stack[-1])

    def _append_shape_to_current_buffer(self, command: Shape) -> None:
        self._get_current_buffer().append_shape(command)

    def _append_paste_to_current_buffer(self, command: PasteLayer) -> None:
        self._get_current_buffer().append_paste(command)

    def _expand_buffer_to_current_buffer(self, buffer: CommandBuffer) -> None:
        for command in buffer.commands:
            if isinstance(command, Shape):
                self._append_shape_to_current_buffer(command)
            elif isinstance(command, PasteLayer):
                self._append_paste_to_current_buffer(command)
            else:
                raise NotImplementedError(type(command))

    def _create_main_buffer(self) -> CommandBuffer:
        buffer = CommandBuffer(
            self.MAIN_BUFFER_ID,
            box=None,
            origin=Vector(x=0, y=0),
            commands=[],
            depends_on=set(),
            resolved_dependencies=[],
        )
        assert self.MAIN_BUFFER_ID not in self._buffers
        self._set_buffer(buffer)
        self._push_buffer(self.MAIN_BUFFER_ID)

        return buffer

    def _push_buffer(self, id_: str) -> None:
        self._buffer_stack.append(id_)

    def _pop_buffer(self) -> None:
        self._buffer_stack.pop()

    def _get_line_thickness(self, line_direction: Vector) -> float:
        current_aperture = self.state.current_aperture
        if isinstance(current_aperture, ADC):
            return current_aperture.diameter

        if isinstance(current_aperture, (ADR, ADO)):
            angle = line_direction.angle_between(Vector.unit.x)
            sin_angle = sin(radians(-angle))
            cos_angle = cos(radians(-angle))

            return Vector(
                x=current_aperture.width * cos_angle,
                y=current_aperture.height * sin_angle,
            ).length()

        if isinstance(current_aperture, ADP):
            return current_aperture.outer_diameter

        raise NotImplementedError(type(current_aperture))

    def on_ab(self, node: AB) -> AB:
        """Handle `AB` node."""
        aperture_buffer = self._create_aperture_buffer(node.open.aperture_id)
        self._push_buffer(aperture_buffer.id_str)

        super().on_ab(node)

        self._pop_buffer()

        return node

    def on_sr(self, node: SR) -> SR:
        """Handle `SR` node."""
        aperture_buffer = self._create_aperture_buffer(
            ApertureIdStr(f"%%SR%{id(node)}%{time.time():.0f}")
        )
        self._push_buffer(aperture_buffer.id_str)

        super().on_sr(node)

        self._pop_buffer()

        buffer = self._get_buffer(aperture_buffer.id_str)

        x_delta = node.open.x_delta
        y_delta = node.open.y_delta

        for x in range(node.open.x_repeats):
            for y in range(node.open.y_repeats):
                x_coordinate = self.coordinate_x + (x * x_delta)
                y_coordinate = self.coordinate_y + (y * y_delta)
                tmp_buffer = self._apply_transform_to_buffer_non_recursive_tmp(
                    buffer, Matrix3x3.new_translate(x=x_coordinate, y=y_coordinate)
                )

                self._expand_buffer_to_current_buffer(tmp_buffer)

        self._del_buffer(aperture_buffer.id_str)
        return node

    def on_adc(self, node: ADC) -> ADC:
        """Handle `AD` circle node."""
        self.on_ad(node)
        aperture_buffer = self._create_aperture_buffer(node.aperture_id)

        aperture_buffer.append_shape(
            Shape.new_circle(
                (0.0, 0.0),
                node.diameter,
                is_negative=False,
            )
        )
        if node.hole_diameter is not None and node.hole_diameter > 0:
            aperture_buffer.append_shape(
                Shape.new_circle(
                    (0.0, 0.0),
                    min(node.hole_diameter, node.diameter),
                    is_negative=True,
                )
            )
        return node

    def _create_aperture_buffer(self, aperture_id: ApertureIdStr) -> CommandBuffer:
        buffer = CommandBuffer(
            aperture_id,
            box=None,
            origin=Vector(x=0, y=0),
            commands=[],
            depends_on=set(),
            resolved_dependencies=[],
        )
        self._set_buffer(buffer)

        return buffer

    def on_adr(self, node: ADR) -> ADR:
        """Handle `AD` rectangle node."""
        self.on_ad(node)
        aperture_buffer = self._create_aperture_buffer(node.aperture_id)

        aperture_buffer.append_shape(
            Shape.new_rectangle(
                (0.0, 0.0),
                node.width,
                node.height,
                is_negative=False,
            )
        )
        if node.hole_diameter is not None and node.hole_diameter > 0:
            aperture_buffer.append_shape(
                Shape.new_circle(
                    (0.0, 0.0),
                    min(node.hole_diameter, node.width, node.height),
                    is_negative=True,
                )
            )
        return node

    def on_ado(self, node: ADO) -> ADO:
        """Handle `AD` obround node."""
        self.on_ad(node)
        aperture_buffer = self._create_aperture_buffer(node.aperture_id)

        aperture_buffer.append_shape(
            Shape.new_obround(
                (0.0, 0.0),
                node.width,
                node.height,
                is_negative=False,
            )
        )
        if node.hole_diameter is not None and node.hole_diameter > 0:
            aperture_buffer.append_shape(
                Shape.new_circle(
                    (0.0, 0.0),
                    min(node.hole_diameter, node.width, node.height),
                    is_negative=True,
                )
            )
        return node

    def on_adp(self, node: ADP) -> ADP:
        """Handle `AD` polygon node."""
        self.on_ad(node)
        aperture_buffer = self._create_aperture_buffer(node.aperture_id)

        aperture_buffer.append_shape(
            Shape.new_polygon(
                (0.0, 0.0),
                node.outer_diameter,
                node.vertices,
                node.rotation or 0.0,
                is_negative=False,
            )
        )
        if node.hole_diameter is not None and node.hole_diameter > 0:
            aperture_buffer.append_shape(
                Shape.new_circle(
                    (0.0, 0.0),
                    min(node.hole_diameter, node.outer_diameter),
                    is_negative=True,
                )
            )
        return node

    def on_ad_macro(self, node: ADmacro) -> ADmacro:
        """Handle `AD` macro node."""
        self.on_ad(node)
        aperture_buffer = self._create_aperture_buffer(node.aperture_id)

        if node.params is None:
            scope = {}
        else:
            scope = {f"${i + 1}": param for i, param in enumerate(node.params)}

        macro = self.state.apertures.macros.get(node.name)
        if macro is None:
            raise MacroNotDefinedError(node.name)

        macro.visit(MacroEvalVisitor(self, aperture_buffer, scope))
        return node

    def on_draw_line(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in linear interpolation mode."""
        start_x = self.state.current_x
        start_y = self.state.current_y
        start_point = (start_x, start_y)

        end_x = self.coordinate_x
        end_y = self.coordinate_y
        end_point = (end_x, end_y)

        thickness = self._get_line_thickness(
            Vector.from_tuple((self.coordinate_x, self.coordinate_y))
        )
        self._append_shape_to_current_buffer(
            Shape.new_circle(
                start_point,
                thickness,
                is_negative=self.is_negative,
            )
        )
        self._append_shape_to_current_buffer(
            Shape.new_line(
                start_point,
                end_point,
                thickness=thickness,
                is_negative=self.is_negative,
            ),
        )
        self._append_shape_to_current_buffer(
            Shape.new_circle(
                end_point,
                thickness,
                is_negative=self.is_negative,
            )
        )

    def on_draw_cw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in clockwise circular interpolation multi quadrant mode."""
        self._on_draw_arc_mq(Shape.new_cw_arc)

    def _on_draw_arc_mq(self, factory_method: _ArcFactory) -> None:
        start_x = self.state.current_x
        start_y = self.state.current_y
        start_point = (start_x, start_y)

        end_x = self.coordinate_x
        end_y = self.coordinate_y
        end_point = (end_x, end_y)

        center_x = start_x + self.coordinate_i
        center_y = start_y + self.coordinate_j
        center = (center_x, center_y)

        thickness = self._get_line_thickness(
            Vector.from_tuple((self.coordinate_x, self.coordinate_y))
        )

        self._append_shape_to_current_buffer(
            Shape.new_circle(
                start_point,
                thickness,
                is_negative=self.is_negative,
            )
        )
        if start_point == end_point:
            length = Vector(x=self.coordinate_i, y=self.coordinate_j).length()
            start_point = (center_x - length, center_y)
            end_point = (center_x + length, center_y)

            self._append_shape_to_current_buffer(
                factory_method(
                    start_point,
                    end_point,
                    center,
                    thickness,
                    is_negative=self.is_negative,
                )
            )
            self._append_shape_to_current_buffer(
                factory_method(
                    end_point,
                    start_point,
                    center,
                    thickness,
                    is_negative=self.is_negative,
                )
            )

        else:
            self._append_shape_to_current_buffer(
                factory_method(
                    start_point,
                    end_point,
                    center,
                    thickness,
                    is_negative=self.is_negative,
                )
            )
        self._append_shape_to_current_buffer(
            Shape.new_circle(
                end_point,
                thickness,
                is_negative=self.is_negative,
            )
        )

    def on_draw_ccw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in counter-clockwise circular interpolation multi quadrant
        mode.
        """
        self._on_draw_arc_mq(Shape.new_ccw_arc)

    def on_start_region(self) -> None:
        """Handle start of region."""
        super().on_start_region()
        self._contour_buffer = []

    def on_in_region_draw_line(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in linear interpolation mode in region."""
        if self._contour_buffer is None:
            raise ContourBufferNotSetError

        start_x = self.state.current_x
        start_y = self.state.current_y
        start_point = (start_x, start_y)

        end_x = self.coordinate_x
        end_y = self.coordinate_y
        end_point = (end_x, end_y)

        self._contour_buffer.append(Line.from_tuples(start_point, end_point))

    def on_in_region_draw_cw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in clockwise circular interpolation multi quadrant mode
        within region statement.
        """
        self._on_in_region_draw_arc_mq(is_clockwise=True)

    def _on_in_region_draw_arc_mq(self, *, is_clockwise: bool) -> None:
        if self._contour_buffer is None:
            raise ContourBufferNotSetError

        start_x = self.state.current_x
        start_y = self.state.current_y
        start_point = (start_x, start_y)

        end_x = self.coordinate_x
        end_y = self.coordinate_y
        end_point = (end_x, end_y)

        center_x = start_x + self.coordinate_i
        center_y = start_y + self.coordinate_j
        center = (center_x, center_y)

        if start_point == end_point:
            end_point = (center_x + self.coordinate_i, center_y + self.coordinate_j)

            self._contour_buffer.append(
                Arc.from_tuples(
                    start_point,
                    end_point,
                    center,
                    clockwise=is_clockwise,
                )
            )
            self._contour_buffer.append(
                Arc.from_tuples(
                    end_point,
                    start_point,
                    center,
                    clockwise=is_clockwise,
                )
            )

        else:
            self._contour_buffer.append(
                Arc.from_tuples(
                    start_point,
                    end_point,
                    center,
                    clockwise=is_clockwise,
                )
            )

    def on_in_region_draw_ccw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
        """Handle `D01` node in counter-clockwise circular interpolation multi quadrant
        mode within region statement.
        """
        self._on_in_region_draw_arc_mq(is_clockwise=False)

    def on_flush_region(self) -> None:
        """Handle flush region after D02 command or after G37."""
        if self._contour_buffer is None:
            raise ContourBufferNotSetError

        if len(self._contour_buffer) > 0:
            self._append_shape_to_current_buffer(
                Shape(commands=self._contour_buffer, is_negative=self.is_negative)
            )

        self._contour_buffer = []

    def on_end_region(self) -> None:
        """Handle end of region."""
        super().on_end_region()
        self._contour_buffer = None

    def on_flash_circle(self, node: D03, aperture: ADC) -> None:  # noqa: ARG002
        """Handle `D03` node with `ADC` aperture."""
        self._on_flash_aperture(aperture.aperture_id)

    def _on_flash_aperture(self, aperture_id: ApertureIdStr) -> None:
        buffer = self._get_aperture_buffer(aperture_id)

        self._append_paste_to_current_buffer(
            PasteLayer(
                source_layer_id=buffer.layer_id,
                center=Vector(x=self.coordinate_x, y=self.coordinate_y),
                is_negative=self.is_negative,
            ),
        )

    def _get_aperture_buffer(self, aperture_id: str) -> CommandBuffer:
        transform = self.state.transform

        mirroring_matrix = Matrix3x3.new_reflect(**transform.mirroring.kwargs)
        rotation_matrix = Matrix3x3.new_rotate(transform.rotation)
        scale_matrix = Matrix3x3.new_scale(transform.scaling, transform.scaling)

        transform_matrix = mirroring_matrix @ rotation_matrix @ scale_matrix

        return self._get_buffer_with_transform(aperture_id, transform_matrix)

    def _get_buffer_with_transform(
        self, aperture_id: str, transform_matrix: Matrix3x3
    ) -> CommandBuffer:
        layer_id = f"{aperture_id}%{transform_matrix.tag}"
        buffer = self._get_buffer_opt(layer_id)

        if buffer is None:
            aperture_base_buffer = self._get_buffer(aperture_id)
            buffer = self._apply_transform_to_buffer(
                aperture_base_buffer, layer_id, transform_matrix
            )
            assert buffer.id_str == layer_id
            self._set_buffer(buffer)

        return buffer

    def _apply_transform_to_buffer(
        self, buffer: CommandBuffer, layer_id: str, transform_matrix: Matrix3x3
    ) -> CommandBuffer:
        commands: list[DrawCmdT] = []
        depends_on: set[str] = set()

        for cmd in buffer.commands:
            if isinstance(cmd, Shape):
                commands.append(cmd.transform(transform_matrix))

            elif isinstance(cmd, PasteLayer):
                aperture_buffer = self._get_aperture_buffer(cmd.source_layer_id.id)
                depends_on.add(aperture_buffer.id_str)
                commands.append(
                    PasteLayer(
                        source_layer_id=LayerID(id=aperture_buffer.id_str),
                        center=cmd.center.transform(transform_matrix),
                        is_negative=cmd.is_negative,
                    )
                )

            else:
                raise NotImplementedError(type(cmd))

        return CommandBuffer(
            layer_id,
            None,
            origin=buffer.origin,
            commands=commands,
            depends_on=depends_on,
            resolved_dependencies=[],
        )

    def on_flash_rectangle(self, node: D03, aperture: ADR) -> None:  # noqa: ARG002
        """Handle `D03` node with `ADC` aperture."""
        self._on_flash_aperture(aperture.aperture_id)

    def on_flash_obround(self, node: D03, aperture: ADO) -> None:  # noqa: ARG002
        """Handle `D03` node with `ADO` aperture."""
        self._on_flash_aperture(aperture.aperture_id)

    def on_flash_polygon(self, node: D03, aperture: ADP) -> None:  # noqa: ARG002
        """Handle `D03` node with `ADP` aperture."""
        self._on_flash_aperture(aperture.aperture_id)

    def on_flash_macro(self, node: D03, aperture: ADmacro) -> None:  # noqa: ARG002
        """Handle `D03` node with `ADM` aperture."""
        self._on_flash_aperture(aperture.aperture_id)

    def on_flash_block(self, node: D03, aperture: AB) -> None:  # noqa: ARG002
        """Handle `D03` node with `AB` aperture."""
        aperture_id = aperture.open.aperture_id
        buffer = self._get_aperture_buffer(aperture_id)
        tmp_buffer = self._apply_transform_to_buffer_non_recursive_tmp(
            buffer, Matrix3x3.new_translate(x=self.coordinate_x, y=self.coordinate_y)
        )

        self._expand_buffer_to_current_buffer(tmp_buffer)

    def _apply_transform_to_buffer_non_recursive_tmp(
        self, buffer: CommandBuffer, transform_matrix: Matrix3x3
    ) -> CommandBuffer:
        commands: list[DrawCmdT] = []
        depends_on: set[str] = set()

        for cmd in buffer.commands:
            if isinstance(cmd, Shape):
                commands.append(cmd.transform(transform_matrix))

            elif isinstance(cmd, PasteLayer):
                depends_on.add(cmd.source_layer_id.id)
                commands.append(
                    PasteLayer(
                        source_layer_id=cmd.source_layer_id,
                        center=cmd.center.transform(transform_matrix),
                        is_negative=cmd.is_negative,
                    )
                )

            else:
                raise NotImplementedError(type(cmd))

        return CommandBuffer(
            "%temporary%",
            None,
            origin=buffer.origin,
            commands=commands,
            depends_on=depends_on,
            resolved_dependencies=[],
        )

    def _resolve_buffer_submit_order(self) -> list[CommandBuffer]:
        buffer_submit_order: list[str] = []

        def _(buffer: CommandBuffer) -> None:
            for dependency_id in buffer.depends_on:
                dependency = self._get_buffer(dependency_id)
                _(dependency)

                if buffer.id_str in buffer_submit_order:
                    raise CyclicBufferDependencyError(buffer, dependency)

            buffer_submit_order.append(buffer.id_str)

        _(self._get_buffer(self.MAIN_BUFFER_ID))

        return [self._get_buffer(id_) for id_ in buffer_submit_order]

    def _convert_buffers_to_rvmc(self) -> RVMC:
        commands: list[Command] = []
        buffer_submit_order = self._resolve_buffer_submit_order()

        for buffer in buffer_submit_order:
            commands.append(StartLayer(id=LayerID(id=buffer.id_str), box=buffer.box))
            commands.extend(buffer.commands)
            commands.append(EndLayer())

        return RVMC(commands=commands)

    def compile(self, ast: File) -> RVMC:
        """Compile Gerber AST to RVMC."""
        ast.visit(self)

        return self._convert_buffers_to_rvmc()

on_ab

on_ab(node: AB) -> AB

Handle AB node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_ab(self, node: AB) -> AB:
    """Handle `AB` node."""
    aperture_buffer = self._create_aperture_buffer(node.open.aperture_id)
    self._push_buffer(aperture_buffer.id_str)

    super().on_ab(node)

    self._pop_buffer()

    return node

on_sr

on_sr(node: SR) -> SR

Handle SR node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_sr(self, node: SR) -> SR:
    """Handle `SR` node."""
    aperture_buffer = self._create_aperture_buffer(
        ApertureIdStr(f"%%SR%{id(node)}%{time.time():.0f}")
    )
    self._push_buffer(aperture_buffer.id_str)

    super().on_sr(node)

    self._pop_buffer()

    buffer = self._get_buffer(aperture_buffer.id_str)

    x_delta = node.open.x_delta
    y_delta = node.open.y_delta

    for x in range(node.open.x_repeats):
        for y in range(node.open.y_repeats):
            x_coordinate = self.coordinate_x + (x * x_delta)
            y_coordinate = self.coordinate_y + (y * y_delta)
            tmp_buffer = self._apply_transform_to_buffer_non_recursive_tmp(
                buffer, Matrix3x3.new_translate(x=x_coordinate, y=y_coordinate)
            )

            self._expand_buffer_to_current_buffer(tmp_buffer)

    self._del_buffer(aperture_buffer.id_str)
    return node

on_adc

on_adc(node: ADC) -> ADC

Handle AD circle node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_adc(self, node: ADC) -> ADC:
    """Handle `AD` circle node."""
    self.on_ad(node)
    aperture_buffer = self._create_aperture_buffer(node.aperture_id)

    aperture_buffer.append_shape(
        Shape.new_circle(
            (0.0, 0.0),
            node.diameter,
            is_negative=False,
        )
    )
    if node.hole_diameter is not None and node.hole_diameter > 0:
        aperture_buffer.append_shape(
            Shape.new_circle(
                (0.0, 0.0),
                min(node.hole_diameter, node.diameter),
                is_negative=True,
            )
        )
    return node

on_adr

on_adr(node: ADR) -> ADR

Handle AD rectangle node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_adr(self, node: ADR) -> ADR:
    """Handle `AD` rectangle node."""
    self.on_ad(node)
    aperture_buffer = self._create_aperture_buffer(node.aperture_id)

    aperture_buffer.append_shape(
        Shape.new_rectangle(
            (0.0, 0.0),
            node.width,
            node.height,
            is_negative=False,
        )
    )
    if node.hole_diameter is not None and node.hole_diameter > 0:
        aperture_buffer.append_shape(
            Shape.new_circle(
                (0.0, 0.0),
                min(node.hole_diameter, node.width, node.height),
                is_negative=True,
            )
        )
    return node

on_ado

on_ado(node: ADO) -> ADO

Handle AD obround node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_ado(self, node: ADO) -> ADO:
    """Handle `AD` obround node."""
    self.on_ad(node)
    aperture_buffer = self._create_aperture_buffer(node.aperture_id)

    aperture_buffer.append_shape(
        Shape.new_obround(
            (0.0, 0.0),
            node.width,
            node.height,
            is_negative=False,
        )
    )
    if node.hole_diameter is not None and node.hole_diameter > 0:
        aperture_buffer.append_shape(
            Shape.new_circle(
                (0.0, 0.0),
                min(node.hole_diameter, node.width, node.height),
                is_negative=True,
            )
        )
    return node

on_adp

on_adp(node: ADP) -> ADP

Handle AD polygon node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_adp(self, node: ADP) -> ADP:
    """Handle `AD` polygon node."""
    self.on_ad(node)
    aperture_buffer = self._create_aperture_buffer(node.aperture_id)

    aperture_buffer.append_shape(
        Shape.new_polygon(
            (0.0, 0.0),
            node.outer_diameter,
            node.vertices,
            node.rotation or 0.0,
            is_negative=False,
        )
    )
    if node.hole_diameter is not None and node.hole_diameter > 0:
        aperture_buffer.append_shape(
            Shape.new_circle(
                (0.0, 0.0),
                min(node.hole_diameter, node.outer_diameter),
                is_negative=True,
            )
        )
    return node

on_ad_macro

on_ad_macro(node: ADmacro) -> ADmacro

Handle AD macro node.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_ad_macro(self, node: ADmacro) -> ADmacro:
    """Handle `AD` macro node."""
    self.on_ad(node)
    aperture_buffer = self._create_aperture_buffer(node.aperture_id)

    if node.params is None:
        scope = {}
    else:
        scope = {f"${i + 1}": param for i, param in enumerate(node.params)}

    macro = self.state.apertures.macros.get(node.name)
    if macro is None:
        raise MacroNotDefinedError(node.name)

    macro.visit(MacroEvalVisitor(self, aperture_buffer, scope))
    return node

on_draw_line

on_draw_line(node: D01) -> None

Handle D01 node in linear interpolation mode.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_draw_line(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in linear interpolation mode."""
    start_x = self.state.current_x
    start_y = self.state.current_y
    start_point = (start_x, start_y)

    end_x = self.coordinate_x
    end_y = self.coordinate_y
    end_point = (end_x, end_y)

    thickness = self._get_line_thickness(
        Vector.from_tuple((self.coordinate_x, self.coordinate_y))
    )
    self._append_shape_to_current_buffer(
        Shape.new_circle(
            start_point,
            thickness,
            is_negative=self.is_negative,
        )
    )
    self._append_shape_to_current_buffer(
        Shape.new_line(
            start_point,
            end_point,
            thickness=thickness,
            is_negative=self.is_negative,
        ),
    )
    self._append_shape_to_current_buffer(
        Shape.new_circle(
            end_point,
            thickness,
            is_negative=self.is_negative,
        )
    )

on_draw_cw_arc_mq

on_draw_cw_arc_mq(node: D01) -> None

Handle D01 node in clockwise circular interpolation multi quadrant mode.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_draw_cw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in clockwise circular interpolation multi quadrant mode."""
    self._on_draw_arc_mq(Shape.new_cw_arc)

on_draw_ccw_arc_mq

on_draw_ccw_arc_mq(node: D01) -> None

Handle D01 node in counter-clockwise circular interpolation multi quadrant mode.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_draw_ccw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in counter-clockwise circular interpolation multi quadrant
    mode.
    """
    self._on_draw_arc_mq(Shape.new_ccw_arc)

on_start_region

on_start_region() -> None

Handle start of region.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_start_region(self) -> None:
    """Handle start of region."""
    super().on_start_region()
    self._contour_buffer = []

on_in_region_draw_line

on_in_region_draw_line(node: D01) -> None

Handle D01 node in linear interpolation mode in region.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_in_region_draw_line(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in linear interpolation mode in region."""
    if self._contour_buffer is None:
        raise ContourBufferNotSetError

    start_x = self.state.current_x
    start_y = self.state.current_y
    start_point = (start_x, start_y)

    end_x = self.coordinate_x
    end_y = self.coordinate_y
    end_point = (end_x, end_y)

    self._contour_buffer.append(Line.from_tuples(start_point, end_point))

on_in_region_draw_cw_arc_mq

on_in_region_draw_cw_arc_mq(node: D01) -> None

Handle D01 node in clockwise circular interpolation multi quadrant mode within region statement.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_in_region_draw_cw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in clockwise circular interpolation multi quadrant mode
    within region statement.
    """
    self._on_in_region_draw_arc_mq(is_clockwise=True)

on_in_region_draw_ccw_arc_mq

on_in_region_draw_ccw_arc_mq(node: D01) -> None

Handle D01 node in counter-clockwise circular interpolation multi quadrant mode within region statement.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_in_region_draw_ccw_arc_mq(self, node: D01) -> None:  # noqa: ARG002
    """Handle `D01` node in counter-clockwise circular interpolation multi quadrant
    mode within region statement.
    """
    self._on_in_region_draw_arc_mq(is_clockwise=False)

on_flush_region

on_flush_region() -> None

Handle flush region after D02 command or after G37.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flush_region(self) -> None:
    """Handle flush region after D02 command or after G37."""
    if self._contour_buffer is None:
        raise ContourBufferNotSetError

    if len(self._contour_buffer) > 0:
        self._append_shape_to_current_buffer(
            Shape(commands=self._contour_buffer, is_negative=self.is_negative)
        )

    self._contour_buffer = []

on_end_region

on_end_region() -> None

Handle end of region.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_end_region(self) -> None:
    """Handle end of region."""
    super().on_end_region()
    self._contour_buffer = None

on_flash_circle

on_flash_circle(node: D03, aperture: ADC) -> None

Handle D03 node with ADC aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_circle(self, node: D03, aperture: ADC) -> None:  # noqa: ARG002
    """Handle `D03` node with `ADC` aperture."""
    self._on_flash_aperture(aperture.aperture_id)

on_flash_rectangle

on_flash_rectangle(node: D03, aperture: ADR) -> None

Handle D03 node with ADC aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_rectangle(self, node: D03, aperture: ADR) -> None:  # noqa: ARG002
    """Handle `D03` node with `ADC` aperture."""
    self._on_flash_aperture(aperture.aperture_id)

on_flash_obround

on_flash_obround(node: D03, aperture: ADO) -> None

Handle D03 node with ADO aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_obround(self, node: D03, aperture: ADO) -> None:  # noqa: ARG002
    """Handle `D03` node with `ADO` aperture."""
    self._on_flash_aperture(aperture.aperture_id)

on_flash_polygon

on_flash_polygon(node: D03, aperture: ADP) -> None

Handle D03 node with ADP aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_polygon(self, node: D03, aperture: ADP) -> None:  # noqa: ARG002
    """Handle `D03` node with `ADP` aperture."""
    self._on_flash_aperture(aperture.aperture_id)

on_flash_macro

on_flash_macro(node: D03, aperture: ADmacro) -> None

Handle D03 node with ADM aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_macro(self, node: D03, aperture: ADmacro) -> None:  # noqa: ARG002
    """Handle `D03` node with `ADM` aperture."""
    self._on_flash_aperture(aperture.aperture_id)

on_flash_block

on_flash_block(node: D03, aperture: AB) -> None

Handle D03 node with AB aperture.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def on_flash_block(self, node: D03, aperture: AB) -> None:  # noqa: ARG002
    """Handle `D03` node with `AB` aperture."""
    aperture_id = aperture.open.aperture_id
    buffer = self._get_aperture_buffer(aperture_id)
    tmp_buffer = self._apply_transform_to_buffer_non_recursive_tmp(
        buffer, Matrix3x3.new_translate(x=self.coordinate_x, y=self.coordinate_y)
    )

    self._expand_buffer_to_current_buffer(tmp_buffer)

compile

compile(ast: File) -> RVMC

Compile Gerber AST to RVMC.

Source code in src/pygerber/gerberx3/compiler/compiler.py
def compile(self, ast: File) -> RVMC:
    """Compile Gerber AST to RVMC."""
    ast.visit(self)

    return self._convert_buffers_to_rvmc()

CompilerError

Bases: Exception

Base class for all exceptions raised by Compiler class.

Source code in src/pygerber/gerberx3/compiler/errors.py
class CompilerError(Exception):
    """Base class for all exceptions raised by Compiler class."""

CyclicBufferDependencyError

Bases: CompilerError

Raised when cyclic dependency between buffers is detected.

Source code in src/pygerber/gerberx3/compiler/errors.py
class CyclicBufferDependencyError(CompilerError):
    """Raised when cyclic dependency between buffers is detected."""

    def __init__(
        self, parent_buffer: CommandBuffer, child_buffer: CommandBuffer
    ) -> None:
        super().__init__(
            f"Cyclic dependency between buffers {parent_buffer.id_str} and "
            f"{child_buffer.id_str} detected. Cyclic dependencies are not allowed."
        )
        self.parent_buffer = parent_buffer
        self.child_buffer = child_buffer

compile

compile(ast: Any, **options: Any) -> RVMC

Compile GerberX3 AST to RVMC code.

Source code in src/pygerber/gerberx3/compiler/__init__.py
def compile(ast: Any, **options: Any) -> RVMC:  # noqa: A001
    """Compile GerberX3 AST to RVMC code."""
    return Compiler(**options).compile(ast)