Do not open label edit dialog when adding label to an already named wire

This commit is contained in:
Miklós Márton 2022-05-02 08:42:15 +00:00 committed by Jeff Young
parent c34fb494b1
commit 7ebdb4853c
3 changed files with 112 additions and 7 deletions

View File

@ -86,6 +86,39 @@ SCH_LINE::SCH_LINE( const SCH_LINE& aLine ) :
m_lastResolvedColor = aLine.m_lastResolvedColor;
}
wxString SCH_LINE::GetNetname( const SCH_SHEET_PATH& aSheet )
{
std::list<const SCH_LINE *> checkedLines;
checkedLines.push_back(this);
return FindWireSegmentNetNameRecursive( this, checkedLines, aSheet );
}
wxString SCH_LINE::FindWireSegmentNetNameRecursive( SCH_LINE *line, std::list<const SCH_LINE *> &checkedLines,
const SCH_SHEET_PATH& aSheet ) const
{
for ( auto connected : line->ConnectedItems( aSheet ) )
{
if( connected->Type() == SCH_LINE_T )
{
if( std::find(checkedLines.begin(), checkedLines.end(), connected ) == checkedLines.end() )
{
auto connectedLine = static_cast<SCH_LINE*>( connected );
checkedLines.push_back( connectedLine );
auto netName = FindWireSegmentNetNameRecursive( connectedLine, checkedLines, aSheet );
if (!netName.IsEmpty())
return netName;
}
}
else if( connected->Type() == SCH_LABEL_T
|| connected->Type() == SCH_GLOBAL_LABEL_T
|| connected->Type() == SCH_DIRECTIVE_LABEL_T)
{
return static_cast<SCH_TEXT*>( connected )->GetText();
}
}
return "";
}
EDA_ITEM* SCH_LINE::Clone() const
{
@ -934,7 +967,6 @@ bool SCH_LINE::IsBus() const
return ( GetLayer() == LAYER_BUS );
}
bool SCH_LINE::UsesDefaultStroke() const
{
return m_stroke.GetWidth() == 0 && m_stroke.GetColor() == COLOR4D::UNSPECIFIED

View File

@ -59,6 +59,14 @@ public:
return wxT( "SCH_LINE" );
}
/**
* @brief This function travel though all the connected wire segments
* to look for connected labels.
* @param aSheet - the sheet where the current wire segment is located
* @return returns the name of the wire if connected labels found, otherwise empty string
*/
wxString GetNetname(const SCH_SHEET_PATH &aSheet);
bool IsType( const KICAD_T aScanTypes[] ) const override
{
if( SCH_ITEM::IsType( aScanTypes ) )
@ -301,6 +309,17 @@ public:
bool IsBus() const;
private:
/**
* @brief Recursively called function to travel through the connected wires and find a connected
* net name label
* @param line - the wire segment to start the recursive lookup
* @param checkedLines - a lsit containing the already checked wire segments, to prevent the
* infinite recursion in the case if someone draws a rectangle for e.g.
* @param aSheet - the sheet where the lookup is performed
* @return With the net name if a connected label found, otherwise with an empty string
*/
wxString FindWireSegmentNetNameRecursive( SCH_LINE *line, std::list<const SCH_LINE*>& checkedLines,
const SCH_SHEET_PATH &aSheet ) const;
bool doIsConnected( const VECTOR2I& aPosition ) const override;
bool m_startIsDangling; ///< True if start point is not connected.

View File

@ -1186,18 +1186,72 @@ int SCH_DRAWING_TOOLS::TwoClickPlace( const TOOL_EVENT& aEvent )
{
item = createNewText( cursorPos, LAYER_NOTES );
}
else if( isGlobalLabel )
{
item = createNewText( cursorPos, LAYER_GLOBLABEL );
}
else if( isHierLabel )
{
item = createNewText( cursorPos, LAYER_HIERLABEL );
}
else if( isNetLabel )
else if( isNetLabel || isGlobalLabel )
{
wxString netName;
for( SCH_ITEM* overlapItem :
m_frame->GetScreen()->Items().Overlapping( cursorPos ) )
{
if( overlapItem->GetEditFlags() & STRUCT_DELETED )
continue;
if( overlapItem->Type() == SCH_LINE_T )
{
SCH_LINE* line = static_cast<SCH_LINE*>( overlapItem );
if( line->IsWire() )
{
netName = line->GetNetname(m_frame->GetCurrentSheet());
break;
}
}
}
if( netName.IsEmpty() )
{
// no connected net label found -> open up the new label dialog
if( isGlobalLabel )
{
item = createNewText( cursorPos, LAYER_GLOBLABEL );
}
else
{
item = createNewText( cursorPos, LAYER_LOCLABEL );
}
}
else
{
// connected net label found -> create the label immediately
SCHEMATIC* schematic = getModel<SCHEMATIC>();
SCHEMATIC_SETTINGS& settings = schematic->Settings();
SCH_LABEL_BASE* labelItem = nullptr;
if( isGlobalLabel )
{
labelItem = new SCH_GLOBALLABEL( cursorPos );
labelItem->SetShape( m_lastGlobalLabelShape );
// make intersheets reference visible based on settings
labelItem->GetFields()[0].SetVisible( settings.m_IntersheetRefsShow );
}
else
{
labelItem = new SCH_LABEL( cursorPos );
}
labelItem->SetParent( getModel<SCHEMATIC>() );
labelItem->SetBold( m_lastTextBold );
labelItem->SetItalic( m_lastTextItalic );
labelItem->SetTextSpinStyle( m_lastTextOrientation );
labelItem->SetTextSize(
wxSize( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
labelItem->SetFlags( IS_NEW | IS_MOVING );
labelItem->SetText( netName );
item = labelItem;
}
}
else if( isClassLabel )
{
item = createNewText( cursorPos, LAYER_NETCLASS_REFS );