KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_pcb_utils.cpp
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 (C) 2023 Jon Evans <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <api/api_pcb_utils.h>
22#include <api/api_enums.h>
23#include <board.h>
25#include <footprint.h>
26#include <lset.h>
27#include <pad.h>
28#include <pcb_group.h>
29#include <pcb_reference_image.h>
30#include <pcb_shape.h>
31#include <pcb_track.h>
32#include <pcb_text.h>
33#include <pcb_textbox.h>
34#include <zone.h>
35
36
37std::unique_ptr<BOARD_ITEM> CreateItemForType( KICAD_T aType, BOARD_ITEM_CONTAINER* aContainer )
38{
39 switch( aType )
40 {
41 case PCB_TRACE_T: return std::make_unique<PCB_TRACK>( aContainer );
42 case PCB_ARC_T: return std::make_unique<PCB_ARC>( aContainer );
43 case PCB_VIA_T: return std::make_unique<PCB_VIA>( aContainer );
44 case PCB_TEXT_T: return std::make_unique<PCB_TEXT>( aContainer );
45 case PCB_TEXTBOX_T: return std::make_unique<PCB_TEXTBOX>( aContainer );
46 case PCB_SHAPE_T: return std::make_unique<PCB_SHAPE>( aContainer );
47 case PCB_ZONE_T: return std::make_unique<ZONE>( aContainer );
48 case PCB_GROUP_T: return std::make_unique<PCB_GROUP>( aContainer );
49 case PCB_REFERENCE_IMAGE_T: return std::make_unique<PCB_REFERENCE_IMAGE>( aContainer );
50
51 case PCB_PAD_T:
52 {
53 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( aContainer );
54
55 if( !footprint )
56 return nullptr;
57
58 return std::make_unique<PAD>( footprint );
59 }
60
61 case PCB_FOOTPRINT_T:
62 {
63 BOARD* board = dynamic_cast<BOARD*>( aContainer );
64
65 if( !board )
66 return nullptr;
67
68 return std::make_unique<FOOTPRINT>( board );
69 }
70
71 default:
72 return nullptr;
73 }
74}
75
76namespace kiapi::board
77{
78
79void PackLayerSet( google::protobuf::RepeatedField<int>& aOutput, const LSET& aLayerSet )
80{
81 for( const PCB_LAYER_ID& layer : aLayerSet.Seq() )
82 aOutput.Add( ToProtoEnum<PCB_LAYER_ID, types::BoardLayer>( layer ) );
83}
84
85
86LSET UnpackLayerSet( const google::protobuf::RepeatedField<int>& aProtoLayerSet )
87{
88 LSET set;
89
90 for( int layer : aProtoLayerSet )
91 {
92 wxCHECK2( layer >= F_Cu && layer < PCB_LAYER_ID_COUNT, continue );
93 PCB_LAYER_ID boardLayer =
94 FromProtoEnum<PCB_LAYER_ID>( static_cast<types::BoardLayer>( layer ) );
95 set.set( boardLayer );
96 }
97
98 return set;
99}
100
101} // namespace kiapi::board
std::unique_ptr< BOARD_ITEM > CreateItemForType(KICAD_T aType, BOARD_ITEM_CONTAINER *aContainer)
BASE_SET & set(size_t pos=std::numeric_limits< size_t >::max(), bool value=true)
Definition: base_set.h:61
Abstract interface for BOARD_ITEMs capable of storing other items inside.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:289
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:35
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:392
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
@ F_Cu
Definition: layer_ids.h:64
void PackLayerSet(google::protobuf::RepeatedField< int > &aOutput, const LSET &aLayerSet)
LSET UnpackLayerSet(const google::protobuf::RepeatedField< int > &aProtoLayerSet)
Class to handle a set of BOARD_ITEMs.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96