KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <nlohmann/json.hpp>
22
24#include <pcb_marker.h>
25
26#include <api/board/board_rules.pb.h>
27#include <google/protobuf/any.h>
28#include <google/protobuf/util/json_util.h>
29
30
32{
33 kiapi::board::DrcExclusion message;
34};
35
36
37static void canonicalizeExclusion( kiapi::board::DrcExclusion& aMessage )
38{
39 if( !aMessage.has_marker() )
40 return;
41
42 auto& marker = *aMessage.mutable_marker();
43
44 marker.clear_violating_rule();
45 marker.clear_description();
46 marker.clear_severity();
47 marker.clear_excluded();
48}
49
50
52 m_impl( std::make_unique<IMPL>() )
53{
54}
55
56
58 m_impl( std::make_unique<IMPL>( *aOther.m_impl ) )
59{
60}
61
62
64{
65 *m_impl = *aOther.m_impl;
66 return *this;
67}
68
69
71
74{
76 google::protobuf::Any container;
77
78 aMarker.Serialize( container );
79
80 if( !container.UnpackTo( ex.m_impl->message.mutable_marker() ) )
81 wxFAIL_MSG( wxT( "Failed to unpack DRC exclusion from internal serialization") );
82
83 canonicalizeExclusion( ex.m_impl->message );
84 ex.SetComment( aMarker.GetComment() );
85
86 return ex;
87}
88
89
90DRC_EXCLUSION DRC_EXCLUSION::FromProto( const kiapi::board::DrcExclusion& aMessage )
91{
93 ex.m_impl->message.CopyFrom( aMessage );
94 canonicalizeExclusion( ex.m_impl->message );
95 return ex;
96}
97
98
99DRC_EXCLUSION DRC_EXCLUSION::FromLegacyStrings( const wxString& aMarkerData, const wxString& aComment )
100{
101 DRC_EXCLUSION ex;
102
103 if( PCB_MARKER* marker = PCB_MARKER::FromLegacyString( aMarkerData ) )
104 {
105 google::protobuf::Any container;
106 marker->Serialize( container );
107
108 if( !container.UnpackTo( ex.m_impl->message.mutable_marker() ) )
109 wxFAIL_MSG( wxT( "Failed to unpack legacy DRC exclusion from internal serialization" ) );
110
111 delete marker;
112 }
113
114 canonicalizeExclusion( ex.m_impl->message );
115 ex.SetComment( aComment );
116 return ex;
117}
118
119
120const kiapi::board::DrcExclusion& DRC_EXCLUSION::ToProto() const
121{
122 return m_impl->message;
123}
124
125
126void to_json( nlohmann::json& aJson, const DRC_EXCLUSION& aEx )
127{
128 google::protobuf::util::JsonPrintOptions jsonOptions;
129 jsonOptions.preserve_proto_field_names = true;
130
131 std::string json;
132 std::ignore = google::protobuf::util::MessageToJsonString( aEx.m_impl->message, &json, jsonOptions ).ok();
133
134 try
135 {
136 aJson = nlohmann::json::parse( json );
137 }
138 catch( ... )
139 {
140 aJson = nlohmann::json{};
141 }
142}
143
144
145void from_json( const nlohmann::json& aJson, DRC_EXCLUSION& aEx )
146{
147 std::ignore = google::protobuf::util::JsonStringToMessage( aJson.dump(), &aEx.m_impl->message ).ok();
148 canonicalizeExclusion( aEx.m_impl->message );
149}
150
151
152std::string DRC_EXCLUSION::GetSortKey() const
153{
154 if( !m_impl->message.has_marker() )
155 return std::string();
156
157 // Unconnected-item markers may be reported on several layers
158 if( m_impl->message.marker().error_type() == kiapi::board::DRCET_UNCONNECTED_ITEMS )
159 {
160 kiapi::board::DrcMarker marker = m_impl->message.marker();
161 marker.clear_layer();
162 return marker.SerializeAsString();
163 }
164
165 return m_impl->message.marker().SerializeAsString();
166}
167
168
170{
171 return wxString::FromUTF8( m_impl->message.comment() );
172}
173
174
175void DRC_EXCLUSION::SetComment( const wxString& aComment )
176{
177 m_impl->message.set_comment( aComment.ToUTF8() );
178}
DRC_EXCLUSION & operator=(const DRC_EXCLUSION &aOther)
static DRC_EXCLUSION FromLegacyStrings(const wxString &aMarkerData, const wxString &aComment)
void SetComment(const wxString &aComment)
std::string GetSortKey() const
static DRC_EXCLUSION FromProto(const kiapi::board::DrcExclusion &aMessage)
std::unique_ptr< IMPL > m_impl
wxString GetComment() const
static DRC_EXCLUSION FromMarker(const PCB_MARKER &aMarker)
const kiapi::board::DrcExclusion & ToProto() const
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 PCB_MARKER * FromLegacyString(const wxString &aData)
void to_json(nlohmann::json &aJson, const DRC_EXCLUSION &aEx)
void from_json(const nlohmann::json &aJson, DRC_EXCLUSION &aEx)
static void canonicalizeExclusion(kiapi::board::DrcExclusion &aMessage)
static void canonicalizeExclusion(kiapi::schematic::ErcExclusion &aMessage)
nlohmann::json json
Definition gerbview.cpp:50
STL namespace.
kiapi::board::DrcExclusion message