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 // Clear additional fields not part of the exclusion
48 marker.clear_description();
49 marker.clear_severity();
50 marker.clear_excluded();
51
52 if( marker.has_sheet_specific_path() )
53 marker.mutable_sheet_specific_path()->clear_path_human_readable();
54
55 if( marker.has_main_item_sheet_path() )
56 marker.mutable_main_item_sheet_path()->clear_path_human_readable();
57
58 if( marker.has_aux_item_sheet_path() )
59 marker.mutable_aux_item_sheet_path()->clear_path_human_readable();
60}
61
62
64{
65 kiapi::schematic::ErcExclusion message;
66};
67
68
70
71
73 m_impl( std::make_unique<IMPL>( *aOther.m_impl ) )
74{}
78{
79 *m_impl = *aOther.m_impl;
80 return *this;
81}
82
83
85
86
88{
90 google::protobuf::Any container;
91
92 aMarker.Serialize( container );
93
94 if( !container.UnpackTo( ex.m_impl->message.mutable_marker() ) )
95 wxFAIL_MSG( wxT( "Failed to unpack ERC_EXCLUSION from internal serialization" ) );
96
97 canonicalizeExclusion( ex.m_impl->message );
98 ex.SetComment( aMarker.GetComment() );
99
100 return ex;
101}
102
103
104ERC_EXCLUSION ERC_EXCLUSION::FromProto( const kiapi::schematic::ErcExclusion& aMessage )
105{
106 ERC_EXCLUSION ex;
107 ex.m_impl->message.CopyFrom( aMessage );
108 canonicalizeExclusion( ex.m_impl->message );
109 return ex;
110}
111
112
113ERC_EXCLUSION ERC_EXCLUSION::FromLegacyStrings( const SCH_SHEET_LIST& aSheetList, const wxString& aMarkerData,
114 const wxString& aComment )
115{
116 ERC_EXCLUSION ex;
117 const wxArrayString props = wxSplit( aMarkerData, '|' );
118
119 if( props.size() != 5 && props.size() != 8 )
120 return ex;
121
122 const auto item = ERC_ITEM::Create( props[0] );
123
124 if( !item )
125 return ex;
126
127 const auto code = static_cast<ERCE_T>( item->GetErrorCode() );
128 auto& marker = *ex.m_impl->message.mutable_marker();
129 marker.set_error_type( ToProtoEnum<ERCE_T, kiapi::schematic::ErcErrorType>( code ) );
130 const VECTOR2I position( static_cast<int>( std::strtol( props[1].c_str(), nullptr, 10 ) ),
131 static_cast<int>( std::strtol( props[2].c_str(), nullptr, 10 ) ) );
132 kiapi::common::PackVector2( *marker.mutable_position(), position, schIUScale );
133
134 const KIID main( props[3] );
135
136 if( main != niluuid )
137 marker.add_items()->set_value( main.AsStdString() );
138
139 const bool childText = ( code == ERCE_GENERIC_WARNING || code == ERCE_GENERIC_ERROR
140 || code == ERCE_UNRESOLVED_VARIABLE )
141 && main != niluuid && !props[4].IsEmpty() && props[4] != niluuid.AsString();
142
143 if( childText )
144 {
145 marker.mutable_child()->set_text_value( props[4].ToUTF8() );
146
147 // Match the current child ID without discarding an unavailable instance's saved path.
148 std::unique_ptr<SCH_MARKER> resolved( SCH_MARKER::FromProto( marker, aSheetList ) );
149
150 if( resolved )
151 marker.add_items()->set_value( resolved->GetRCItem()->GetMainItemID().AsStdString() );
152 }
153 else if( !props[4].IsEmpty() )
154 {
155 const KIID auxiliary( props[4] );
156
157 if( auxiliary != niluuid )
158 marker.add_items()->set_value( auxiliary.AsStdString() );
159 }
160
161 if( props.size() == 8 )
162 {
163 if( !props[5].IsEmpty() )
164 kiapi::common::PackSheetPath( *marker.mutable_sheet_specific_path(), KIID_PATH( props[5] ) );
165
166 if( !props[6].IsEmpty() )
167 kiapi::common::PackSheetPath( *marker.mutable_main_item_sheet_path(), KIID_PATH( props[6] ) );
168
169 if( !props[7].IsEmpty() )
170 kiapi::common::PackSheetPath( *marker.mutable_aux_item_sheet_path(), KIID_PATH( props[7] ) );
171 }
172
173 ex.SetComment( aComment );
174 return ex;
175}
176
177
178const kiapi::schematic::ErcExclusion& ERC_EXCLUSION::ToProto() const
179{
180 return m_impl->message;
181}
182
183
184void to_json( nlohmann::json& aJson, const ERC_EXCLUSION& aEx )
185{
186 google::protobuf::util::JsonPrintOptions jsonOptions;
187 jsonOptions.preserve_proto_field_names = true;
188
189 std::string json;
190 std::ignore = google::protobuf::util::MessageToJsonString( aEx.m_impl->message, &json, jsonOptions ).ok();
191
192 try
193 {
194 aJson = nlohmann::json::parse( json );
195 }
196 catch( ... )
197 {
198 aJson = nlohmann::json{};
199 }
200}
201
202
203void from_json( const nlohmann::json& aJson, ERC_EXCLUSION& aEx )
204{
205 std::ignore = google::protobuf::util::JsonStringToMessage( aJson.dump(), &aEx.m_impl->message ).ok();
206 canonicalizeExclusion( aEx.m_impl->message );
207}
208
209
210std::string ERC_EXCLUSION::GetSortKey() const
211{
212 if( m_impl->message.has_marker() )
213 return m_impl->message.marker().SerializeAsString();
214
215 return std::string();
216}
217
218
220{
221 return wxString::FromUTF8( m_impl->message.comment() );
222}
223
224
225void ERC_EXCLUSION::SetComment( const wxString& aComment )
226{
227 m_impl->message.set_comment( aComment.ToUTF8() );
228}
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
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:321
Definition kiid.h:46
std::string AsStdString() const
Definition kiid.cpp:270
wxString GetComment() const
Definition marker_base.h:95
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:708