KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_unconnected_save.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 *
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, see <https://www.gnu.org/licenses/>.
18 */
19
24
27#include <board.h>
29#include <pcb_marker.h>
30#include <drc/drc_item.h>
33
35{
37 m_settingsManager( true /* headless */ )
38 {
39 m_board = std::make_unique<BOARD>();
40 m_board->SetProject( &m_settingsManager.Prj() );
41 }
42
44 std::unique_ptr<BOARD> m_board;
45};
46
47
49{
50 // Create a mock unconnected items DRC marker
51 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNCONNECTED_ITEMS );
52 drcItem->SetItems( KIID( "12345678-1234-1234-1234-123456789abc" ), KIID( "87654321-4321-4321-4321-cba987654321" ) );
53
54 PCB_MARKER* marker = new PCB_MARKER( drcItem, VECTOR2I( 1000, 1000 ) );
55 m_board->Add( marker );
56
57 // Exclude the marker
58 marker->SetExcluded( true, "Test exclusion comment" );
59
60 // Record the DRC exclusions (this is what happens during save)
61 m_board->RecordDRCExclusions();
62
63 // Verify the exclusion was recorded
64 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
65 BOOST_CHECK( !bds.m_DrcExclusions.empty() );
66
67 // Get the serialized exclusion
68 wxString serialized = marker->SerializeToString();
69 BOOST_CHECK( bds.m_DrcExclusions.count( serialized ) == 1 );
70 BOOST_CHECK( bds.m_DrcExclusionComments[serialized] == "Test exclusion comment" );
71
72 // Simulate saving and reloading
73 // Clear the marker but keep the exclusions in design settings
74 m_board->DeleteMARKERs();
75
76 // Create a new marker with the same properties
77 std::shared_ptr<DRC_ITEM> newDrcItem = DRC_ITEM::Create( DRCE_UNCONNECTED_ITEMS );
78 newDrcItem->SetItems( KIID( "12345678-1234-1234-1234-123456789abc" ),
79 KIID( "87654321-4321-4321-4321-cba987654321" ) );
80
81 PCB_MARKER* newMarker = new PCB_MARKER( newDrcItem, VECTOR2I( 1000, 1000 ) );
82 m_board->Add( newMarker );
83
84 // Resolve exclusions (this is what happens when loading)
85 m_board->ResolveDRCExclusions( false );
86
87 // Verify the exclusion was restored
88 BOOST_CHECK( newMarker->IsExcluded() );
89 BOOST_CHECK( newMarker->GetComment() == "Test exclusion comment" );
90}
91
92
93BOOST_FIXTURE_TEST_CASE( DRCUnconnectedExclusionsMultipleSave, DRC_UNCONNECTED_SAVE_FIXTURE )
94{
95 // Create multiple unconnected items DRC markers
96 for( int i = 0; i < 5; ++i )
97 {
98 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNCONNECTED_ITEMS );
99 wxString id1 = wxString::Format( "12345678-1234-1234-1234-12345678%04d", i );
100 wxString id2 = wxString::Format( "87654321-4321-4321-4321-87654321%04d", i );
101 drcItem->SetItems( KIID( id1 ), KIID( id2 ) );
102
103 PCB_MARKER* marker = new PCB_MARKER( drcItem, VECTOR2I( 1000 * i, 1000 * i ) );
104 m_board->Add( marker );
105
106 // Exclude odd-numbered markers
107 if( i % 2 == 1 )
108 {
109 marker->SetExcluded( true, wxString::Format( "Exclusion %d", i ) );
110 }
111 }
112
113 // Record the DRC exclusions
114 m_board->RecordDRCExclusions();
115
116 // Verify the correct number of exclusions
117 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
118 BOOST_CHECK_EQUAL( bds.m_DrcExclusions.size(), 2 ); // Only odd markers: 1 and 3
119
120 // Clear all markers
121 m_board->DeleteMARKERs();
122
123 // Recreate all markers
124 for( int i = 0; i < 5; ++i )
125 {
126 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNCONNECTED_ITEMS );
127 wxString id1 = wxString::Format( "12345678-1234-1234-1234-12345678%04d", i );
128 wxString id2 = wxString::Format( "87654321-4321-4321-4321-87654321%04d", i );
129 drcItem->SetItems( KIID( id1 ), KIID( id2 ) );
130
131 PCB_MARKER* marker = new PCB_MARKER( drcItem, VECTOR2I( 1000 * i, 1000 * i ) );
132 m_board->Add( marker );
133 }
134
135 // Resolve exclusions
136 m_board->ResolveDRCExclusions( false );
137
138 // Verify only the correct markers are excluded
139 int excludedCount = 0;
140 for( PCB_MARKER* marker : m_board->Markers() )
141 {
142 if( marker->IsExcluded() )
143 {
144 excludedCount++;
145 // Verify it was one of the odd-numbered markers
146 BOOST_CHECK( marker->GetComment().Contains( "1" ) || marker->GetComment().Contains( "3" ) );
147 }
148 }
149 BOOST_CHECK_EQUAL( excludedCount, 2 );
150}
Container for design settings for a BOARD object.
std::map< wxString, wxString > m_DrcExclusionComments
std::set< wxString > m_DrcExclusions
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:417
Definition kiid.h:44
bool IsExcluded() const
Definition marker_base.h:89
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition marker_base.h:90
wxString GetComment() const
Definition marker_base.h:96
wxString SerializeToString() const
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:36
BOOST_FIXTURE_TEST_CASE(DRCUnconnectedExclusionsSave, DRC_UNCONNECTED_SAVE_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683