Files
kicad/pcbnew/dimension.cpp
T

464 lines
13 KiB
C++
Raw Normal View History

/**
* @file dimension.cpp
* @brief Dialog and code for editing a dimension object.
*/
/*
* 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) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2016 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 <fctsys.h>
#include <confirm.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <wxPcbStruct.h>
#include <drawtxt.h>
#include <dialog_helpers.h>
#include <macros.h>
2012-04-13 14:51:24 -04:00
#include <base_units.h>
#include <board_commit.h>
#include <class_board.h>
#include <class_pcb_text.h>
#include <class_dimension.h>
#include <pcbnew.h>
#include <dialog_dimension_editor_base.h>
#include <class_pcb_layer_box_selector.h>
2011-09-23 09:57:12 -04:00
/* Local functions */
static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
const wxPoint& aPosition, bool aErase );
static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
const wxPoint& aPosition, bool aErase );
static void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC );
/* Local variables : */
static int status_dimension; /* Used in dimension creation:
2010-08-23 13:30:51 -04:00
* = 0 : initial value: no dimension in progress
* = 1 : First point created
* = 2 : Second point created, the text must be placed */
/*
* A dimension has this shape:
* It has 2 reference points, and a text
* | |
* | dist |
* |<---------->|
* | |
2008-03-04 04:22:27 +00:00
*
2007-08-20 01:20:48 +00:00
*/
/*********************************/
/* class DIALOG_DIMENSION_EDITOR */
/*********************************/
class DIALOG_DIMENSION_EDITOR : public DIALOG_DIMENSION_EDITOR_BASE
{
private:
PCB_EDIT_FRAME* m_parent;
wxDC* m_DC;
DIMENSION* m_currentDimension;
public:
2007-08-20 01:20:48 +00:00
// Constructor and destructor
DIALOG_DIMENSION_EDITOR( PCB_EDIT_FRAME* aParent, DIMENSION* aDimension, wxDC* aDC );
~DIALOG_DIMENSION_EDITOR()
2007-08-20 01:20:48 +00:00
{
}
private:
virtual void OnOKClick( wxCommandEvent& event ) override;
};
DIALOG_DIMENSION_EDITOR::DIALOG_DIMENSION_EDITOR( PCB_EDIT_FRAME* aParent,
DIMENSION* aDimension, wxDC* aDC ) :
DIALOG_DIMENSION_EDITOR_BASE( aParent )
{
SetFocus();
2007-08-20 01:20:48 +00:00
m_parent = aParent;
m_DC = aDC;
2007-08-20 01:20:48 +00:00
m_currentDimension = aDimension;
2007-08-20 01:20:48 +00:00
2013-03-13 14:53:58 -04:00
if( aDimension->Text().IsMirrored() )
m_rbMirror->SetSelection( 1 );
else
m_rbMirror->SetSelection( 0 );
2013-03-13 14:53:58 -04:00
m_Name->SetValue( aDimension->Text().GetText() );
// Enter size value in dialog
2013-03-13 14:53:58 -04:00
PutValueInLocalUnits( *m_TxtSizeXCtrl, aDimension->Text().GetSize().x );
AddUnitSymbol( *m_staticTextSizeX );
2013-03-13 14:53:58 -04:00
PutValueInLocalUnits( *m_TxtSizeYCtrl, aDimension->Text().GetSize().y );
AddUnitSymbol( *m_staticTextSizeY );
// Enter lines thickness value in dialog
2013-03-13 14:53:58 -04:00
PutValueInLocalUnits( *m_TxtWidthCtrl, aDimension->GetWidth() );
AddUnitSymbol( *m_staticTextWidth );
// Enter position value in dialog
2013-03-18 15:36:07 -04:00
PutValueInLocalUnits( *m_textCtrlPosX, aDimension->Text().GetTextPosition().x );
AddUnitSymbol( *m_staticTextPosX );
2013-03-18 15:36:07 -04:00
PutValueInLocalUnits( *m_textCtrlPosY, aDimension->Text().GetTextPosition().y );
AddUnitSymbol( *m_staticTextPosY );
2008-03-04 04:22:27 +00:00
// Configure the layers list selector
if( !m_parent->GetBoard()->IsLayerEnabled( aDimension->GetLayer() ) )
// Should not happens, because one cannot select a board item on a
// not activated layer, but ...
m_SelLayerBox->ShowNonActivatedLayers( true );
m_SelLayerBox->SetLayersHotkeys( false );
2014-06-24 11:17:18 -05:00
m_SelLayerBox->SetLayerSet( LSET::AllCuMask().set( Edge_Cuts ) );
m_SelLayerBox->SetBoardFrame( m_parent );
m_SelLayerBox->Resync();
if( m_SelLayerBox->SetLayerSelection( aDimension->GetLayer() ) < 0 )
{
wxMessageBox( _( "This item has an illegal layer id.\n"
"Now, forced on the drawings layer. Please, fix it" ) );
2014-06-24 11:17:18 -05:00
m_SelLayerBox->SetLayerSelection( Dwgs_User );
}
2007-08-20 01:20:48 +00:00
m_sdbSizerBtsOK->SetDefault();
FixOSXCancelButtonIssue();
2007-08-20 01:20:48 +00:00
// Now all widgets have the size fixed, call FinishDialogSettings
FinishDialogSettings();
}
void DIALOG_DIMENSION_EDITOR::OnOKClick( wxCommandEvent& event )
{
BOARD_COMMIT commit( m_parent );
LAYER_ID newlayer = ToLAYER_ID( m_SelLayerBox->GetLayerSelection() );
if( !m_parent->GetBoard()->IsLayerEnabled( newlayer ) )
{
wxMessageBox( _( "The layer currently selected is not enabled for this board\n"
"You cannot use it" ) );
return;
}
#ifndef USE_WX_OVERLAY
if( m_DC ) // Delete old text.
2007-08-20 01:20:48 +00:00
{
m_currentDimension->Draw( m_parent->GetCanvas(), m_DC, GR_XOR );
2007-08-20 01:20:48 +00:00
}
2014-06-24 11:17:18 -05:00
#endif
2007-08-20 01:20:48 +00:00
commit.Modify( m_currentDimension );
2007-08-20 01:20:48 +00:00
if( m_Name->GetValue() != wxEmptyString )
{
m_currentDimension->SetText( m_Name->GetValue() );
2007-08-20 01:20:48 +00:00
}
wxString msg;
// Get new size value:
msg = m_TxtSizeXCtrl->GetValue();
m_currentDimension->Text().SetWidth( ValueFromString( g_UserUnit, msg ) );
msg = m_TxtSizeYCtrl->GetValue();
m_currentDimension->Text().SetHeight( ValueFromString( g_UserUnit, msg ) );
// Get new position value:
// It will be copied later in dimension, because
msg = m_textCtrlPosX->GetValue();
2013-03-13 14:53:58 -04:00
wxPoint pos;
pos.x = ValueFromString( g_UserUnit, msg );
msg = m_textCtrlPosY->GetValue();
pos.y = ValueFromString( g_UserUnit, msg );
m_currentDimension->Text().SetTextPosition( pos );
// Get new line thickness value:
msg = m_TxtWidthCtrl->GetValue();
int width = ValueFromString( g_UserUnit, msg );
int maxthickness = Clamp_Text_PenSize( width, m_currentDimension->Text().GetSize() );
if( width > maxthickness )
{
2010-08-23 13:30:51 -04:00
DisplayError( NULL,
_( "The text thickness is too large for the text size. "
"It will be clamped" ) );
width = maxthickness;
}
m_currentDimension->SetWidth( width );
m_currentDimension->Text().SetThickness( width );
m_currentDimension->Text().SetMirrored( ( m_rbMirror->GetSelection() == 1 ) ? true : false );
m_currentDimension->SetLayer( newlayer );
#ifndef USE_WX_OVERLAY
if( m_DC ) // Display new text
2007-08-20 01:20:48 +00:00
{
m_currentDimension->Draw( m_parent->GetCanvas(), m_DC, GR_OR );
2007-08-20 01:20:48 +00:00
}
#else
m_parent->Refresh();
#endif
commit.Push( _( "Modifed dimensions properties" ) );
event.Skip(); // ends returning wxID_OK (default behavior)
}
static void AbortBuildDimension( EDA_DRAW_PANEL* Panel, wxDC* aDC )
{
DIMENSION* dimension = (DIMENSION*) Panel->GetScreen()->GetCurItem();
2007-08-20 01:20:48 +00:00
if( dimension )
2007-08-20 01:20:48 +00:00
{
if( dimension->IsNew() )
2007-08-20 01:20:48 +00:00
{
dimension->Draw( Panel, aDC, GR_XOR );
dimension->DeleteStructure();
2007-08-20 01:20:48 +00:00
}
else
{
dimension->Draw( Panel, aDC, GR_OR );
2007-08-20 01:20:48 +00:00
}
}
status_dimension = 0;
((PCB_EDIT_FRAME*)Panel->GetParent())->SetCurItem( NULL );
}
2007-08-20 01:20:48 +00:00
DIMENSION* PCB_EDIT_FRAME::EditDimension( DIMENSION* aDimension, wxDC* aDC )
{
2007-08-20 01:20:48 +00:00
wxPoint pos;
if( aDimension == NULL )
2007-08-20 01:20:48 +00:00
{
status_dimension = 1;
pos = GetCrossHairPosition();
aDimension = new DIMENSION( GetBoard() );
aDimension->SetFlags( IS_NEW );
aDimension->SetLayer( GetActiveLayer() );
aDimension->SetOrigin( pos );
aDimension->SetEnd( pos );
2013-03-13 14:53:58 -04:00
aDimension->Text().SetSize( GetBoard()->GetDesignSettings().m_PcbTextSize );
2011-12-05 00:15:33 -06:00
int width = GetBoard()->GetDesignSettings().m_PcbTextWidth;
2013-03-13 14:53:58 -04:00
int maxthickness = Clamp_Text_PenSize(width, aDimension->Text().GetSize() );
if( width > maxthickness )
{
width = maxthickness;
}
2013-03-13 14:53:58 -04:00
aDimension->Text().SetThickness( width );
aDimension->SetWidth( width );
aDimension->AdjustDimensionDetails();
2011-12-22 08:28:11 -05:00
aDimension->Draw( m_canvas, aDC, GR_XOR );
2011-12-22 08:28:11 -05:00
m_canvas->SetMouseCapture( BuildDimension, AbortBuildDimension );
return aDimension;
2007-08-20 01:20:48 +00:00
}
// Dimension != NULL
if( status_dimension == 1 )
2007-08-20 01:20:48 +00:00
{
status_dimension = 2;
return aDimension;
2007-08-20 01:20:48 +00:00
}
2011-12-22 08:28:11 -05:00
aDimension->Draw( m_canvas, aDC, GR_OR );
aDimension->ClearFlags();
/* ADD this new item in list */
GetBoard()->Add( aDimension );
// Add store it in undo/redo list
SaveCopyInUndoList( aDimension, UR_NEW );
OnModify();
2011-12-22 08:28:11 -05:00
m_canvas->SetMouseCapture( NULL, NULL );
2007-08-20 01:20:48 +00:00
return NULL;
}
static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
const wxPoint& aPosition, bool aErase )
{
PCB_SCREEN* screen = (PCB_SCREEN*) aPanel->GetScreen();
DIMENSION* Dimension = (DIMENSION*) screen->GetCurItem();
wxPoint pos = aPanel->GetParent()->GetCrossHairPosition();
2007-08-20 01:20:48 +00:00
if( Dimension == NULL )
2007-08-20 01:20:48 +00:00
return;
// Erase previous dimension.
if( aErase )
2007-08-20 01:20:48 +00:00
{
Dimension->Draw( aPanel, aDC, GR_XOR );
2007-08-20 01:20:48 +00:00
}
Dimension->SetLayer( screen->m_Active_Layer );
if( status_dimension == 1 )
2007-08-20 01:20:48 +00:00
{
2012-12-19 20:31:36 +01:00
Dimension->m_featureLineDO = pos;
Dimension->m_crossBarF = Dimension->m_featureLineDO;
Dimension->AdjustDimensionDetails( );
2007-08-20 01:20:48 +00:00
}
else
{
/* Calculating the direction of travel perpendicular to the selected axis. */
double angle = Dimension->GetAngle() + (M_PI / 2);
2007-08-20 01:20:48 +00:00
wxPoint delta = pos - Dimension->m_featureLineDO;
double depl = ( delta.x * cos( angle ) ) + ( delta.y * sin( angle ) );
Dimension->SetHeight( depl );
2007-08-20 01:20:48 +00:00
}
Dimension->Draw( aPanel, aDC, GR_XOR );
}
void PCB_EDIT_FRAME::ShowDimensionPropertyDialog( DIMENSION* aDimension, wxDC* aDC )
{
if( aDimension == NULL )
2007-08-20 01:20:48 +00:00
return;
DIALOG_DIMENSION_EDITOR dlg( this, aDimension, aDC );
dlg.ShowModal();
}
2007-08-20 01:20:48 +00:00
void PCB_EDIT_FRAME::DeleteDimension( DIMENSION* aDimension, wxDC* aDC )
{
if( aDimension == NULL )
2007-08-20 01:20:48 +00:00
return;
if( aDC )
2011-12-22 08:28:11 -05:00
aDimension->Draw( m_canvas, aDC, GR_XOR );
SaveCopyInUndoList( aDimension, UR_DELETED );
aDimension->UnLink();
OnModify();
}
/* Initialize parameters to move a pcb text
*/
static wxPoint initialTextPosition;
void PCB_EDIT_FRAME::BeginMoveDimensionText( DIMENSION* aItem, wxDC* DC )
{
if( aItem == NULL )
return;
// Store the initial position for undo/abort command
2013-03-18 15:36:07 -04:00
initialTextPosition = aItem->Text().GetTextPosition();
2011-12-22 08:28:11 -05:00
aItem->Draw( m_canvas, DC, GR_XOR );
aItem->SetFlags( IS_MOVED );
2013-01-12 12:32:24 -05:00
SetMsgPanel( aItem );
SetCrossHairPosition( aItem->Text().GetTextPosition() );
2011-12-22 08:28:11 -05:00
m_canvas->MoveCursorToCrossHair();
2011-12-22 08:28:11 -05:00
m_canvas->SetMouseCapture( MoveDimensionText, AbortMoveDimensionText );
SetCurItem( aItem );
2011-12-29 15:11:42 -05:00
m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
}
/* Move dimension text following the cursor. */
static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
bool aErase )
{
DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
if( dimension == NULL )
return;
if( aErase )
dimension->Draw( aPanel, aDC, GR_XOR );
dimension->Text().SetTextPosition( aPanel->GetParent()->GetCrossHairPosition() );
dimension->Draw( aPanel, aDC, GR_XOR );
}
/*
* Abort current text edit progress.
*
* If a text is selected, its initial coord are regenerated
*/
void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
{
DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
( (PCB_EDIT_FRAME*) aPanel->GetParent() )->SetCurItem( NULL );
aPanel->SetMouseCapture( NULL, NULL );
if( dimension == NULL ) // Should not occur
return;
dimension->Draw( aPanel, aDC, GR_XOR );
2013-03-18 15:36:07 -04:00
dimension->Text().SetTextPosition( initialTextPosition );
dimension->ClearFlags();
dimension->Draw( aPanel, aDC, GR_OR );
}
/*
* Place the current dimension text being moving
*/
void PCB_EDIT_FRAME::PlaceDimensionText( DIMENSION* aItem, wxDC* DC )
{
2011-12-22 08:28:11 -05:00
m_canvas->SetMouseCapture( NULL, NULL );
SetCurItem( NULL );
if( aItem == NULL )
return;
2011-12-22 08:28:11 -05:00
aItem->Draw( m_canvas, DC, GR_OR );
OnModify();
2013-03-18 15:36:07 -04:00
wxPoint tmp = aItem->Text().GetTextPosition();
aItem->Text().SetTextPosition( initialTextPosition );
SaveCopyInUndoList( aItem, UR_CHANGED );
2013-03-18 15:36:07 -04:00
aItem->Text().SetTextPosition( tmp );
aItem->ClearFlags();
}