36#include <wx/filename.h>
42const wxString TWO_CLEARANCES_AND_A_HOLE = wxS(
"(version 2)\n"
45 "\t# keep this comment\n"
46 "\t(constraint clearance (min 0.3mm))\n"
47 "\t(condition \"A.NetClass == 'HV'\"))\n"
49 "\t(constraint hole_size (min 0.2mm)))\n"
51 "\t(constraint clearance (min 0.5mm)))\n" );
54int countOf(
const wxString& aText,
const wxString& aNeedle )
57 size_t pos = aText.find( aNeedle );
59 while( pos != wxString::npos )
62 pos = aText.find( aNeedle, pos + 1 );
69const wxString ONE_RULE_THREE_CONSTRAINTS = wxS(
"(version 2)\n"
70 "(rule \"under_fpga\"\n"
71 "\t(constraint clearance (min 0.1mm))\n"
72 "\t(constraint hole_size (min 0.2mm))\n"
73 "\t(constraint via_diameter (min 0.4mm))\n"
74 "\t(condition \"A.intersectsArea('underFPGA')\"))\n" );
87 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
109 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
113 data->SetConstraintCode( wxS(
"clearance" ) );
114 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( data )->SetNumericInputValue( 0.7 );
126 BOOST_CHECK(
text.Contains( wxS(
"(constraint clearance (min 0.7mm))" ) ) );
138 document.
SetText( wxS(
"(version 2)\n"
140 "\t(constraint clearance (min 0.3mm))\n"
141 "\t(constraint hole_size (min 0.2mm)))\n"
143 "\t(constraint clearance (min 0.5mm)))\n" ) );
151 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( hole.
constraintData )
152 ->SetNumericInputValue( 0.25 );
159 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(constraint clearance (min 0.3mm))" ) )
160 &&
text.Contains( wxS(
"(constraint hole_size (min 0.25mm))" ) ),
161 "the merged rule lost a constraint:\n" <<
text );
170 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
184 BOOST_CHECK(
text.find( wxS(
"(rule \"second\"" ) ) <
text.find( wxS(
"(rule \"hole\"" ) ) );
185 BOOST_CHECK(
text.find( wxS(
"(rule \"hole\"" ) ) <
text.find( wxS(
"(rule \"first\"" ) ) );
197 BOOST_CHECK(
text.find( wxS(
"(rule \"first\"" ) ) <
text.find( wxS(
"(rule \"hole\"" ) ) );
198 BOOST_CHECK(
text.find( wxS(
"(rule \"hole\"" ) ) <
text.find( wxS(
"(rule \"second\"" ) ) );
208 auto custom = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( base );
209 custom->SetRuleText( wxS(
"(constraint clearance (min 0.2mm))\n(constraint hole_size (min 0.3mm))" ) );
226 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
227 wxString before = document.
GetText();
229 wxFileName missing( wxFileName::CreateTempFileName( wxS(
"qa_missing" ) ) );
230 wxRemoveFile( missing.GetFullPath() );
233 document.
LoadFrom( missing.GetFullPath() );
245 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
247 wxFileName other( wxFileName::CreateTempFileName( wxS(
"qa_import_source" ) ) );
248 wxFFile file( other.GetFullPath(), wxS(
"wb" ) );
251 file.Write( wxS(
"(version 2)\n(rule \"imported\"\n\t(constraint clearance (min 0.9mm)))\n" ) );
254 document.
LoadFrom( other.GetFullPath() );
261 wxRemoveFile( other.GetFullPath() );
272 const wxString smartQuoted =
L"(version 2)\n"
273 L"(rule \u201Coutside\u201D\n"
274 L"\t(constraint clearance (min 0.3mm))\n"
275 L"\t(condition \u201CA.NetClass == \u2018HV\u2019\u201D))\n";
277 wxFileName quoted( wxFileName::CreateTempFileName( wxS(
"qa_smart_quotes" ) ) );
278 wxFFile file( quoted.GetFullPath(), wxS(
"wb" ) );
281 file.Write( smartQuoted, wxConvUTF8 );
284 document.
LoadFrom( quoted.GetFullPath() );
290 wxRemoveFile( quoted.GetFullPath() );
298 const std::vector<wxString> openEnded = {
299 wxS(
"(constraint length (min 10mm))" ),
300 wxS(
"(constraint length (min 10mm))\n\t(constraint skew (max 1mm))" ),
301 wxS(
"(constraint skew (min 1mm))" ),
302 wxS(
"(constraint diff_pair_uncoupled (min 1mm))" ),
305 for(
const wxString& clauses : openEnded )
310 document.
SetText( wxS(
"(version 2)\n(rule \"open_ended\"\n\t" ) + clauses + wxS(
")\n" ) );
320 BOOST_CHECK_MESSAGE( !
text.Contains( wxS(
"2147" ) ),
321 "an absent bound was written back as the largest int, from " << clauses <<
":\n"
323 BOOST_CHECK_MESSAGE(
text.Contains( clauses ),
"the clauses were rewritten, from " << clauses <<
":\n"
334 document.
SetText( wxS(
"(version 2)\n"
335 "(rule \"both_ends\"\n"
336 "\t(constraint length (min 10mm) (max 20mm)))\n"
337 "(rule \"uncoupled\"\n"
338 "\t(constraint diff_pair_uncoupled (max 5mm)))\n"
340 "\t(constraint skew (max 1mm)))\n" ) );
354 document.
SetText( wxS(
"(version 2)\n"
355 "(rule \"min_only\"\n"
356 "\t(condition \"A.NetClass == 'Default'\")\n"
357 "\t(constraint track_width (min 0.2mm)))\n" ) );
369 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(rule \"renamed\"\n"
370 "\t(condition \"A.NetClass == 'Default'\")\n"
371 "\t(constraint track_width (min 0.2mm)))" ) ),
372 "the body lost its indent:\n"
381 const std::vector<wxString> partial = {
382 wxS(
"(constraint track_width (min 0.2mm))" ),
383 wxS(
"(constraint track_width (min 0.2mm) (max 0.4mm))" ),
384 wxS(
"(constraint diff_pair_gap (min 0.1mm))" ),
387 for(
const wxString& clauses : partial )
392 document.
SetText( wxS(
"(version 2)\n(rule \"partial\"\n\t" ) + clauses + wxS(
")\n" ) );
402 BOOST_CHECK_MESSAGE(
text.Contains( clauses ),
"the clauses were rewritten, from " << clauses <<
":\n"
411 const std::vector<std::pair<wxString, wxString>> rules = {
412 { wxS(
"(constraint via_diameter (min 0.2mm))\n\t(constraint hole_size (min 0.1mm))" ),
413 wxS(
"A.Via_Type == 'Micro' && A.NetClass == 'HS'" ) },
414 { wxS(
"(constraint disallow via)" ), wxS(
"A.Pad_Type == 'SMD' && A.NetClass == 'HS'" ) },
417 for(
const auto& [clauses, condition] : rules )
419 for(
size_t i = 0;; ++i )
424 document.
SetText( wxS(
"(version 2)\n(rule \"split\"\n\t" ) + clauses
425 + wxS(
"\n\t(constraint clearance (min 0.15mm))\n\t(condition \"" ) + condition
428 if( i >= document.
Entries().size() )
439 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(condition \"" ) + condition + wxS(
"\")" ) ),
440 "editing part " << i <<
" changed the condition:\n"
450 const std::vector<wxString> conditions = {
451 wxS(
"A.Via_Type != 'Micro'" ),
452 wxS(
"A.Via_Type == 'Micro' || A.Via_Type == 'Blind'" ),
453 wxS(
"A.NetClass == 'HS' && A.Via_Type == 'Micro'" ),
454 wxS(
"A.NetName == 'Micro'" ),
457 for(
const wxString& condition : conditions )
462 document.
SetText( wxS(
"(version 2)\n(rule \"vias\"\n"
463 "\t(constraint via_diameter (min 0.2mm))\n"
464 "\t(constraint hole_size (min 0.1mm))\n"
466 + condition + wxS(
"\"))\n" ) );
477 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(condition \"" ) + condition + wxS(
"\")" ) ),
478 "the condition was rewritten, from " << condition <<
":\n"
489 document.
SetText( ONE_RULE_THREE_CONSTRAINTS );
496 std::static_pointer_cast<DRC_RE_VIA_STYLE_CONSTRAINT_DATA>( edited.
constraintData )
504 text.Contains( wxS(
"(condition \"A.Via_Type == 'Micro' && A.intersectsArea('underFPGA')\")" ) ),
505 "the via type was dropped:\n"
509 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(condition \"A.intersectsArea('underFPGA')\")" ) ),
510 "the via type narrowed the other parts:\n"
520 document.
SetText( ONE_RULE_THREE_CONSTRAINTS );
532 BOOST_CHECK_MESSAGE( countOf(
text, wxS(
"(rule " ) ) == 1,
"the rule was split in two:\n" <<
text );
533 BOOST_CHECK_MESSAGE(
text.Contains( wxS(
"(rule renamed" ) ),
"the new name was dropped:\n" <<
text );
534 BOOST_CHECK_MESSAGE( !
text.Contains( wxS(
"under_fpga\"\n" ) ),
"the old name survived:\n" <<
text );
551 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( data )->SetNumericInputValue( 0.3 );
559 BOOST_CHECK_MESSAGE( countOf(
text, wxS(
"(rule " ) ) == 2,
"two unrelated rules were merged:\n" <<
text );
568 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
571 auto custom = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( base );
572 custom->SetRuleText( wxS(
"(constraint clearance (min 0.2mm))\n(constraint hole_size (min 0.3mm))" ) );
595 document.
SetText( wxS(
"(version 2)\n"
597 "\t(constraint length (min 39mm) (max 40mm) (opt 40.5mm))\n"
598 "\t(condition \"A.NetClass == 'DDR4'\"))\n" ) );
603 "a plain length rule belongs under absolute length" );
605 "the same rule was also listed under matched length on the same page" );
614 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
616 wxString broken = wxS(
"(version 2)\n(rule \"unfinished\"\n\t(constraint clearance (min 0.3mm)" );
623 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
633 wxString
text = wxS(
"(constraint clearance_under_fpga (min 1mm))\n(constraint clearance (min 2mm))" );
636 text.find( wxS(
"(constraint clearance (" ) ) );
648 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
664 BOOST_CHECK_MESSAGE( document.
Entries()[clearances[0]].constraintData
665 != document.
Entries()[clearances[1]].constraintData,
666 "the copy shares its data with the original" );
668 auto original = std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>(
669 document.
Entries()[clearances[0]].constraintData );
670 auto duplicated = std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>(
671 document.
Entries()[clearances[1]].constraintData );
675 BOOST_CHECK_CLOSE( duplicated->GetNumericInputValue(), original->GetNumericInputValue(), 0.0001 );
676 BOOST_CHECK_EQUAL( duplicated->GetRuleCondition(), original->GetRuleCondition() );
680 BOOST_CHECK_MESSAGE( countOf(
text, wxS(
"(rule " ) ) == 4,
"the copy was not written once:\n" <<
text );
683 BOOST_CHECK_MESSAGE( countOf(
text, wxS(
"# keep this comment" ) ) == 2,
684 "the copy lost the comment written inside the rule:\n"
686 BOOST_CHECK_MESSAGE( countOf(
text, wxS(
"# header comment" ) ) == 1,
687 "the file header was emitted more than once:\n"
703 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
706 BOOST_REQUIRE_GE( first, 0 );
709 int second = document.
Duplicate( first );
710 BOOST_REQUIRE_GE( second, 0 );
720 document.
SetText( TWO_CLEARANCES_AND_A_HOLE );
744 document.
SetText( wxS(
"(version 2)\n"
746 "\t(constraint clearance (min 0.2mm))\n"
747 "\t(constraint zone_connection solid))\n" ) );
761 const std::vector<wxString> governed = { wxS(
"min_clearance" ), wxS(
"min_connection" ),
762 wxS(
"min_track_width" ), wxS(
"min_via_annular_width" ),
763 wxS(
"min_via_diameter" ), wxS(
"min_through_hole_diameter" ),
764 wxS(
"min_hole_to_hole" ), wxS(
"min_hole_clearance" ),
765 wxS(
"min_silk_clearance" ), wxS(
"min_text_height" ),
766 wxS(
"min_silk_line_width" ), wxS(
"min_copper_edge_clearance" ) };
770 for(
const wxString& setting : governed )
775 "no rule constraint governs " << setting.ToStdString() );
777 "no page title for " << setting.ToStdString() );
789 const std::vector<std::pair<wxString, CONSTRAINT_DEFAULT_FIELD>> edited =
793 for(
size_t i = 0; i < edited.size(); ++i )
795 for(
size_t j = i + 1; j < edited.size(); ++j )
797 BOOST_CHECK_MESSAGE( edited[i].second.settingName != edited[j].second.settingName,
798 "two rule pages edit " << edited[i].second.settingName.ToStdString() );
799 BOOST_CHECK_MESSAGE( edited[i].second.member != edited[j].second.member,
800 "two rule pages write the member behind "
801 << edited[i].second.settingName.ToStdString() );
806 for(
const auto& [
group, field] : edited )
809 "no page edits " << field.settingName.ToStdString() );
818 for(
const wxString& setting :
819 { wxS(
"solder_mask_expansion" ), wxS(
"solder_mask_min_width" ), wxS(
"solder_paste_margin" ) } )
822 "no rule page holds " << setting.ToStdString() );
835 using COPY = void ( * )(
const BDS&, BDS& );
840 { wxS(
"Solder Mask/Paste" ),
843 auto travels = []( COPY aCopy,
int BDS::* aMember )
856 auto it = copies.find(
group );
859 BOOST_CHECK_MESSAGE( travels( it->second, field.member ),
860 field.settingName.ToStdString() <<
" does not travel with " <<
group.ToStdString() );
Container for design settings for a BOARD object.
Information pertinent to a Pcbnew printed circuit board.
BOARD_DESIGN_SETTINGS & GetDesignSettings()
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 GetRuleCondition()
The custom rules of one board, held in memory while Board Setup is open.
bool HasUnparsedText() const
True while the Custom Rules text could not be parsed.
void SetText(const wxString &aText)
Take the text of the Custom Rules page.
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< DRC_RE_LOADED_PANEL_ENTRY > & Entries()
std::vector< int > IndicesFor(DRC_RULE_EDITOR_CONSTRAINT_NAME aType) const
wxString UniqueRuleName(const wxString &aBase) const
int Add(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
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
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 wxString ConstraintToKicadDrc(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Convert a constraint type into the keyword used in a .kicad_drc file.
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...
LSET is a set of PCB_LAYER_IDs.
static void CopySettings(const BOARD_DESIGN_SETTINGS &aFrom, BOARD_DESIGN_SETTINGS &aTo)
Copies every value this page edits, which is what the Constraints import group carries.
static wxString PageEditingDefault(const wxString &aSetting)
The title of the page that edits a board default, or empty where no rule page edits one.
static bool RulesCanOverride(const wxString &aSetting, DRC_CONSTRAINT_T *aConstraint)
Whether rules can set a value other than this board default, and the DRC constraint that does it.
static wxString PageTitleFor(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
The title of the page that edits rules of this type, or empty when no page does.
static std::vector< std::pair< wxString, CONSTRAINT_DEFAULT_FIELD > > EditedDefaults()
Every board default a rule page edits, each with the group it is imported with.
static void CopySettings(const BOARD_DESIGN_SETTINGS &aFrom, BOARD_DESIGN_SETTINGS &aTo)
Copies every value this page edits, which is what the Solder Mask/Paste import group carries.
DRC_RULE_EDITOR_CONSTRAINT_NAME
@ MATCHED_LENGTH_DIFF_PAIR
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_CASE(UneditedRulesRoundTripVerbatim)
BOOST_AUTO_TEST_SUITE_END()