2018-11-13 13:30:56 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
* @author Jon Evans <jon@craftyjon.com>
|
|
|
|
*
|
|
|
|
* 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 <board_commit.h>
|
|
|
|
#include <class_board.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_shape.h>
|
|
|
|
#include <fp_shape.h>
|
2018-11-13 13:30:56 +00:00
|
|
|
#include <class_track.h>
|
|
|
|
#include <class_zone.h>
|
|
|
|
#include <collectors.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <menus_helpers.h>
|
|
|
|
#include <pcb_edit_frame.h>
|
2020-10-25 12:25:46 +00:00
|
|
|
#include <footprint_edit_frame.h>
|
2018-11-13 13:30:56 +00:00
|
|
|
#include <trigo.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <tools/edit_tool.h>
|
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
#include <tools/selection_tool.h>
|
|
|
|
|
|
|
|
#include "convert_tool.h"
|
|
|
|
|
|
|
|
|
|
|
|
CONVERT_TOOL::CONVERT_TOOL() :
|
|
|
|
TOOL_INTERACTIVE( "pcbnew.Convert" ), m_selectionTool( NULL ),
|
|
|
|
m_menu( NULL ), m_frame( NULL )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CONVERT_TOOL::~CONVERT_TOOL()
|
|
|
|
{
|
|
|
|
delete m_menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using S_C = SELECTION_CONDITIONS;
|
|
|
|
using P_S_C = PCB_SELECTION_CONDITIONS;
|
|
|
|
|
|
|
|
|
|
|
|
bool CONVERT_TOOL::Init()
|
|
|
|
{
|
2020-08-31 13:19:57 +00:00
|
|
|
m_selectionTool = m_toolMgr->GetTool<SELECTION_TOOL>();
|
|
|
|
m_frame = getEditFrame<PCB_BASE_FRAME>();
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
// Create a context menu and make it available through selection tool
|
|
|
|
m_menu = new CONDITIONAL_MENU( this );
|
|
|
|
m_menu->SetIcon( refresh_xpm );
|
2020-08-29 16:12:40 +00:00
|
|
|
m_menu->SetTitle( _( "Convert..." ) );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
static KICAD_T convertableTracks[] = { PCB_TRACE_T, PCB_ARC_T, EOT };
|
2020-10-04 14:19:33 +00:00
|
|
|
static KICAD_T convertableZones[] = { PCB_ZONE_AREA_T, PCB_FP_ZONE_AREA_T, EOT };
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
auto graphicLines = P_S_C::OnlyGraphicShapeTypes( { S_SEGMENT, S_RECT } ) && P_S_C::SameLayer();
|
|
|
|
|
|
|
|
auto trackLines = S_C::MoreThan( 1 ) &&
|
|
|
|
S_C::OnlyTypes( convertableTracks ) && P_S_C::SameLayer();
|
|
|
|
|
|
|
|
auto anyLines = graphicLines || trackLines;
|
|
|
|
|
2020-10-02 22:42:32 +00:00
|
|
|
auto anyPolys = ( S_C::OnlyTypes( convertableZones ) ||
|
2020-09-08 12:41:22 +00:00
|
|
|
P_S_C::OnlyGraphicShapeTypes( { S_POLYGON, S_RECT } ) );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
2020-10-02 22:42:32 +00:00
|
|
|
auto lineToArc = S_C::Count( 1 ) && ( P_S_C::OnlyGraphicShapeTypes( { S_SEGMENT } ) ||
|
|
|
|
S_C::OnlyType( PCB_TRACE_T ) );
|
2020-08-29 16:12:40 +00:00
|
|
|
|
|
|
|
auto showConvert = anyPolys || anyLines || lineToArc;
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToPoly, anyLines );
|
2020-10-02 22:42:32 +00:00
|
|
|
|
|
|
|
if( m_frame->IsType( FRAME_PCB_EDITOR ) )
|
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToZone, anyLines );
|
|
|
|
|
2020-10-25 12:25:46 +00:00
|
|
|
if( m_frame->IsType( FRAME_PCB_EDITOR ) )
|
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToKeepout, anyLines );
|
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToLines, anyPolys );
|
2020-10-25 12:25:46 +00:00
|
|
|
|
|
|
|
// Currently the code exists, but tracks are not really existing in footprints
|
|
|
|
// only segments on copper layers
|
|
|
|
if( m_frame->IsType( FRAME_PCB_EDITOR ) )
|
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToTracks, anyPolys );
|
|
|
|
|
2020-08-29 16:12:40 +00:00
|
|
|
m_menu->AddItem( PCB_ACTIONS::convertToArc, lineToArc );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
for( std::shared_ptr<ACTION_MENU>& subMenu : m_selectionTool->GetToolMenu().GetSubMenus() )
|
|
|
|
{
|
|
|
|
if( dynamic_cast<SPECIAL_TOOLS_CONTEXT_MENU*>( subMenu.get() ) )
|
2020-10-02 22:42:32 +00:00
|
|
|
static_cast<CONDITIONAL_MENU*>( subMenu.get() )->AddMenu( m_menu, SELECTION_CONDITIONS::ShowAlways );
|
2018-11-13 13:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CONVERT_TOOL::LinesToPoly( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2020-10-02 22:42:32 +00:00
|
|
|
MODULE* mod = nullptr;
|
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
auto& selection = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, SELECTION_TOOL* sTool )
|
|
|
|
{
|
|
|
|
EditToolSelectionFilter( aCollector,
|
|
|
|
EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS, sTool );
|
|
|
|
|
|
|
|
for( int i = aCollector.GetCount() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* item = aCollector[i];
|
|
|
|
|
|
|
|
switch( item->Type() )
|
|
|
|
{
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_SHAPE_T:
|
|
|
|
case PCB_FP_SHAPE_T:
|
2020-10-04 23:34:59 +00:00
|
|
|
switch( static_cast<PCB_SHAPE*>( item )->GetShape() )
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
|
|
|
case S_SEGMENT:
|
|
|
|
case S_RECT:
|
2020-10-02 22:42:32 +00:00
|
|
|
// case S_ARC: // Not yet
|
2018-11-13 13:30:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:43:05 +00:00
|
|
|
break;
|
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
case PCB_TRACE_T:
|
|
|
|
// case PCB_ARC_T: // Not yet
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( selection.Empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// TODO(JE) From a context menu, the selection condition enforces that the items are on
|
|
|
|
// a single layer. But, you can still trigger this with items on multiple layer selected.
|
|
|
|
// Technically we should make this work if each contiguous poly shares a layer
|
|
|
|
PCB_LAYER_ID destLayer = static_cast<BOARD_ITEM*>( selection.Front() )->GetLayer();
|
|
|
|
|
|
|
|
SHAPE_POLY_SET polySet = makePolysFromSegs( selection.GetItems() );
|
|
|
|
|
|
|
|
polySet.Append( makePolysFromRects( selection.GetItems() ) );
|
|
|
|
|
|
|
|
if( polySet.IsEmpty() )
|
|
|
|
return 0;
|
|
|
|
|
2020-10-02 22:42:32 +00:00
|
|
|
bool isFootprint = m_frame->IsType( FRAME_FOOTPRINT_EDITOR );
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
if( FP_SHAPE* graphic = dynamic_cast<FP_SHAPE*>( selection.Front() ) )
|
|
|
|
mod = graphic->GetParentModule();
|
2020-10-02 22:42:32 +00:00
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
|
|
|
|
|
|
|
// For now, we convert each outline in the returned shape to its own polygon
|
|
|
|
std::vector<SHAPE_POLY_SET> polys;
|
|
|
|
|
|
|
|
for( int i = 0; i < polySet.OutlineCount(); i++ )
|
|
|
|
polys.emplace_back( SHAPE_POLY_SET( polySet.COutline( i ) ) );
|
|
|
|
|
|
|
|
if( aEvent.IsAction( &PCB_ACTIONS::convertToPoly ) )
|
|
|
|
{
|
|
|
|
for( const SHAPE_POLY_SET& poly : polys )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* graphic = isFootprint ? new FP_SHAPE( mod ) : new PCB_SHAPE;
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
graphic->SetShape( S_POLYGON );
|
|
|
|
graphic->SetLayer( destLayer );
|
|
|
|
graphic->SetPolyShape( poly );
|
|
|
|
|
|
|
|
commit.Add( graphic );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Convert shapes to polygon" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Creating zone or keepout
|
|
|
|
PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
BOARD_ITEM_CONTAINER* parent = frame->GetModel();
|
|
|
|
ZONE_SETTINGS zoneInfo = frame->GetZoneSettings();
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( aEvent.IsAction( &PCB_ACTIONS::convertToKeepout ) )
|
2020-09-21 23:32:07 +00:00
|
|
|
ret = InvokeRuleAreaEditor( frame, &zoneInfo );
|
2018-11-13 13:30:56 +00:00
|
|
|
else
|
|
|
|
ret = InvokeCopperZonesEditor( frame, &zoneInfo );
|
|
|
|
|
|
|
|
if( ret == wxID_CANCEL )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for( const SHAPE_POLY_SET& poly : polys )
|
|
|
|
{
|
2020-10-02 22:42:32 +00:00
|
|
|
ZONE_CONTAINER* zone = isFootprint ? new MODULE_ZONE_CONTAINER( parent )
|
|
|
|
: new ZONE_CONTAINER( parent );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
*zone->Outline() = poly;
|
|
|
|
zone->HatchBorder();
|
|
|
|
|
|
|
|
zoneInfo.ExportSetting( *zone );
|
|
|
|
|
|
|
|
commit.Add( zone );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Convert shapes to zone" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SHAPE_POLY_SET CONVERT_TOOL::makePolysFromSegs( const std::deque<EDA_ITEM*>& aItems )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET poly;
|
|
|
|
|
|
|
|
std::map<VECTOR2I, std::vector<EDA_ITEM*>> connections;
|
|
|
|
std::set<EDA_ITEM*> used;
|
|
|
|
std::deque<EDA_ITEM*> toCheck;
|
|
|
|
|
|
|
|
for( EDA_ITEM* item : aItems )
|
|
|
|
{
|
|
|
|
if( OPT<SEG> seg = getStartEndPoints( item ) )
|
|
|
|
{
|
|
|
|
toCheck.push_back( item );
|
|
|
|
connections[seg->A].emplace_back( item );
|
|
|
|
connections[seg->B].emplace_back( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while( !toCheck.empty() )
|
|
|
|
{
|
|
|
|
EDA_ITEM* candidate = toCheck.front();
|
|
|
|
toCheck.pop_front();
|
|
|
|
|
|
|
|
if( used.count( candidate ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
OPT<SEG> seg = getStartEndPoints( candidate );
|
|
|
|
wxASSERT( seg );
|
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN outline;
|
|
|
|
std::deque<VECTOR2I> points;
|
|
|
|
|
|
|
|
// aDirection == true for walking "right" and appending to the end of points
|
|
|
|
// false for walking "left" and prepending to the beginning
|
|
|
|
std::function<void( EDA_ITEM*, bool )> process =
|
|
|
|
[&]( EDA_ITEM* aItem, bool aDirection )
|
|
|
|
{
|
|
|
|
if( used.count( aItem ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
used.insert( aItem );
|
|
|
|
|
|
|
|
OPT<SEG> nextSeg = getStartEndPoints( aItem );
|
|
|
|
wxASSERT( nextSeg );
|
|
|
|
|
|
|
|
// The reference point, i.e. last added point in the direction we're headed
|
|
|
|
VECTOR2I& ref = aDirection ? points.back() : points.front();
|
|
|
|
|
|
|
|
// The next point, i.e. the other point on this segment
|
|
|
|
VECTOR2I& next = ( ref == nextSeg->A ) ? nextSeg->B : nextSeg->A;
|
|
|
|
|
|
|
|
if( aDirection )
|
|
|
|
points.push_back( next );
|
|
|
|
else
|
|
|
|
points.push_front( next );
|
|
|
|
|
|
|
|
for( EDA_ITEM* neighbor : connections[next] )
|
|
|
|
process( neighbor, aDirection );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start with just one point and walk one direction
|
|
|
|
points.push_back( seg->A );
|
|
|
|
process( candidate, true );
|
|
|
|
|
|
|
|
// check for any candidates on the "left"
|
|
|
|
EDA_ITEM* left = nullptr;
|
|
|
|
|
|
|
|
for( EDA_ITEM* possibleLeft : connections[seg->A] )
|
|
|
|
{
|
|
|
|
if( possibleLeft != candidate )
|
|
|
|
{
|
|
|
|
left = possibleLeft;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( left )
|
|
|
|
process( left, false );
|
|
|
|
|
|
|
|
if( points.size() < 3 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( const VECTOR2I& point : points )
|
|
|
|
outline.Append( point );
|
|
|
|
|
|
|
|
outline.SetClosed( true );
|
|
|
|
|
|
|
|
poly.AddOutline( outline );
|
|
|
|
}
|
|
|
|
|
|
|
|
return poly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SHAPE_POLY_SET CONVERT_TOOL::makePolysFromRects( const std::deque<EDA_ITEM*>& aItems )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET poly;
|
|
|
|
|
|
|
|
for( EDA_ITEM* item : aItems )
|
|
|
|
{
|
2020-10-04 14:19:33 +00:00
|
|
|
if( item->Type() != PCB_SHAPE_T && item->Type() != PCB_FP_SHAPE_T )
|
2018-11-13 13:30:56 +00:00
|
|
|
continue;
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* graphic = static_cast<PCB_SHAPE*>( item );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
if( graphic->GetShape() != S_RECT )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN outline;
|
|
|
|
VECTOR2I start( graphic->GetStart() );
|
|
|
|
VECTOR2I end( graphic->GetEnd() );
|
|
|
|
|
|
|
|
outline.Append( start );
|
|
|
|
outline.Append( VECTOR2I( end.x, start.y ) );
|
|
|
|
outline.Append( end );
|
|
|
|
outline.Append( VECTOR2I( start.x, end.y ) );
|
|
|
|
outline.SetClosed( true );
|
|
|
|
|
|
|
|
poly.AddOutline( outline );
|
|
|
|
}
|
|
|
|
|
|
|
|
return poly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CONVERT_TOOL::PolyToLines( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
auto& selection = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, SELECTION_TOOL* sTool )
|
|
|
|
{
|
|
|
|
EditToolSelectionFilter( aCollector,
|
|
|
|
EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS, sTool );
|
|
|
|
|
|
|
|
for( int i = aCollector.GetCount() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* item = aCollector[i];
|
|
|
|
|
|
|
|
switch( item->Type() )
|
|
|
|
{
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_SHAPE_T:
|
|
|
|
case PCB_FP_SHAPE_T:
|
2020-10-04 23:34:59 +00:00
|
|
|
switch( static_cast<PCB_SHAPE*>( item )->GetShape() )
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
|
|
|
case S_POLYGON:
|
|
|
|
break;
|
|
|
|
|
2020-09-08 12:41:22 +00:00
|
|
|
case S_RECT:
|
|
|
|
break;
|
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
default:
|
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:43:05 +00:00
|
|
|
break;
|
|
|
|
|
2018-11-13 13:30:56 +00:00
|
|
|
case PCB_ZONE_AREA_T:
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_FP_ZONE_AREA_T:
|
2018-11-13 13:30:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( selection.Empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto getPolySet =
|
|
|
|
[]( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET set;
|
|
|
|
|
|
|
|
switch( aItem->Type() )
|
|
|
|
{
|
|
|
|
case PCB_ZONE_AREA_T:
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_FP_ZONE_AREA_T:
|
2018-11-13 13:30:56 +00:00
|
|
|
set = *static_cast<ZONE_CONTAINER*>( aItem )->Outline();
|
|
|
|
break;
|
|
|
|
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_SHAPE_T:
|
|
|
|
case PCB_FP_SHAPE_T:
|
2020-09-08 12:41:22 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* graphic = static_cast<PCB_SHAPE*>( aItem );
|
2020-09-08 12:41:22 +00:00
|
|
|
|
|
|
|
if( graphic->GetShape() == S_POLYGON )
|
|
|
|
{
|
|
|
|
set = graphic->GetPolyShape();
|
|
|
|
}
|
|
|
|
else if( graphic->GetShape() == S_RECT )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN outline;
|
|
|
|
VECTOR2I start( graphic->GetStart() );
|
|
|
|
VECTOR2I end( graphic->GetEnd() );
|
|
|
|
|
|
|
|
outline.Append( start );
|
|
|
|
outline.Append( VECTOR2I( end.x, start.y ) );
|
|
|
|
outline.Append( end );
|
|
|
|
outline.Append( VECTOR2I( start.x, end.y ) );
|
|
|
|
outline.SetClosed( true );
|
|
|
|
|
|
|
|
set.AddOutline( outline );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( "Unhandled graphic shape type in PolyToLines - getPolySet" );
|
|
|
|
}
|
2018-11-13 13:30:56 +00:00
|
|
|
break;
|
2020-09-08 12:41:22 +00:00
|
|
|
}
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "Unhandled type in PolyToLines - getPolySet" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto getSegList =
|
|
|
|
[]( SHAPE_POLY_SET& aPoly )
|
|
|
|
{
|
|
|
|
std::vector<SEG> segs;
|
|
|
|
|
|
|
|
// Our input should be valid polys, so OK to assert here
|
|
|
|
wxASSERT( aPoly.VertexCount() >= 2 );
|
|
|
|
|
|
|
|
for( int i = 1; i < aPoly.VertexCount(); i++ )
|
|
|
|
segs.emplace_back( SEG( aPoly.CVertex( i - 1 ), aPoly.CVertex( i ) ) );
|
|
|
|
|
|
|
|
segs.emplace_back( SEG( aPoly.CVertex( aPoly.VertexCount() - 1 ),
|
|
|
|
aPoly.CVertex( 0 ) ) );
|
|
|
|
|
|
|
|
return segs;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOARD_COMMIT commit( m_frame );
|
2020-10-25 12:25:46 +00:00
|
|
|
FOOTPRINT_EDIT_FRAME* fpEditor = dynamic_cast<FOOTPRINT_EDIT_FRAME*>( m_frame );
|
|
|
|
|
|
|
|
MODULE* footprint = nullptr;
|
|
|
|
|
|
|
|
if( fpEditor )
|
|
|
|
footprint = fpEditor->GetBoard()->GetFirstModule();
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
{
|
|
|
|
PCB_LAYER_ID layer = static_cast<BOARD_ITEM*>( item )->GetLayer();
|
|
|
|
SHAPE_POLY_SET polySet = getPolySet( item );
|
|
|
|
std::vector<SEG> segs = getSegList( polySet );
|
|
|
|
|
|
|
|
if( aEvent.IsAction( &PCB_ACTIONS::convertToLines ) )
|
|
|
|
{
|
|
|
|
for( SEG& seg : segs )
|
|
|
|
{
|
2020-10-25 12:25:46 +00:00
|
|
|
if( fpEditor )
|
|
|
|
{
|
|
|
|
FP_SHAPE* graphic = new FP_SHAPE( footprint, S_SEGMENT );
|
|
|
|
|
|
|
|
graphic->SetLayer( layer );
|
|
|
|
graphic->SetStart( wxPoint( seg.A ) );
|
|
|
|
graphic->SetStart0( wxPoint( seg.A ) );
|
|
|
|
graphic->SetEnd( wxPoint( seg.B ) );
|
|
|
|
graphic->SetEnd0( wxPoint( seg.B ) );
|
|
|
|
commit.Add( graphic );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PCB_SHAPE* graphic = new PCB_SHAPE;
|
2018-11-13 13:30:56 +00:00
|
|
|
|
2020-10-25 12:25:46 +00:00
|
|
|
graphic->SetShape( S_SEGMENT );
|
|
|
|
graphic->SetLayer( layer );
|
|
|
|
graphic->SetStart( wxPoint( seg.A ) );
|
|
|
|
graphic->SetEnd( wxPoint( seg.B ) );
|
|
|
|
commit.Add( graphic );
|
|
|
|
}
|
2018-11-13 13:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
BOARD_ITEM_CONTAINER* parent = frame->GetModel();
|
|
|
|
|
|
|
|
if( !IsCopperLayer( layer ) )
|
|
|
|
layer = frame->SelectLayer( F_Cu, LSET::AllNonCuMask() );
|
|
|
|
|
2020-10-25 12:25:46 +00:00
|
|
|
// I am really unsure converting a polygon to "tracks" (i.e. segments on
|
|
|
|
// copper layers) make sense for footprints, but anyway this code exists
|
|
|
|
if( fpEditor )
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
2020-10-25 12:25:46 +00:00
|
|
|
// Creating segments on copper layer
|
|
|
|
for( SEG& seg : segs )
|
|
|
|
{
|
|
|
|
FP_SHAPE* graphic = new FP_SHAPE( footprint, S_SEGMENT );
|
|
|
|
graphic->SetLayer( layer );
|
|
|
|
graphic->SetStart( wxPoint( seg.A ) );
|
|
|
|
graphic->SetStart0( wxPoint( seg.A ) );
|
|
|
|
graphic->SetEnd( wxPoint( seg.B ) );
|
|
|
|
graphic->SetEnd0( wxPoint( seg.B ) );
|
|
|
|
commit.Add( graphic );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Creating tracks
|
|
|
|
for( SEG& seg : segs )
|
|
|
|
{
|
|
|
|
TRACK* track = new TRACK( parent );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
2020-10-25 12:25:46 +00:00
|
|
|
track->SetLayer( layer );
|
|
|
|
track->SetStart( wxPoint( seg.A ) );
|
|
|
|
track->SetEnd( wxPoint( seg.B ) );
|
|
|
|
commit.Add( track );
|
|
|
|
}
|
2018-11-13 13:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Convert polygons to lines" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CONVERT_TOOL::SegmentToArc( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
auto& selection = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, SELECTION_TOOL* sTool )
|
|
|
|
{
|
|
|
|
EditToolSelectionFilter( aCollector,
|
|
|
|
EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS, sTool );
|
|
|
|
|
|
|
|
for( int i = aCollector.GetCount() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* item = aCollector[i];
|
|
|
|
|
2020-10-04 14:19:33 +00:00
|
|
|
if( !( item->Type() == PCB_SHAPE_T ||
|
2020-10-02 22:42:32 +00:00
|
|
|
item->Type() == PCB_TRACE_T ||
|
2020-10-04 14:19:33 +00:00
|
|
|
item->Type() == PCB_FP_SHAPE_T ) )
|
2018-11-13 13:30:56 +00:00
|
|
|
aCollector.Remove( item );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
EDA_ITEM* source = selection.Front();
|
2020-08-29 16:12:40 +00:00
|
|
|
VECTOR2I start, end, mid;
|
|
|
|
|
|
|
|
// Offset the midpoint along the normal a little bit so that it's more obviously an arc
|
|
|
|
const double offsetRatio = 0.1;
|
2018-11-13 13:30:56 +00:00
|
|
|
|
2020-08-29 16:12:40 +00:00
|
|
|
if( OPT<SEG> seg = getStartEndPoints( source ) )
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
2020-08-29 16:12:40 +00:00
|
|
|
start = seg->A;
|
|
|
|
end = seg->B;
|
|
|
|
|
|
|
|
VECTOR2I normal = ( seg->B - seg->A ).Perpendicular().Resize( offsetRatio * seg->Length() );
|
|
|
|
mid = seg->Center() + normal;
|
2018-11-13 13:30:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
BOARD_ITEM_CONTAINER* parent = frame->GetModel();
|
|
|
|
|
|
|
|
BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( source );
|
2020-09-20 01:20:41 +00:00
|
|
|
|
|
|
|
// Don't continue processing if we don't actually have a board item
|
|
|
|
if( !boardItem )
|
|
|
|
return 0;
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
PCB_LAYER_ID layer = boardItem->GetLayer();
|
|
|
|
|
|
|
|
BOARD_COMMIT commit( m_frame );
|
|
|
|
|
2020-10-04 14:19:33 +00:00
|
|
|
if( source->Type() == PCB_SHAPE_T || source->Type() == PCB_FP_SHAPE_T )
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* line = static_cast<PCB_SHAPE*>( source );
|
|
|
|
PCB_SHAPE* arc = new PCB_SHAPE( parent );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
2020-08-29 16:12:40 +00:00
|
|
|
VECTOR2I center = GetArcCenter( start, mid, end );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
arc->SetShape( S_ARC );
|
|
|
|
arc->SetLayer( layer );
|
|
|
|
arc->SetWidth( line->GetWidth() );
|
|
|
|
|
2020-08-29 16:12:40 +00:00
|
|
|
arc->SetCenter( wxPoint( center ) );
|
|
|
|
arc->SetArcStart( wxPoint( start ) );
|
|
|
|
arc->SetAngle( GetArcAngle( start, mid, end ) );
|
|
|
|
|
|
|
|
arc->SetArcEnd( wxPoint( end ) );
|
2018-11-13 13:30:56 +00:00
|
|
|
commit.Add( arc );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxASSERT( source->Type() == PCB_TRACE_T );
|
|
|
|
TRACK* line = static_cast<TRACK*>( source );
|
|
|
|
ARC* arc = new ARC( parent );
|
|
|
|
|
|
|
|
arc->SetLayer( layer );
|
|
|
|
arc->SetWidth( line->GetWidth() );
|
2020-08-29 16:12:40 +00:00
|
|
|
arc->SetStart( wxPoint( start ) );
|
|
|
|
arc->SetMid( wxPoint( mid ) );
|
|
|
|
arc->SetEnd( wxPoint( end ) );
|
2018-11-13 13:30:56 +00:00
|
|
|
|
|
|
|
commit.Add( arc );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Create arc from line segment" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OPT<SEG> CONVERT_TOOL::getStartEndPoints( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
switch( aItem->Type() )
|
|
|
|
{
|
2020-10-04 14:19:33 +00:00
|
|
|
case PCB_SHAPE_T:
|
|
|
|
case PCB_FP_SHAPE_T:
|
2018-11-13 13:30:56 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* line = static_cast<PCB_SHAPE*>( aItem );
|
2018-11-13 13:30:56 +00:00
|
|
|
return boost::make_optional<SEG>( { VECTOR2I( line->GetStart() ),
|
|
|
|
VECTOR2I( line->GetEnd() ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
case PCB_TRACE_T:
|
|
|
|
{
|
|
|
|
TRACK* line = static_cast<TRACK*>( aItem );
|
|
|
|
return boost::make_optional<SEG>( { VECTOR2I( line->GetStart() ),
|
|
|
|
VECTOR2I( line->GetEnd() ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
case PCB_ARC_T:
|
|
|
|
{
|
|
|
|
ARC* arc = static_cast<ARC*>( aItem );
|
|
|
|
return boost::make_optional<SEG>( { VECTOR2I( arc->GetStart() ),
|
|
|
|
VECTOR2I( arc->GetEnd() ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULLOPT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CONVERT_TOOL::setTransitions()
|
|
|
|
{
|
|
|
|
Go( &CONVERT_TOOL::LinesToPoly, PCB_ACTIONS::convertToPoly.MakeEvent() );
|
|
|
|
Go( &CONVERT_TOOL::LinesToPoly, PCB_ACTIONS::convertToZone.MakeEvent() );
|
|
|
|
Go( &CONVERT_TOOL::LinesToPoly, PCB_ACTIONS::convertToKeepout.MakeEvent() );
|
|
|
|
Go( &CONVERT_TOOL::PolyToLines, PCB_ACTIONS::convertToLines.MakeEvent() );
|
|
|
|
Go( &CONVERT_TOOL::PolyToLines, PCB_ACTIONS::convertToTracks.MakeEvent() );
|
|
|
|
Go( &CONVERT_TOOL::SegmentToArc, PCB_ACTIONS::convertToArc.MakeEvent() );
|
|
|
|
}
|