2020-02-28 22:13:25 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2020 CERN
|
2021-07-08 12:33:08 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-02-28 22:13:25 +00:00
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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 <memory>
|
|
|
|
|
2021-07-03 23:50:04 +00:00
|
|
|
#include "pns_arc.h"
|
2020-02-28 22:13:25 +00:00
|
|
|
#include "pns_line.h"
|
|
|
|
#include "pns_solid.h"
|
|
|
|
#include "pns_router.h"
|
|
|
|
|
|
|
|
#include "pns_component_dragger.h"
|
|
|
|
#include "pns_debug_decorator.h"
|
|
|
|
|
|
|
|
namespace PNS
|
|
|
|
{
|
|
|
|
|
|
|
|
COMPONENT_DRAGGER::COMPONENT_DRAGGER( ROUTER* aRouter ) : DRAG_ALGO( aRouter )
|
|
|
|
{
|
2020-03-30 07:24:56 +00:00
|
|
|
// ensure all variables are initialized
|
|
|
|
m_dragStatus = false;
|
|
|
|
m_currentNode = nullptr;
|
2020-02-28 22:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COMPONENT_DRAGGER::~COMPONENT_DRAGGER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool COMPONENT_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
|
|
|
|
{
|
2021-07-08 12:33:08 +00:00
|
|
|
assert( m_world );
|
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
m_currentNode = nullptr;
|
|
|
|
m_initialDraggedItems = aPrimitives;
|
|
|
|
m_p0 = aP;
|
|
|
|
|
2020-12-23 03:28:13 +00:00
|
|
|
std::unordered_set<LINKED_ITEM*> seenItems;
|
|
|
|
|
|
|
|
auto addLinked =
|
2021-07-03 23:50:04 +00:00
|
|
|
[&]( SOLID* aSolid, JOINT* aJoint, LINKED_ITEM* aItem, VECTOR2I aOffset = {} )
|
2020-12-23 03:28:13 +00:00
|
|
|
{
|
|
|
|
if( seenItems.count( aItem ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
seenItems.insert( aItem );
|
|
|
|
|
2021-07-03 23:50:04 +00:00
|
|
|
// Segments that go directly between two linked pads are special-cased
|
|
|
|
VECTOR2I otherEnd = ( aJoint->Pos() == aItem->Anchor( 0 ) ) ?
|
|
|
|
aItem->Anchor( 1 ) : aItem->Anchor( 0 );
|
|
|
|
JOINT* otherJoint = m_world->FindJoint( otherEnd, aItem->Layer(), aItem->Net() );
|
|
|
|
|
|
|
|
if( otherJoint && otherJoint->LinkCount( ITEM::SOLID_T ) )
|
|
|
|
{
|
|
|
|
for( const ITEM_SET::ENTRY& otherItem : otherJoint->LinkList() )
|
|
|
|
{
|
|
|
|
if( aPrimitives.Contains( otherItem.item ) )
|
|
|
|
{
|
|
|
|
m_fixedItems.insert( aItem );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:28:13 +00:00
|
|
|
int segIndex;
|
|
|
|
DRAGGED_CONNECTION cn;
|
|
|
|
|
|
|
|
cn.origLine = m_world->AssembleLine( aItem, &segIndex );
|
|
|
|
cn.attachedPad = aSolid;
|
|
|
|
cn.offset = aOffset;
|
|
|
|
|
2021-07-04 00:00:54 +00:00
|
|
|
// Lines that go directly between two linked pads are also special-cased
|
|
|
|
const SHAPE_LINE_CHAIN& line = cn.origLine.CLine();
|
|
|
|
JOINT* jA = m_world->FindJoint( line.CPoint( 0 ), aItem->Layer(), aItem->Net() );
|
|
|
|
JOINT* jB = m_world->FindJoint( line.CPoint( -1 ), aItem->Layer(), aItem->Net() );
|
|
|
|
|
|
|
|
wxASSERT( jA == aJoint || jB == aJoint );
|
|
|
|
JOINT* jSearch = ( jA == aJoint ) ? jB : jA;
|
|
|
|
|
|
|
|
if( jSearch && jSearch->LinkCount( ITEM::SOLID_T ) )
|
|
|
|
{
|
|
|
|
for( const ITEM_SET::ENTRY& otherItem : jSearch->LinkList() )
|
|
|
|
{
|
|
|
|
if( aPrimitives.Contains( otherItem.item ) )
|
|
|
|
{
|
|
|
|
for( ITEM* item : cn.origLine.Links() )
|
|
|
|
m_fixedItems.insert( item );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:28:13 +00:00
|
|
|
m_conns.push_back( cn );
|
|
|
|
};
|
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
for( auto item : aPrimitives.Items() )
|
|
|
|
{
|
|
|
|
if( item.item->Kind() != ITEM::SOLID_T )
|
|
|
|
continue;
|
|
|
|
|
2020-08-26 21:57:32 +00:00
|
|
|
if( ! item.item->IsRoutable() )
|
|
|
|
continue;
|
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
auto solid = static_cast<SOLID*>( item.item );
|
|
|
|
auto jt = m_world->FindJoint( solid->Pos(), solid );
|
|
|
|
|
|
|
|
m_solids.insert( solid );
|
|
|
|
|
|
|
|
for( auto link : jt->LinkList() )
|
|
|
|
{
|
|
|
|
if( link.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
|
2021-07-03 23:50:04 +00:00
|
|
|
addLinked( solid, jt, static_cast<LINKED_ITEM*>( link.item ) );
|
2020-12-23 03:28:13 +00:00
|
|
|
}
|
2020-02-28 22:13:25 +00:00
|
|
|
|
2020-12-23 03:28:13 +00:00
|
|
|
std::vector<JOINT*> extraJoints;
|
2020-02-28 22:13:25 +00:00
|
|
|
|
2020-12-23 03:43:44 +00:00
|
|
|
m_world->QueryJoints( solid->Hull().BBox(), extraJoints, solid->Layers(),
|
|
|
|
ITEM::SEGMENT_T | ITEM::ARC_T );
|
|
|
|
|
|
|
|
for( JOINT* extraJoint : extraJoints )
|
2020-12-23 03:28:13 +00:00
|
|
|
{
|
2020-12-23 03:43:44 +00:00
|
|
|
if( extraJoint->Net() == jt->Net() && extraJoint->LinkCount() == 1 )
|
2020-12-23 03:28:13 +00:00
|
|
|
{
|
2020-12-23 03:43:44 +00:00
|
|
|
LINKED_ITEM* li = static_cast<LINKED_ITEM*>( extraJoint->LinkList()[0].item );
|
2020-02-28 22:13:25 +00:00
|
|
|
|
2021-04-22 21:20:34 +00:00
|
|
|
if( li->Collide( solid, nullptr, m_world ) )
|
2021-07-03 23:50:04 +00:00
|
|
|
addLinked( solid, extraJoint, li, extraJoint->Pos() - solid->Pos() );
|
2020-02-28 22:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:33:08 +00:00
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
bool COMPONENT_DRAGGER::Drag( const VECTOR2I& aP )
|
|
|
|
{
|
2021-07-08 12:33:08 +00:00
|
|
|
assert( m_world );
|
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
m_world->KillChildren();
|
|
|
|
m_currentNode = m_world->Branch();
|
|
|
|
|
|
|
|
for( auto item : m_initialDraggedItems.Items() )
|
|
|
|
m_currentNode->Remove( item );
|
|
|
|
|
|
|
|
m_draggedItems.Clear();
|
|
|
|
|
|
|
|
for( auto item : m_solids )
|
|
|
|
{
|
|
|
|
SOLID* s = static_cast<SOLID*>( item );
|
|
|
|
auto p_next = aP - m_p0 + s->Pos();
|
|
|
|
std::unique_ptr<SOLID> snew( static_cast<SOLID*>( s->Clone() ) );
|
|
|
|
snew->SetPos( p_next );
|
|
|
|
|
|
|
|
m_draggedItems.Add( snew.get() );
|
|
|
|
m_currentNode->Add( std::move( snew ) );
|
|
|
|
|
|
|
|
for( auto& l : m_conns )
|
|
|
|
{
|
|
|
|
if( l.attachedPad == s )
|
|
|
|
{
|
2020-12-23 03:28:13 +00:00
|
|
|
l.p_orig = s->Pos() + l.offset;
|
|
|
|
l.p_next = p_next + l.offset;
|
2020-02-28 22:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 23:50:04 +00:00
|
|
|
for( ITEM* item : m_fixedItems )
|
|
|
|
{
|
|
|
|
m_currentNode->Remove( item );
|
|
|
|
|
|
|
|
switch( item->Kind() )
|
|
|
|
{
|
|
|
|
case ITEM::SEGMENT_T:
|
|
|
|
{
|
|
|
|
SEGMENT* s = static_cast<SEGMENT*>( item );
|
|
|
|
std::unique_ptr<SEGMENT> s_new( s->Clone() );
|
|
|
|
|
|
|
|
SEG orig = s->Seg();
|
|
|
|
s_new->SetEnds( aP - m_p0 + orig.A, aP - m_p0 + orig.B );
|
|
|
|
|
|
|
|
m_draggedItems.Add( s_new.get() );
|
|
|
|
m_currentNode->Add( std::move( s_new ) );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ITEM::ARC_T:
|
|
|
|
{
|
|
|
|
ARC* a = static_cast<ARC*>( item );
|
|
|
|
std::unique_ptr<ARC> a_new( a->Clone() );
|
|
|
|
|
|
|
|
SHAPE_ARC& arc = a_new->Arc();
|
|
|
|
arc.Move( aP - m_p0 );
|
|
|
|
|
|
|
|
m_draggedItems.Add( a_new.get() );
|
|
|
|
m_currentNode->Add( std::move( a_new ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "Unexpected item type in COMPONENT_DRAGGER::m_fixedItems" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
for( auto& cn : m_conns )
|
|
|
|
{
|
|
|
|
auto l_new( cn.origLine );
|
|
|
|
l_new.Unmark();
|
2020-04-15 20:16:24 +00:00
|
|
|
l_new.ClearLinks();
|
2020-02-28 22:13:25 +00:00
|
|
|
l_new.DragCorner( cn.p_next, cn.origLine.CLine().Find( cn.p_orig ) );
|
|
|
|
|
2021-05-28 22:09:21 +00:00
|
|
|
PNS_DBG( Dbg(), AddLine, l_new.CLine(), BLUE, 100000, "cdrag-new-fanout" );
|
2020-02-28 22:13:25 +00:00
|
|
|
m_draggedItems.Add( l_new );
|
|
|
|
|
|
|
|
auto l_orig( cn.origLine );
|
|
|
|
m_currentNode->Remove( l_orig );
|
|
|
|
m_currentNode->Add( l_new );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:33:08 +00:00
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
bool COMPONENT_DRAGGER::FixRoute()
|
|
|
|
{
|
|
|
|
NODE* node = CurrentNode();
|
|
|
|
|
|
|
|
if( node )
|
|
|
|
{
|
|
|
|
bool ok;
|
2021-04-10 09:31:42 +00:00
|
|
|
|
|
|
|
if( Settings().AllowDRCViolations() )
|
2020-02-28 22:13:25 +00:00
|
|
|
ok = true;
|
|
|
|
else
|
|
|
|
ok = !node->CheckColliding( m_draggedItems );
|
|
|
|
|
|
|
|
if( !ok )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Router()->CommitRouting( node );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:33:08 +00:00
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
NODE* COMPONENT_DRAGGER::CurrentNode() const
|
|
|
|
{
|
|
|
|
return m_currentNode ? m_currentNode : m_world;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:33:08 +00:00
|
|
|
|
2020-02-28 22:13:25 +00:00
|
|
|
const ITEM_SET COMPONENT_DRAGGER::Traces()
|
|
|
|
{
|
|
|
|
return m_draggedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace PNS
|