2020-05-13 02:00:37 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-03-18 19:31:02 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-05-13 02:00:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <bus_alias.h>
|
|
|
|
#include <connection_graph.h>
|
|
|
|
#include <erc_settings.h>
|
2020-11-18 22:55:38 +00:00
|
|
|
#include <sch_marker.h>
|
2020-06-08 02:19:46 +00:00
|
|
|
#include <project.h>
|
|
|
|
#include <project/project_file.h>
|
2020-07-06 10:51:04 +00:00
|
|
|
#include <project/net_settings.h>
|
2020-05-13 02:00:37 +00:00
|
|
|
#include <schematic.h>
|
|
|
|
#include <sch_screen.h>
|
2021-03-22 21:42:54 +00:00
|
|
|
#include <sim/spice_settings.h>
|
2020-05-13 02:00:37 +00:00
|
|
|
|
|
|
|
|
2020-05-21 02:27:48 +00:00
|
|
|
SCHEMATIC::SCHEMATIC( PROJECT* aPrj ) :
|
2020-05-13 02:00:37 +00:00
|
|
|
EDA_ITEM( nullptr, SCHEMATIC_T ),
|
2020-07-03 12:21:19 +00:00
|
|
|
m_project( nullptr ),
|
2020-05-13 02:00:37 +00:00
|
|
|
m_rootSheet( nullptr )
|
|
|
|
{
|
|
|
|
m_currentSheet = new SCH_SHEET_PATH();
|
|
|
|
m_connectionGraph = new CONNECTION_GRAPH( this );
|
2020-06-08 02:19:46 +00:00
|
|
|
|
|
|
|
SetProject( aPrj );
|
2020-05-13 02:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCHEMATIC::~SCHEMATIC()
|
|
|
|
{
|
|
|
|
delete m_currentSheet;
|
|
|
|
delete m_connectionGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCHEMATIC::Reset()
|
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
// Assume project already saved
|
|
|
|
if( m_project )
|
|
|
|
{
|
|
|
|
PROJECT_FILE& project = m_project->GetProjectFile();
|
|
|
|
|
|
|
|
delete project.m_ErcSettings;
|
|
|
|
delete project.m_SchematicSettings;
|
|
|
|
|
|
|
|
project.m_ErcSettings = nullptr;
|
|
|
|
project.m_SchematicSettings = nullptr;
|
2021-05-01 22:27:23 +00:00
|
|
|
|
|
|
|
m_project = nullptr; // clear the project, so we don't do this again when setting a new one
|
2020-06-08 02:19:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
delete m_rootSheet;
|
|
|
|
|
|
|
|
m_rootSheet = nullptr;
|
|
|
|
|
|
|
|
m_connectionGraph->Reset();
|
|
|
|
m_currentSheet->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
void SCHEMATIC::SetProject( PROJECT* aPrj )
|
|
|
|
{
|
2020-07-03 03:55:46 +00:00
|
|
|
if( m_project )
|
|
|
|
{
|
|
|
|
PROJECT_FILE& project = m_project->GetProjectFile();
|
|
|
|
|
|
|
|
delete project.m_ErcSettings;
|
|
|
|
delete project.m_SchematicSettings;
|
|
|
|
|
|
|
|
project.m_ErcSettings = nullptr;
|
|
|
|
project.m_SchematicSettings = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
m_project = aPrj;
|
|
|
|
|
|
|
|
if( m_project )
|
|
|
|
{
|
|
|
|
PROJECT_FILE& project = m_project->GetProjectFile();
|
|
|
|
project.m_ErcSettings = new ERC_SETTINGS( &project, "erc" );
|
|
|
|
project.m_SchematicSettings = new SCHEMATIC_SETTINGS( &project, "schematic" );
|
|
|
|
|
|
|
|
project.m_SchematicSettings->LoadFromFile();
|
2021-03-18 19:31:02 +00:00
|
|
|
project.m_SchematicSettings->m_NgspiceSimulatorSettings->LoadFromFile();
|
2020-06-08 02:19:46 +00:00
|
|
|
project.m_ErcSettings->LoadFromFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-23 19:11:02 +00:00
|
|
|
void SCHEMATIC::SetRoot( SCH_SHEET* aRootSheet )
|
|
|
|
{
|
|
|
|
wxCHECK_RET( aRootSheet, "Call to SetRoot with null SCH_SHEET!" );
|
|
|
|
|
|
|
|
m_rootSheet = aRootSheet;
|
|
|
|
|
2020-07-05 00:48:55 +00:00
|
|
|
m_currentSheet->clear();
|
|
|
|
m_currentSheet->push_back( m_rootSheet );
|
|
|
|
|
2020-05-23 19:11:02 +00:00
|
|
|
m_connectionGraph->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SCREEN* SCHEMATIC::RootScreen() const
|
|
|
|
{
|
|
|
|
return IsValid() ? m_rootSheet->GetScreen() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SCHEMATIC::GetFileName() const
|
|
|
|
{
|
|
|
|
return IsValid() ? m_rootSheet->GetScreen()->GetFileName() : wxString( wxEmptyString );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
SCHEMATIC_SETTINGS& SCHEMATIC::Settings() const
|
2020-05-13 02:00:37 +00:00
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
wxASSERT( m_project );
|
|
|
|
return *m_project->GetProjectFile().m_SchematicSettings;
|
2020-05-13 02:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
ERC_SETTINGS& SCHEMATIC::ErcSettings() const
|
2020-05-13 02:00:37 +00:00
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
wxASSERT( m_project );
|
|
|
|
return *m_project->GetProjectFile().m_ErcSettings;
|
2020-05-13 02:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-18 22:55:38 +00:00
|
|
|
std::vector<SCH_MARKER*> SCHEMATIC::ResolveERCExclusions()
|
|
|
|
{
|
|
|
|
SCH_SHEET_LIST sheetList = GetSheets();
|
|
|
|
ERC_SETTINGS& settings = ErcSettings();
|
|
|
|
|
|
|
|
for( const SCH_SHEET_PATH& sheet : sheetList )
|
|
|
|
{
|
|
|
|
for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_MARKER_T ) )
|
|
|
|
{
|
|
|
|
SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
|
2020-12-16 23:54:53 +00:00
|
|
|
auto it = settings.m_ErcExclusions.find( marker->Serialize() );
|
2020-11-18 22:55:38 +00:00
|
|
|
|
2020-12-16 23:54:53 +00:00
|
|
|
if( it != settings.m_ErcExclusions.end() )
|
2020-11-18 22:55:38 +00:00
|
|
|
{
|
|
|
|
marker->SetExcluded( true );
|
|
|
|
settings.m_ErcExclusions.erase( it );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SCH_MARKER*> newMarkers;
|
|
|
|
|
|
|
|
for( const wxString& exclusionData : settings.m_ErcExclusions )
|
|
|
|
{
|
|
|
|
SCH_MARKER* marker = SCH_MARKER::Deserialize( exclusionData );
|
|
|
|
|
|
|
|
if( marker )
|
|
|
|
{
|
|
|
|
marker->SetExcluded( true );
|
|
|
|
newMarkers.push_back( marker );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.m_ErcExclusions.clear();
|
|
|
|
|
|
|
|
return newMarkers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
std::shared_ptr<BUS_ALIAS> SCHEMATIC::GetBusAlias( const wxString& aLabel ) const
|
2020-05-13 02:00:37 +00:00
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
for( const auto& sheet : GetSheets() )
|
2020-05-13 02:00:37 +00:00
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
for( const auto& alias : sheet.LastScreen()->GetBusAliases() )
|
2020-05-13 02:00:37 +00:00
|
|
|
{
|
2020-06-08 02:19:46 +00:00
|
|
|
if( alias->GetName() == aLabel )
|
|
|
|
return alias;
|
2020-05-13 02:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
return nullptr;
|
2020-05-13 02:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-06 10:51:04 +00:00
|
|
|
std::vector<wxString> SCHEMATIC::GetNetClassAssignmentCandidates()
|
|
|
|
{
|
|
|
|
std::vector<wxString> names;
|
|
|
|
|
2020-08-24 01:46:01 +00:00
|
|
|
// Key is a NET_NAME_CODE aka std::pair<name, code>
|
|
|
|
for( const NET_MAP::value_type& pair: m_connectionGraph->GetNetMap() )
|
2020-10-11 23:29:06 +00:00
|
|
|
{
|
|
|
|
CONNECTION_SUBGRAPH* subgraph = pair.second[0];
|
|
|
|
|
2021-03-05 19:14:53 +00:00
|
|
|
if( subgraph->GetDriverPriority() >= CONNECTION_SUBGRAPH::PRIORITY::PIN )
|
2020-10-11 23:29:06 +00:00
|
|
|
names.emplace_back( pair.first.first );
|
|
|
|
}
|
2020-07-06 10:51:04 +00:00
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
|
2020-07-29 17:02:39 +00:00
|
|
|
bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
|
|
|
|
{
|
|
|
|
SCH_SHEET_LIST sheetList = GetSheets();
|
|
|
|
wxString remainder;
|
|
|
|
wxString ref = token->BeforeFirst( ':', &remainder );
|
|
|
|
SCH_SHEET_PATH dummy;
|
|
|
|
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &dummy );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( refItem && refItem->Type() == SCH_SYMBOL_T )
|
2020-07-29 17:02:39 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
|
2020-07-29 17:02:39 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( refSymbol->ResolveTextVar( &remainder, aDepth + 1 ) )
|
2020-07-29 17:02:39 +00:00
|
|
|
{
|
|
|
|
*token = remainder;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( refItem && refItem->Type() == SCH_SHEET_T )
|
|
|
|
{
|
|
|
|
SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
|
|
|
|
|
|
|
|
if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) )
|
|
|
|
{
|
|
|
|
*token = remainder;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const
|
|
|
|
{
|
|
|
|
wxString newbuf;
|
|
|
|
size_t sourceLen = aSource.length();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
|
|
|
|
{
|
|
|
|
wxString token;
|
|
|
|
bool isCrossRef = false;
|
|
|
|
|
|
|
|
for( i = i + 2; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '}' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( aSource[i] == ':' )
|
|
|
|
isCrossRef = true;
|
|
|
|
|
|
|
|
token.append( aSource[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( isCrossRef )
|
|
|
|
{
|
|
|
|
SCH_SHEET_LIST sheetList = GetSheets();
|
|
|
|
wxString remainder;
|
|
|
|
wxString ref = token.BeforeFirst( ':', &remainder );
|
|
|
|
SCH_REFERENCE_LIST references;
|
|
|
|
|
2020-11-15 16:08:31 +00:00
|
|
|
sheetList.GetSymbols( references );
|
2020-07-29 17:02:39 +00:00
|
|
|
|
|
|
|
for( size_t jj = 0; jj < references.GetCount(); jj++ )
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
SCH_SYMBOL* refSymbol = references[ jj ].GetSymbol();
|
2020-07-29 17:02:39 +00:00
|
|
|
|
2020-11-15 16:08:31 +00:00
|
|
|
if( ref == refSymbol->GetRef( &references[ jj ].GetSheetPath(), true ) )
|
2020-07-29 17:02:39 +00:00
|
|
|
{
|
|
|
|
wxString test( remainder );
|
|
|
|
|
2020-11-15 16:08:31 +00:00
|
|
|
if( refSymbol->ResolveTextVar( &test ) )
|
|
|
|
token = refSymbol->m_Uuid.AsString() + ":" + remainder;
|
2020-07-29 17:02:39 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newbuf.append( "${" + token + "}" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newbuf.append( aSource[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
|
|
|
|
{
|
|
|
|
wxString newbuf;
|
|
|
|
size_t sourceLen = aSource.length();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
|
|
|
|
{
|
|
|
|
wxString token;
|
|
|
|
bool isCrossRef = false;
|
|
|
|
|
|
|
|
for( i = i + 2; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '}' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( aSource[i] == ':' )
|
|
|
|
isCrossRef = true;
|
|
|
|
|
|
|
|
token.append( aSource[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( isCrossRef )
|
|
|
|
{
|
|
|
|
SCH_SHEET_LIST sheetList = GetSheets();
|
|
|
|
wxString remainder;
|
|
|
|
wxString ref = token.BeforeFirst( ':', &remainder );
|
|
|
|
|
|
|
|
SCH_SHEET_PATH refSheetPath;
|
|
|
|
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &refSheetPath );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( refItem && refItem->Type() == SCH_SYMBOL_T )
|
2020-07-29 17:02:39 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
|
|
|
|
token = refSymbol->GetRef( &refSheetPath, true ) + ":" + remainder;
|
2020-07-29 17:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newbuf.append( "${" + token + "}" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newbuf.append( aSource[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
SCH_SHEET_LIST& SCHEMATIC::GetFullHierarchy() const
|
|
|
|
{
|
|
|
|
static SCH_SHEET_LIST hierarchy;
|
|
|
|
|
|
|
|
hierarchy.clear();
|
|
|
|
hierarchy.BuildSheetList( m_rootSheet, false );
|
2020-11-29 18:30:58 +00:00
|
|
|
hierarchy.SortByPageNumbers();
|
2020-10-18 20:30:37 +00:00
|
|
|
|
|
|
|
return hierarchy;
|
|
|
|
}
|