kicad/eeschema/sch_sheet.cpp

1238 lines
32 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 1992-2021 Kicad Developers, see AUTHORS.txt for contributors.
*
* 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 2
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <bitmaps.h>
#include <core/mirror.h>
#include <sch_draw_panel.h>
#include <trigo.h>
2018-01-30 10:49:51 +00:00
#include <sch_edit_frame.h>
#include <plotters/plotter.h>
#include <string_utils.h>
#include <widgets/msgpanel.h>
#include <math/util.h> // for KiROUND
#include <sch_sheet.h>
#include <sch_sheet_path.h>
#include <sch_sheet_pin.h>
#include <sch_symbol.h>
#include <sch_painter.h>
#include <schematic.h>
#include <settings/color_settings.h>
#include <trace_helpers.h>
#include <pgm_base.h>
2021-06-03 12:11:15 +00:00
#include <wx/log.h>
#define SHEET_NAME_CANONICAL "Sheet name"
#define SHEET_FILE_CANONICAL "Sheet file"
#define USER_FIELD_CANONICAL "Field%d"
const wxString SCH_SHEET::GetDefaultFieldName( int aFieldNdx, bool aTranslated )
{
static void* locale = nullptr;
static wxString sheetnameDefault;
static wxString sheetfilenameDefault;
2021-10-05 20:15:17 +00:00
static wxString userFieldDefault;
if( !aTranslated )
{
switch( aFieldNdx )
{
case SHEETNAME: return SHEET_NAME_CANONICAL;
case SHEETFILENAME: return SHEET_FILE_CANONICAL;
default: return wxString::Format( USER_FIELD_CANONICAL, aFieldNdx );
}
}
// Fetching translations can take a surprising amount of time when loading libraries,
// so only do it when necessary.
if( Pgm().GetLocale() != locale )
{
sheetnameDefault = _( SHEET_NAME_CANONICAL );
sheetfilenameDefault = _( SHEET_FILE_CANONICAL );
userFieldDefault = _( USER_FIELD_CANONICAL );
locale = Pgm().GetLocale();
}
// Fixed values for the mandatory fields
switch( aFieldNdx )
{
case SHEETNAME: return sheetnameDefault;
case SHEETFILENAME: return sheetfilenameDefault;
2021-10-05 20:15:17 +00:00
default: return wxString::Format( userFieldDefault, aFieldNdx );
}
}
SCH_SHEET::SCH_SHEET( EDA_ITEM* aParent, const wxPoint& aPos, wxSize aSize,
FIELDS_AUTOPLACED aAutoplaceFields ) :
SCH_ITEM( aParent, SCH_SHEET_T )
{
2020-11-14 14:29:11 +00:00
m_layer = LAYER_SHEET;
m_pos = aPos;
m_size = aSize;
2021-07-16 20:13:26 +00:00
m_screen = nullptr;
for( int i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
{
m_fields.emplace_back( aPos, i, this, GetDefaultFieldName( i ) );
m_fields.back().SetVisible( true );
if( i == SHEETNAME )
m_fields.back().SetLayer( LAYER_SHEETNAME );
else if( i == SHEETFILENAME )
m_fields.back().SetLayer( LAYER_SHEETFILENAME );
else
m_fields.back().SetLayer( LAYER_SHEETFIELDS );
}
m_fieldsAutoplaced = aAutoplaceFields;
AutoAutoplaceFields( nullptr );
m_borderWidth = 0;
m_borderColor = COLOR4D::UNSPECIFIED;
m_backgroundColor = COLOR4D::UNSPECIFIED;
}
SCH_SHEET::SCH_SHEET( const SCH_SHEET& aSheet ) :
SCH_ITEM( aSheet )
{
m_pos = aSheet.m_pos;
m_size = aSheet.m_size;
2020-11-14 14:29:11 +00:00
m_layer = aSheet.m_layer;
2020-02-21 22:20:42 +00:00
const_cast<KIID&>( m_Uuid ) = aSheet.m_Uuid;
m_fields = aSheet.m_fields;
m_fieldsAutoplaced = aSheet.m_fieldsAutoplaced;
m_screen = aSheet.m_screen;
for( SCH_SHEET_PIN* pin : aSheet.m_pins )
{
m_pins.emplace_back( new SCH_SHEET_PIN( *pin ) );
m_pins.back()->SetParent( this );
}
2021-01-19 02:16:48 +00:00
for( SCH_FIELD& field : m_fields )
field.SetParent( this );
m_borderWidth = aSheet.m_borderWidth;
m_borderColor = aSheet.m_borderColor;
m_backgroundColor = aSheet.m_backgroundColor;
m_instances = aSheet.m_instances;
if( m_screen )
m_screen->IncRefCount();
}
SCH_SHEET::~SCH_SHEET()
{
// also, look at the associated sheet & its reference count
// perhaps it should be deleted also.
if( m_screen )
2008-02-26 19:19:54 +00:00
{
m_screen->DecRefCount();
if( m_screen->GetRefCount() == 0 )
delete m_screen;
2008-02-26 19:19:54 +00:00
}
// We own our pins; delete them
for( SCH_SHEET_PIN* pin : m_pins )
delete pin;
}
2008-02-26 19:19:54 +00:00
EDA_ITEM* SCH_SHEET::Clone() const
{
return new SCH_SHEET( *this );
}
void SCH_SHEET::SetScreen( SCH_SCREEN* aScreen )
{
if( aScreen == m_screen )
return;
2021-07-16 20:13:26 +00:00
if( m_screen != nullptr )
{
m_screen->DecRefCount();
if( m_screen->GetRefCount() == 0 )
{
delete m_screen;
2021-07-16 20:13:26 +00:00
m_screen = nullptr;
}
}
m_screen = aScreen;
if( m_screen )
m_screen->IncRefCount();
}
int SCH_SHEET::GetScreenCount() const
{
2021-07-16 20:13:26 +00:00
if( m_screen == nullptr )
return 0;
return m_screen->GetRefCount();
}
bool SCH_SHEET::IsRootSheet() const
{
wxCHECK_MSG( Schematic(), false, "Can't call IsRootSheet without setting a schematic" );
return &Schematic()->Root() == this;
}
void SCH_SHEET::GetContextualTextVars( wxArrayString* aVars ) const
{
for( int i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
aVars->push_back( m_fields[i].GetCanonicalName().Upper() );
for( size_t i = SHEET_MANDATORY_FIELDS; i < m_fields.size(); ++i )
aVars->push_back( m_fields[i].GetName() );
aVars->push_back( wxT( "#" ) );
aVars->push_back( wxT( "##" ) );
m_screen->GetTitleBlock().GetContextualTextVars( aVars );
}
bool SCH_SHEET::ResolveTextVar( wxString* token, int aDepth ) const
{
for( int i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
{
if( token->IsSameAs( m_fields[i].GetCanonicalName().Upper() ) )
{
*token = m_fields[i].GetShownText( aDepth + 1 );
return true;
}
}
for( size_t i = SHEET_MANDATORY_FIELDS; i < m_fields.size(); ++i )
{
if( token->IsSameAs( m_fields[i].GetName() ) )
{
*token = m_fields[i].GetShownText( aDepth + 1 );
return true;
}
}
PROJECT *project = &Schematic()->Prj();
if( m_screen->GetTitleBlock().TextVarResolver( token, project ) )
{
return true;
}
if( token->IsSameAs( wxT( "#" ) ) )
{
for( const SCH_SHEET_PATH& sheet : Schematic()->GetSheets() )
{
if( sheet.Last() == this ) // Current sheet path found
{
*token = wxString::Format( "%s", sheet.GetPageNumber() );
return true;
}
}
}
else if( token->IsSameAs( wxT( "##" ) ) )
{
SCH_SHEET_LIST sheetList = Schematic()->GetSheets();
*token = wxString::Format( wxT( "%d" ), (int) sheetList.size() );
return true;
}
return false;
}
bool SCH_SHEET::UsesDefaultStroke() const
{
return m_borderWidth == 0 && m_borderColor == COLOR4D::UNSPECIFIED;
}
void SCH_SHEET::SwapData( SCH_ITEM* aItem )
{
wxCHECK_RET( aItem->Type() == SCH_SHEET_T,
2021-10-05 20:15:17 +00:00
wxString::Format( wxT( "SCH_SHEET object cannot swap data with %s object." ),
aItem->GetClass() ) );
SCH_SHEET* sheet = ( SCH_SHEET* ) aItem;
std::swap( m_pos, sheet->m_pos );
std::swap( m_size, sheet->m_size );
m_fields.swap( sheet->m_fields );
std::swap( m_fieldsAutoplaced, sheet->m_fieldsAutoplaced );
m_pins.swap( sheet->m_pins );
// Update parent pointers after swapping.
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->SetParent( this );
for( SCH_SHEET_PIN* sheetPin : sheet->m_pins )
sheetPin->SetParent( sheet );
for( SCH_FIELD& field : m_fields )
field.SetParent( this );
for( SCH_FIELD& field : sheet->m_fields )
field.SetParent( sheet );
std::swap( m_borderWidth, sheet->m_borderWidth );
std::swap( m_borderColor, sheet->m_borderColor );
std::swap( m_backgroundColor, sheet->m_backgroundColor );
std::swap( m_instances, sheet->m_instances );
}
void SCH_SHEET::AddPin( SCH_SHEET_PIN* aSheetPin )
{
2021-07-16 20:13:26 +00:00
wxASSERT( aSheetPin != nullptr );
wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
aSheetPin->SetParent( this );
m_pins.push_back( aSheetPin );
renumberPins();
}
2020-12-20 18:44:13 +00:00
void SCH_SHEET::RemovePin( const SCH_SHEET_PIN* aSheetPin )
{
2021-07-16 20:13:26 +00:00
wxASSERT( aSheetPin != nullptr );
wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
2020-12-20 18:44:13 +00:00
for( auto i = m_pins.begin(); i < m_pins.end(); ++i )
{
if( *i == aSheetPin )
{
m_pins.erase( i );
renumberPins();
return;
}
}
}
2020-12-20 18:44:13 +00:00
bool SCH_SHEET::HasPin( const wxString& aName ) const
{
for( SCH_SHEET_PIN* pin : m_pins )
{
if( pin->GetText().CmpNoCase( aName ) == 0 )
return true;
}
return false;
}
bool SCH_SHEET::doIsConnected( const wxPoint& aPosition ) const
{
for( SCH_SHEET_PIN* sheetPin : m_pins )
{
if( sheetPin->GetPosition() == aPosition )
return true;
}
return false;
}
bool SCH_SHEET::IsVerticalOrientation() const
{
int leftRight = 0;
int topBottom = 0;
for( SCH_SHEET_PIN* pin : m_pins )
{
switch( pin->GetSide() )
{
2021-10-05 20:15:17 +00:00
case SHEET_SIDE::LEFT: leftRight++; break;
case SHEET_SIDE::RIGHT: leftRight++; break;
case SHEET_SIDE::TOP: topBottom++; break;
2021-05-31 01:20:43 +00:00
case SHEET_SIDE::BOTTOM: topBottom++; break;
2021-10-05 20:15:17 +00:00
default: break;
}
}
return topBottom > 0 && leftRight == 0;
}
2020-12-20 18:44:13 +00:00
bool SCH_SHEET::HasUndefinedPins() const
{
for( SCH_SHEET_PIN* pin : m_pins )
{
/* Search the schematic for a hierarchical label corresponding to this sheet label. */
const SCH_HIERLABEL* HLabel = nullptr;
2021-10-05 20:15:17 +00:00
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_HIER_LABEL_T ) )
{
if( !pin->GetText().CmpNoCase( static_cast<SCH_HIERLABEL*>( aItem )->GetText() ) )
{
HLabel = static_cast<SCH_HIERLABEL*>( aItem );
break;
}
}
if( HLabel == nullptr ) // Corresponding hierarchical label not found.
return true;
}
return false;
}
2008-02-26 19:19:54 +00:00
int bumpToNextGrid( const int aVal, const int aDirection )
{
constexpr int gridSize = Mils2iu( 50 );
return ( KiROUND( aVal / gridSize ) * gridSize ) + ( aDirection * gridSize );
}
int SCH_SHEET::GetMinWidth( bool aFromLeft ) const
{
int pinsLeft = m_pos.x + m_size.x;
int pinsRight = m_pos.x;
for( size_t i = 0; i < m_pins.size(); i++ )
{
SHEET_SIDE edge = m_pins[i]->GetSide();
2021-05-31 01:20:43 +00:00
if( edge == SHEET_SIDE::TOP || edge == SHEET_SIDE::BOTTOM )
{
EDA_RECT pinRect = m_pins[i]->GetBoundingBox();
pinsLeft = std::min( pinsLeft, pinRect.GetLeft() );
pinsRight = std::max( pinsRight, pinRect.GetRight() );
}
}
pinsLeft = bumpToNextGrid( pinsLeft, -1 );
pinsRight = bumpToNextGrid( pinsRight, 1 );
int pinMinWidth;
if( pinsLeft >= pinsRight )
pinMinWidth = 0;
else if( aFromLeft )
pinMinWidth = pinsRight - m_pos.x;
else
pinMinWidth = m_pos.x + m_size.x - pinsLeft;
return std::max( pinMinWidth, Mils2iu( MIN_SHEET_WIDTH ) );
}
int SCH_SHEET::GetMinHeight( bool aFromTop ) const
{
int pinsTop = m_pos.y + m_size.y;
int pinsBottom = m_pos.y;
for( size_t i = 0; i < m_pins.size(); i++ )
{
SHEET_SIDE edge = m_pins[i]->GetSide();
2021-05-31 01:20:43 +00:00
if( edge == SHEET_SIDE::RIGHT || edge == SHEET_SIDE::LEFT )
{
EDA_RECT pinRect = m_pins[i]->GetBoundingBox();
pinsTop = std::min( pinsTop, pinRect.GetTop() );
pinsBottom = std::max( pinsBottom, pinRect.GetBottom() );
}
}
pinsTop = bumpToNextGrid( pinsTop, -1 );
pinsBottom = bumpToNextGrid( pinsBottom, 1 );
int pinMinHeight;
if( pinsTop >= pinsBottom )
pinMinHeight = 0;
else if( aFromTop )
pinMinHeight = pinsBottom - m_pos.y;
else
pinMinHeight = m_pos.y + m_size.y - pinsTop;
return std::max( pinMinHeight, Mils2iu( MIN_SHEET_HEIGHT ) );
}
void SCH_SHEET::CleanupSheet()
{
2021-10-05 20:15:17 +00:00
std::vector<SCH_SHEET_PIN*> pins = m_pins;
m_pins.clear();
2008-02-26 19:19:54 +00:00
2021-10-05 20:15:17 +00:00
for( SCH_SHEET_PIN* pin : pins )
2008-02-26 19:19:54 +00:00
{
/* Search the schematic for a hierarchical label corresponding to this sheet label. */
2021-07-16 20:13:26 +00:00
const SCH_HIERLABEL* HLabel = nullptr;
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_HIER_LABEL_T ) )
2008-02-26 19:19:54 +00:00
{
2021-10-05 20:15:17 +00:00
if( pin->GetText().CmpNoCase( static_cast<SCH_HIERLABEL*>( aItem )->GetText() ) == 0 )
{
HLabel = static_cast<SCH_HIERLABEL*>( aItem );
break;
}
2008-02-26 19:19:54 +00:00
}
2021-10-05 20:15:17 +00:00
if( HLabel )
m_pins.push_back( pin );
2008-02-26 19:19:54 +00:00
}
}
SCH_SHEET_PIN* SCH_SHEET::GetPin( const wxPoint& aPosition )
{
for( SCH_SHEET_PIN* pin : m_pins )
{
if( pin->HitTest( aPosition ) )
return pin;
}
2021-07-16 20:13:26 +00:00
return nullptr;
}
2008-02-26 19:19:54 +00:00
2020-04-14 12:25:00 +00:00
int SCH_SHEET::GetPenWidth() const
{
if( GetBorderWidth() > 0 )
return GetBorderWidth();
if( Schematic() )
return Schematic()->Settings().m_DefaultLineWidth;
return Mils2iu( DEFAULT_LINE_WIDTH_MILS );
}
2020-03-06 20:02:58 +00:00
void SCH_SHEET::AutoplaceFields( SCH_SCREEN* aScreen, bool aManual )
{
2020-04-14 12:25:00 +00:00
wxSize textSize = m_fields[ SHEETNAME ].GetTextSize();
int borderMargin = KiROUND( GetPenWidth() / 2.0 ) + 4;
int margin = borderMargin + KiROUND( std::max( textSize.x, textSize.y ) * 0.5 );
if( IsVerticalOrientation() )
2020-03-06 20:02:58 +00:00
{
m_fields[ SHEETNAME ].SetTextPos( m_pos + wxPoint( -margin, m_size.y ) );
m_fields[ SHEETNAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
m_fields[ SHEETNAME ].SetVertJustify(GR_TEXT_VJUSTIFY_BOTTOM );
m_fields[ SHEETNAME ].SetTextAngle( TEXT_ANGLE_VERT );
}
else
2020-03-06 20:02:58 +00:00
{
m_fields[ SHEETNAME ].SetTextPos( m_pos + wxPoint( 0, -margin ) );
m_fields[ SHEETNAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
m_fields[ SHEETNAME ].SetVertJustify(GR_TEXT_VJUSTIFY_BOTTOM );
m_fields[ SHEETNAME ].SetTextAngle( TEXT_ANGLE_HORIZ );
}
2020-03-06 20:02:58 +00:00
textSize = m_fields[ SHEETFILENAME ].GetTextSize();
2020-04-14 12:25:00 +00:00
margin = borderMargin + KiROUND( std::max( textSize.x, textSize.y ) * 0.4 );
if( IsVerticalOrientation() )
2020-03-06 20:02:58 +00:00
{
m_fields[ SHEETFILENAME ].SetTextPos( m_pos + wxPoint( m_size.x + margin, m_size.y ) );
m_fields[ SHEETFILENAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
m_fields[ SHEETFILENAME ].SetVertJustify(GR_TEXT_VJUSTIFY_TOP );
m_fields[ SHEETFILENAME ].SetTextAngle( TEXT_ANGLE_VERT );
}
else
2020-03-06 20:02:58 +00:00
{
m_fields[ SHEETFILENAME ].SetTextPos( m_pos + wxPoint( 0, m_size.y + margin ) );
m_fields[ SHEETFILENAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
m_fields[ SHEETFILENAME ].SetVertJustify(GR_TEXT_VJUSTIFY_TOP );
m_fields[ SHEETFILENAME ].SetTextAngle( TEXT_ANGLE_HORIZ );
}
m_fieldsAutoplaced = FIELDS_AUTOPLACED_AUTO;
}
void SCH_SHEET::ViewGetLayers( int aLayers[], int& aCount ) const
{
aCount = 4;
aLayers[0] = LAYER_HIERLABEL;
aLayers[1] = LAYER_SHEET;
aLayers[2] = LAYER_SHEET_BACKGROUND;
aLayers[3] = LAYER_SELECTION_SHADOWS;
}
2020-03-06 20:02:58 +00:00
const EDA_RECT SCH_SHEET::GetBodyBoundingBox() const
{
wxPoint end;
EDA_RECT box( m_pos, m_size );
2020-04-14 12:25:00 +00:00
int lineWidth = GetPenWidth();
int textLength = 0;
2008-04-15 19:38:19 +00:00
// Calculate bounding box X size:
end.x = std::max( m_size.x, textLength );
2008-04-15 19:38:19 +00:00
// Calculate bounding box pos:
end.y = m_size.y;
end += m_pos;
box.SetEnd( end );
box.Inflate( lineWidth / 2 );
2020-03-06 20:02:58 +00:00
return box;
}
const EDA_RECT SCH_SHEET::GetBoundingBox() const
{
EDA_RECT box = GetBodyBoundingBox();
for( const SCH_FIELD& field : m_fields )
box.Merge( field.GetBoundingBox() );
return box;
}
2008-02-26 19:19:54 +00:00
wxPoint SCH_SHEET::GetRotationCenter() const
{
EDA_RECT box( m_pos, m_size );
return box.GetCenter();
}
2020-11-15 17:03:27 +00:00
int SCH_SHEET::SymbolCount() const
{
2008-02-26 19:19:54 +00:00
int n = 0;
if( m_screen )
2008-02-26 19:19:54 +00:00
{
2021-06-10 14:10:55 +00:00
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_SYMBOL_T ) )
2008-02-26 19:19:54 +00:00
{
2021-06-10 14:10:55 +00:00
SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem;
2020-11-15 17:03:27 +00:00
if( symbol->GetField( VALUE_FIELD )->GetText().GetChar( 0 ) != '#' )
n++;
2008-02-26 19:19:54 +00:00
}
2020-09-06 12:04:52 +00:00
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
2020-11-15 17:03:27 +00:00
n += static_cast<const SCH_SHEET*>( aItem )->SymbolCount();
2008-02-26 19:19:54 +00:00
}
2008-02-26 19:19:54 +00:00
return n;
}
2008-02-26 19:19:54 +00:00
bool SCH_SHEET::SearchHierarchy( const wxString& aFilename, SCH_SCREEN** aScreen )
{
if( m_screen )
2008-02-26 19:19:54 +00:00
{
// Only check the root sheet once and don't recurse.
if( !GetParent() )
{
if( m_screen && m_screen->GetFileName().Cmp( aFilename ) == 0 )
{
*aScreen = m_screen;
return true;
}
}
2021-10-05 20:15:17 +00:00
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
2008-02-26 19:19:54 +00:00
{
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
SCH_SCREEN* screen = sheet->m_screen;
// Must use the screen's path (which is always absolute) rather than the
// sheet's (which could be relative).
if( screen && screen->GetFileName().Cmp( aFilename ) == 0 )
{
*aScreen = screen;
return true;
2008-02-26 19:19:54 +00:00
}
if( sheet->SearchHierarchy( aFilename, aScreen ) )
return true;
2008-02-26 19:19:54 +00:00
}
}
2008-02-26 19:19:54 +00:00
return false;
}
2008-02-26 19:19:54 +00:00
2016-02-15 20:24:59 +00:00
bool SCH_SHEET::LocatePathOfScreen( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aList )
{
if( m_screen )
{
aList->push_back( this );
2016-02-15 20:24:59 +00:00
if( m_screen == aScreen )
return true;
for( auto item : m_screen->Items().OfType( SCH_SHEET_T ) )
2016-02-15 20:24:59 +00:00
{
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
2016-02-15 20:24:59 +00:00
if( sheet->LocatePathOfScreen( aScreen, aList ) )
{
return true;
2016-02-15 20:24:59 +00:00
}
}
aList->pop_back();
2016-02-15 20:24:59 +00:00
}
2016-02-15 20:24:59 +00:00
return false;
}
int SCH_SHEET::CountSheets() const
{
2008-02-26 19:19:54 +00:00
int count = 1; //1 = this!!
if( m_screen )
2008-02-26 19:19:54 +00:00
{
for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
count += static_cast<SCH_SHEET*>( aItem )->CountSheets();
2008-02-26 19:19:54 +00:00
}
2008-02-26 19:19:54 +00:00
return count;
}
void SCH_SHEET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
aList.emplace_back( _( "Sheet Name" ), m_fields[ SHEETNAME ].GetText() );
if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
{
SCH_SHEET_PATH path = schframe->GetCurrentSheet();
path.push_back( this );
aList.emplace_back( _( "Hierarchical Path" ), path.PathHumanReadable( false ) );
}
aList.emplace_back( _( "File Name" ), m_fields[ SHEETFILENAME ].GetText() );
}
void SCH_SHEET::Move( const wxPoint& aMoveVector )
{
m_pos += aMoveVector;
for( SCH_SHEET_PIN* pin : m_pins )
pin->Move( aMoveVector );
for( SCH_FIELD& field : m_fields )
field.Move( aMoveVector );
}
void SCH_SHEET::Rotate( const wxPoint& aCenter )
{
wxPoint prev = m_pos;
2021-03-19 19:55:59 +00:00
RotatePoint( &m_pos, aCenter, 900 );
RotatePoint( &m_size.x, &m_size.y, 900 );
if( m_size.x < 0 )
{
m_pos.x += m_size.x;
m_size.x = -m_size.x;
}
if( m_size.y < 0 )
{
m_pos.y += m_size.y;
m_size.y = -m_size.y;
}
2020-03-06 20:02:58 +00:00
// Pins must be rotated first as that's how we determine vertical vs horizontal
// orientation for auto-placement
for( SCH_SHEET_PIN* sheetPin : m_pins )
2021-03-19 19:55:59 +00:00
sheetPin->Rotate( aCenter );
2020-03-06 20:02:58 +00:00
if( m_fieldsAutoplaced == FIELDS_AUTOPLACED_AUTO )
{
2021-07-16 20:13:26 +00:00
AutoplaceFields( /* aScreen */ nullptr, /* aManual */ false );
2020-03-06 20:02:58 +00:00
}
else
{
2021-05-05 21:41:51 +00:00
// Move the fields to the new position because the parent itself has moved.
2020-03-06 20:02:58 +00:00
for( SCH_FIELD& field : m_fields )
{
wxPoint pos = field.GetTextPos();
pos.x -= prev.x - m_pos.x;
pos.y -= prev.y - m_pos.y;
field.SetTextPos( pos );
}
}
}
void SCH_SHEET::MirrorVertically( int aCenter )
{
int dy = m_pos.y;
MIRROR( m_pos.y, aCenter );
m_pos.y -= m_size.y;
dy -= m_pos.y; // 0,dy is the move vector for this transform
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->MirrorVertically( aCenter );
for( SCH_FIELD& field : m_fields )
{
wxPoint pos = field.GetTextPos();
pos.y -= dy;
field.SetTextPos( pos );
}
}
void SCH_SHEET::MirrorHorizontally( int aCenter )
{
int dx = m_pos.x;
MIRROR( m_pos.x, aCenter );
m_pos.x -= m_size.x;
dx -= m_pos.x; // dx,0 is the move vector for this transform
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->MirrorHorizontally( aCenter );
for( SCH_FIELD& field : m_fields )
{
wxPoint pos = field.GetTextPos();
pos.x -= dx;
field.SetTextPos( pos );
}
}
void SCH_SHEET::SetPosition( const wxPoint& aPosition )
{
// Remember the sheet and all pin sheet positions must be
// modified. So use Move function to do that.
Move( aPosition - m_pos );
}
void SCH_SHEET::Resize( const wxSize& aSize )
{
if( aSize == m_size )
return;
m_size = aSize;
// Move the fields if we're in autoplace mode
if( m_fieldsAutoplaced == FIELDS_AUTOPLACED_AUTO )
2021-07-16 20:13:26 +00:00
AutoplaceFields( /* aScreen */ nullptr, /* aManual */ false );
// Move the sheet labels according to the new sheet size.
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->ConstrainOnEdge( sheetPin->GetPosition() );
}
2020-12-20 18:27:51 +00:00
bool SCH_SHEET::Matches( const wxFindReplaceData& aSearchData, void* aAuxData ) const
{
2019-12-20 14:11:39 +00:00
wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText( EDA_UNITS::MILLIMETRES ) );
// Sheets are searchable via the child field and pin item text.
return false;
}
void SCH_SHEET::renumberPins()
{
int id = 2;
for( SCH_SHEET_PIN* pin : m_pins )
{
pin->SetNumber( id );
id++;
}
}
void SCH_SHEET::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
{
for( SCH_SHEET_PIN* sheetPin : m_pins )
{
wxCHECK2_MSG( sheetPin->Type() == SCH_SHEET_PIN_T, continue,
wxT( "Invalid item in schematic sheet pin list. Bad programmer!" ) );
sheetPin->GetEndPoints( aItemList );
}
}
bool SCH_SHEET::UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
const SCH_SHEET_PATH* aPath )
{
bool changed = false;
for( SCH_SHEET_PIN* sheetPin : m_pins )
changed |= sheetPin->UpdateDanglingState( aItemList );
return changed;
}
std::vector<wxPoint> SCH_SHEET::GetConnectionPoints() const
{
std::vector<wxPoint> retval;
for( SCH_SHEET_PIN* sheetPin : m_pins )
retval.push_back( sheetPin->GetPosition() );
return retval;
}
SEARCH_RESULT SCH_SHEET::Visit( INSPECTOR aInspector, void* testData, const KICAD_T aFilterTypes[] )
{
KICAD_T stype;
for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
{
// If caller wants to inspect my type
2019-05-08 18:56:03 +00:00
if( stype == SCH_LOCATE_ANY_T || stype == Type() )
{
2021-07-16 20:13:26 +00:00
if( SEARCH_RESULT::QUIT == aInspector( this, nullptr ) )
2019-12-28 00:55:11 +00:00
return SEARCH_RESULT::QUIT;
}
2019-05-08 18:56:03 +00:00
2020-03-06 20:02:58 +00:00
if( stype == SCH_LOCATE_ANY_T || stype == SCH_FIELD_T )
{
// Test the sheet fields.
for( SCH_FIELD& field : m_fields )
{
if( SEARCH_RESULT::QUIT == aInspector( &field, this ) )
return SEARCH_RESULT::QUIT;
}
}
2019-05-08 18:56:03 +00:00
if( stype == SCH_LOCATE_ANY_T || stype == SCH_SHEET_PIN_T )
{
// Test the sheet labels.
for( SCH_SHEET_PIN* sheetPin : m_pins )
{
if( SEARCH_RESULT::QUIT == aInspector( sheetPin, this ) )
2019-12-28 00:55:11 +00:00
return SEARCH_RESULT::QUIT;
}
}
}
2019-12-28 00:55:11 +00:00
return SEARCH_RESULT::CONTINUE;
}
void SCH_SHEET::RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction )
{
for( SCH_FIELD& field : m_fields )
aFunction( &field );
for( SCH_SHEET_PIN* pin : m_pins )
aFunction( pin );
}
2019-12-20 14:11:39 +00:00
wxString SCH_SHEET::GetSelectMenuText( EDA_UNITS aUnits ) const
{
return wxString::Format( _( "Hierarchical Sheet %s" ),
m_fields[ SHEETNAME ].GetText() );
}
BITMAPS SCH_SHEET::GetMenuImage() const
{
return BITMAPS::add_hierarchical_subsheet;
}
bool SCH_SHEET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
{
2020-03-06 20:02:58 +00:00
EDA_RECT rect = GetBodyBoundingBox();
rect.Inflate( aAccuracy );
return rect.Contains( aPosition );
}
bool SCH_SHEET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
{
EDA_RECT rect = aRect;
rect.Inflate( aAccuracy );
if( aContained )
2020-03-06 20:02:58 +00:00
return rect.Contains( GetBodyBoundingBox() );
2020-03-06 20:02:58 +00:00
return rect.Intersects( GetBodyBoundingBox() );
}
void SCH_SHEET::Plot( PLOTTER* aPlotter ) const
{
wxString msg;
wxPoint pos;
auto* settings = dynamic_cast<KIGFX::SCH_RENDER_SETTINGS*>( aPlotter->RenderSettings() );
bool override = settings ? settings->m_OverrideItemColors : false;
COLOR4D borderColor = GetBorderColor();
COLOR4D backgroundColor = GetBackgroundColor();
if( override || borderColor == COLOR4D::UNSPECIFIED )
2020-04-14 12:25:00 +00:00
borderColor = aPlotter->RenderSettings()->GetLayerColor( LAYER_SHEET );
if( override || backgroundColor == COLOR4D::UNSPECIFIED )
backgroundColor = aPlotter->RenderSettings()->GetLayerColor( LAYER_SHEET_BACKGROUND );
// Do not fill shape in B&W mode, otherwise texts are unreadable
bool fill = aPlotter->GetColorMode();
if( fill )
{
aPlotter->SetColor( backgroundColor );
aPlotter->Rect( m_pos, m_pos + m_size, FILL_T::FILLED_SHAPE, 1 );
}
2020-04-14 12:25:00 +00:00
aPlotter->SetColor( borderColor );
int penWidth = std::max( GetPenWidth(), aPlotter->RenderSettings()->GetMinPenWidth() );
aPlotter->Rect( m_pos, m_pos + m_size, FILL_T::NO_FILL, penWidth );
// Plot sheet pins
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->Plot( aPlotter );
// Plot the fields
for( const SCH_FIELD& field : m_fields )
field.Plot( aPlotter );
}
2020-12-20 18:18:54 +00:00
void SCH_SHEET::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
{
wxDC* DC = aSettings->GetPrintDC();
wxPoint pos = m_pos + aOffset;
int lineWidth = std::max( GetPenWidth(), aSettings->GetDefaultPenWidth() );
const auto* settings = dynamic_cast<const KIGFX::SCH_RENDER_SETTINGS*>( aSettings );
bool override = settings && settings->m_OverrideItemColors;
COLOR4D border = GetBorderColor();
COLOR4D background = GetBackgroundColor();
if( override || border == COLOR4D::UNSPECIFIED )
border = aSettings->GetLayerColor( LAYER_SHEET );
if( override || background == COLOR4D::UNSPECIFIED )
background = aSettings->GetLayerColor( LAYER_SHEET_BACKGROUND );
if( GetGRForceBlackPenState() ) // printing in black & white
background = COLOR4D::UNSPECIFIED;
if( background != COLOR4D::UNSPECIFIED )
{
GRFilledRect( nullptr, DC, pos.x, pos.y, pos.x + m_size.x, pos.y + m_size.y, 0,
background, background );
}
GRRect( nullptr, DC, pos.x, pos.y, pos.x + m_size.x, pos.y + m_size.y, lineWidth, border );
for( SCH_FIELD& field : m_fields )
field.Print( aSettings, aOffset );
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->Print( aSettings, aOffset );
}
SCH_SHEET& SCH_SHEET::operator=( const SCH_ITEM& aItem )
{
wxCHECK_MSG( Type() == aItem.Type(), *this,
wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
GetClass() );
if( &aItem != this )
{
SCH_ITEM::operator=( aItem );
SCH_SHEET* sheet = (SCH_SHEET*) &aItem;
m_pos = sheet->m_pos;
m_size = sheet->m_size;
m_fields = sheet->m_fields;
for( SCH_SHEET_PIN* pin : sheet->m_pins )
{
m_pins.emplace_back( new SCH_SHEET_PIN( *pin ) );
m_pins.back()->SetParent( this );
}
for( const SCH_SHEET_INSTANCE& instance : sheet->m_instances )
m_instances.emplace_back( instance );
}
return *this;
}
bool SCH_SHEET::operator <( const SCH_ITEM& aItem ) const
{
if( Type() != aItem.Type() )
return Type() < aItem.Type();
auto sheet = static_cast<const SCH_SHEET*>( &aItem );
if (m_fields[ SHEETNAME ].GetText() != sheet->m_fields[ SHEETNAME ].GetText() )
return m_fields[ SHEETNAME ].GetText() < sheet->m_fields[ SHEETNAME ].GetText();
if (m_fields[ SHEETFILENAME ].GetText() != sheet->m_fields[ SHEETFILENAME ].GetText() )
return m_fields[ SHEETFILENAME ].GetText() < sheet->m_fields[ SHEETFILENAME ].GetText();
return false;
}
bool SCH_SHEET::AddInstance( const SCH_SHEET_PATH& aSheetPath )
{
wxCHECK( aSheetPath.IsFullPath(), false );
for( const SCH_SHEET_INSTANCE& instance : m_instances )
{
// if aSheetPath is found, nothing to do:
if( instance.m_Path == aSheetPath.PathWithoutRootUuid() )
return false;
}
wxLogTrace( traceSchSheetPaths, wxT( "Adding instance `%s` to sheet `%s`." ),
aSheetPath.PathWithoutRootUuid().AsString(),
( GetName().IsEmpty() ) ? wxT( "root" ) : GetName() );
SCH_SHEET_INSTANCE instance;
instance.m_Path = aSheetPath.PathWithoutRootUuid();
// This entry does not exist: add it with an empty page number.
m_instances.emplace_back( instance );
return true;
}
wxString SCH_SHEET::GetPageNumber( const SCH_SHEET_PATH& aSheetPath ) const
{
wxCHECK( aSheetPath.IsFullPath(), wxEmptyString );
wxString pageNumber;
KIID_PATH path = aSheetPath.PathWithoutRootUuid();
for( const SCH_SHEET_INSTANCE& instance : m_instances )
{
if( instance.m_Path == path )
{
pageNumber = instance.m_PageNumber;
break;
}
}
return pageNumber;
}
void SCH_SHEET::SetPageNumber( const SCH_SHEET_PATH& aSheetPath, const wxString& aPageNumber )
{
wxCHECK( aSheetPath.IsFullPath(), /* void */ );
KIID_PATH path = aSheetPath.PathWithoutRootUuid();
for( SCH_SHEET_INSTANCE& instance : m_instances )
{
if( instance.m_Path == path )
{
instance.m_PageNumber = aPageNumber;
break;
}
}
}
int SCH_SHEET::ComparePageNum( const wxString& aPageNumberA, const wxString& aPageNumberB )
{
if( aPageNumberA == aPageNumberB )
return 0; // A == B
// First sort numerically if the page numbers are integers
long pageA, pageB;
bool isIntegerPageA = aPageNumberA.ToLong( &pageA );
bool isIntegerPageB = aPageNumberB.ToLong( &pageB );
if( isIntegerPageA && isIntegerPageB )
{
if( pageA < pageB )
return -1; //A < B
else
return 1; // A > B
}
// Numerical page numbers always before strings
if( isIntegerPageA )
return -1; //A < B
else if( isIntegerPageB )
return 1; // A > B
// If not numeric, then sort as strings using natural sort
int result = StrNumCmp( aPageNumberA, aPageNumberB );
if( result > 0 )
return 1; // A > B
return -1; //A < B
}
2008-04-22 16:38:23 +00:00
#if defined(DEBUG)
void SCH_SHEET::Show( int nestLevel, std::ostream& os ) const
2008-04-22 16:38:23 +00:00
{
// XML output:
wxString s = GetClass();
NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">" << " sheet_name=\""
<< TO_UTF8( m_fields[ SHEETNAME ].GetText() ) << '"' << ">\n";
2008-04-22 16:38:23 +00:00
// show all the pins, and check the linked list integrity
for( SCH_SHEET_PIN* sheetPin : m_pins )
sheetPin->Show( nestLevel + 1, os );
2008-04-22 16:38:23 +00:00
NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n" << std::flush;
}
2008-02-26 19:19:54 +00:00
#endif