KiCad PCB EDA Suite
Loading...
Searching...
No Matches
operating_point.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
20#include "operating_point.h"
21
22#include <advanced_config.h>
23#include <connection_graph.h>
25#include <sch_line.h>
26#include <schematic.h>
27
28#include <map>
29
30
31void UpdateOperatingPointVoltages( SCHEMATIC& aSchematic, const std::function<void( SCH_LINE* )>& aOnChanged )
32{
33 const SCHEMATIC_SETTINGS& settings = aSchematic.Settings();
34 const wxString variant = aSchematic.GetCurrentVariant();
35
36 auto assignVoltage =
37 [&]( const wxString& aNetName, const SCH_SHEET_PATH& aSheet, const auto& aItems )
38 {
39 if( aSheet.GetExcludedFromSim( variant ) )
40 return;
41
42 wxString op = aSchematic.GetOperatingPoint( aNetName, settings.m_OPO_VPrecision,
43 settings.m_OPO_VRange );
44
45 if( op.IsEmpty() || op == wxS( "--" ) || op == wxS( "?" ) )
46 return;
47
48 SCH_LINE* longestWire = nullptr;
49 double length = 0.0;
50
51 for( SCH_ITEM* item : aItems )
52 {
53 if( item->Type() == SCH_LINE_T )
54 {
55 SCH_LINE* wire = static_cast<SCH_LINE*>( item );
56
57 if( wire->IsWire() && wire->GetLength() > length )
58 {
59 longestWire = wire;
60 length = wire->GetLength();
61 }
62 }
63 }
64
65 if( longestWire )
66 {
67 longestWire->SetOperatingPoint( op );
68
69 if( aOnChanged )
70 aOnChanged( longestWire );
71 }
72 };
73
74 if( ADVANCED_CFG::GetCfg().m_ConnectivityEngine )
75 {
76 const SCH_SHEET_LIST hierarchy = aSchematic.Hierarchy();
77 std::map<KIID_PATH, const SCH_SHEET_PATH*> paths;
78
79 for( const SCH_SHEET_PATH& path : hierarchy )
80 paths.emplace( path.Path(), &path );
81
82 // The active engine does not populate the legacy CONNECTION_GRAPH net map.
83 for( const SCH_CONNECTIVITY::NET_GROUP& group : aSchematic.Connectivity().GetNetMap() )
84 {
85 for( const SCH_CONNECTIVITY::NET_VIEW& net : group.instances )
86 {
87 const auto path = paths.find( net.Instance() );
88
89 if( net.IsNet() && path != paths.end() )
90 assignVoltage( group.name, *path->second, net.Items() );
91 }
92 }
93 }
94 else
95 {
96 for( const auto& [key, subgraphs] : aSchematic.ConnectionGraph()->GetNetMap() )
97 {
98 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
99 assignVoltage( key.Name, subgraph->GetSheet(), subgraph->GetItems() );
100 }
101 }
102}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
const NET_MAP & GetNetMap() const
A subgraph is a set of items that are electrically connected on a single sheet.
These are loaded from Eeschema settings but then overwritten by the project settings.
Holds all the data relating to one schematic.
Definition schematic.h:155
wxString GetOperatingPoint(const wxString &aNetName, int aPrecision, const wxString &aRange)
SCHEMATIC_SETTINGS & Settings() const
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_CONNECTIVITY::FACADE & Connectivity() const
Definition schematic.h:411
wxString GetCurrentVariant() const
Return the current variant being edited.
CONNECTION_GRAPH * ConnectionGraph() const
Definition schematic.h:414
std::vector< NET_GROUP > GetNetMap() const
Owned query-time groups in canonical name order, with unique instances in KIID_PATH order.
std::vector< SCH_ITEM * > Items() const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
void SetOperatingPoint(const wxString &aText)
Definition sch_line.h:379
bool IsWire() const
Return true if the line is a wire.
double GetLength() const
Definition sch_line.cpp:343
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...
void UpdateOperatingPointVoltages(SCHEMATIC &aSchematic, const std::function< void(SCH_LINE *)> &aOnChanged)
Assign loaded operating-point voltages to the longest wire in each connected net portion.
std::string path
@ SCH_LINE_T
Definition typeinfo.h:159