kicad/pcbnew/gen_modules_placefile.cpp

535 lines
16 KiB
C++
Raw Normal View History

/*****************************/
/* gen_modules_placefile.cpp */
/*****************************/
/*
2007-08-23 04:28:46 +00:00
* 1 - create ascii files for automatic placement of smd components
* 2 - create a module report (pos and module descr) (ascii file)
*/
#include "fctsys.h"
#include "common.h"
#include "confirm.h"
#include "kicad_string.h"
#include "gestfich.h"
#include "pcbnew.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "trigo.h"
#include "appl_wxstruct.h"
#include "build_version.h"
class LIST_MOD /* Can list the elements of useful modules. */
{
public:
2007-08-23 04:28:46 +00:00
MODULE* m_Module;
const wxChar* m_Reference;
const wxChar* m_Value;
};
static wxPoint File_Place_Offset; /* Offset coordinates for generated file. */
2007-08-23 04:28:46 +00:00
static void WriteDrawSegmentPcb( DRAWSEGMENT* PtDrawSegment, FILE* rptfile );
/* Sort function use by GenereModulesPosition() */
static int ListeModCmp( const void* o1, const void* o2 )
{
LIST_MOD* ref = (LIST_MOD*) o1;
LIST_MOD* cmp = (LIST_MOD*) o2;
return StrLenNumCmp( ref->m_Reference, cmp->m_Reference, 16 );
}
#if defined(DEBUG)
/**
* Function HasNonSMDPins
* returns true if the given module has any non smd pins, such as through hole
* and therefore cannot be placed automatically.
*/
static bool HasNonSMDPins( MODULE* aModule )
{
D_PAD* pad;
for( pad = aModule->m_Pads; pad; pad = pad->Next() )
{
if( pad->m_Attribut != PAD_SMD )
return true;
}
return false;
}
#endif
/* Generate the module positions, used component placement.
2007-08-23 04:28:46 +00:00
*/
void WinEDA_PcbFrame::GenModulesPosition( wxCommandEvent& event )
{
bool doBoardBack = false;
MODULE* module;
LIST_MOD* Liste = 0;
char line[1024];
char Buff[80];
wxFileName fnFront;
wxFileName fnBack;
wxString msg;
wxString frontLayerName;
wxString backLayerName;
wxString Title;
FILE* fpFront = 0;
FILE* fpBack = 0;
bool switchedLocale = false;
2007-08-23 04:28:46 +00:00
/* Calculate conversion scales. */
2009-03-15 21:11:12 +00:00
double conv_unit = 0.0001; /* unites = INCHES */
2007-08-23 04:28:46 +00:00
// if(IF_DRILL_METRIC) conv_unit = 0.000254; /* unites = mm */
2007-08-23 04:28:46 +00:00
File_Place_Offset = m_Auxiliary_Axis_Position;
/* Calculating the number of useful modules (CMS attribute, not VIRTUAL) */
int moduleCount = 0;
for( module = GetBoard()->m_Modules; module; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
if( module->m_Attributs & MOD_VIRTUAL )
{
D( printf( "skipping module %s because it's virtual\n",
CONV_TO_UTF8( module->GetReference() ) );)
2007-08-23 04:28:46 +00:00
continue;
}
if( ( module->m_Attributs & MOD_CMS ) == 0 )
{
#if 1 && defined(DEBUG) // enable this code to fix a bunch of mis-labeled modules:
if( !HasNonSMDPins( module ) )
{
// all module's pins are SMD, mark the part for pick and place
module->m_Attributs |= MOD_CMS;
}
else
{
printf( "skipping %s because its attribute is not CMS and it has non SMD pins\n",
CONV_TO_UTF8(module->GetReference()) );
continue;
}
#else
2007-08-23 04:28:46 +00:00
continue;
#endif
}
if( module->GetLayer() == LAYER_N_BACK )
doBoardBack = true;
moduleCount++;
2007-08-23 04:28:46 +00:00
}
if( moduleCount == 0 )
2007-08-23 04:28:46 +00:00
{
DisplayError( this, _( "No modules for automated placement." ) );
return;
2007-08-23 04:28:46 +00:00
}
fnFront = GetScreen()->m_FileName;
frontLayerName = GetBoard()->GetLayerName( LAYER_N_FRONT );
fnFront.SetName( fnFront.GetName() + frontLayerName );
fnFront.SetExt( wxT( "pos") );
2007-08-23 04:28:46 +00:00
fpFront = wxFopen( fnFront.GetFullPath(), wxT( "wt" ) );
if( fpFront == 0 )
2007-08-23 04:28:46 +00:00
{
msg = _( "Unable to create " ) + fnFront.GetFullPath();
DisplayError( this, msg );
goto exit;
2007-08-23 04:28:46 +00:00
}
if( doBoardBack )
2007-08-23 04:28:46 +00:00
{
fnBack = GetScreen()->m_FileName;
backLayerName = GetBoard()->GetLayerName( LAYER_N_BACK );
fnBack.SetName( fnBack.GetName() + backLayerName );
fnBack.SetExt( wxT( "pos" ) );
fpBack = wxFopen( fnBack.GetFullPath(), wxT( "wt" ) );
if( fpBack == 0 )
2007-08-23 04:28:46 +00:00
{
msg = _( "Unable to create " ) + fnBack.GetFullPath();
2007-08-23 04:28:46 +00:00
DisplayError( this, msg );
goto exit;
2007-08-23 04:28:46 +00:00
}
}
// Switch the locale to standard C (needed to print floating point
// numbers like 1.3)
2008-06-06 16:39:45 +00:00
SetLocaleTo_C_standard( );
switchedLocale = true;
2007-08-23 04:28:46 +00:00
/* Display results */
2007-08-23 04:28:46 +00:00
MsgPanel->EraseMsgBox();
Affiche_1_Parametre( this, 0, _( "Component side place file:" ),
fnFront.GetFullPath(), BLUE );
2007-08-23 04:28:46 +00:00
if( doBoardBack )
Affiche_1_Parametre( this, 32, _( "Copper side place file:" ),
fnBack.GetFullPath(), BLUE );
2007-08-23 04:28:46 +00:00
msg.Empty(); msg << moduleCount;
2007-08-23 04:28:46 +00:00
Affiche_1_Parametre( this, 65, _( "Module count" ), msg, RED );
/* Sort the list of modules by alphabetical order */
Liste = (LIST_MOD*) MyZMalloc( moduleCount * sizeof(LIST_MOD) );
2007-08-23 04:28:46 +00:00
module = GetBoard()->m_Modules;
for( int ii = 0; module; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
if( module->m_Attributs & MOD_VIRTUAL )
2007-08-23 04:28:46 +00:00
continue;
if( (module->m_Attributs & MOD_CMS) == 0 )
2007-08-23 04:28:46 +00:00
continue;
Liste[ii].m_Module = module;
Liste[ii].m_Reference = module->m_Reference->m_Text;
Liste[ii].m_Value = module->m_Value->m_Text;
2007-08-23 04:28:46 +00:00
ii++;
}
qsort( Liste, moduleCount, sizeof(LIST_MOD), ListeModCmp );
2007-08-23 04:28:46 +00:00
/* Generation header file comments. */
sprintf( line, "### Module positions - created on %s ###\n",
DateAndTime( Buff ) );
fputs( line, fpFront );
if( doBoardBack )
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
Title = wxGetApp().GetAppName() + wxT( " " ) + GetBuildVersion();
sprintf( line, "### Printed by PcbNew version %s\n", CONV_TO_UTF8( Title ) );
fputs( line, fpFront );
if( doBoardBack )
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
sprintf( line, "## Unit = inches, Angle = deg.\n" );
fputs( line, fpFront );
if( doBoardBack )
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
sprintf( line, "## Side : %s\n", CONV_TO_UTF8( frontLayerName ) );
fputs( line, fpFront );
2007-08-23 04:28:46 +00:00
if( doBoardBack )
2007-08-23 04:28:46 +00:00
{
sprintf( line, "## Side : %s\n", CONV_TO_UTF8( backLayerName ) );
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
}
sprintf( line,
2007-08-23 04:28:46 +00:00
"# Ref Val PosX PosY Rot Side\n" );
fputs( line, fpFront );
if( doBoardBack )
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
for( int ii = 0; ii < moduleCount; ii++ )
2007-08-23 04:28:46 +00:00
{
wxPoint module_pos;
wxString ref = Liste[ii].m_Reference;
wxString val = Liste[ii].m_Value;
sprintf( line, "%-8.8s %-16.16s ", CONV_TO_UTF8( ref ),
CONV_TO_UTF8( val ) );
2007-08-23 04:28:46 +00:00
module_pos = Liste[ii].m_Module->m_Pos;
module_pos.x -= File_Place_Offset.x;
module_pos.y -= File_Place_Offset.y;
char* text = line + strlen( line );
2007-08-23 04:28:46 +00:00
sprintf( text, " %9.4f %9.4f %8.1f ",
2009-03-15 21:11:12 +00:00
module_pos.x * conv_unit,
module_pos.y * conv_unit,
double(Liste[ii].m_Module->m_Orient) / 10 );
2007-08-23 04:28:46 +00:00
int layer = Liste[ii].m_Module->GetLayer();
wxASSERT( layer==LAYER_N_FRONT || layer==LAYER_N_BACK );
if( layer == LAYER_N_FRONT )
2007-08-23 04:28:46 +00:00
{
strcat( line, CONV_TO_UTF8( frontLayerName ) );
strcat( line, "\n" );
fputs( line, fpFront );
2007-08-23 04:28:46 +00:00
}
else if( layer == LAYER_N_BACK )
2007-08-23 04:28:46 +00:00
{
strcat( line, CONV_TO_UTF8( backLayerName ) );
strcat( line, "\n" );
fputs( line, fpBack );
2007-08-23 04:28:46 +00:00
}
}
/* Generate EOF. */
fputs( "## End\n", fpFront );
if( doBoardBack )
fputs( "## End\n", fpBack );
msg = frontLayerName + wxT( " File: " ) + fnFront.GetFullPath();
if( doBoardBack )
msg += wxT("\n\n") + backLayerName + wxT( " File: " ) +
fnBack.GetFullPath();
2007-08-23 04:28:46 +00:00
DisplayInfoMessage( this, msg );
exit: // the only safe way out of here, no returns please.
if( Liste )
MyFree( Liste );
if( switchedLocale )
SetLocaleTo_Default( ); // revert to the current locale
if( fpFront )
fclose( fpFront );
if( fpBack )
fclose( fpBack );
}
/* Print a module report.
2007-08-23 04:28:46 +00:00
*/
void WinEDA_PcbFrame::GenModuleReport( wxCommandEvent& event )
{
2009-03-15 21:11:12 +00:00
double conv_unit;
2007-08-23 04:28:46 +00:00
MODULE* Module;
D_PAD* pad;
char line[1024], Buff[80];
wxFileName fn;
wxString fnFront, msg;
2007-08-23 04:28:46 +00:00
FILE* rptfile;
wxPoint module_pos;
conv_unit = 0.0001; /* unites = INCHES */
// if(IF_DRILL_METRIC) conv_unit = 0.000254; /* unites = mm */
2007-08-23 04:28:46 +00:00
File_Place_Offset = wxPoint( 0, 0 );
fn = GetScreen()->m_FileName;
fn.SetExt( wxT( "rpt" ) );
2007-08-23 04:28:46 +00:00
rptfile = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
2007-08-23 04:28:46 +00:00
if( rptfile == NULL )
{
msg = _( "Unable to create " ) + fn.GetFullPath();
DisplayError( this, msg );
return;
2007-08-23 04:28:46 +00:00
}
// Switch the locale to standard C (needed to print floating point
// numbers like 1.3)
2008-06-06 16:39:45 +00:00
SetLocaleTo_C_standard( );
2007-08-23 04:28:46 +00:00
/* Generate header file comments.) */
sprintf( line, "## Module report - date %s\n", DateAndTime( Buff ) );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
wxString Title = wxGetApp().GetAppName() + wxT( " " ) + GetBuildVersion();
sprintf( line, "## Created by PcbNew version %s\n", CONV_TO_UTF8( Title ) );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
fputs( "## Unit = inches, Angle = deg.\n", rptfile );
fputs( "##\n", rptfile );
fputs( "\n$BeginDESCRIPTION\n", rptfile );
GetBoard()->ComputeBoundaryBox();
2007-08-23 04:28:46 +00:00
fputs( "\n$BOARD\n", rptfile );
fputs( "unit INCH\n", rptfile );
sprintf( line, "upper_left_corner %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
GetBoard()->m_BoundaryBox.GetX() * conv_unit,
GetBoard()->m_BoundaryBox.GetY() * conv_unit );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
sprintf( line, "lower_right_corner %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
GetBoard()->m_BoundaryBox.GetRight() * conv_unit,
GetBoard()->m_BoundaryBox.GetBottom() * conv_unit );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
fputs( "$EndBOARD\n\n", rptfile );
Module = (MODULE*) GetBoard()->m_Modules;
2007-08-23 04:28:46 +00:00
for( ; Module != NULL; Module = Module->Next() )
{
sprintf( line, "$MODULE \"%s\"\n",
CONV_TO_UTF8( Module->m_Reference->m_Text ) );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
sprintf( line, "reference \"%s\"\n",
CONV_TO_UTF8( Module->m_Reference->m_Text ) );
fputs( line, rptfile );
sprintf( line, "value \"%s\"\n",
CONV_TO_UTF8( Module->m_Value->m_Text ) );
fputs( line, rptfile );
sprintf( line, "footprint \"%s\"\n", CONV_TO_UTF8( Module->m_LibRef ) );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
msg = wxT( "attribut" );
if( Module->m_Attributs & MOD_VIRTUAL )
msg += wxT( " virtual" );
if( Module->m_Attributs & MOD_CMS )
msg += wxT( " smd" );
if( ( Module->m_Attributs & (MOD_VIRTUAL | MOD_CMS) ) == 0 )
msg += wxT( " none" );
msg += wxT( "\n" );
fputs( CONV_TO_UTF8( msg ), rptfile );
module_pos = Module->m_Pos;
module_pos.x -= File_Place_Offset.x;
module_pos.y -= File_Place_Offset.y;
2009-03-15 21:11:12 +00:00
sprintf( line, "position %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
module_pos.x * conv_unit,
module_pos.y * conv_unit );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
2009-03-15 21:11:12 +00:00
sprintf( line, "orientation %.2f\n", (double) Module->m_Orient / 10 );
if( Module->GetLayer() == LAYER_N_FRONT )
strcat( line, "layer component\n" );
else if( Module->GetLayer() == LAYER_N_BACK )
strcat( line, "layer copper\n" );
2007-08-23 04:28:46 +00:00
else
strcat( line, "layer other\n" );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
Module->Write_3D_Descr( rptfile );
for( pad = Module->m_Pads; pad != NULL; pad = pad->Next() )
{
fprintf( rptfile, "$PAD \"%.4s\"\n", pad->m_Padname );
sprintf( line, "position %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
pad->m_Pos0.x * conv_unit,
pad->m_Pos0.y * conv_unit );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
sprintf( line, "size %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
pad->m_Size.x * conv_unit,
pad->m_Size.y * conv_unit );
fputs( line, rptfile );
2009-03-15 21:11:12 +00:00
sprintf( line, "drill %9.6f\n", pad->m_Drill.x * conv_unit );
fputs( line, rptfile );
sprintf( line, "shape_offset %9.6f %9.6f\n",
2009-03-15 21:11:12 +00:00
pad->m_Offset.x * conv_unit,
pad->m_Offset.y * conv_unit );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
sprintf( line, "orientation %.2f\n",
double(pad->m_Orient - Module->m_Orient) / 10 );
fputs( line, rptfile );
const char* shape_name[6] =
{ "??? ", "Circ", "Rect", "Oval", "trap", "spec" };
sprintf( line, "Shape %s\n", shape_name[pad->m_PadShape] );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
int layer = 0;
if( pad->m_Masque_Layer & LAYER_BACK )
2007-08-23 04:28:46 +00:00
layer = 1;
if( pad->m_Masque_Layer & LAYER_FRONT )
2007-08-23 04:28:46 +00:00
layer |= 2;
const char* layer_name[4] = { "??? ", "copper", "component", "all" };
sprintf( line, "Layer %s\n", layer_name[layer] );
fputs( line, rptfile );
2007-08-23 04:28:46 +00:00
fprintf( rptfile, "$EndPAD\n" );
}
fprintf( rptfile, "$EndMODULE %s\n\n",
CONV_TO_UTF8(Module->m_Reference->m_Text ) );
2007-08-23 04:28:46 +00:00
}
/* Write board Edges */
EDA_BaseStruct* PtStruct;
for( PtStruct = GetBoard()->m_Drawings; PtStruct != NULL; PtStruct = PtStruct->Next() )
2007-08-23 04:28:46 +00:00
{
if( PtStruct->Type() != TYPE_DRAWSEGMENT )
2007-08-23 04:28:46 +00:00
continue;
if( ( (DRAWSEGMENT*) PtStruct )->GetLayer() != EDGE_N )
continue;
WriteDrawSegmentPcb( (DRAWSEGMENT*) PtStruct, rptfile );
}
/* Generate EOF. */
2007-08-23 04:28:46 +00:00
fputs( "$EndDESCRIPTION\n", rptfile );
fclose( rptfile );
SetLocaleTo_Default( ); // revert to the current locale
}
/* Output to rpt file a segment type from the PCB drawing.
* The contours are of different types:
* Segment
* Circle
* Arc
2007-08-23 04:28:46 +00:00
*/
void WriteDrawSegmentPcb( DRAWSEGMENT* PtDrawSegment, FILE* rptfile )
{
2007-08-23 04:28:46 +00:00
double conv_unit, ux0, uy0, dx, dy;
double rayon, width;
char line[1024];
2007-08-23 04:28:46 +00:00
conv_unit = 0.0001; /* units = INCHES */
2007-08-23 04:28:46 +00:00
ux0 = PtDrawSegment->m_Start.x * conv_unit;
uy0 = PtDrawSegment->m_Start.y * conv_unit;
2007-08-23 04:28:46 +00:00
dx = PtDrawSegment->m_End.x * conv_unit;
dy = PtDrawSegment->m_End.y * conv_unit;
width = PtDrawSegment->m_Width * conv_unit;
switch( PtDrawSegment->m_Shape )
{
case S_CIRCLE:
rayon = hypot( dx - ux0, dy - uy0 );
2009-01-18 15:51:06 +00:00
fprintf( rptfile, "$CIRCLE \n" );
fprintf( rptfile, "centre %.6lf %.6lf\n", ux0, uy0 );
fprintf( rptfile, "radius %.6lf\n", rayon );
fprintf( rptfile, "width %.6lf\n", width );
fprintf( rptfile, "$EndCIRCLE \n" );
2007-08-23 04:28:46 +00:00
break;
case S_ARC:
{
int endx = PtDrawSegment->m_End.x, endy = PtDrawSegment->m_End.y;
rayon = hypot( dx - ux0, dy - uy0 );
RotatePoint( &endx,
&endy,
PtDrawSegment->m_Start.x,
PtDrawSegment->m_Start.y,
PtDrawSegment->m_Angle );
2009-01-18 15:51:06 +00:00
fprintf( rptfile, "$ARC \n" );
fprintf( rptfile, "centre %.6lf %.6lf\n", ux0, uy0 );
fprintf( rptfile, "start %.6lf %.6lf\n",
endx * conv_unit, endy * conv_unit );
2009-01-18 15:51:06 +00:00
fprintf( rptfile, "end %.6lf %.6lf\n", dx, dy );
fprintf( rptfile, "width %.6lf\n", width );
fprintf( rptfile, "$EndARC \n" );
}
2007-08-23 04:28:46 +00:00
break;
default:
sprintf( line, "$LINE \n" );
fputs( line, rptfile );
2009-01-18 15:51:06 +00:00
fprintf( rptfile, "start %.6lf %.6lf\n", ux0, uy0 );
fprintf( rptfile, "end %.6lf %.6lf\n", dx, dy );
fprintf( rptfile, "width %.6lf\n", width );
fprintf( rptfile, "$EndLINE \n" );
2007-08-23 04:28:46 +00:00
break;
}
}