KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_connectivity_export.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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#include <richio.h>
25#include <reporter.h>
26#include <schematic.h>
27#include <sch_screen.h>
28#include <sch_symbol.h>
29#include <sch_rule_area.h>
30#include <wx/xml/xml.h>
31#include <wx/filename.h>
33#include <algorithm>
34
35BOOST_AUTO_TEST_SUITE( ConnectivityExport )
36
37BOOST_AUTO_TEST_CASE( ExportRefreshesSymbolRuleAreaMembership )
38{
39 SETTINGS_MANAGER settings;
40 std::unique_ptr<SCHEMATIC> schematic;
41 KI_TEST::LoadSchematic( settings, "netlists/multinetclasses/multinetclasses", schematic );
42 const auto sheet = schematic->Hierarchy().front();
43 SCH_SCREEN& screen = *sheet.LastScreen();
44 SCH_SYMBOL* symbol = nullptr;
45
46 for( SCH_ITEM* item : screen.Items().OfType( SCH_SYMBOL_T ) )
47 {
48 auto* candidate = static_cast<SCH_SYMBOL*>( item );
49
50 if( !candidate->IsPower() && !candidate->GetRef( &sheet ).StartsWith( "#" ) )
51 {
52 symbol = candidate;
53 break;
54 }
55 }
56
57 BOOST_REQUIRE( symbol );
58 auto areas = screen.Items().OfType( SCH_RULE_AREA_T );
59 BOOST_REQUIRE( areas.begin() != areas.end() );
60 auto* area = static_cast<SCH_RULE_AREA*>( *areas.begin() );
61 screen.Remove( symbol, false );
62 symbol->SetPosition( area->GetBoundingBox().Centre() );
63 screen.Append( symbol );
64 std::unordered_set<SCH_SCREEN*> screens{ &screen };
66 BOOST_REQUIRE( symbol->GetRuleAreaCache().contains( area ) );
67 const VECTOR2I originalPosition = symbol->GetPosition();
68 area->SetDNP( true );
69 symbol->SetDNP( false, &sheet );
70 symbol->SetPosition( VECTOR2I( -100000000, -100000000 ) );
71 BOOST_REQUIRE( symbol->ResolveDNP( &sheet ) );
72 const wxString reference = symbol->GetRef( &sheet );
73 const wxString path = wxFileName::CreateTempFileName(
74 wxFileName::GetTempDir() + "/connectivity-export-areas-" );
75 NETLIST_EXPORTER_XML exporter( schematic.get() );
76 auto exportDnp = [&]()
77 {
79 BOOST_REQUIRE_MESSAGE( exporter.WriteNetlist( path, 0, reporter ), reporter.GetMessages() );
80 wxXmlDocument document;
81 BOOST_REQUIRE( document.Load( path ) );
82 bool found = false;
83 bool dnp = false;
84
85 for( wxXmlNode* section = document.GetRoot()->GetChildren(); section; section = section->GetNext() )
86 {
87 if( section->GetName() != "components" )
88 continue;
89
90 for( wxXmlNode* component = section->GetChildren(); component; component = component->GetNext() )
91 {
92 if( component->GetAttribute( "ref" ) != reference )
93 continue;
94
95 found = true;
96
97 for( wxXmlNode* property = component->GetChildren(); property; property = property->GetNext() )
98 dnp |= property->GetName() == "property" && property->GetAttribute( "name" ) == "dnp";
99 }
100 }
101
102 BOOST_REQUIRE( found );
103 return dnp;
104 };
105
106 BOOST_CHECK( !exportDnp() );
107 BOOST_CHECK( !symbol->ResolveDNP( &sheet ) );
108 symbol->SetPosition( originalPosition );
109 BOOST_CHECK( exportDnp() );
110 BOOST_CHECK( symbol->ResolveDNP( &sheet ) );
111}
112
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter)
Write to specified output file.
Generate a generic XML based netlist file.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:165
const std::unordered_set< SCH_RULE_AREA * > & GetRuleAreaCache() const
Get the cache of rule areas enclosing this item.
Definition sch_item.h:694
bool ResolveDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
Definition sch_item.cpp:375
static std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > UpdateRuleAreasInScreens(std::unordered_set< SCH_SCREEN * > &screens, KIGFX::SCH_VIEW *view)
Update all rule area connectvity / caches in the given sheet paths.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Schematic symbol object.
Definition sch_symbol.h:75
virtual void SetDNP(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:935
VECTOR2I GetPosition() const override
Definition sch_symbol.h:934
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
A wrapper for reporting to a wxString object.
Definition reporter.h:242
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
IbisParser parser & reporter
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_RULE_AREA_T
Definition typeinfo.h:166
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683