KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_dump.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
20#include "conn_dump.h"
21#include "conn_facade.h"
22
23#include <json_common.h>
24#include <netclass.h>
25#include <project.h>
28#include <sch_bus_entry.h>
29#include <sch_connection.h>
30#include <sch_line.h>
31#include <sch_pin.h>
32#include <sch_screen.h>
33#include <sch_sheet.h>
34#include <sch_sheet_pin.h>
35#include <sch_symbol.h>
36#include <schematic.h>
37
38#include <map>
39#include <set>
40#include <stdexcept>
41
42
43namespace
44{
45using JSON = nlohmann::json;
46using KEY = std::pair<std::string, std::string>;
50
51std::string utf8( const wxString& aText )
52{
53 return aText.ToStdString( wxConvUTF8 );
54}
55
56
57const char* typeName( CONNECTION_TYPE aType )
58{
59 switch( aType )
60 {
61 case CONNECTION_TYPE::NET: return "net";
62 case CONNECTION_TYPE::BUS: return "vector";
63 case CONNECTION_TYPE::BUS_GROUP: return "group";
64 case CONNECTION_TYPE::NONE: return "none";
65 }
66
67 return "none";
68}
69
70
71JSON valueJson( const DUMP_VALUE& aValue )
72{
73 std::set<std::string> classes;
74
75 for( const wxString& name : aValue.netclasses )
76 classes.insert( utf8( name ) );
77
78 JSON row = { { "name", utf8( aValue.name ) }, { "local_name", utf8( aValue.localName ) },
79 { "type", typeName( aValue.type ) },
80 { "driver", aValue.driver ? utf8( aValue.driver->AsString() ) : "" },
81 { "driver_path", aValue.driver ? utf8( aValue.driverPath.AsString() ) : "" },
82 { "members", JSON::array() }, { "netclasses", classes } };
83
84 for( const DUMP_VALUE& member : aValue.members )
85 row["members"].push_back( valueJson( member ) );
86
87 return row;
88}
89
90
91std::set<wxString> resolvedClasses( const wxString& aName, NET_SETTINGS* aSettings )
92{
93 std::set<wxString> classes;
94
95 if( aSettings )
96 {
97 for( const NETCLASS* constituent : aSettings->GetEffectiveNetClass( aName )->GetConstituentNetclasses() )
98 classes.insert( constituent->GetName() );
99 }
100
101 return classes;
102}
103
104
105DUMP_VALUE valueRow( const wxString& aName, const wxString& aLocalName, CONNECTION_TYPE aType,
106 SCH_ITEM* aDriver, const KIID_PATH& aDriverPath, NET_SETTINGS* aSettings )
107{
108 DUMP_VALUE value;
109 value.name = aName;
110 value.localName = aLocalName;
111 value.type = aType;
112 value.netclasses = resolvedClasses( aName, aSettings );
113
114 if( aDriver )
115 {
116 value.driver = aDriver->m_Uuid;
117 value.driverPath = aDriverPath;
118 }
119
120 return value;
121}
122
123
124DUMP_VALUE memberRow( const SCH_CONNECTIVITY::BUS_SCHEMA::NODE& aNode,
125 const SCH_CONNECTIVITY::BUS_MEMBERS& aMembers, const wxString& aPath,
126 const wxString& aNamePrefix, const wxString& aLocalPrefix, SCH_ITEM* aDriver,
127 const KIID_PATH& aDriverPath, NET_SETTINGS* aSettings )
128{
130
131 if( aNode.kind == NODE::KIND::NET )
132 {
133 const auto& leaf = aMembers.leaves.at( aNode.leaf.value() );
134 return valueRow( leaf.Name(), aMembers.schema->leaves.at( aNode.leaf.value() ).name,
135 CONNECTION_TYPE::NET, leaf.Driver(), leaf.Sheet(), aSettings );
136 }
137
138 const wxString localName = aLocalPrefix + aNode.text;
139 DUMP_VALUE row = valueRow( aPath + aNamePrefix + aNode.text, localName,
140 aNode.kind == NODE::KIND::VECTOR ? CONNECTION_TYPE::BUS : CONNECTION_TYPE::BUS_GROUP,
141 aDriver, aDriverPath, aSettings );
142 wxString namePrefix = aNamePrefix;
143 wxString localPrefix = aLocalPrefix;
144
145 if( aNode.kind == NODE::KIND::GROUP && !aNode.prefix.empty() )
146 {
147 namePrefix += aNode.prefix + ".";
148 localPrefix += aNode.prefix + ".";
149 }
150
151 for( const auto& member : aNode.members )
152 {
153 row.members.push_back( memberRow( member, aMembers, aPath, namePrefix, localPrefix, aDriver, aDriverPath,
154 aSettings ) );
155 }
156
157 return row;
158}
159
160
161DUMP_VALUE connectionRow( const SCH_CONNECTIVITY::ITEM_VIEW& aView,
163 CONNECTION_TYPE aType, NET_SETTINGS* aSettings )
164{
165 const wxString name = aView.Name();
166 SCH_ITEM* driver = aView.Driver();
167 const KIID_PATH driverPath = aView.Sheet();
168 DUMP_VALUE row = valueRow( name, aView.FullLocalName(), aType, driver, driverPath, aSettings );
169 const auto members = aView.Members();
170
171 if( !members.tree )
172 return row;
173
174 const auto groupPrefix = [&]( const wxString& aPrefix )
175 {
176 if( aPrefix.empty() )
177 return wxString();
178
179 return aComponent.suffix ? wxString::Format( "%s_%u.", aPrefix, aComponent.suffix ) : aPrefix + wxS( "." );
180 };
181 const bool group = members.tree->kind == SCH_CONNECTIVITY::BUS_SCHEMA::NODE::KIND::GROUP;
182 const wxString localPrefix = group ? groupPrefix( members.tree->prefix ) : wxString();
183 const auto& best = aComponent.content->best;
184
185 // Members are named after the canonical group even where this row spells another prefix
186 const wxString namePrefix = group && best && best->schema
187 && best->schema->shape == SCH_CONNECTIVITY::BUS_SCHEMA::SHAPE::GROUP
188 ? groupPrefix( best->schema->prefix )
189 : localPrefix;
190 const wxString path = name.Left( name.length() - aView.Name( true ).length() );
191
192 for( const auto& member : members.tree->members )
193 {
194 row.members.push_back( memberRow( member, members, path, namePrefix, localPrefix, driver, driverPath,
195 aSettings ) );
196 }
197
198 return row;
199}
200
201
202DUMP_VALUE connectionRow( const SCH_CONNECTION* aConnection, NET_SETTINGS* aSettings )
203{
204 if( !aConnection )
205 return valueRow( wxString(), wxString(), CONNECTION_TYPE::NONE, nullptr, KIID_PATH(), nullptr );
206
207 const wxString name = !aConnection->Driver() && aConnection->Name( true ) == "<NO NET>"
208 ? wxString() : aConnection->Name();
209 DUMP_VALUE row = valueRow( name, aConnection->FullLocalName(), aConnection->Type(), aConnection->Driver(),
210 aConnection->Sheet().Path(), aSettings );
211
212 // Member order encodes positional bus alignment, so only item rows are sorted
213 for( const auto& member : aConnection->Members() )
214 row.members.push_back( connectionRow( member.get(), aSettings ) );
215
216 return row;
217}
218}
219
220
221std::string SCH_CONNECTIVITY::Dump( const DUMP_ROWS& aRows )
222{
223 std::map<KEY, JSON> sorted;
224
225 for( const auto& [key, row] : aRows )
226 {
227 const std::string pathId = utf8( key.first.AsString() );
228 const std::string itemId = utf8( key.second.AsString() );
229 std::set<std::string> connected;
230
231 for( const KIID& neighbour : row.connected )
232 connected.insert( utf8( neighbour.AsString() ) );
233
234 JSON json = valueJson( row.value );
235 json["path"] = pathId;
236 json["item"] = itemId;
237 json["dangling"] = row.dangling;
238 json["dangling_ends"] = row.danglingEnds;
239 json["connected"] = connected;
240 sorted.emplace( KEY{ pathId, itemId }, std::move( json ) );
241 }
242
243 JSON result = JSON::array();
244
245 for( auto& [key, row] : sorted )
246 result.push_back( std::move( row ) );
247
248 return result.dump( 2 ) + "\n";
249}
250
251
252std::string SCH_CONNECTIVITY::Dump( SCHEMATIC& aSchematic )
253{
254 return Dump( CaptureRows( aSchematic ) );
255}
256
257
258std::string SCH_CONNECTIVITY::Dump( SCHEMATIC& aSchematic, const FACADE& aFacade )
259{
260 return Dump( CaptureRows( aSchematic, aFacade ) );
261}
262
263
265{
267 NET_SETTINGS* settings = aSchematic.IsValid()
268 ? aSchematic.Project().GetProjectFile().NetSettings().get()
269 : nullptr;
270
271 for( const SCH_SHEET_PATH& path : aSchematic.Hierarchy() )
272 {
273 const KIID_PATH pathId = path.Path();
274 const auto addItem =
275 [&]( SCH_ITEM* aItem )
276 {
277 DUMP_ROW row;
278 row.value = connectionRow( aItem->Connection( &path ), settings );
279 row.dangling = aItem->IsDangling();
280 row.danglingEnds = { aItem->IsDangling() };
281
282 if( auto* line = dynamic_cast<SCH_LINE*>( aItem ) )
283 {
284 row.danglingEnds = { line->IsStartDangling(), line->IsEndDangling() };
285 }
286 else if( auto* entry = dynamic_cast<SCH_BUS_ENTRY_BASE*>( aItem ) )
287 {
288 row.danglingEnds = { entry->IsStartDangling(), entry->IsEndDangling() };
289 }
290
291 for( const SCH_ITEM* neighbour : aItem->ConnectedItems( path ) )
292 row.connected.insert( neighbour->m_Uuid );
293
294 if( !rows.emplace( std::pair( pathId, aItem->m_Uuid ), std::move( row ) ).second )
295 {
296 throw std::runtime_error( "Duplicate connectivity item " + utf8( pathId.AsString() )
297 + utf8( aItem->m_Uuid.AsString() ) );
298 }
299 };
300
301 for( SCH_ITEM* item : path.LastScreen()->Items() )
302 {
303 if( item->Type() == SCH_SYMBOL_T )
304 {
305 for( SCH_PIN* pin : static_cast<SCH_SYMBOL*>( item )->GetPins( &path ) )
306 addItem( pin );
307 }
308 else if( item->Type() == SCH_SHEET_T )
309 {
310 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( item )->GetPins() )
311 addItem( pin );
312 }
313 else if( item->IsConnectable() )
314 {
315 addItem( item );
316 }
317 }
318 }
319
320 return rows;
321}
322
323
325{
326 const auto& publication = aFacade.Published();
327 const auto& keys = aFacade.Keys();
328 const auto& auxiliary = publication.Auxiliary();
329 std::unique_ptr<NET_SETTINGS> settings;
330
331 if( aSchematic.IsValid() )
332 {
333 auto source = aSchematic.Project().GetProjectFile().NetSettings();
334 settings = std::make_unique<NET_SETTINGS>( nullptr, "" );
335 settings->CopyFrom( *source );
336 aFacade.ApplyNetclasses( *settings );
337
338 // Chain patterns are shared consumer context, not the legacy graph's label assignments
339 for( const auto& [owner, patterns] : source->GetChainPatternAssignments() )
340 {
341 for( const auto& [matcher, name] : patterns )
342 settings->SetChainPatternAssignment( owner, matcher->GetPattern(), name );
343 }
344 }
345
346 std::map<INST_ID, SCH_SCREEN*> screens;
347
348 for( const SCH_SHEET_PATH& path : aSchematic.BuildSheetListSortedByPageNumbers() )
349 {
350 if( auto instance = keys.FindInstance( path.Path() ) )
351 screens.emplace( *instance, path.LastScreen() );
352 }
353
355
356 for( const auto& [key, result] : publication.Rows() )
357 {
358 const KIID_PATH& path = keys.Instance( key.inst );
359 auto view = aFacade.Connection( key.item, path );
360
361 if( !view )
362 throw std::runtime_error( "Cannot dump stale connectivity publication" );
363
364 DUMP_ROW row;
365 row.value = connectionRow( *view, publication.Components().at( result.component ), result.itemType,
366 settings.get() );
367 const auto& island = auxiliary.Islands().at( auxiliary.IslandOf().at( key ) ).value;
368 const auto dangling = island.dangling.at( key.item );
369 row.dangling = dangling != 0;
370 row.danglingEnds = { dangling != 0 };
371 const auto screen = screens.find( key.inst );
372
373 if( screen == screens.end() || !screen->second )
374 throw std::runtime_error( "Cannot dump connectivity without its source instance" );
375
376 SCH_ITEM* item = screen->second->GetConnectivityItem( key.item );
377
378 if( !item )
379 throw std::runtime_error( "Cannot dump connectivity without its source item" );
380
381 if( dynamic_cast<SCH_LINE*>( item ) || dynamic_cast<SCH_BUS_ENTRY_BASE*>( item ) )
382 row.danglingEnds = { ( dangling & 1 ) != 0, ( dangling & 2 ) != 0 };
383
384 // Undriven items keep the type their connection gets when it is created
386 && ( item->Type() == SCH_PIN_T || item->Type() == SCH_BUS_WIRE_ENTRY_T
387 || ( item->Type() == SCH_LINE_T && item->GetLayer() != LAYER_BUS ) ) )
388 {
390 }
391
392 const auto neighbors = auxiliary.NeighborsOf().find( key );
393
394 if( neighbors != auxiliary.NeighborsOf().end() )
395 row.connected.insert( neighbors->second.begin(), neighbors->second.end() );
396
397 rows.emplace( std::pair( path, key.item ), std::move( row ) );
398 }
399
400 return rows;
401}
const char * name
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
wxString AsString() const
Definition kiid.cpp:423
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:44
const std::vector< NETCLASS * > & GetConstituentNetclasses() const
Gets the netclasses which make up this netclass.
Definition netclass.cpp:314
NET_SETTINGS stores various net-related settings in a project context.
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const wxString &aNetName)
Fetches the effective (may be aggregate) netclass for the given net name.
std::shared_ptr< NET_SETTINGS > & NetSettings()
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
Holds all the data relating to one schematic.
Definition schematic.h:155
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:177
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:384
Base class for a bus or wire entry.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
wxString FullLocalName() const
SCH_SHEET_PATH Sheet() const
CONNECTION_TYPE Type() const
wxString Name(bool aIgnoreSheet=false) const
SCH_ITEM * Driver() const
const std::vector< std::shared_ptr< SCH_CONNECTION > > & Members() const
Model-owned source resolver.
void ApplyNetclasses(NET_SETTINGS &aSettings) const
Replace label-derived assignments from a complete publication; invalidate changed net caches.
const SESSION_KEYS & Keys() const
std::optional< ITEM_VIEW > Connection(const KIID &aItem, const KIID_PATH &aPath) const
const PUBLICATION & Published() const
BUS_MEMBERS Members() const
wxString Name(bool aIgnoreSheet=false) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
const std::vector< SCH_ITEM * > & ConnectedItems(const SCH_SHEET_PATH &aPath) const
Retrieve the set of items connected to this item on the given sheet.
Definition sch_item.cpp:766
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:353
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
Schematic symbol object.
Definition sch_symbol.h:76
std::string utf8(const wxString &aText)
nlohmann::json json
Definition gerbview.cpp:50
std::string source
@ LAYER_BUS
Definition layer_ids.h:484
std::map< std::pair< KIID_PATH, KIID >, DUMP_ROW > DUMP_ROWS
Rows by sheet path and item.
Definition conn_dump.h:64
DUMP_ROWS CaptureRows(SCHEMATIC &aSchematic)
Canonical, pointer-free connectivity rows for migration and rebuild comparisons.
std::string Dump(const DUMP_ROWS &aRows)
CONNECTION_TYPE
@ BUS
This item represents a bus vector.
@ NET
This item represents a net.
@ NONE
No connection to this item.
@ BUS_GROUP
This item represents a bus group.
Query-time snapshot; reacquire after Update.
std::shared_ptr< const BUS_SCHEMA > schema
std::vector< NET_VIEW > leaves
One node of the presentation tree.
Definition conn_bus.h:72
wxString text
Container label text.
Definition conn_bus.h:82
std::optional< size_t > leaf
Leaf ordinal of a NET node.
Definition conn_bus.h:86
std::vector< NODE > members
Definition conn_bus.h:84
@ GROUP
A list such as I2C{SDA SCL}. Members align by name with another group.
Definition conn_bus.h:52
std::set< KIID > connected
Definition conn_dump.h:58
std::vector< bool > danglingEnds
Definition conn_dump.h:57
One connection or bus member.
Definition conn_dump.h:41
One published net or bus with its identity.
uint32_t suffix
Zero for the holder of the base name.
std::shared_ptr< const COMPONENT_CONTENT > content
std::vector< DATABASE_CONNECTION::ROW > rows
std::string path
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_PIN_T
Definition typeinfo.h:149