KiCad PCB EDA Suite
Loading...
Searching...
No Matches
erc_exclusion.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
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 <json_common.h>
22#include <cstdlib>
23#include <api/api_enums.h>
24#include <api/api_utils.h>
25#include <base_units.h>
26#include <erc/erc_item.h>
27#include <wx/arrstr.h>
28
29#include <erc/erc_exclusion.h>
30#include <erc/erc_settings.h> // for ERCE_T enum
31#include <sch_marker.h>
32#include <sch_sheet_path.h>
33
34#include <api/schematic/schematic_rules.pb.h>
35#include <google/protobuf/any.h>
36#include <google/protobuf/util/json_util.h>
37
38
39static void canonicalizeExclusion( kiapi::schematic::ErcExclusion& aMessage )
40{
41 if( !aMessage.has_marker() )
42 return;
43
44 // Display names can change without changing an exclusion's sheet identity.
45 auto& marker = *aMessage.mutable_marker();
46
47 if( marker.has_sheet_specific_path() )
48 marker.mutable_sheet_specific_path()->clear_path_human_readable();
49
50 if( marker.has_main_item_sheet_path() )
51 marker.mutable_main_item_sheet_path()->clear_path_human_readable();
52
53 if( marker.has_aux_item_sheet_path() )
54 marker.mutable_aux_item_sheet_path()->clear_path_human_readable();
55}
56
57
59{
60 kiapi::schematic::ErcExclusion message;
61};
62
63
65
66
68 m_impl( std::make_unique<IMPL>( *aOther.m_impl ) )
69{}
70
71
73{
74 *m_impl = *aOther.m_impl;
75 return *this;
77
78
80
81
83{
85 google::protobuf::Any container;
86
87 aMarker.Serialize( container );
88 container.UnpackTo( ex.m_impl->message.mutable_marker() );
89 canonicalizeExclusion( ex.m_impl->message );
90 ex.SetComment( aMarker.GetComment() );
91
92 return ex;
93}
94
95
96ERC_EXCLUSION ERC_EXCLUSION::FromProto( const kiapi::schematic::ErcExclusion& aMessage )
97{
99 ex.m_impl->message.CopyFrom( aMessage );
100 canonicalizeExclusion( ex.m_impl->message );
101 return ex;
102}
103
104
105ERC_EXCLUSION ERC_EXCLUSION::FromLegacyStrings( const SCH_SHEET_LIST& aSheetList, const wxString& aMarkerData,
106 const wxString& aComment )
107{
108 ERC_EXCLUSION ex;
109 const wxArrayString props = wxSplit( aMarkerData, '|' );
110
111 if( props.size() != 5 && props.size() != 8 )
112 return ex;
113
114 const auto item = ERC_ITEM::Create( props[0] );
115
116 if( !item )
117 return ex;
118
119 const auto code = static_cast<ERCE_T>( item->GetErrorCode() );
120 auto& marker = *ex.m_impl->message.mutable_marker();
121 marker.set_error_type( ToProtoEnum<ERCE_T, kiapi::schematic::ErcErrorType>( code ) );
122 const VECTOR2I position( static_cast<int>( std::strtol( props[1].c_str(), nullptr, 10 ) ),
123 static_cast<int>( std::strtol( props[2].c_str(), nullptr, 10 ) ) );
124 kiapi::common::PackVector2( *marker.mutable_position(), position, schIUScale );
125
126 const KIID main( props[3] );
127
128 if( main != niluuid )
129 marker.add_items()->set_value( main.AsStdString() );
130
131 const bool childText = ( code == ERCE_GENERIC_WARNING || code == ERCE_GENERIC_ERROR
132 || code == ERCE_UNRESOLVED_VARIABLE )
133 && main != niluuid && !props[4].IsEmpty() && props[4] != niluuid.AsString();
134
135 if( childText )
136 {
137 marker.mutable_child()->set_text_value( props[4].ToUTF8() );
138
139 // Match the current child ID without discarding an unavailable instance's saved path.
140 std::unique_ptr<SCH_MARKER> resolved( SCH_MARKER::FromProto( marker, aSheetList ) );
141
142 if( resolved )
143 marker.add_items()->set_value( resolved->GetRCItem()->GetMainItemID().AsStdString() );
144 }
145 else if( !props[4].IsEmpty() )
146 {
147 const KIID auxiliary( props[4] );
148
149 if( auxiliary != niluuid )
150 marker.add_items()->set_value( auxiliary.AsStdString() );
151 }
152
153 if( props.size() == 8 )
154 {
155 if( !props[5].IsEmpty() )
156 kiapi::common::PackSheetPath( *marker.mutable_sheet_specific_path(), KIID_PATH( props[5] ) );
157
158 if( !props[6].IsEmpty() )
159 kiapi::common::PackSheetPath( *marker.mutable_main_item_sheet_path(), KIID_PATH( props[6] ) );
160
161 if( !props[7].IsEmpty() )
162 kiapi::common::PackSheetPath( *marker.mutable_aux_item_sheet_path(), KIID_PATH( props[7] ) );
163 }
164
165 ex.SetComment( aComment );
166 return ex;
167}
168
169
170const kiapi::schematic::ErcExclusion& ERC_EXCLUSION::ToProto() const
171{
172 return m_impl->message;
173}
174
175
176void to_json( nlohmann::json& aJson, const ERC_EXCLUSION& aEx )
177{
178 google::protobuf::util::JsonPrintOptions jsonOptions;
179 jsonOptions.preserve_proto_field_names = true;
180
181 std::string json;
182 std::ignore = google::protobuf::util::MessageToJsonString( aEx.m_impl->message, &json, jsonOptions ).ok();
183
184 try
185 {
186 aJson = nlohmann::json::parse( json );
187 }
188 catch( ... )
189 {
190 aJson = nlohmann::json{};
191 }
192}
193
194
195void from_json( const nlohmann::json& aJson, ERC_EXCLUSION& aEx )
196{
197 std::ignore = google::protobuf::util::JsonStringToMessage( aJson.dump(), &aEx.m_impl->message ).ok();
198 canonicalizeExclusion( aEx.m_impl->message );
199}
200
201
202std::string ERC_EXCLUSION::GetSortKey() const
203{
204 if( m_impl->message.has_marker() )
205 return m_impl->message.marker().SerializeAsString();
206
207 return std::string();
208}
209
210
212{
213 return wxString::FromUTF8( m_impl->message.comment() );
214}
215
216
217void ERC_EXCLUSION::SetComment( const wxString& aComment )
218{
219 m_impl->message.set_comment( aComment.ToUTF8() );
220}
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
static ERC_EXCLUSION FromLegacyStrings(const SCH_SHEET_LIST &aSheetList, const wxString &aMarkerData, const wxString &aComment)
ERC_EXCLUSION & operator=(const ERC_EXCLUSION &aOther)
std::unique_ptr< IMPL > m_impl
const kiapi::schematic::ErcExclusion & ToProto() const
void SetComment(const wxString &aComment)
static ERC_EXCLUSION FromMarker(const SCH_MARKER &aMarker)
wxString GetComment() const
std::string GetSortKey() const
static ERC_EXCLUSION FromProto(const kiapi::schematic::ErcExclusion &aMessage)
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition erc_item.cpp:305
Definition kiid.h:46
std::string AsStdString() const
Definition kiid.cpp:270
wxString GetComment() const
Definition marker_base.h:96
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
static SCH_MARKER * FromProto(const kiapi::schematic::ErcMarker &aMsg, const SCH_SHEET_LIST &aSheetList)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void from_json(const nlohmann::json &aJson, ERC_EXCLUSION &aEx)
static void canonicalizeExclusion(kiapi::schematic::ErcExclusion &aMessage)
void to_json(nlohmann::json &aJson, const ERC_EXCLUSION &aEx)
ERCE_T
ERC error codes.
@ ERCE_UNRESOLVED_VARIABLE
A text variable could not be resolved.
@ ERCE_GENERIC_ERROR
@ ERCE_GENERIC_WARNING
nlohmann::json json
Definition gerbview.cpp:50
KIID niluuid(0)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackSheetPath(types::SheetPath &aOutput, const KIID_PATH &aInput)
STL namespace.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
kiapi::schematic::ErcExclusion message
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683