kicad/pcbnew/dialogs/dialog_orient_footprints.cpp

173 lines
4.7 KiB
C++
Raw Normal View History

2010-11-26 17:47:35 +00:00
/**
* @file DIALOG_ORIENT_FOOTPRINTS.cpp
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
2010-11-26 17:47:35 +00:00
*
* Copyright (C) 1992-2010 Jean_Pierre Charras <jp.charras@ujf-grenoble.fr>
* Copyright (C) 1992-2010 KiCad Developers, see change_log.txt for contributors.
2010-11-26 17:47:35 +00:00
*
* 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 <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <pcbnew.h>
#include <wxPcbStruct.h>
#include <macros.h>
#include <class_board.h>
#include <class_module.h>
#include <dialog_orient_footprints_base.h>
2010-11-26 17:47:35 +00:00
/* DIALOG_ORIENT_FOOTPRINTS class declaration
*/
class DIALOG_ORIENT_FOOTPRINTS: public DIALOG_ORIENT_FOOTPRINTS_BASE
{
private:
PCB_EDIT_FRAME * m_Parent;
2010-11-26 17:47:35 +00:00
static int newOrientation;
public:
DIALOG_ORIENT_FOOTPRINTS( PCB_EDIT_FRAME* parent );
2010-11-26 17:47:35 +00:00
~DIALOG_ORIENT_FOOTPRINTS() {}
2010-11-26 17:47:35 +00:00
bool ApplyToLockedModules()
{
return m_ApplyToLocked->IsChecked();
}
int GetOrientation()
{
return newOrientation;
}
wxString GetFilter()
{
return m_FilterPattern->GetValue();
}
private:
void init();
void OnOkClick( wxCommandEvent& event );
void OnCancelClick( wxCommandEvent& event );
};
int DIALOG_ORIENT_FOOTPRINTS::newOrientation = 0;
DIALOG_ORIENT_FOOTPRINTS::DIALOG_ORIENT_FOOTPRINTS( PCB_EDIT_FRAME* parent )
2010-11-26 17:47:35 +00:00
: DIALOG_ORIENT_FOOTPRINTS_BASE( parent )
{
m_Parent = parent;
wxString txt;
txt.Printf(wxT("%g"), (double) newOrientation/10);
m_OrientationCtrl->SetValue(txt);
SetFocus( );
GetSizer()->SetSizeHints(this);
Centre();
}
/****************************************************************/
void PCB_EDIT_FRAME::OnOrientFootprints( wxCommandEvent& event )
2010-11-26 17:47:35 +00:00
/****************************************************************/
/**
* Function OnOrientFootprints
* install the dialog box for the comman Orient Footprints
*/
{
DIALOG_ORIENT_FOOTPRINTS dlg(this);
if( dlg.ShowModal() != wxID_OK )
return;
wxString text = dlg.GetFilter();
2010-11-26 17:47:35 +00:00
if( ReOrientModules( text, dlg.GetOrientation(), dlg.ApplyToLockedModules() ) )
{
m_canvas->Refresh();
2010-11-26 17:47:35 +00:00
Compile_Ratsnest( NULL, true );
}
}
/*******************************************************************/
bool PCB_EDIT_FRAME::ReOrientModules( const wxString& ModuleMask,
int Orient, bool include_fixe )
2010-11-26 17:47:35 +00:00
/*******************************************************************/
/**
* Function ReOrientModules
* Set the orientation of footprints
* @param ModuleMask = mask (wildcard allowed) selection
* @param Orient = new orientation
* @param include_fixe = true to orient locked footprints
* @return true if some footprints modified, false if no change
*/
{
wxString line;
bool modified = false;
line.Printf( _( "Ok to set footprints orientation to %.1f degrees ?" ), (double)Orient / 10 );
if( !IsOK( this, line ) )
return false;
for( MODULE* module = GetBoard()->m_Modules; module; module = module->Next() )
{
if( module->IsLocked() && !include_fixe )
continue;
if( WildCompareString( ModuleMask, module->m_Reference->m_Text, false ) )
2010-11-26 17:47:35 +00:00
{
modified = true;
Rotate_Module( NULL, module, Orient, false );
2010-11-26 17:47:35 +00:00
}
}
if ( modified )
OnModify();
2010-11-26 17:47:35 +00:00
return modified;
}
void DIALOG_ORIENT_FOOTPRINTS::OnOkClick( wxCommandEvent& event )
{
double d_orient;
wxString text = m_OrientationCtrl->GetValue();
if ( ! text.ToDouble(&d_orient) )
{
DisplayError(this, _("Bad value for footprints orientation"));
return;
}
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
2012-04-19 06:55:45 +00:00
newOrientation = KiROUND(d_orient * 10);
2010-11-26 17:47:35 +00:00
NORMALIZE_ANGLE_180( newOrientation );
EndModal( wxID_OK );
}
void DIALOG_ORIENT_FOOTPRINTS::OnCancelClick( wxCommandEvent& event )
{
EndModal( wxID_CANCEL );
}