KiCad PCB EDA Suite
Loading...
Searching...
No Matches
allegro_builder.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright Quilter
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <functional>
24#include <memory>
25#include <optional>
26#include <unordered_map>
27#include <utility>
28#include <vector>
29#include <unordered_set>
30
32#include <convert/allegro_db.h>
33
34#include <board.h>
37#include <reporter.h>
38#include <progress_reporter.h>
40
41
42class BOARD;
43class FOOTPRINT;
44class NETINFO_ITEM;
45class PCB_TEXT;
46
47namespace ALLEGRO
48{
49
50class LAYER_MAPPER;
51
57{
58public:
59 BOARD_BUILDER( const BRD_DB& aBrdDb, BOARD& aBoard, REPORTER& aReporter,
60 PROGRESS_REPORTER* aProgressReporter,
61 const LAYER_MAPPING_HANDLER& aLayerMappingHandler );
63
64 bool BuildBoard();
65
66private:
67 VECTOR2I scale( const VECTOR2I& aVector ) const;
68 VECTOR2I scaleSize( const VECTOR2I& aSize ) const;
69 int scale( int aVal ) const;
70
76 template <ALLEGRO_BLOCK_DATA T>
77 const T* expectBlockByKey( uint32_t aKey ) const
78 {
79 // The type code trait
80 constexpr uint8_t kType = T::BLOCK_TYPE_CODE;
81
82 if( aKey == 0 )
83 return nullptr;
84
85 const BLOCK_BASE* block = m_brdDb.GetObjectByKey( aKey );
86
87 if( !block )
88 {
89 reportMissingBlock( aKey, kType );
90 return nullptr;
91 }
92
93 if( block->GetBlockType() != kType )
94 {
95 reportUnexpectedBlockType( block->GetBlockType(), kType, aKey, block->GetOffset() );
96 return nullptr;
97 }
98
99 return &static_cast<const BLOCK<T>&>( *block ).GetData();
100 }
101
102 void reportMissingBlock( uint32_t aKey, uint8_t aType ) const;
103 void reportUnexpectedBlockType( uint8_t aGot, uint8_t aExpected, uint32_t aKey = 0, size_t aOffset = 0,
104 const wxString& aName = wxEmptyString ) const;
105
106 PCB_LAYER_ID getLayer( const LAYER_INFO& aLayerInfo ) const;
107
109 {
110 size_t operator()( const BOARD_ITEM* aItem ) const;
111 };
112
114 {
115 bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
116 };
117
127 {
128 public:
130 bool IsFirst( const BOARD_ITEM* aItem );
131
132 private:
133 std::unordered_set<const BOARD_ITEM*, GRAPHIC_KEY_HASH, GRAPHIC_KEY_EQ> m_seen;
134 };
135
138
139
146 static void stampIds( BOARD_ITEM& aItem, uint32_t aKey, uint32_t& aSeq );
147
148 static void stampIds( BOARD_ITEM& aItem, uint32_t aKey )
149 {
150 uint32_t seq = 0;
151 stampIds( aItem, aKey, seq );
152 }
153
159 wxString get0x30StringValue( uint32_t a0x30Key ) const;
160
164 std::unique_ptr<PCB_SHAPE> buildLineSegment( const BLK_0x15_16_17_SEGMENT& aSegment, const LAYER_INFO& aLayerInfo,
165 PCB_LAYER_ID aLayer, BOARD_ITEM_CONTAINER& aParent );
166
167 std::unique_ptr<PCB_SHAPE> buildArc( const BLK_0x01_ARC& aArc, const LAYER_INFO& aLayerInfo, PCB_LAYER_ID aLayer,
168 BOARD_ITEM_CONTAINER& aParent );
169
173 std::vector<std::unique_ptr<PCB_SHAPE>> buildShapes( const BLK_0x14_GRAPHIC& aGraphicList, BOARD_ITEM_CONTAINER& aParent );
174 std::unique_ptr<PCB_TEXT> buildPcbText( const BLK_0x30_STR_WRAPPER& aStrWrapper, BOARD_ITEM_CONTAINER& aParent );
175
179 std::unique_ptr<PCB_SHAPE> buildRect( const BLK_0x24_RECT& aRect, BOARD_ITEM_CONTAINER& aParent );
180
184 std::unique_ptr<PCB_SHAPE> buildRect( const BLK_0x0E_RECT& aShape, BOARD_ITEM_CONTAINER& aParent );
185
186
190 std::unique_ptr<PCB_SHAPE> buildPolygon( const BLK_0x28_SHAPE& aShape, BOARD_ITEM_CONTAINER& aParent );
191
195 std::vector<std::unique_ptr<PCB_SHAPE>> buildPolygonShapes( const BLK_0x28_SHAPE& aShape,
196 BOARD_ITEM_CONTAINER& aParent );
197
201 std::vector<std::unique_ptr<BOARD_ITEM>> buildDrillMarker( const BLK_0x0C_PIN_DEF& aPinDef,
202 BOARD_ITEM_CONTAINER& aParent );
203
207 std::vector<std::unique_ptr<BOARD_ITEM>> buildGraphicItems( const BLOCK_BASE& aBlock,
208 BOARD_ITEM_CONTAINER& aParent );
209
213 std::vector<std::unique_ptr<BOARD_ITEM>> buildPadItems( const BLK_0x1C_PADSTACK& aPadstack, FOOTPRINT& aFp,
214 const wxString& aPadName, int aNetcode );
215 std::unique_ptr<FOOTPRINT> buildFootprint( const BLK_0x2D_FOOTPRINT_INST& aFpInstance );
216 std::vector<std::unique_ptr<BOARD_ITEM>> buildTrack( const BLK_0x05_TRACK& aBlock, int aNetcode );
217 std::unique_ptr<BOARD_ITEM> buildVia( const BLK_0x33_VIA& aBlock, int aNetcode );
218
219 class ZONE_FILL_HANDLER;
220
227 std::unique_ptr<ZONE> buildZone( const BLOCK_BASE& aBoundaryBlock,
228 const std::vector<const BLOCK_BASE*>& aRelatedBlocks,
229 ZONE_FILL_HANDLER& aZoneFillHandler, BOARD_ITEM_CONTAINER& aParent );
230
231 SHAPE_LINE_CHAIN buildOutline( const BLK_0x0E_RECT& aRect, const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
232 SHAPE_LINE_CHAIN buildOutline( const BLK_0x24_RECT& aRect, const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
233 SHAPE_LINE_CHAIN buildOutline( const BLK_0x28_SHAPE& aShape, const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
234
235 SHAPE_POLY_SET shapeToPolySet( const BLK_0x28_SHAPE& aShape, const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
236
241 const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
242
248 SHAPE_LINE_CHAIN buildSegmentChain( uint32_t aStartKey,
249 const TRANSFORM_TRS& aXform = TRANSFORM_TRS() ) const;
250
254 std::vector<const BLOCK_BASE*> getShapeRelatedBlocks( const BLK_0x28_SHAPE& aShape ) const;
255
265 wxString resolveMatchGroupName( const BLK_0x1B_NET& aNet ) const;
266
274 wxString resolveConstraintSetNameFromField( uint32_t aFieldKey ) const;
275
276 void cacheFontDefs();
277 void setupLayers();
278 void createNets();
279 void createTracks();
280 void createBoardShapes();
281 void createBoardText();
282 void createZones();
283 void createTables();
284 void applyZoneFills();
285 void enablePadTeardrops();
286 void applyConstraintSets();
287 void applyNetConstraints();
288 void applyMatchGroups();
289
296 const BLK_0x36_DEF_TABLE::FontDef_X08* getFontDef( unsigned aIndex ) const;
297
301 const BLK_0x07_COMPONENT_INST* getFpInstRef( const BLK_0x2D_FOOTPRINT_INST& aFpInstance ) const;
302
308
309 // Cached list of font defs in the 0x36 node
310 std::vector<const BLK_0x36_DEF_TABLE::FontDef_X08*> m_fontDefList;
311
312 // Cached list of KiCad nets corresponding to Allegro 0x1B NET keys
313 std::unordered_map<uint32_t, NETINFO_ITEM*> m_netCache;
314
321
322 std::unordered_map<uint32_t, ZoneFillEntry> m_zoneFillShapes;
323
324 // Keys that have been used as zone fills already
325 std::unordered_set<uint32_t> m_usedZoneFillShapes;
326
327 std::unique_ptr<LAYER_MAPPER> m_layerMapper;
328
329 // Cache of computed outlines keyed by shape block key, avoiding redundant geometry rebuilds
330 mutable std::unordered_map<uint32_t, SHAPE_LINE_CHAIN> m_outlineCache;
331
332 // Cache of segment chains keyed by start key, avoiding redundant hole geometry rebuilds
333 mutable std::unordered_map<uint32_t, SHAPE_LINE_CHAIN> m_segChainCache;
334
335 // Conversion factor from internal units to nanometers.
336 double m_scale;
337};
338
339} // namespace ALLEGRO
The base class for all blocks in the main body of an Allegro file.
uint8_t GetBlockType() const
This is the actual type code as read from the file.
const T & GetData() const
Rejects graphics that repeat one already accepted into the same container.
bool IsFirst(const BOARD_ITEM *aItem)
True when this graphic has not been seen before, and records it.
std::unordered_set< const BOARD_ITEM *, GRAPHIC_KEY_HASH, GRAPHIC_KEY_EQ > m_seen
wxString resolveMatchGroupName(const BLK_0x1B_NET &aNet) const
Follow m_MatchGroupPtr through the 0x26/0x2C pointer chain to get the match group name for a NET.
LAYER_MAPPING_HANDLER m_layerMappingHandler
PROGRESS_REPORTER * m_progressReporter
void reportUnexpectedBlockType(uint8_t aGot, uint8_t aExpected, uint32_t aKey=0, size_t aOffset=0, const wxString &aName=wxEmptyString) const
const BLK_0x07_COMPONENT_INST * getFpInstRef(const BLK_0x2D_FOOTPRINT_INST &aFpInstance) const
Look up 0x07 FP instance data (0x07) for a given 0x2D FP instance.
SHAPE_POLY_SET tryBuildZoneShape(const BLOCK_BASE &aBlock, const TRANSFORM_TRS &aXform=TRANSFORM_TRS()) const
Try to build a zone shape for the given block, with holes.
static void stampIds(BOARD_ITEM &aItem, uint32_t aKey)
std::vector< const BLOCK_BASE * > getShapeRelatedBlocks(const BLK_0x28_SHAPE &aShape) const
Get blocks that are related to the BOUNDARY shape, i.e.
VECTOR2I scaleSize(const VECTOR2I &aSize) const
PCB_LAYER_ID getLayer(const LAYER_INFO &aLayerInfo) const
std::unordered_map< uint32_t, SHAPE_LINE_CHAIN > m_segChainCache
std::vector< std::unique_ptr< PCB_SHAPE > > buildShapes(const BLK_0x14_GRAPHIC &aGraphicList, BOARD_ITEM_CONTAINER &aParent)
Build the shapes from an 0x14 shape list.
static void stampIds(BOARD_ITEM &aItem, uint32_t aKey, uint32_t &aSeq)
Give aItem and its children ids derived from the Allegro block they came from.
GRAPHIC_DEDUP m_boardGraphics
Coincidence filter for everything added straight to the board, which is one container.
BOARD_BUILDER(const BRD_DB &aBrdDb, BOARD &aBoard, REPORTER &aReporter, PROGRESS_REPORTER *aProgressReporter, const LAYER_MAPPING_HANDLER &aLayerMappingHandler)
std::unique_ptr< LAYER_MAPPER > m_layerMapper
const T * expectBlockByKey(uint32_t aKey) const
Get a block by its key, and check that it is of the expected type.
std::vector< std::unique_ptr< BOARD_ITEM > > buildDrillMarker(const BLK_0x0C_PIN_DEF &aPinDef, BOARD_ITEM_CONTAINER &aParent)
Build a drill marker from a 0x0C PIN_DEF block.
std::unique_ptr< PCB_SHAPE > buildRect(const BLK_0x24_RECT &aRect, BOARD_ITEM_CONTAINER &aParent)
Build a rectangular shape from a 0x24 RECT block.
std::vector< std::unique_ptr< PCB_SHAPE > > buildPolygonShapes(const BLK_0x28_SHAPE &aShape, BOARD_ITEM_CONTAINER &aParent)
Build graphics from an 0x28 SHAPE, with separate items per segment/arc.
SHAPE_POLY_SET shapeToPolySet(const BLK_0x28_SHAPE &aShape, const TRANSFORM_TRS &aXform=TRANSFORM_TRS()) const
std::unique_ptr< PCB_SHAPE > buildArc(const BLK_0x01_ARC &aArc, const LAYER_INFO &aLayerInfo, PCB_LAYER_ID aLayer, BOARD_ITEM_CONTAINER &aParent)
std::unique_ptr< PCB_TEXT > buildPcbText(const BLK_0x30_STR_WRAPPER &aStrWrapper, BOARD_ITEM_CONTAINER &aParent)
std::unique_ptr< FOOTPRINT > buildFootprint(const BLK_0x2D_FOOTPRINT_INST &aFpInstance)
void reportMissingBlock(uint32_t aKey, uint8_t aType) const
std::unordered_map< uint32_t, NETINFO_ITEM * > m_netCache
std::unique_ptr< PCB_SHAPE > buildLineSegment(const BLK_0x15_16_17_SEGMENT &aSegment, const LAYER_INFO &aLayerInfo, PCB_LAYER_ID aLayer, BOARD_ITEM_CONTAINER &aParent)
Build a single line segment.
std::vector< std::unique_ptr< BOARD_ITEM > > buildTrack(const BLK_0x05_TRACK &aBlock, int aNetcode)
std::vector< const BLK_0x36_DEF_TABLE::FontDef_X08 * > m_fontDefList
std::unordered_map< uint32_t, ZoneFillEntry > m_zoneFillShapes
std::unordered_map< uint32_t, SHAPE_LINE_CHAIN > m_outlineCache
wxString get0x30StringValue(uint32_t a0x30Key) const
Get just the string value from a 0x31 STRING WRAPPER -> 0x30 STRING GRAPHIC pair.
const BLK_0x36_DEF_TABLE::FontDef_X08 * getFontDef(unsigned aIndex) const
Get the font definition for a given index in a 0x30, etc.
std::vector< std::unique_ptr< BOARD_ITEM > > buildGraphicItems(const BLOCK_BASE &aBlock, BOARD_ITEM_CONTAINER &aParent)
Build a list of graphic items, e.g.
std::unique_ptr< BOARD_ITEM > buildVia(const BLK_0x33_VIA &aBlock, int aNetcode)
std::unique_ptr< PCB_SHAPE > buildPolygon(const BLK_0x28_SHAPE &aShape, BOARD_ITEM_CONTAINER &aParent)
Build a graphic polygon from a 0x28 SHAPE block.
wxString resolveConstraintSetNameFromField(uint32_t aFieldKey) const
Extract constraint set name from a 0x03 FIELD block pointer.
std::unordered_set< uint32_t > m_usedZoneFillShapes
SHAPE_LINE_CHAIN buildOutline(const BLK_0x0E_RECT &aRect, const TRANSFORM_TRS &aXform=TRANSFORM_TRS()) const
SHAPE_LINE_CHAIN buildSegmentChain(uint32_t aStartKey, const TRANSFORM_TRS &aXform=TRANSFORM_TRS()) const
Walk a geometry chain (0x01 arcs and 0x15-17 segments) starting from the given key,...
std::unique_ptr< ZONE > buildZone(const BLOCK_BASE &aBoundaryBlock, const std::vector< const BLOCK_BASE * > &aRelatedBlocks, ZONE_FILL_HANDLER &aZoneFillHandler, BOARD_ITEM_CONTAINER &aParent)
Build a ZONE from an 0x0E, 0x24 or 0x28 block.
std::vector< std::unique_ptr< BOARD_ITEM > > buildPadItems(const BLK_0x1C_PADSTACK &aPadstack, FOOTPRINT &aFp, const wxString &aPadName, int aNetcode)
Construct "pad" items for a given 0x1C PADSTACK block.
An Allegro board database representing the contents of a .brd (and presumably .dra) file.
Definition allegro_db.h:43
Class to handle the mapping for Allegro CLASS/SUBCLASS idiom to KiCad layers.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
Handle the data for a net.
Definition netinfo.h:50
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
std::function< std::map< wxString, PCB_LAYER_ID >(const std::vector< INPUT_LAYER_DESC > &)> LAYER_MAPPING_HANDLER
Pointer to a function that takes a map of source and KiCad layers and returns a re-mapped version.
const int scale
Arc segment used in tracks, zone outlines, and shape boundaries.
Track segment container.
Component instance reference data.
Pin definition with shape type, drill character, coordinates, and size.
Graphics container holding a chain of line segments and arcs.
0x15 , 0x16, 0x17 are segments:
0x1B objects are nets.
Padstack definition containing drill dimensions and a table of per-layer pad/antipad/thermal componen...
Rectangle defined by four coordinates.
Polygon shape defined by a linked list of segments starting at m_FirstSegmentPtr (0x15/0x16/0x17 line...
Placed footprint instance on the board.
Text object with position, rotation, layer, font properties, and alignment.
Via instance with board position, padstack reference (m_Padstack for drill/annular ring definitions),...
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
size_t operator()(const BOARD_ITEM *aItem) const
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683