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 The 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_barcode.h>
30#include <pcb_reference_image.h>
31#include <pcb_shape.h>
32#include <pcb_track.h>
33#include <pcb_field.h>
34#include <pcb_text.h>
35#include <pcb_textbox.h>
36#include <zone.h>
37
38
39std::unique_ptr<BOARD_ITEM> CreateItemForType( KICAD_T aType, BOARD_ITEM_CONTAINER* aContainer )
40{
41 switch( aType )
42 {
43 case PCB_TRACE_T: return std::make_unique<PCB_TRACK>( aContainer );
44 case PCB_ARC_T: return std::make_unique<PCB_ARC>( aContainer );
45 case PCB_VIA_T: return std::make_unique<PCB_VIA>( aContainer );
46 case PCB_TEXT_T: return std::make_unique<PCB_TEXT>( aContainer );
47 case PCB_TEXTBOX_T: return std::make_unique<PCB_TEXTBOX>( aContainer );
48 case PCB_SHAPE_T: return std::make_unique<PCB_SHAPE>( aContainer );
49 case PCB_BARCODE_T: return std::make_unique<PCB_BARCODE>( aContainer );
50 case PCB_ZONE_T: return std::make_unique<ZONE>( aContainer );
51 case PCB_GROUP_T: return std::make_unique<PCB_GROUP>( aContainer );
52 case PCB_REFERENCE_IMAGE_T: return std::make_unique<PCB_REFERENCE_IMAGE>( aContainer );
53
54 case PCB_PAD_T:
55 {
56 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( aContainer );
57
58 if( !footprint )
59 return nullptr;
60
61 return std::make_unique<PAD>( footprint );
62 }
63
64 case PCB_FIELD_T:
65 {
66 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( aContainer );
67
68 if( !footprint )
69 return nullptr;
70
71 return std::make_unique<PCB_FIELD>( footprint, FIELD_T::USER );
72 }
73
74 case PCB_FOOTPRINT_T:
75 {
76 BOARD* board = dynamic_cast<BOARD*>( aContainer );
77
78 if( !board )
79 return nullptr;
80
81 return std::make_unique<FOOTPRINT>( board );
82 }
83
84 default:
85 return nullptr;
86 }
87}
88
89namespace kiapi::board
90{
91
92void PackLayerSet( google::protobuf::RepeatedField<int>& aOutput, const LSET& aLayerSet )
93{
94 for( const PCB_LAYER_ID& layer : aLayerSet.Seq() )
96}
97
98
99LSET UnpackLayerSet( const google::protobuf::RepeatedField<int>& aProtoLayerSet )
100{
101 LSET set;
102
103 for( int layer : aProtoLayerSet )
104 {
105 wxCHECK2( layer >= F_Cu && layer < PCB_LAYER_ID_COUNT, continue );
106 PCB_LAYER_ID boardLayer =
107 FromProtoEnum<PCB_LAYER_ID>( static_cast<types::BoardLayer>( layer ) );
108
109 if( boardLayer >= 0 && IsValidLayer( boardLayer ) )
110 set.set( boardLayer );
111 }
112
113 return set;
114}
115
116} // namespace kiapi::board
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
Definition api_enums.cpp:97
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:36
std::unique_ptr< BOARD_ITEM > CreateItemForType(KICAD_T aType, BOARD_ITEM_CONTAINER *aContainer)
BASE_SET & set(size_t pos)
Definition base_set.h:116
Abstract interface for BOARD_ITEMs capable of storing other items inside.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:296
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:171
@ F_Cu
Definition layer_ids.h:64
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition layer_ids.h:655
void PackLayerSet(google::protobuf::RepeatedField< int > &aOutput, const LSET &aLayerSet)
LSET UnpackLayerSet(const google::protobuf::RepeatedField< int > &aProtoLayerSet)
BARCODE class definition.
Class to handle a set of BOARD_ITEMs.
@ USER
The field ID hasn't been set yet; field is invalid.
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:111
@ 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:108
@ 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_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
@ 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