27#include <wx/convauto.h>
29#include <wx/filename.h>
58 if( !wxFileName::FileExists( aPath ) )
68 bool exists = wxFileName::FileExists( aPath );
73 wxFFile file( aPath, wxS(
"rb" ) );
76 if( !file.IsOpened() || !file.ReadAll( &
text, wxConvAuto() ) || ( file.Length() > 0 &&
text.IsEmpty() ) )
79 text.Replace( wxS(
"\r\n" ), wxS(
"\n" ) );
106 std::vector<DRC_RE_LOADED_PANEL_ENTRY> entries = loader.
LoadFromString( aText, &parsed );
125 if( !entry.constraintData )
128 entry.constraintData->SetId(
m_nextId++ );
130 wxString layerSource = entry.layerSource;
133 if(
m_board && entry.layerCondition.count() == 1 )
135 PCB_LAYER_ID layer = entry.layerCondition.Seq().front();
137 if( layerSource ==
m_board->GetLayerName( layer ) )
141 entry.constraintData->SetLayerSource( layerSource );
143 if( !entry.layerSource.IsEmpty() )
144 entry.constraintData->SetLayers( entry.layerCondition.Seq() );
146 entry.constraintData->SetSeverity( entry.severity );
154 std::string
utf8 = std::string(
text.mb_str( wxConvUTF8 ) );
188 std::set<DRC_CONSTRAINT_T>
result;
199 std::vector<std::shared_ptr<DRC_RULE>> rules;
203 DRC_RULES_PARSER parser( wxS(
"(version 2)\n" ) + ruleText, wxS(
"Rule Document" ) );
204 parser.
Parse( rules,
nullptr );
211 for(
const std::shared_ptr<DRC_RULE>& rule : rules )
232 for(
size_t i = 0; i <
m_entries.size(); ++i )
241 result.push_back( (
int) i );
249 result.push_back( (
int) i );
267 for(
size_t i = 0; i <
m_entries.size(); ++i )
270 result.push_back( (
int) i );
304 for(
size_t i = 0; i <
m_entries.size(); ++i )
355 wxString baseName =
source.ruleName;
356 int lastSpace = baseName.Find(
' ',
true );
358 if( lastSpace != wxNOT_FOUND )
362 if( baseName.Mid( lastSpace + 1 ).ToLong( &number ) )
363 baseName = baseName.Left( lastSpace );
368 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> data =
371 data->CopyFrom( *
source.constraintData );
372 data->SetRuleName(
name );
373 data->SetIsNew(
false );
376 data->SetGeneratedRule( wxEmptyString );
382 copy.wasEdited =
true;
398 auto it = std::find( order.begin(), order.end(), aIndex );
400 if( it == order.end() )
403 int position = (int) ( it - order.begin() ) + ( aDelta > 0 ? 1 : -1 );
405 if( position < 0 || position >= (
int) order.size() )
408 int movedGroup =
m_entries[aIndex].ruleGroupId;
409 int otherGroup =
m_entries[order[position]].ruleGroupId;
411 if( movedGroup <= 0 || otherGroup <= 0 || movedGroup == otherGroup )
419 std::vector<int> groups;
423 if( std::find( groups.begin(), groups.end(), entry.ruleGroupId ) == groups.end() )
424 groups.push_back( entry.ruleGroupId );
427 std::iter_swap( std::find( groups.begin(), groups.end(), movedGroup ),
428 std::find( groups.begin(), groups.end(), otherGroup ) );
430 std::vector<DRC_RE_LOADED_PANEL_ENTRY> reordered;
433 for(
int group : groups )
437 if( entry.ruleGroupId ==
group )
438 reordered.push_back( std::move( entry ) );
449 int aExceptIndex )
const
451 if( aExceptIndex >= 0 && aExceptIndex < (
int)
m_entries.size() )
452 aType =
m_entries[aExceptIndex].panelType;
454 for(
size_t i = 0; i <
m_entries.size(); ++i )
456 if( (
int) i == aExceptIndex )
469 auto exists = [
this](
const wxString& aName )
473 if( entry.ruleName == aName )
481 wxString
name = wxString::Format( wxS(
"%s %d" ), aBase, n );
483 while( exists(
name ) )
484 name = wxString::Format( wxS(
"%s %d" ), aBase, ++n );
499 const wxString keyword = wxS(
"(constraint" );
500 size_t pos = aText.find( keyword );
502 auto isSpace = []( wxUniChar aChar )
504 return aChar ==
' ' || aChar ==
'\t' || aChar ==
'\n' || aChar ==
'\r';
507 while( pos != wxString::npos )
509 size_t code = pos + keyword.length();
511 while( code < aText.length() &&
isSpace( aText[code] ) )
514 size_t end = code + aCode.length();
516 if( code > pos + keyword.length() && aText.compare( code, aCode.length(), aCode ) == 0
517 && (
end >= aText.length() ||
isSpace( aText[
end] ) || aText[
end] ==
')' ) )
522 pos = aText.find( keyword, pos + keyword.length() );
525 return wxString::npos;
Information pertinent to a Pcbnew printed circuit board.
Maps DRC rule constraints to appropriate editor panels.
DRC_RULE_EDITOR_CONSTRAINT_NAME GetHomePanel(DRC_CONSTRAINT_T aConstraintType)
The panel whose page lists a text rule that sets this constraint, or CUSTOM_RULE when no panel edits ...
wxString GetConstraintCode() const
void SetLayers(std::vector< PCB_LAYER_ID > aLayers)
void SetGeneratedRule(const wxString &aRule)
void SetLayerSource(const wxString &aSource)
void SetSeverity(SEVERITY aSeverity)
void SetRuleCondition(const wxString &aRuleCondition)
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
std::vector< int > IndicesSetting(DRC_CONSTRAINT_T aConstraint) const
Every rule that sets aConstraint, whichever page edits it.
void SetText(const wxString &aText)
Take the text of the Custom Rules page.
bool Save(wxString *aError)
Write the board's rules file.
static size_t FindConstraintClause(const wxString &aText, const wxString &aCode)
Position of the constraint clause for aCode in rule text, or npos.
void UpdateEntry(int aIndex, const DRC_RE_LOADED_PANEL_ENTRY &aEdited)
Write an edited rule back.
std::vector< int > IndicesFor(DRC_RULE_EDITOR_CONSTRAINT_NAME aType) const
void Load()
Read the board's own rules file.
static std::set< DRC_CONSTRAINT_T > ConstraintsOf(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
The DRC constraints a rule sets, read back through the rules parser.
bool parseInto(const wxString &aText)
wxString UniqueRuleName(const wxString &aBase) const
std::vector< DRC_RE_LOADED_PANEL_ENTRY > m_entries
int Add(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
DRC_RULE_DOCUMENT(BOARD *aBoard)
void LoadFrom(const wxString &aPath)
Replace the rules with those of another board's file, as an import.
bool IsRuleNameUnique(const wxString &aName, DRC_RULE_EDITOR_CONSTRAINT_NAME aType, int aExceptIndex) const
DRC_RE_FILE_TRIVIA m_trivia
bool load(const wxString &aPath)
Returns false when the file could not be read, leaving the document as it was.
wxString GetText() const
The rules as text, for the Custom Rules page.
int Duplicate(int aIndex)
bool Move(int aIndex, int aDelta, DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Move a rule one step among the rules listed for aType.
static std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > CreateConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType, const DRC_RE_BASE_CONSTRAINT_DATA &aBase)
Create the data object that holds a rule of the given constraint type, taking the identity fields fro...
Loads DRC rules from .kicad_dru files and converts them to panel entries.
const DRC_RE_FILE_TRIVIA & GetFileTrivia() const
std::vector< DRC_RE_LOADED_PANEL_ENTRY > LoadFromString(const wxString &aRulesText, bool *aParsed=nullptr)
Load rules from a text string.
Saves DRC panel entries back to .kicad_dru files.
wxString GenerateRulesText(const std::vector< DRC_RE_LOADED_PANEL_ENTRY > &aEntries, const BOARD *aBoard=nullptr, const DRC_RE_FILE_TRIVIA &aTrivia={})
Generate rule text from panel entries.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
wxString GetComment()
Get the comment associated with the rule.
void SetRuleName(wxString aRuleName)
Set the name of the rule.
void SetComment(wxString aComment)
Set the comment for the rule.
std::string utf8(const wxString &aText)
DRC_RULE_EDITOR_CONSTRAINT_NAME
static bool isSpace(char cc)
Test for whitespace.
PCB_LAYER_ID
A quick note on layer IDs:
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
The text of a rules file that lies outside every rule, kept so a save can reproduce it.
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString layerSource
Original layer text: "inner", "outer", or layer name.
int ruleGroupId
Shared by entries split out of one rule. Zero when the entry stands alone.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
DRC_RULE_EDITOR_CONSTRAINT_NAME panelType
wxString result
Test unit parsing edge cases and error handling.