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
18 * along with this program. If not, see <https://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>
30#include <pcb_barcode.h>
31#include <pcb_reference_image.h>
32#include <pcb_shape.h>
33#include <pcb_track.h>
34#include <pcb_field.h>
35#include <pcb_text.h>
36#include <pcb_textbox.h>
37#include <pcb_dimension.h>
38#include <zone.h>
39
40
41std::unique_ptr<BOARD_ITEM> CreateItemForType( KICAD_T aType, BOARD_ITEM_CONTAINER* aContainer )
42{
43 switch( aType )
44 {
45 case PCB_TRACE_T: return std::make_unique<PCB_TRACK>( aContainer );
46 case PCB_ARC_T: return std::make_unique<PCB_ARC>( aContainer );
47 case PCB_VIA_T: return std::make_unique<PCB_VIA>( aContainer );
48 case PCB_TEXT_T: return std::make_unique<PCB_TEXT>( aContainer );
49 case PCB_TEXTBOX_T: return std::make_unique<PCB_TEXTBOX>( aContainer );
50 case PCB_SHAPE_T: return std::make_unique<PCB_SHAPE>( aContainer );
51 case PCB_BARCODE_T: return std::make_unique<PCB_BARCODE>( aContainer );
52 case PCB_ZONE_T: return std::make_unique<ZONE>( aContainer );
53 case PCB_GROUP_T: return std::make_unique<PCB_GROUP>( aContainer );
54 case PCB_CONSTRAINT_T: return std::make_unique<PCB_CONSTRAINT>( aContainer );
55 case PCB_REFERENCE_IMAGE_T: return std::make_unique<PCB_REFERENCE_IMAGE>( aContainer );
56
57 case PCB_PAD_T:
58 {
59 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( aContainer );
60
61 if( !footprint )
62 return nullptr;
63
64 return std::make_unique<PAD>( footprint );
65 }
66
67 case PCB_FIELD_T:
68 {
69 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( aContainer );
70
71 if( !footprint )
72 return nullptr;
73
74 return std::make_unique<PCB_FIELD>( footprint, FIELD_T::USER );
75 }
76
77 case PCB_FOOTPRINT_T:
78 {
79 BOARD* board = dynamic_cast<BOARD*>( aContainer );
80
81 if( !board )
82 return nullptr;
83
84 return std::make_unique<FOOTPRINT>( board );
85 }
86
87 case PCB_DIM_ALIGNED_T: return std::make_unique<PCB_DIM_ALIGNED>( aContainer );
88 case PCB_DIM_ORTHOGONAL_T: return std::make_unique<PCB_DIM_ORTHOGONAL>( aContainer );
89 case PCB_DIM_RADIAL_T: return std::make_unique<PCB_DIM_RADIAL>( aContainer );
90 case PCB_DIM_LEADER_T: return std::make_unique<PCB_DIM_LEADER>( aContainer );
91 case PCB_DIM_CENTER_T: return std::make_unique<PCB_DIM_CENTER>( aContainer );
92
93 default:
94 return nullptr;
95 }
96}
97
98namespace kiapi::board
99{
100
101void PackLayerSet( google::protobuf::RepeatedField<int>& aOutput, const LSET& aLayerSet )
102{
103 for( const PCB_LAYER_ID& layer : aLayerSet.Seq() )
104 aOutput.Add( ToProtoEnum<PCB_LAYER_ID, types::BoardLayer>( layer ) );
105}
106
107
108LSET UnpackLayerSet( const google::protobuf::RepeatedField<int>& aProtoLayerSet )
109{
110 LSET set;
111
112 for( int layer : aProtoLayerSet )
113 {
114 wxCHECK2( layer >= F_Cu && layer < PCB_LAYER_ID_COUNT, continue );
115 PCB_LAYER_ID boardLayer =
116 FromProtoEnum<PCB_LAYER_ID>( static_cast<types::BoardLayer>( layer ) );
117
118 if( boardLayer >= 0 && IsValidLayer( boardLayer ) )
119 set.set( boardLayer );
120 }
121
122 return set;
123}
124
125} // namespace kiapi::board
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:47
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:373
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:309
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
@ F_Cu
Definition layer_ids.h:60
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition layer_ids.h:661
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:71
@ PCB_CONSTRAINT_T
class PCB_CONSTRAINT, a geometric constraint between board items
Definition typeinfo.h:238
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:81
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:99
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:96
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:90
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:104
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:86
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:101
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:85
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:82
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:83
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:94
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:95
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:80
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:91
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:98