Horizon
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 
11 namespace horizon {
12 
13 class GerberWriter {
14 public:
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_pads();
23  void write_regions();
24  unsigned int get_or_create_aperture_circle(uint64_t diameter);
25  // unsigned int get_or_create_aperture_padstack(const class Padstack *ps,
26  // int layer, )
27  void draw_line(const Coordi &from, const Coordi &to, uint64_t width);
28  void draw_padstack(const Padstack &ps, int layer, const Placement &transform);
29  void draw_region(const ClipperLib::Path &path, bool dark = true, int prio = 0);
30  const std::string &get_filename();
31 
32 private:
33  class Line {
34  public:
35  Line(const Coordi &f, const Coordi &t, unsigned int ap) : from(f), to(t), aperture(ap)
36  {
37  }
38  Coordi from;
39  Coordi to;
40  unsigned int aperture;
41  };
42 
43  class Region {
44  public:
45  Region(const ClipperLib::Path &p, bool d = true, int prio = 0) : path(p), dark(d), priority(prio){};
46  ClipperLib::Path path;
47  bool dark;
48  int priority;
49  };
50 
51  class ApertureMacro {
52  public:
53  class Primitive {
54  public:
55  enum class Code { CIRCLE = 1, CENTER_LINE = 21, OUTLINE = 4 };
56  const Code code;
57  std::vector<int64_t> modifiers;
58  Primitive(Code c) : code(c)
59  {
60  }
61  virtual ~Primitive()
62  {
63  }
64  };
65 
66  class PrimitiveCircle : public Primitive {
67  public:
68  PrimitiveCircle() : Primitive(Code::CIRCLE){};
69  int64_t diameter = 0;
70  Coordi center;
71  };
72 
73  class PrimitiveCenterLine : public Primitive {
74  public:
75  PrimitiveCenterLine() : Primitive(Code::CENTER_LINE){};
76  int64_t width = 0;
77  int64_t height = 0;
78  int angle = 0;
79  Coordi center;
80  };
81  class PrimitiveOutline : public Primitive {
82  public:
83  PrimitiveOutline() : Primitive(Code::OUTLINE){};
84  std::vector<Coordi> vertices;
85  };
86 
87  ApertureMacro(unsigned int n) : name(n)
88  {
89  }
90 
91  unsigned int name;
92  std::vector<std::unique_ptr<Primitive>> primitives;
93  };
94 
95  std::ofstream ofs;
96  std::string out_filename;
97  void check_open();
98  std::map<uint64_t, unsigned int> apertures_circle;
99  std::map<std::tuple<UUID, std::string, int, bool>, ApertureMacro> apertures_macro;
100 
101  unsigned int aperture_n = 10;
102 
103  std::deque<Line> lines;
104  std::deque<Region> regions;
105  std::deque<std::pair<unsigned int, Coordi>> pads;
106  void write_decimal(int64_t x, bool comma = true);
107 };
108 } // namespace horizon
Definition: gerber_writer.hpp:53
Definition: placement.hpp:8
Definition: padstack.hpp:21
Definition: gerber_writer.hpp:13
Definition: block.cpp:9
Your typical coordinate class.
Definition: common.hpp:71