Horizon
Loading...
Searching...
No Matches
gerber_writer.hpp
1#pragma once
2#include "common/common.hpp"
3#include <iostream>
4#include <fstream>
5#include <map>
6#include <deque>
7#include "pool/padstack.hpp"
8#include "util/placement.hpp"
9#include "clipper/clipper.hpp"
10
11namespace horizon {
12
14public:
15 GerberWriter(const std::string &filename);
16 void write_line(const std::string &s);
17 void close();
18 void comment(const std::string &s);
19 void write_format();
20 void write_apertures();
21 void write_lines();
22 void write_arcs();
23 void write_pads();
24 void write_regions();
25 unsigned int get_or_create_aperture_circle(uint64_t diameter);
26 // unsigned int get_or_create_aperture_padstack(const class Padstack *ps,
27 // int layer, )
28 void draw_line(const Coordi &from, const Coordi &to, uint64_t width);
29 void draw_arc(const Coordi &from, const Coordi &to, const Coordi &center, bool flip, uint64_t width);
30 void draw_padstack(const Padstack &ps, int layer, const Placement &transform);
31 void draw_polygon(const ClipperLib::Path &path);
32 void draw_fragments(const ClipperLib::Paths &paths);
33 const std::string &get_filename();
34
35private:
36 class Line {
37 public:
38 Line(const Coordi &f, const Coordi &t, unsigned int ap) : from(f), to(t), aperture(ap)
39 {
40 }
41 Coordi from;
42 Coordi to;
43 unsigned int aperture;
44 };
45 class Arc {
46 public:
47 Arc(const Coordi &f, const Coordi &t, const Coordi &c, bool fl, unsigned int ap)
48 : from(f), to(t), center(c), flip(fl), aperture(ap)
49 {
50 }
51 Coordi from;
52 Coordi to;
53 Coordi center;
54 bool flip = false;
55 unsigned int aperture;
56 };
57
58 class Region {
59 public:
60 Region(const ClipperLib::Path &p, bool d = true, int prio = 0) : path(p), dark(d), priority(prio){};
61 ClipperLib::Path path;
62 bool dark;
63 int priority;
64 };
65
66 class ApertureMacro {
67 public:
68 class Primitive {
69 public:
70 enum class Code { CIRCLE = 1, CENTER_LINE = 21, OUTLINE = 4 };
71 const Code code;
72 std::vector<int64_t> modifiers;
73 Primitive(Code c) : code(c)
74 {
75 }
76 virtual ~Primitive()
77 {
78 }
79 };
80
81 class PrimitiveCircle : public Primitive {
82 public:
83 PrimitiveCircle() : Primitive(Code::CIRCLE){};
84 int64_t diameter = 0;
85 Coordi center;
86 };
87
89 public:
90 PrimitiveCenterLine() : Primitive(Code::CENTER_LINE){};
91 int64_t width = 0;
92 int64_t height = 0;
93 int angle = 0;
94 Coordi center;
95 };
96 class PrimitiveOutline : public Primitive {
97 public:
98 PrimitiveOutline() : Primitive(Code::OUTLINE){};
99 std::vector<Coordi> vertices;
100 };
101
102 ApertureMacro(unsigned int n) : name(n)
103 {
104 }
105
106 unsigned int name;
107 std::vector<std::unique_ptr<Primitive>> primitives;
108 };
109
110 std::ofstream ofs;
111 std::string out_filename;
112 void check_open();
113 std::map<uint64_t, unsigned int> apertures_circle;
114 std::map<std::tuple<UUID, std::string, int, bool>, ApertureMacro> apertures_macro;
115
116 unsigned int aperture_n = 10;
117
118 std::deque<Line> lines;
119 std::deque<Arc> arcs;
120 ClipperLib::Paths fragments;
121 std::deque<ClipperLib::Path> polygons;
122 std::deque<std::pair<unsigned int, Coordi>> pads;
123 void write_decimal(int64_t x, bool comma = true);
124 void write_prim(const ApertureMacro::PrimitiveOutline *prim);
125 void write_prim(const ApertureMacro::PrimitiveCenterLine *prim);
126 void write_polynode(const ClipperLib::PolyNode *node);
127 void write_path(const ClipperLib::Path &path);
128};
129} // namespace horizon
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Definition clipper.hpp:137
Definition gerber_writer.hpp:13
Definition padstack.hpp:19
Definition placement.hpp:8