KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_frame.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_frame.h"
21
22#include <sch_screen.h>
23#include <sch_sheet_path.h>
24#include <limits>
25#include <stdexcept>
26#include <wx/thread.h>
27
28namespace SCH_CONNECTIVITY
29{
30std::vector<FRAME_INSTANCE> CaptureHierarchy( const SCH_SHEET_LIST& aPaths, SESSION_KEYS& aKeys )
31{
32 wxASSERT( wxThread::IsMain() );
33 std::map<KIID_PATH, std::pair<const SCH_SHEET_PATH*, size_t>> paths;
34
35 for( const SCH_SHEET_PATH& path : aPaths )
36 {
37 if( path.empty() || !path.LastScreen() )
38 continue;
39
40 if( !paths.try_emplace( path.Path(), &path, 0 ).second )
41 throw std::invalid_argument( "Connectivity hierarchy has duplicate instance paths" );
42 }
43
44 std::vector<FRAME_INSTANCE> result;
45 result.reserve( paths.size() );
46
47 for( auto& [key, entry] : paths )
48 {
49 auto& [path, index] = entry;
50
51 if( path->size() > std::numeric_limits<uint16_t>::max() )
52 throw std::overflow_error( "Connectivity hierarchy depth exhausted" );
53
54 INSTANCE_SCOPE scope;
55 scope.instance = aKeys.InternInstance( key );
56 scope.path = path->PathHumanReadable( true, false, true );
57 scope.depth = static_cast<uint16_t>( path->size() );
58 index = result.size();
59 KIID_PATH parent = key;
60 parent.pop_back();
61
62 // KIID_PATH orders shorter paths first, so a parent is always already captured
63 if( const auto found = paths.find( parent ); found != paths.end() )
64 result[found->second.second].scope.children.emplace( key.back(), scope.instance );
65
66 result.push_back( { std::move( scope ), path->LastScreen()->ConnectivityId() } );
67 }
68
69 return result;
70}
71
72} // namespace SCH_CONNECTIVITY
int index
Session IDs are dense handles, never a canonical ordering.
Definition conn_keys.h:146
INST_ID InternInstance(const KIID_PATH &aPath)
Definition conn_keys.h:152
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Value keys and the key session of the schematic connectivity engine.
std::vector< FRAME_INSTANCE > CaptureHierarchy(const SCH_SHEET_LIST &aPaths, SESSION_KEYS &aKeys)
Interns every sheet instance and returns the frame in KIID_PATH order, so each parent precedes its ch...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
The hierarchy position of one sheet instance.
Definition conn_frame.h:35
wxString path
The human-readable sheet path, which prefixes scoped names.
Definition conn_frame.h:37
INST_ID instance
The session handle of the instance KIID_PATH.
Definition conn_frame.h:36
uint16_t depth
The number of sheets in the path, with the root sheet as one.
Definition conn_frame.h:38
std::string path
wxString result
Test unit parsing edge cases and error handling.