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
17 class CloneEnv;
18 class Component;
19 class Pin;
20 class Net;
21 class Connection;
22 class NavTriangulation;
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
35 class PCBoard
36{
37 public:
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
81 void forceConnectionsToGrid();
82
84 void initPathfindingForNet(const Connection&);
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
164 private:
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
196 private:
197 void rebuildBVH();
198 void removedLayersUpdate(uint removedZMin, uint removedZMax);
199};
200
201 inline 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
CloneEnv
Definition Clone.hpp:17
Component
Definition Component.hpp:18
Connection
Definition Connection.hpp:17
Layer
Definition Layer.hpp:10
LayoutArea
Definition LayoutArea.hpp:9
NavGrid
The grid-representation of the board.
Definition NavGrid.hpp:61
NavTriangulation
Interface to the Delaunay triangulation.
Definition NavTriangulation.hpp:25
Net
Definition Net.hpp:19
ObjectsBVH
Definition BVH.hpp:11
PCBoard::adjustLayoutAreaMargins
void adjustLayoutAreaMargins(Real min, Real max=std::numeric_limits< Real >::infinity())
PCBoard::setNumLayers
void setNumLayers(uint N)
NOTE: Deletes objects on no-longer existing layers.
PCBoard::forceConnectionsToGrid
void forceConnectionsToGrid()
Move all connection endpoints to the nearest gridcell center.
PCBoard::initPathfindingForNet
void initPathfindingForNet(const Connection &)
These unblock/restore pins and tracks associated with this connection.
PCBoard::runPathFinding
bool runPathFinding(Connection &, Connection *ref=0, const AStarCosts *=0)
PCBoard::getLock
std::mutex & getLock() const
Definition PCBoard.hpp:159
Pin
Definition Pin.hpp:18
AStarCosts
Definition NavGrid.hpp:47