PCB Environment 2
Loading...
Searching...
No Matches
PCBoard.hpp
1
2#ifndef GYM_PCB_PCBOARD_H
3#define GYM_PCB_PCBOARD_H
4
5#include "Units.hpp"
6#include "Geometry.hpp"
7#include <map>
8#include <vector>
9#include <future>
10#include <shared_mutex>
11#include <atomic>
12#include "LayoutArea.hpp"
13#include "Layer.hpp"
14#include "NavGrid.hpp"
15#include "BVH.hpp"
16
17class CloneEnv;
18class Component;
19class Pin;
20class Net;
21class Connection;
23
24#define PCB_CHANGED_NAV_GRID (1 << 0)
25#define PCB_CHANGED_NAV_TRIS (1 << 1)
26#define PCB_CHANGED_ROUTES (1 << 2)
27#define PCB_CHANGED_COMPONENTS (1 << 3)
28#define PCB_CHANGED_PINS (1 << 4)
29#define PCB_CHANGED_NET_COLORS (1 << 5)
30#define PCB_CHANGED_OBJECTS (1 << 6) // use after add/remove(Component)
31#define PCB_CHANGED_NEW_BOARD (1 << 15)
32
33#define PCB_CHANGED_OBJECTS_SET (PCB_CHANGED_OBJECTS | PCB_CHANGED_COMPONENTS | PCB_CHANGED_PINS | PCB_CHANGED_ROUTES)
34
35class PCBoard
36{
37public:
38 PCBoard(const Nanometers UnitLength = Nanometers(163840.0));
39 PCBoard *clone(CloneEnv&) const;
40 ~PCBoard();
41
42 const std::string& name() const { return mName; }
43 const std::string& getSourceFilePath() const { return mSourceFilePath; }
44
45 uint getNumLayers() const { return mLayers.size(); }
46 const Layer& getLayer(uint i) const { return mLayers.at(i); }
47
48 LayoutArea& getLayoutArea() { return mLayoutArea; }
49 const LayoutArea& getLayoutArea() const { return mLayoutArea; }
50
51 PCBoard& operator=(const PCBoard&) = delete;
52
53 Net *getNet(const std::string &name) const;
54 Net *getNet(uint) const;
55 Net *getNet(PyObject *) const;
56 Component *getComponent(const std::string &name) const;
57 Component *getComponent(uint) const;
58 Component *getComponent(PyObject *) const;
59 Component *getComponentAt(const Point_2&, int z = -1) const; // -1 for all layers
60 Pin *getPin(const std::string &name) const;
61 Pin *getPin(PyObject *) const;
62 Connection *getConnection(PyObject *) const;
63
64 uint getNumNets() const { return mNets.size(); }
65 const std::vector<Net *>& getNets() const { return mNets; }
66
67 uint getNumComponents() const { return mParts.size(); }
68 const std::vector<Component *>& getComponents() const { return mParts; }
69 void renumberComs();
70 void renumberPins();
71 void renumberNets();
72
73 const ObjectsBVH *getBVH() const { return mBVH; }
74
75 const NavGrid& getNavGrid() const { return mNavGrid; }
76 NavGrid& getNavGrid() { return mNavGrid; }
77 NavTriangulation *getTNG() const { return mTNG; }
78 bool rebuildTNG();
79
82
85 void finiPathfindingForNet(const Connection&);
86 void initPathfindingFor(const Connection&);
87 void finiPathfindingFor(const Connection&);
88
89 void add(Component&);
90 void add(Net&);
91 Component *remove(Component&);
92 Net *remove(Net&);
93
94 void pruneLayers(std::set<uint> &keep);
95 void pruneConnections(std::set<Connection *> &keep, bool deleteNets = true);
96 void pruneNets(std::set<Net *> &keep);
97 void prunePins(std::set<Pin *> &keep);
98 void pruneComponents(std::set<Component *> &keep);
99 void removeEmptyNets();
100
101 Real metersToUnits(Nanometers nm) const { return nm.value() / mUnitLength_nm.value(); }
102 Nanometers unitsToNanometers(Real r) const { return mUnitLength_nm * r; }
103 Micrometers unitsToMicrometers(Real r) const { return Micrometers(mUnitLength_nm * r); }
104
105 uint getLayerForSide(char t_or_b) const;
106 const std::vector<Layer>& getLayers() const { return mLayers; }
107 Layer& getLayer(uint i) { return mLayers[i]; }
108
109 void setName(const std::string &name) { mName = name; }
110 void setSourceFilePath(const std::string &path) { mSourceFilePath = path; }
112 void setNumLayers(uint N);
113
122 void adjustLayoutAreaMargins(Real min, Real max = std::numeric_limits<Real>::infinity());
123
124 void setMinViaDiameter(Micrometers);
125 Real getMinViaDiameter() const { return mTech.vias.minDiameter; }
126
127 void setMinClearancePins(Micrometers);
128 Real getMinClearancePins() const { return mTech.pins.minClearance; }
129 void setMinClearanceNets(Micrometers);
130 Real getMinClearanceNets() const { return mTech.nets.minClearance; }
131 void setMinTrackWidth(Micrometers);
132 Real getMinTrackWidth() const { return mTech.nets.minTrackWidth; }
133
134 Bbox_2 getComponentAreaBbox() const { return mComponentAreaBbox; }
135 void recomputeComponentAreaBbox();
136 Bbox_2 getActiveAreaBbox() const { return mActiveAreaBbox; }
137 void setActiveAreaBbox(const Bbox_2 &box) { mActiveAreaBbox = box; }
138
143 bool runPathFinding(Connection&, Connection *ref = 0, const AStarCosts * = 0);
144 bool runPathFindingTri(Connection&);
145
146 void rasterizeTracks(const Connection&, int8_t count = 1);
147 void unrasterizeTracks(const Connection&, int8_t count = -1);
148 void eraseTracks(Connection&);
149 void wipe();
150
151 Real sumViolationArea(Connection&, Connection *ref = 0);
152
153 PyObject *getPy(PyObject *dict, uint depth = 3, bool asNumpy = true) const;
154 PyObject *getLayersPy() const;
155
159 std::mutex& getLock() const { return mLock; }
160 bool hasChanged() const { return mHasChanged; }
161 void setChanged(uint32_t mask) { mHasChanged |= mask; }
162 uint32_t ackChanges() const { return mHasChanged.exchange(0); }
163
164private:
165 LayoutArea mLayoutArea;
166 std::string mName;
167 std::string mSourceFilePath;
168 std::vector<Component *> mParts;
169 Bbox_2 mComponentAreaBbox;
170 std::vector<Net *> mNets;
171 std::vector<Layer> mLayers;
172 std::map<std::string, Component *> mPartsByName;
173 std::map<std::string, Net *> mNetsByName;
174 Point_2 mLayoutAreaOrigin;
175 Bbox_2 mLayoutAreaBbox;
176 NavGrid mNavGrid;
177 NavTriangulation *mTNG{0};
178 ObjectsBVH *mBVH{0};
179 Bbox_2 mActiveAreaBbox;
180 struct {
181 struct {
182 Real minDiameter{0.0};
183 } vias;
184 struct {
185 Real minClearance{0.0};
186 } pins;
187 struct {
188 Real minClearance{0.0};
189 Real minTrackWidth{0.0};
190 } nets;
191 } mTech;
192 const Nanometers mUnitLength_nm;
193 mutable std::atomic<uint32_t> mHasChanged{0};
194 mutable std::mutex mLock;
195
196private:
197 void rebuildBVH();
198 void removedLayersUpdate(uint removedZMin, uint removedZMax);
199};
200
201inline uint PCBoard::getLayerForSide(char side) const
202{
203 if (side == 't')
204 return 0;
205 assert(side == 'b');
206 return mLayers.size() - 1;
207}
208
209#endif // GYM_PCB_PCBOARD_H
Definition Clone.hpp:17
Definition Component.hpp:18
Definition Connection.hpp:17
Definition Layer.hpp:10
Definition LayoutArea.hpp:9
The grid-representation of the board.
Definition NavGrid.hpp:61
Interface to the Delaunay triangulation.
Definition NavTriangulation.hpp:25
Definition Net.hpp:19
Definition BVH.hpp:11
void adjustLayoutAreaMargins(Real min, Real max=std::numeric_limits< Real >::infinity())
void setNumLayers(uint N)
NOTE: Deletes objects on no-longer existing layers.
void forceConnectionsToGrid()
Move all connection endpoints to the nearest gridcell center.
void initPathfindingForNet(const Connection &)
These unblock/restore pins and tracks associated with this connection.
bool runPathFinding(Connection &, Connection *ref=0, const AStarCosts *=0)
std::mutex & getLock() const
Definition PCBoard.hpp:159
Definition Pin.hpp:18
Definition NavGrid.hpp:47