ADDED: Align to Grid function in Eeschema
Adds tool option to align selected elements to the current grid. Fixes https://gitlab.com/kicad/code/kicad/issues/5481
This commit is contained in:
parent
7b2a111a1c
commit
471f81742f
|
@ -715,3 +715,7 @@ TOOL_ACTION EE_ACTIONS::moveActivate( "eeschema.InteractiveMove",
|
|||
TOOL_ACTION EE_ACTIONS::symbolMoveActivate( "eeschema.SymbolMoveTool",
|
||||
AS_GLOBAL, 0, "",
|
||||
_( "Symbol Move Activate" ), "", move_xpm, AF_ACTIVATE );
|
||||
|
||||
TOOL_ACTION EE_ACTIONS::alignToGrid( "eeschema.AlignToGrid",
|
||||
AS_GLOBAL, 0, "",
|
||||
_( "Align Elements to Grid" ), "", move_xpm, AF_ACTIVATE );
|
||||
|
|
|
@ -108,6 +108,7 @@ public:
|
|||
static TOOL_ACTION finishDrawing;
|
||||
|
||||
// Interactive Editing
|
||||
static TOOL_ACTION alignToGrid;
|
||||
static TOOL_ACTION symbolMoveActivate; // Symbol editor move tool activate
|
||||
static TOOL_ACTION moveActivate; // Schematic editor move tool activate
|
||||
static TOOL_ACTION move;
|
||||
|
|
|
@ -105,17 +105,25 @@ void EE_GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
|
|||
}
|
||||
|
||||
|
||||
VECTOR2I EE_GRID_HELPER::Align( const VECTOR2I& aPoint ) const
|
||||
VECTOR2I EE_GRID_HELPER::AlignGrid( const VECTOR2I& aPoint ) const
|
||||
{
|
||||
if( !m_toolMgr->GetView()->GetGAL()->GetGridSnapping() )
|
||||
return aPoint;
|
||||
|
||||
const VECTOR2D gridOffset( GetOrigin() );
|
||||
const VECTOR2D grid( GetGrid() );
|
||||
|
||||
VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
|
||||
KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
|
||||
|
||||
return nearest;
|
||||
}
|
||||
|
||||
|
||||
VECTOR2I EE_GRID_HELPER::Align( const VECTOR2I& aPoint ) const
|
||||
{
|
||||
if( !m_toolMgr->GetView()->GetGAL()->GetGridSnapping() )
|
||||
return aPoint;
|
||||
|
||||
VECTOR2I nearest = AlignGrid( aPoint );
|
||||
|
||||
if( !m_auxAxis )
|
||||
return nearest;
|
||||
|
||||
|
|
|
@ -56,6 +56,8 @@ public:
|
|||
|
||||
VECTOR2I Align( const VECTOR2I& aPoint ) const;
|
||||
|
||||
VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
|
||||
|
||||
VECTOR2I AlignToWire( const VECTOR2I& aPoint, const SEG& aSeg );
|
||||
|
||||
VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, std::vector<SCH_ITEM*>& aItem );
|
||||
|
|
|
@ -74,21 +74,13 @@ bool SCH_MOVE_TOOL::Init()
|
|||
|
||||
selToolMenu.AddItem( EE_ACTIONS::move, moveCondition, 150 );
|
||||
selToolMenu.AddItem( EE_ACTIONS::drag, moveCondition, 150 );
|
||||
selToolMenu.AddItem( EE_ACTIONS::alignToGrid, moveCondition, 150 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* TODO - Tom/Jeff
|
||||
- add preferences option "Move origin: always cursor / item origin"
|
||||
- add preferences option "Default drag action: drag items / move"
|
||||
- add preferences option "Drag always selects"
|
||||
*/
|
||||
|
||||
|
||||
int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
|
||||
{
|
||||
const KICAD_T movableItems[] =
|
||||
static const KICAD_T movableItems[] =
|
||||
{
|
||||
SCH_MARKER_T,
|
||||
SCH_JUNCTION_T,
|
||||
|
@ -108,6 +100,16 @@ int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
|
|||
EOT
|
||||
};
|
||||
|
||||
|
||||
/* TODO - Tom/Jeff
|
||||
- add preferences option "Move origin: always cursor / item origin"
|
||||
- add preferences option "Default drag action: drag items / move"
|
||||
- add preferences option "Drag always selects"
|
||||
*/
|
||||
|
||||
|
||||
int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
|
||||
{
|
||||
EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
|
||||
KIGFX::VIEW_CONTROLS* controls = getViewControls();
|
||||
EE_GRID_HELPER grid( m_toolMgr );
|
||||
|
@ -497,7 +499,7 @@ void SCH_MOVE_TOOL::getConnectedDragItems( SCH_ITEM* aOriginalItem, wxPoint aPoi
|
|||
|
||||
for( SCH_ITEM *test : items.Overlapping( aOriginalItem->GetBoundingBox() ) )
|
||||
{
|
||||
if( test->IsSelected() || !test->CanConnect( aOriginalItem ) )
|
||||
if( test == aOriginalItem || test->IsSelected() || !test->CanConnect( aOriginalItem ) )
|
||||
continue;
|
||||
|
||||
KICAD_T testType = test->Type();
|
||||
|
@ -618,7 +620,7 @@ void SCH_MOVE_TOOL::getConnectedDragItems( SCH_ITEM* aOriginalItem, wxPoint aPoi
|
|||
else
|
||||
otherEnd = ends[0];
|
||||
|
||||
getConnectedDragItems( (SCH_ITEM*) test, otherEnd, m_dragAdditions );
|
||||
getConnectedDragItems( test, otherEnd, aList );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -705,9 +707,106 @@ void SCH_MOVE_TOOL::moveItem( EDA_ITEM* aItem, const VECTOR2I& aDelta )
|
|||
}
|
||||
|
||||
|
||||
int SCH_MOVE_TOOL::AlignElements( const TOOL_EVENT& aEvent )
|
||||
{
|
||||
EE_GRID_HELPER grid( m_toolMgr);
|
||||
EE_SELECTION& selection = m_selectionTool->RequestSelection( movableItems );
|
||||
bool append_undo = false;
|
||||
|
||||
for( EDA_ITEM* item : selection )
|
||||
{
|
||||
if( item->Type() == SCH_LINE_T )
|
||||
{
|
||||
SCH_LINE* line = static_cast<SCH_LINE*>( item );
|
||||
std::vector<int> flags{ STARTPOINT, ENDPOINT };
|
||||
std::vector<wxPoint> pts{ line->GetStartPoint(), line->GetEndPoint() };
|
||||
|
||||
for( int ii = 0; ii < 2; ++ii )
|
||||
{
|
||||
EDA_ITEMS drag_items{ item };
|
||||
line->ClearFlags();
|
||||
line->SetFlags( flags[ii] );
|
||||
getConnectedDragItems( line, pts[ii], drag_items );
|
||||
std::set<EDA_ITEM*> unique_items( drag_items.begin(), drag_items.end() );
|
||||
|
||||
VECTOR2I gridpt = grid.AlignGrid( pts[ii] ) - pts[ii];
|
||||
|
||||
if( gridpt != VECTOR2I( 0, 0 ) )
|
||||
{
|
||||
for( auto dritem : unique_items )
|
||||
{
|
||||
if( dritem->GetParent() && dritem->GetParent()->IsSelected() )
|
||||
continue;
|
||||
|
||||
saveCopyInUndoList( dritem, UNDO_REDO::CHANGED, append_undo );
|
||||
append_undo = true;
|
||||
|
||||
moveItem( dritem, gridpt );
|
||||
updateView( dritem );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<wxPoint> connections;
|
||||
EDA_ITEMS drag_items{ item };
|
||||
connections = static_cast<SCH_ITEM*>( item )->GetConnectionPoints();
|
||||
|
||||
for( wxPoint point : connections )
|
||||
getConnectedDragItems( static_cast<SCH_ITEM*>( item ), point, drag_items );
|
||||
|
||||
std::map<VECTOR2I, int> shifts;
|
||||
VECTOR2I most_common( 0, 0 );
|
||||
int max_count = 0;
|
||||
|
||||
for( auto& conn : connections )
|
||||
{
|
||||
VECTOR2I gridpt = grid.AlignGrid( conn ) - conn;
|
||||
|
||||
shifts[gridpt]++;
|
||||
|
||||
if( shifts[gridpt] > max_count )
|
||||
{
|
||||
most_common = gridpt;
|
||||
max_count = shifts[most_common];
|
||||
}
|
||||
}
|
||||
|
||||
if( most_common != VECTOR2I( 0, 0 ) )
|
||||
{
|
||||
for( auto dritem : drag_items )
|
||||
{
|
||||
if( dritem->GetParent() && dritem->GetParent()->IsSelected() )
|
||||
continue;
|
||||
|
||||
saveCopyInUndoList( dritem, UNDO_REDO::CHANGED, append_undo );
|
||||
append_undo = true;
|
||||
|
||||
moveItem( dritem, most_common );
|
||||
updateView( dritem );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
m_toolMgr->PostEvent( EVENTS::SelectedItemsMoved );
|
||||
m_toolMgr->RunAction( EE_ACTIONS::addNeededJunctions, true, &selection );
|
||||
|
||||
m_frame->SchematicCleanUp();
|
||||
m_frame->TestDanglingEnds();
|
||||
|
||||
m_frame->OnModify();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void SCH_MOVE_TOOL::setTransitions()
|
||||
{
|
||||
Go( &SCH_MOVE_TOOL::Main, EE_ACTIONS::moveActivate.MakeEvent() );
|
||||
Go( &SCH_MOVE_TOOL::Main, EE_ACTIONS::move.MakeEvent() );
|
||||
Go( &SCH_MOVE_TOOL::Main, EE_ACTIONS::drag.MakeEvent() );
|
||||
Go( &SCH_MOVE_TOOL::AlignElements, EE_ACTIONS::alignToGrid.MakeEvent() );
|
||||
}
|
||||
|
|
|
@ -50,12 +50,17 @@ public:
|
|||
bool Init() override;
|
||||
|
||||
/**
|
||||
* Function Main()
|
||||
*
|
||||
* Runs an interactive move of the selected items, or the item under the cursor.
|
||||
*/
|
||||
int Main( const TOOL_EVENT& aEvent );
|
||||
|
||||
/**
|
||||
* Aligns selected elements to the grid
|
||||
* @param aEvent current event that activated the tool
|
||||
* @return 0
|
||||
*/
|
||||
int AlignElements( const TOOL_EVENT& aEvent );
|
||||
|
||||
private:
|
||||
void moveItem( EDA_ITEM* aItem, const VECTOR2I& aDelta );
|
||||
|
||||
|
|
Loading…
Reference in New Issue