KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_rule_saver.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "drc_re_rule_saver.h"
25
26#include <board.h>
27#include <lset.h>
28#include <wx/ffile.h>
29
31
32
36
37
38bool DRC_RULE_SAVER::SaveFile( const wxString& aPath,
39 const std::vector<DRC_RE_LOADED_PANEL_ENTRY>& aEntries,
40 const BOARD* aBoard )
41{
42 wxFFile file( aPath, "w" );
43
44 if( !file.IsOpened() )
45 return false;
46
47 wxString content = GenerateRulesText( aEntries, aBoard );
48 file.Write( content );
49 file.Close();
50
51 return true;
52}
53
54
55wxString DRC_RULE_SAVER::GenerateRulesText( const std::vector<DRC_RE_LOADED_PANEL_ENTRY>& aEntries,
56 const BOARD* aBoard )
57{
58 wxString result = "(version 1)\n";
59
60 for( const DRC_RE_LOADED_PANEL_ENTRY& entry : aEntries )
61 {
62 wxString ruleText = generateRuleText( entry, aBoard );
63
64 if( !ruleText.IsEmpty() )
65 result += ruleText + "\n";
66 }
67
68 return result;
69}
70
71
73 const BOARD* aBoard )
74{
75 // Round-trip preservation: return original text if not edited
76 if( !aEntry.wasEdited && !aEntry.originalRuleText.IsEmpty() )
77 return aEntry.originalRuleText;
78
79 // Otherwise, regenerate from panel data
80 if( !aEntry.constraintData )
81 return wxEmptyString;
82
84 ctx.ruleName = aEntry.ruleName;
85 ctx.conditionExpression = aEntry.condition;
87 ctx.comment = aEntry.constraintData->GetComment();
88
89 // Generate layer clause if layers are specified
90 if( aEntry.layerCondition.any() && aBoard )
91 ctx.layerClause = generateLayerClause( aEntry.layerCondition, aBoard );
92
93 // Generate the rule text from the constraint data
94 wxString ruleText = aEntry.constraintData->GenerateRule( ctx );
95
96 // If severity is specified and not default, we need to inject it
97 // The GenerateRule method should handle this, but we verify here
99 {
100 wxString severityClause = generateSeverityClause( aEntry.severity );
101
102 if( !severityClause.IsEmpty() && !ruleText.Contains( "(severity" ) )
103 {
104 // Insert severity clause before the closing paren
105 size_t lastParen = ruleText.rfind( ')' );
106
107 if( lastParen != wxString::npos )
108 {
109 ruleText = ruleText.Left( lastParen ) + "\n\t" + severityClause + ")";
110 }
111 }
112 }
113
114 return ruleText;
115}
116
117
118wxString DRC_RULE_SAVER::generateLayerClause( const LSET& aLayers, const BOARD* aBoard )
119{
120 if( !aBoard || !aLayers.any() )
121 return wxEmptyString;
122
123 wxString layerStr = "(layer";
124
125 for( PCB_LAYER_ID layer : aLayers.Seq() )
126 layerStr += " \"" + aBoard->GetLayerName( layer ) + "\"";
127
128 layerStr += ")";
129
130 return layerStr;
131}
132
133
135{
136 switch( aSeverity )
137 {
138 case RPT_SEVERITY_IGNORE: return "(severity ignore)";
139 case RPT_SEVERITY_WARNING: return "(severity warning)";
140 case RPT_SEVERITY_ERROR: return "(severity error)";
141 case RPT_SEVERITY_EXCLUSION: return "(severity exclusion)";
142 default: return wxEmptyString;
143 }
144}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:730
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)
bool SaveFile(const wxString &aPath, const std::vector< DRC_RE_LOADED_PANEL_ENTRY > &aEntries, const BOARD *aBoard=nullptr)
Save all panel entries to a file.
wxString generateLayerClause(const LSET &aLayers, const BOARD *aBoard)
Generate a layer clause from an LSET.
wxString generateRuleText(const DRC_RE_LOADED_PANEL_ENTRY &aEntry, const BOARD *aBoard)
Generate the rule text for a single panel entry.
wxString GenerateRulesText(const std::vector< DRC_RE_LOADED_PANEL_ENTRY > &aEntries, const BOARD *aBoard=nullptr)
Generate rule text from panel entries.
wxString generateSeverityClause(SEVERITY aSeverity)
Generate a severity clause.
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:313
wxString GetComment()
Get the comment associated with the rule.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString ruleName
wxString originalRuleText
wxString condition
bool wasEdited
LSET layerCondition
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
SEVERITY severity
wxString result
Test unit parsing edge cases and error handling.