kicad/pcbnew/microwave.cpp

535 lines
15 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2015-2016 Wayne Stambaugh <stambaughw@verizon.net>
2019-07-01 23:48:05 +00:00
* Copyright (C) 1992-2019 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 <trigo.h>
#include <kicad_string.h>
#include <gestfich.h>
2018-01-29 20:58:58 +00:00
#include <pcb_edit_frame.h>
#include <dialog_helpers.h>
#include <richio.h>
#include <filter_reader.h>
#include <base_units.h>
#include <validators.h>
#include <dialog_text_entry.h>
#include <class_board.h>
#include <class_module.h>
#include <class_edge_mod.h>
#include <microwave/microwave_inductor.h>
#include <pcbnew.h>
static std::vector< wxRealPoint > PolyEdges;
2008-12-06 12:49:33 +00:00
static double ShapeScaleX, ShapeScaleY;
static wxSize ShapeSize;
static int PolyShapeType;
2007-05-06 16:03:28 +00:00
2009-02-26 00:37:04 +00:00
MODULE* PCB_EDIT_FRAME::CreateMuWaveBaseFootprint( const wxString& aValue,
int aTextSize, int aPadCount )
2007-05-06 16:03:28 +00:00
{
MODULE* module = CreateNewModule( aValue );
2011-12-12 08:37:05 +00:00
if( aTextSize > 0 )
{
module->Reference().SetTextSize( wxSize( aTextSize, aTextSize ) );
module->Reference().SetThickness( aTextSize/5 );
module->Value().SetTextSize( wxSize( aTextSize, aTextSize ) );
module->Value().SetThickness( aTextSize/5 );
}
2007-08-08 03:50:44 +00:00
2012-02-19 04:02:19 +00:00
// Create 2 pads used in gaps and stubs. The gap is between these 2 pads
// the stub is the pad 2
wxString Line;
int pad_num = 1;
while( aPadCount-- )
2007-08-08 03:50:44 +00:00
{
2011-12-14 04:29:25 +00:00
D_PAD* pad = new D_PAD( module );
2019-06-01 23:23:36 +00:00
module->Add( pad, ADD_INSERT );
int tw = GetDesignSettings().GetCurrentTrackWidth();
2012-02-19 04:02:19 +00:00
pad->SetSize( wxSize( tw, tw ) );
pad->SetPosition( module->GetPosition() );
pad->SetShape( PAD_SHAPE_RECT );
pad->SetAttribute( PAD_ATTRIB_SMD );
pad->SetLayerSet( F_Cu );
2012-02-19 04:02:19 +00:00
2007-08-08 03:50:44 +00:00
Line.Printf( wxT( "%d" ), pad_num );
pad->SetName( Line );
2007-08-08 03:50:44 +00:00
pad_num++;
}
2011-12-14 04:29:25 +00:00
return module;
2007-05-06 16:03:28 +00:00
}
MODULE* PCB_EDIT_FRAME::Create_MuWaveComponent( int shape_type )
2007-05-06 16:03:28 +00:00
{
int oX;
2008-12-06 12:49:33 +00:00
D_PAD* pad;
2011-12-14 04:29:25 +00:00
MODULE* module;
2007-08-08 03:50:44 +00:00
wxString msg, cmp_name;
int pad_count = 2;
int angle = 0;
// Ref and value text size (O = use board default value.
// will be set to a value depending on the footprint size, if possible
int text_size = 0;
2007-08-08 03:50:44 +00:00
2012-02-19 04:02:19 +00:00
// Enter the size of the gap or stub
int gap_size = GetDesignSettings().GetCurrentTrackWidth();
2007-08-08 03:50:44 +00:00
switch( shape_type )
{
case 0:
msg = _( "Gap Size:" );
cmp_name = "muwave_gap";
text_size = gap_size;
2007-08-08 03:50:44 +00:00
break;
case 1:
msg = _( "Stub Size:" );
cmp_name = "muwave_stub";
text_size = gap_size;
2007-08-08 03:50:44 +00:00
pad_count = 2;
break;
case 2:
msg = _( "Arc Stub Radius Value:" );
cmp_name = "muwave_arcstub";
2007-08-08 03:50:44 +00:00
pad_count = 1;
break;
default:
msg = wxT( "???" );
break;
}
wxString value = StringFromValue( GetUserUnits(), gap_size );
WX_TEXT_ENTRY_DIALOG dlg( this, msg, _( "Create microwave module" ), value );
2010-07-20 18:11:34 +00:00
if( dlg.ShowModal() != wxID_OK )
return NULL; // cancelled by user
2007-08-08 03:50:44 +00:00
value = dlg.GetValue();
gap_size = ValueFromString( GetUserUnits(), value );
2007-08-08 03:50:44 +00:00
2010-07-20 18:11:34 +00:00
bool abort = false;
2010-07-20 18:11:34 +00:00
if( shape_type == 2 )
2007-08-08 03:50:44 +00:00
{
double fcoeff = 10.0, fval;
2010-07-20 18:11:34 +00:00
msg.Printf( wxT( "%3.1f" ), angle / fcoeff );
WX_TEXT_ENTRY_DIALOG angledlg( this, _( "Angle in degrees:" ),
_( "Create microwave module" ), msg );
if( angledlg.ShowModal() != wxID_OK )
return NULL; // cancelled by user
msg = angledlg.GetValue();
2010-07-20 18:11:34 +00:00
if( !msg.ToDouble( &fval ) )
2007-08-08 03:50:44 +00:00
{
DisplayError( this, _( "Incorrect number, abort" ) );
abort = true;
2007-08-08 03:50:44 +00:00
}
angle = std::abs( KiROUND( fval * fcoeff ) );
2007-08-08 03:50:44 +00:00
if( angle > 1800 )
angle = 1800;
}
if( abort )
return NULL;
module = CreateMuWaveBaseFootprint( cmp_name, text_size, pad_count );
2019-06-01 23:23:36 +00:00
auto it = module->Pads().begin();
pad = *it;
2007-08-08 03:50:44 +00:00
switch( shape_type )
{
case 0: //Gap :
2012-02-19 04:02:19 +00:00
oX = -( gap_size + pad->GetSize().x ) / 2;
pad->SetX0( oX );
pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
2019-06-01 23:23:36 +00:00
pad = *( it + 1 );
2012-02-19 04:02:19 +00:00
pad->SetX0( oX + gap_size + pad->GetSize().x );
pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
2007-08-08 03:50:44 +00:00
break;
case 1: //Stub :
pad->SetName( wxT( "1" ) );
2019-06-01 23:23:36 +00:00
pad = *( it + 1 );
2012-02-19 04:02:19 +00:00
pad->SetY0( -( gap_size + pad->GetSize().y ) / 2 );
pad->SetSize( wxSize( pad->GetSize().x, gap_size ) );
pad->SetY( pad->GetPos0().y + pad->GetPosition().y );
2007-08-08 03:50:44 +00:00
break;
case 2: // Arc Stub created by a polygonal approach:
{
pad->SetShape( PAD_SHAPE_CUSTOM );
pad->SetAnchorPadShape( PAD_SHAPE_RECT );
int numPoints = (angle / 50) + 3; // Note: angles are in 0.1 degrees
std::vector<wxPoint> polyPoints;
polyPoints.reserve( numPoints );
2019-07-01 23:48:05 +00:00
polyPoints.emplace_back( wxPoint( 0, 0 ) );
int theta = -angle / 2;
for( int ii = 1; ii<numPoints - 1; ii++ )
{
wxPoint pt( 0, -gap_size );
RotatePoint( &pt.x, &pt.y, theta );
polyPoints.push_back( pt );
theta += 50;
if( theta > angle / 2 )
theta = angle / 2;
2012-02-19 04:02:19 +00:00
}
// Close the polygon:
polyPoints.push_back( polyPoints[0] );
pad->AddPrimitive( polyPoints, 0 ); // add a polygonal basic shape
}
2012-02-19 04:02:19 +00:00
break;
2007-08-08 03:50:44 +00:00
default:
break;
}
2011-12-14 04:29:25 +00:00
module->CalculateBoundingBox();
OnModify();
2011-12-14 04:29:25 +00:00
return module;
2007-05-06 16:03:28 +00:00
}
/**************** Polygon Shapes ***********************/
2007-08-08 03:50:44 +00:00
enum id_mw_cmd {
2007-10-07 03:08:24 +00:00
ID_READ_SHAPE_FILE = 1000
2007-05-06 16:03:28 +00:00
};
2007-08-08 03:50:44 +00:00
/* Setting polynomial form parameters
2007-08-08 03:50:44 +00:00
*/
class MWAVE_POLYGONAL_SHAPE_DLG : public wxDialog
2007-05-06 16:03:28 +00:00
{
private:
PCB_EDIT_FRAME* m_Parent;
2007-08-08 03:50:44 +00:00
wxRadioBox* m_ShapeOptionCtrl;
EDA_SIZE_CTRL* m_SizeCtrl;
2007-05-06 16:03:28 +00:00
public:
MWAVE_POLYGONAL_SHAPE_DLG( PCB_EDIT_FRAME* parent, const wxPoint& pos );
~MWAVE_POLYGONAL_SHAPE_DLG() { };
2007-05-06 16:03:28 +00:00
2007-08-08 03:50:44 +00:00
private:
void OnOkClick( wxCommandEvent& event );
void OnCancelClick( wxCommandEvent& event );
/**
* Function ReadDataShapeDescr
* read a description shape file
* File format is
* Unit=MM
* XScale=271.501
* YScale=1.00133
*
* $COORD
* 0 0.6112600148417837
* 0.001851851851851852 0.6104800531118608
* ....
* $ENDCOORD
*
* Each line is the X Y coord (normalized units from 0 to 1)
*/
void ReadDataShapeDescr( wxCommandEvent& event );
2007-05-06 16:03:28 +00:00
2007-08-08 03:50:44 +00:00
DECLARE_EVENT_TABLE()
2007-05-06 16:03:28 +00:00
};
BEGIN_EVENT_TABLE( MWAVE_POLYGONAL_SHAPE_DLG, wxDialog )
EVT_BUTTON( wxID_OK, MWAVE_POLYGONAL_SHAPE_DLG::OnOkClick )
EVT_BUTTON( wxID_CANCEL, MWAVE_POLYGONAL_SHAPE_DLG::OnCancelClick )
EVT_BUTTON( ID_READ_SHAPE_FILE, MWAVE_POLYGONAL_SHAPE_DLG::ReadDataShapeDescr )
END_EVENT_TABLE()
2007-05-06 16:03:28 +00:00
MWAVE_POLYGONAL_SHAPE_DLG::MWAVE_POLYGONAL_SHAPE_DLG( PCB_EDIT_FRAME* parent,
const wxPoint& framepos ) :
wxDialog( parent, -1, _( "Complex shape" ), framepos, wxSize( 350, 280 ),
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
2007-05-06 16:03:28 +00:00
{
2007-08-08 03:50:44 +00:00
m_Parent = parent;
PolyEdges.clear();
2007-08-08 03:50:44 +00:00
wxBoxSizer* MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
SetSizer( MainBoxSizer );
wxBoxSizer* LeftBoxSizer = new wxBoxSizer( wxVERTICAL );
wxBoxSizer* RightBoxSizer = new wxBoxSizer( wxVERTICAL );
MainBoxSizer->Add( LeftBoxSizer, 0, wxGROW | wxALL, 5 );
MainBoxSizer->Add( RightBoxSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
2007-10-07 03:08:24 +00:00
wxButton* Button = new wxButton( this, wxID_OK, _( "OK" ) );
2007-08-08 03:50:44 +00:00
RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
2007-10-07 03:08:24 +00:00
Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
2007-08-08 03:50:44 +00:00
RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
Button = new wxButton( this, ID_READ_SHAPE_FILE,
_( "Read Shape Description File..." ) );
2007-08-08 03:50:44 +00:00
RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
2019-07-01 23:48:05 +00:00
wxString shapelist[] = { _( "Normal" ), _( "Symmetrical" ), _( "Mirrored" ) };
m_ShapeOptionCtrl = new wxRadioBox( this, -1, _( "Shape Option" ),
wxDefaultPosition, wxDefaultSize, 3,
shapelist, 1,
2007-08-08 03:50:44 +00:00
wxRA_SPECIFY_COLS );
LeftBoxSizer->Add( m_ShapeOptionCtrl, 0, wxGROW | wxALL, 5 );
m_SizeCtrl = new EDA_SIZE_CTRL( this, _( "Size" ), ShapeSize, parent->GetUserUnits(),
LeftBoxSizer );
2007-08-08 03:50:44 +00:00
GetSizer()->SetSizeHints( this );
2007-05-06 16:03:28 +00:00
}
2007-08-08 03:50:44 +00:00
void MWAVE_POLYGONAL_SHAPE_DLG::OnCancelClick( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
{
PolyEdges.clear();
EndModal( wxID_CANCEL );
2007-05-06 16:03:28 +00:00
}
void MWAVE_POLYGONAL_SHAPE_DLG::OnOkClick( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
{
2007-08-08 03:50:44 +00:00
ShapeSize = m_SizeCtrl->GetValue();
PolyShapeType = m_ShapeOptionCtrl->GetSelection();
EndModal( wxID_OK );
2007-05-06 16:03:28 +00:00
}
2007-08-08 03:50:44 +00:00
void MWAVE_POLYGONAL_SHAPE_DLG::ReadDataShapeDescr( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
{
static wxString lastpath; // To remember the last open path during a session
wxString mask = wxFileSelectorDefaultWildcardStr;
2019-07-01 23:48:05 +00:00
wxString FullFileName = EDA_FILE_SELECTOR( _( "Read descr shape file" ), lastpath,
FullFileName, wxEmptyString, mask, this,
wxFD_OPEN, true );
2007-08-08 03:50:44 +00:00
if( FullFileName.IsEmpty() )
return;
wxFileName fn( FullFileName );
lastpath = fn.GetPath();
PolyEdges.clear();
FILE* File = wxFopen( FullFileName, wxT( "rt" ) );
2007-08-08 03:50:44 +00:00
if( File == NULL )
{
DisplayError( this, _( "File not found" ) );
return;
}
double unitconv = IU_PER_MM;
ShapeScaleX = ShapeScaleY = 1.0;
FILE_LINE_READER fileReader( File, FullFileName );
FILTER_READER reader( fileReader );
2007-08-08 03:50:44 +00:00
2012-02-19 04:02:19 +00:00
LOCALE_IO toggle;
while( reader.ReadLine() )
2007-08-08 03:50:44 +00:00
{
char* Line = reader.Line();
char* param1 = strtok( Line, " =\n\r" );
char* param2 = strtok( NULL, " \t\n\r" );
2007-08-08 03:50:44 +00:00
if( strncasecmp( param1, "Unit", 4 ) == 0 )
2007-08-08 03:50:44 +00:00
{
if( strncasecmp( param2, "inch", 4 ) == 0 )
unitconv = IU_PER_MILS*1000;
if( strncasecmp( param2, "mm", 2 ) == 0 )
unitconv = IU_PER_MM;
2007-08-08 03:50:44 +00:00
}
if( strncasecmp( param1, "$ENDCOORD", 8 ) == 0 )
2007-08-08 03:50:44 +00:00
break;
if( strncasecmp( param1, "$COORD", 6 ) == 0 )
2007-08-08 03:50:44 +00:00
{
while( reader.ReadLine() )
2007-08-08 03:50:44 +00:00
{
Line = reader.Line();
2007-08-08 03:50:44 +00:00
param1 = strtok( Line, " \t\n\r" );
param2 = strtok( NULL, " \t\n\r" );
if( strncasecmp( param1, "$ENDCOORD", 8 ) == 0 )
2007-08-08 03:50:44 +00:00
break;
wxRealPoint coord( atof( param1 ), atof( param2 ) );
PolyEdges.push_back( coord );
2007-08-08 03:50:44 +00:00
}
}
if( strncasecmp( Line, "XScale", 6 ) == 0 )
2007-08-08 03:50:44 +00:00
ShapeScaleX = atof( param2 );
if( strncasecmp( Line, "YScale", 6 ) == 0 )
2007-08-08 03:50:44 +00:00
ShapeScaleY = atof( param2 );
}
ShapeScaleX *= unitconv;
ShapeScaleY *= unitconv;
m_SizeCtrl->SetValue( (int) ShapeScaleX, (int) ShapeScaleY );
2007-05-06 16:03:28 +00:00
}
MODULE* PCB_EDIT_FRAME::Create_MuWavePolygonShape()
2007-05-06 16:03:28 +00:00
{
2007-08-08 03:50:44 +00:00
D_PAD* pad1, * pad2;
2011-12-14 04:29:25 +00:00
MODULE* module;
2007-08-08 03:50:44 +00:00
wxString cmp_name;
int pad_count = 2;
EDGE_MODULE* edge;
2007-08-08 03:50:44 +00:00
MWAVE_POLYGONAL_SHAPE_DLG dlg( this, wxDefaultPosition );
int ret = dlg.ShowModal();
2007-08-08 03:50:44 +00:00
if( ret != wxID_OK )
2007-08-08 03:50:44 +00:00
{
PolyEdges.clear();
return NULL;
2007-08-08 03:50:44 +00:00
}
if( PolyShapeType == 2 ) // mirrored
ShapeScaleY = -ShapeScaleY;
// 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
ShapeSize.x = KiROUND( ShapeScaleX );
ShapeSize.y = KiROUND( ShapeScaleY );
2007-08-08 03:50:44 +00:00
if( ( ShapeSize.x ) == 0 || ( ShapeSize.y == 0 ) )
2007-08-08 03:50:44 +00:00
{
DisplayError( this, _( "Shape has a null size!" ) );
return NULL;
}
if( PolyEdges.size() == 0 )
2007-08-08 03:50:44 +00:00
{
DisplayError( this, _( "Shape has no points!" ) );
return NULL;
}
cmp_name = wxT( "muwave_polygon" );
2007-08-08 03:50:44 +00:00
// Create a footprint with 2 pads, orientation = 0, pos 0
module = CreateMuWaveBaseFootprint( cmp_name, 0, pad_count );
2007-08-08 03:50:44 +00:00
// We try to place the footprint anchor to the middle of the shape len
wxPoint offset;
offset.x = -ShapeSize.x / 2;
2019-06-01 23:23:36 +00:00
auto it = module->Pads().begin();
pad1 = *it;
pad1->SetX0( offset.x );
pad1->SetX( pad1->GetPos0().x );
2007-08-08 03:50:44 +00:00
2019-06-01 23:23:36 +00:00
pad2 = *( ++it );
pad2->SetX0( offset.x + ShapeSize.x );
pad2->SetX( pad2->GetPos0().x );
2007-08-08 03:50:44 +00:00
// Add a polygonal edge (corners will be added later) on copper layer
2011-12-14 04:29:25 +00:00
edge = new EDGE_MODULE( module );
edge->SetShape( S_POLYGON );
edge->SetLayer( F_Cu );
2007-08-08 03:50:44 +00:00
module->Add( edge, ADD_INSERT );
// Get the corner buffer of the polygonal edge
std::vector<wxPoint> polyPoints;
polyPoints.reserve( PolyEdges.size() + 2 );
2007-08-08 03:50:44 +00:00
// Init start point coord:
2019-07-01 23:48:05 +00:00
polyPoints.emplace_back( wxPoint( offset.x, 0 ) );
2007-08-08 03:50:44 +00:00
wxPoint last_coordinate;
2019-07-01 23:48:05 +00:00
for( wxRealPoint& pt: PolyEdges ) // Copy points
2007-08-08 03:50:44 +00:00
{
2019-07-01 23:48:05 +00:00
last_coordinate.x = KiROUND( pt.x * ShapeScaleX );
last_coordinate.y = -KiROUND( pt.y * ShapeScaleY );
last_coordinate += offset;
polyPoints.push_back( last_coordinate );
2007-08-08 03:50:44 +00:00
}
// finish the polygonal shape
if( last_coordinate.y != 0 )
2019-07-01 23:48:05 +00:00
polyPoints.emplace_back( wxPoint( last_coordinate.x, 0 ) );
2007-08-08 03:50:44 +00:00
switch( PolyShapeType )
{
case 0: // shape from file
case 2: // shape from file, mirrored (the mirror is already done)
2007-08-08 03:50:44 +00:00
break;
case 1: // Symmetric shape: add the symmetric (mirrored) shape
2019-07-01 23:48:05 +00:00
for( int ndx = (int) polyPoints.size() - 1; ndx >= 0; --ndx )
2007-08-08 03:50:44 +00:00
{
wxPoint pt = polyPoints[ndx];
pt.y = -pt.y; // mirror about X axis
polyPoints.push_back( pt );
}
2007-08-08 03:50:44 +00:00
break;
}
edge->SetPolyPoints( polyPoints );
// Set the polygon outline thickness to 0, only the polygonal shape is filled
// without extra thickness
edge->SetWidth( 0 );
PolyEdges.clear();
2011-12-14 04:29:25 +00:00
module->CalculateBoundingBox();
OnModify();
2011-12-14 04:29:25 +00:00
return module;
2007-05-06 16:03:28 +00:00
}