KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_generated_table_utils.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef API_GENERATED_TABLE_UTILS_H
21#define API_GENERATED_TABLE_UTILS_H
22
23#include <algorithm>
24#include <cstdint>
25#include <type_traits>
26
27#include <api/api_enums.h>
28#include <api/api_utils.h>
29#include <base_units.h>
30#include <pcb_generated_table.h>
31
32#include <google/protobuf/any.pb.h>
33
34
42template <typename MSG>
43void PackGeneratedTable( const PCB_GENERATED_TABLE& aTable, MSG& aMsg )
44{
45 google::protobuf::Any tableAny;
46 aTable.PCB_TABLE::Serialize( tableAny );
47
48 if( !tableAny.UnpackTo( aMsg.mutable_table() ) )
49 wxFAIL_MSG( wxT( "Failed to unpack PCB_GENERATED_TABLE from internal serialization" ) );
50
51 for( const GENERATED_TABLE_COLUMN& col : aTable.AllColumns() )
52 {
53 auto* proto = aMsg.add_columns();
54
55 using ID = std::remove_cvref_t<decltype( proto->id() )>;
56 using ALIGN = std::remove_cvref_t<decltype( proto->align() )>;
57
58 proto->set_id( static_cast<ID>( col.m_Id + 1 ) );
59 proto->set_heading( col.m_Heading.ToStdString() );
60 proto->set_align( static_cast<ALIGN>( static_cast<int>( col.m_Align ) + 1 ) );
61 kiapi::common::PackDistance( *proto->mutable_width(), col.m_Width );
62 proto->set_hidden( !col.m_Visible );
63 }
64
66
67 aMsg.set_precision( aTable.GetPrecision() );
68
69 for( const auto& [row, key] : aTable.RowKeys() )
70 ( *aMsg.mutable_row_keys() )[row] = key;
71}
72
73
79template <typename MSG>
80bool UnpackGeneratedTable( const MSG& aMsg, PCB_GENERATED_TABLE& aTable )
81{
82 google::protobuf::Any tableAny;
83
84 if( !tableAny.PackFrom( aMsg.table() ) )
85 return false;
86
87 if( !aTable.PCB_TABLE::Deserialize( tableAny ) )
88 return false;
89
90 // The table carries the layer. A table on a manufacturing layer would be plotted into a
91 // fabrication output rather than the documentation
92 if( !DocumentationLayers().Contains( aTable.GetLayer() ) )
93 return false;
94
95 std::vector<GENERATED_TABLE_COLUMN> columns;
96
97 for( const auto& proto : aMsg.columns() )
98 {
99 const int wireId = static_cast<int>( proto.id() );
100
101 // Zero is the unset value. A negative id is refused before the offset can overflow
102 if( wireId == 0 )
103 continue;
104
105 if( wireId < 0 )
106 return false;
107
108 const int id = wireId - 1;
109
110 if( !aTable.Schema().Find( id ) )
111 return false;
112
113 const int align = static_cast<int>( proto.align() );
114
115 if( align < 0 || align > static_cast<int>( GENERATED_TABLE_ALIGN::RIGHT ) + 1 )
116 return false;
117
119 col.m_Id = id;
120 col.m_Heading = wxString::FromUTF8( proto.heading() );
121
122 if( align > 0 )
123 col.m_Align = static_cast<GENERATED_TABLE_ALIGN>( align - 1 );
124
125 kiapi::common::types::Distance width;
126 width.set_value_nm( std::clamp<int64_t>( proto.width().value_nm(), 0,
129 col.m_Visible = !proto.hidden();
130 columns.push_back( col );
131 }
132
133 // No columns divides by zero the next time this is rebuilt or autosized. Repeats and
134 // implausible widths reach table geometry
135 if( !aTable.Schema().Validate( columns ) )
136 return false;
137
138 // A visibility edit can leave the message's cell grid at its old width until the caller rebuilds it.
139 if( std::none_of( columns.begin(), columns.end(),
140 []( const GENERATED_TABLE_COLUMN& aCol )
141 {
142 return aCol.m_Visible;
143 } ) )
144 return false;
145
146 aTable.RestoreColumns( columns );
147
149
150 aTable.RowKeys().clear();
151
152 for( const auto& [row, key] : aMsg.row_keys() )
153 aTable.RowKeys()[row] = key;
154
155 // Refused rather than clamped, so a client never reads back a value it did not send
156 if( aMsg.precision() < 0 || aMsg.precision() > GENERATED_TABLE_MAX_PRECISION )
157 return false;
158
159 aTable.SetPrecision( aMsg.precision() );
160
161 return true;
162}
163
164#endif // API_GENERATED_TABLE_UTILS_H
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:56
void PackGeneratedTable(const PCB_GENERATED_TABLE &aTable, MSG &aMsg)
Pack what every generated table shares into its message.
bool UnpackGeneratedTable(const MSG &aMsg, PCB_GENERATED_TABLE &aTable)
The reverse of PackGeneratedTable.
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
const GENERATED_TABLE_COLUMN_DEF * Find(int aId) const
bool Validate(std::vector< GENERATED_TABLE_COLUMN > &aColumns) const
Drop a column whose id the schema does not know and a column that repeats an id already kept,...
A table whose cell text is derived from the board and rebuilt whenever it goes stale.
std::vector< GENERATED_TABLE_COLUMN > AllColumns() const
Every column the table has been given, in order, with hidden ones marked by m_Visible.
void RestoreColumns(const std::vector< GENERATED_TABLE_COLUMN > &aColumns)
Replace the columns without touching any cell, for a loader whose cells already match the visible one...
const std::map< int, std::string > & RowKeys() const
What each generated row reports on, by table row.
void SetPrecision(int aPrecision)
Clamped.
virtual const GENERATED_TABLE_SCHEMA & Schema() const =0
GENERATED_TABLE_UNITS GetUnits() const
void SetUnits(GENERATED_TABLE_UNITS aUnits)
LSET DocumentationLayers()
Layers a generated table or drill map may live on.
constexpr int GENERATED_TABLE_MAX_COLUMN_WIDTH
A column can be no wider than this.
constexpr int GENERATED_TABLE_MAX_PRECISION
The most digits a generated table prints after the decimal point.
GENERATED_TABLE_ALIGN
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
A column as a generated table actually carries it: what a row's cell in that column holds and how it ...
GENERATED_TABLE_ALIGN m_Align