Gerbview code redesign

This commit is contained in:
jean-pierre charras
2010-09-28 16:42:05 +02:00
parent 3ae0df15ee
commit b992af3eb3
39 changed files with 2375 additions and 2318 deletions
+325 -268
View File
@@ -1,5 +1,5 @@
/***************************/
/**** Read GERBER files ****/
/**** class D_CODE ****/
/***************************/
#include "fctsys.h"
@@ -7,53 +7,16 @@
#include "class_drawpanel.h"
#include "confirm.h"
#include "macros.h"
#include "trigo.h"
#include "gerbview.h"
#include "pcbplot.h"
#include "protos.h"
#include "class_gerber_draw_item.h"
#define DEFAULT_SIZE 100
/* Format Gerber: NOTES:
* Features history:
* Gn =
* G01 linear interpolation (right trace)
* G02, G20, G21 Circular interpolation, meaning trig < 0
* G03, G30, G31 Circular interpolation, meaning trig > 0
* G04 review
* G06 parabolic interpolation
* G07 Cubic Interpolation
* G10 linear interpolation (scale x10)
* G11 linear interpolation (0.1x range)
* G12 linear interpolation (0.01x scale)
* G52 plot symbol reference code by Dnn
* G53 plot symbol reference by Dnn; symbol rotates from -90 degrees
* G54 Selection Tool
* G55 Fashion photo exhibition
* G56 plot symbol reference code for DNN
* G57 displays the symbol link to the console
* G58 plot displays the symbol and link to the console
* G60 linear interpolation (scale x100)
* G70 Units = Inches
* G71 Units = Millimeters
* G74 circular interpolation removes 360 degree, has returned G01
* G75 circular interpolation Active 360 degree
* G90 mode absolute coordinates
* G91 Fashion Related Contacts
*
* Coordinates X, Y
* X and Y are followed by + or - and m + n digits (not separated)
* m = integer part
* n = part after the comma
* conventional formats: m = 2, n = 3 (size 2.3)
* m = 3, n = 4 (size 3.4)
* eg
* G__ X00345Y-06123 * D__
*
* Tools and D_CODES
* tool number (identification of shapes)
* 1 to 99 (Classical)
* 1 to 999
*
* D_CODES:
@@ -62,145 +25,14 @@
* D02 = light extinction (lift pen) when di ¿½ placement
* D03 Flash
* D09 = VAPE Flash
* D10 ... = Indentification Tool (Opening)
* D10 ... = Indentification Tool (Shape id)
*
* For tools:
* DCode min = D10
* DCode max = 999
*/
GERBER::GERBER( int aLayer )
{
m_Layer = aLayer; // Layer Number
m_Selected_Tool = FIRST_DCODE;
ResetDefaultValues();
for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
m_Aperture_List[ii] = 0;
m_Pcb = 0;
}
GERBER::~GERBER()
{
for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
{
delete m_Aperture_List[ii];
// m_Aperture_List[ii] = NULL;
}
delete m_Pcb;
}
D_CODE* GERBER::GetDCODE( int aDCODE, bool create )
{
unsigned ndx = aDCODE - FIRST_DCODE;
if( ndx < (unsigned) DIM( m_Aperture_List ) )
{
// lazily create the D_CODE if it does not exist.
if( create )
{
if( m_Aperture_List[ndx] == NULL )
m_Aperture_List[ndx] = new D_CODE( ndx + FIRST_DCODE );
}
return m_Aperture_List[ndx];
}
return NULL;
}
APERTURE_MACRO* GERBER::FindApertureMacro( const APERTURE_MACRO& aLookup )
{
APERTURE_MACRO_SET::iterator iter = m_aperture_macros.find( aLookup );
if( iter != m_aperture_macros.end() )
{
APERTURE_MACRO* pam = (APERTURE_MACRO*) &(*iter);
return pam;
}
return NULL; // not found
}
void GERBER::ResetDefaultValues()
{
m_FileName.Empty();
m_Name = wxT( "no name" ); // Layer name
m_LayerNegative = FALSE; // TRUE = Negative Layer
m_ImageNegative = FALSE; // TRUE = Negative image
m_GerbMetric = FALSE; // FALSE = Inches, TRUE = metric
m_Relative = FALSE; // FALSE = absolute Coord, RUE =
// relative Coord
m_NoTrailingZeros = FALSE; // True: trailing zeros deleted
m_MirorA = FALSE; // True: miror / axe A (X)
m_MirorB = FALSE; // True: miror / axe B (Y)
m_Has_DCode = FALSE; // TRUE = DCodes in file (FALSE = no
// DCode->
// separate DCode file
m_Offset.x = m_Offset.y = 0; // Coord Offset
m_FmtScale.x = m_FmtScale.y = g_Default_GERBER_Format % 10;
m_FmtLen.x = m_FmtLen.y = m_FmtScale.x + (g_Default_GERBER_Format / 10);
m_LayerScale.x = m_LayerScale.y = 1.0; // scale (X and Y) this
// layer
m_Rotation = 0;
m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Linear, 90 arc, Circ.
m_360Arc_enbl = FALSE; // 360 deg circular
// interpolation disable
m_Current_Tool = 0; // Current Tool (Dcode)
// number selected
m_CommandState = 0; // gives tate of the
// stacking order analysis
m_CurrentPos.x = m_CurrentPos.y = 0; // current specified coord
// for plot
m_PreviousPos.x = m_PreviousPos.y = 0; // old current specified
// coord for plot
m_IJPos.x = m_IJPos.y = 0; // current centre coord for
// plot arcs & circles
m_Current_File = NULL; // File to read
m_FilesPtr = 0;
m_Transform[0][0] = m_Transform[1][1] = 1;
m_Transform[0][1] = m_Transform[1][0] = 0; // Rotation/mirror = Normal
m_PolygonFillMode = FALSE;
m_PolygonFillModeState = 0;
}
int GERBER::ReturnUsedDcodeNumber()
{
int count = 0;
for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
{
if( m_Aperture_List[ii] )
if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
++count;
}
return count;
}
void GERBER::InitToolTable()
{
for( int count = 0; count < MAX_TOOLS; count++ )
{
if( m_Aperture_List[count] == NULL )
continue;
m_Aperture_List[count]->m_Num_Dcode = count + FIRST_DCODE;
m_Aperture_List[count]->Clear_D_CODE_Data();
}
}
/***************/
/* Class DCODE */
/***************/
@@ -228,6 +60,7 @@ void D_CODE::Clear_D_CODE_Data()
m_InUse = FALSE;
m_Defined = FALSE;
m_Macro = 0;
m_Rotation = 0.0;
}
@@ -370,7 +203,7 @@ int WinEDA_GerberFrame::Read_D_Code_File( const wxString& D_Code_FullFileName )
if( current_Dcode < FIRST_DCODE )
continue;
if( current_Dcode >= MAX_TOOLS )
if( current_Dcode >= TOOLS_MAX_COUNT )
continue;
dcode = gerber->GetDCODE( current_Dcode );
@@ -393,134 +226,358 @@ void WinEDA_GerberFrame::CopyDCodesSizeToItems()
{
static D_CODE dummy( 999 ); //Used if D_CODE not found in list
for( TRACK* track = GetBoard()->m_Track; track; track = track->Next() )
{
GERBER* gerber = g_GERBER_List[track->GetLayer()];
wxASSERT( gerber );
BOARD_ITEM* item = GetBoard()->m_Drawings;
D_CODE* dcode = gerber->GetDCODE( track->GetNet(), false );
for( ; item; item = item->Next() )
{
GERBER_DRAW_ITEM* gerb_item = (GERBER_DRAW_ITEM*) item;
D_CODE* dcode = gerb_item->GetDcodeDescr();
wxASSERT( dcode );
if( dcode == NULL )
dcode = &dummy;
dcode->m_InUse = TRUE;
if( // Line Item
(track->m_Shape == S_SEGMENT ) /* rectilinear segment */
|| (track->m_Shape == S_RECT ) /* rect segment form (i.e.
* non-rounded ends) */
|| (track->m_Shape == S_ARC ) /* segment arc (rounded tips) */
|| (track->m_Shape == S_CIRCLE ) /* segment in a circle (ring) */
|| (track->m_Shape == S_ARC_RECT ) /* segment arc (stretches)
* (GERBER)*/
gerb_item->m_Size = dcode->m_Size;
if( // Line Item
(gerb_item->m_Shape == GBR_SEGMENT ) /* rectilinear segment */
|| (gerb_item->m_Shape == GBR_ARC ) /* segment arc (rounded tips) */
|| (gerb_item->m_Shape == GBR_CIRCLE ) /* segment in a circle (ring) */
)
{
track->m_Width = dcode->m_Size.x;
}
else // Spots ( Flashed Items )
{
int width, len;
wxSize size = dcode->m_Size;
width = MIN( size.x, size.y );
len = MAX( size.x, size.y ) - width;
track->m_Width = width;
track->m_Start.x = (track->m_Start.x + track->m_End.x) / 2;
track->m_Start.y = (track->m_Start.y + track->m_End.y) / 2;
track->m_End = track->m_Start; // m_Start = m_End = Spot center
switch( dcode->m_Shape )
{
case APT_LINE: // might not appears here, but some broken
// gerber files use it
// gerber files use it
case APT_CIRCLE: /* spot round (for GERBER) */
track->m_Shape = S_SPOT_CIRCLE;
gerb_item->m_Shape = GBR_SPOT_CIRCLE;
break;
case APT_OVAL: /* spot oval (for GERBER)*/
track->m_Shape = S_SPOT_OVALE;
gerb_item->m_Shape = GBR_SPOT_OVAL;
break;
default: /* spot rect (for GERBER)*/
track->m_Shape = S_SPOT_RECT;
gerb_item->m_Shape = GBR_SPOT_RECT;
break;
}
len >>= 1;
if( size.x > size.y )
{
track->m_Start.x -= len;
track->m_End.x += len;
}
else
{
track->m_Start.y -= len;
track->m_End.y += len;
}
}
}
}
void WinEDA_GerberFrame::Liste_D_Codes( )
/** function DrawFlashedShape
* Draw the dcode shape for flashed items.
* When an item is flashed, the DCode shape is the shape of the item
*/
void D_CODE::DrawFlashedShape( EDA_Rect* aClipBox, wxDC* aDC, int aColor,
wxPoint aShapePos, bool aFilledShape )
{
int ii, jj;
D_CODE* pt_D_code;
wxString Line;
WinEDA_TextFrame* List;
int scale = 10000;
int curr_layer = GetScreen()->m_Active_Layer;
int radius;
List = new WinEDA_TextFrame( this, _( "List D codes" ) );
for( int layer = 0; layer < 32; layer++ )
switch( m_Shape )
{
GERBER* gerber = g_GERBER_List[layer];
if( gerber == NULL )
continue;
if( gerber->ReturnUsedDcodeNumber() == 0 )
continue;
if( layer == curr_layer )
Line.Printf( wxT( "*** Active layer (%2.2d) ***" ), layer + 1 );
case APT_CIRCLE:
radius = m_Size.x >> 1;
if( !aFilledShape )
GRCircle( aClipBox, aDC, aShapePos.x, aShapePos.y, radius, aColor );
else
Line.Printf( wxT( "*** layer %2.2d ***" ), layer + 1 );
List->Append( Line );
if( m_DrillShape == 0 )
GRFilledCircle( aClipBox, aDC, aShapePos, radius, aColor );
else if( m_DrillShape == 1 ) // round hole
{
int width = (m_Size.x - m_Drill.x ) / 2;
GRCircle( aClipBox, aDC, aShapePos, radius - (width / 2), width, aColor );
}
else // rectangular hole
{
if( m_PolyCorners.size() == 0 )
ConvertShapeToPolygon();
DrawFlashedPolygon( aClipBox, aDC, aColor, aFilledShape, aShapePos );
}
break;
for( ii = 0, jj = 1; ii < MAX_TOOLS; ii++ )
case APT_LINE:
// not used for flashed items
break;
case APT_RECT:
{
wxPoint start;
start.x = aShapePos.x - m_Size.x/2;
start.y = aShapePos.y - m_Size.y/2;
wxPoint end = start + m_Size;
if( !aFilledShape )
{
pt_D_code = gerber->GetDCODE( ii + FIRST_DCODE, false );
if( pt_D_code == NULL )
continue;
if( !pt_D_code->m_InUse && !pt_D_code->m_Defined )
continue;
Line.Printf( wxT(
"tool %2.2d: D%2.2d V %2.4f H %2.4f %s" ),
jj,
pt_D_code->m_Num_Dcode,
(float) pt_D_code->m_Size.y / scale,
(float) pt_D_code->m_Size.x / scale,
D_CODE::ShowApertureType( pt_D_code->m_Shape )
);
if( !pt_D_code->m_Defined )
Line += wxT( " ?" );
if( !pt_D_code->m_InUse )
Line += wxT( " *" );
List->Append( Line );
jj++;
GRRect( aClipBox, aDC, start.x, start.y, end.x, end.y ,
0, aColor );
}
else if( m_DrillShape == 0 )
{
GRFilledRect( aClipBox, aDC, start.x, start.y, end.x, end.y,
0, aColor, aColor );
}
else
{
if( m_PolyCorners.size() == 0 )
ConvertShapeToPolygon();
DrawFlashedPolygon( aClipBox, aDC, aColor, aFilledShape, aShapePos );
}
}
break;
ii = List->ShowModal();
List->Destroy();
if( ii < 0 )
case APT_OVAL:
{
wxPoint start = aShapePos;
wxPoint end = aShapePos;
if( m_Size.x > m_Size.y ) // horizontal oval
{
int delta = (m_Size.x - m_Size.y) / 2;
start.x -= delta;
end.x += delta;
radius = m_Size.y;
}
else // horizontal oval
{
int delta = (m_Size.y - m_Size.x) / 2;
start.y -= delta;
end.y += delta;
radius = m_Size.x;
}
if( !aFilledShape )
{
GRCSegm( aClipBox, aDC, start.x, start.y,
end.x, end.y, radius, aColor );
}
else if( m_DrillShape == 0 )
{
GRFillCSegm( aClipBox, aDC, start.x,
start.y, end.x, end.y, radius, aColor );
}
else
{
if( m_PolyCorners.size() == 0 )
ConvertShapeToPolygon();
DrawFlashedPolygon( aClipBox, aDC, aColor, aFilledShape, aShapePos );
}
}
break;
case APT_POLYGON:
if( m_PolyCorners.size() == 0 )
ConvertShapeToPolygon();
DrawFlashedPolygon( aClipBox, aDC, aColor, aFilledShape, aShapePos );
break;
case APT_MACRO:
// TODO
break;
}
}
/** function DrawFlashedPolygon
* a helper function used id ::Draw to draw the polygon stored ion m_PolyCorners
* Draw some Apertures shapes when they are defined as filled polygons.
* APT_POLYGON is always a polygon, but some complex shapes are also converted to
* polygons (shapes with holes)
*/
void D_CODE::DrawFlashedPolygon( EDA_Rect* aClipBox, wxDC* aDC,
int aColor, bool aFilled,
const wxPoint& aPosition )
{
if( m_PolyCorners.size() == 0 )
return;
std::vector<wxPoint> points;
points = m_PolyCorners;
for( unsigned ii = 0; ii < points.size(); ii++ )
{
points[ii] += aPosition;
}
GRClosedPoly( aClipBox, aDC, points.size(), &points[0], aFilled, aColor, aColor );
}
/** function ConvertShapeToPolygon
* convert a shape to an equivalent polygon.
* Arcs and circles are approximated by segments
* Useful when a shape is not a graphic primitive (shape with hole,
* Rotated shape ... ) and cannot be easily drawn.
*/
void D_CODE::ConvertShapeToPolygon()
{
#define SEGS_CNT 32 // number of segments to approximate a circle
wxPoint initialpos;
wxPoint currpos;;
m_PolyCorners.clear();
switch( m_Shape )
{
case APT_CIRCLE: // creates only a circle with rectangular hole
currpos.x = m_Size.x >> 1;
initialpos = currpos;
for( unsigned ii = 0; ii <= SEGS_CNT; ii++ )
{
currpos = initialpos;
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
m_PolyCorners.push_back( currpos );
}
if( m_DrillShape == 1 )
{
for( unsigned ii = 0 ; ii <= SEGS_CNT; ii++ )
{
currpos.x = m_Drill.x / 2;
currpos.y = 0;
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
m_PolyCorners.push_back( currpos );
}
m_PolyCorners.push_back( initialpos ); // link to outline
}
if( m_DrillShape == 2 ) // Create rectangular hole
{
currpos.x = m_Drill.x / 2;
currpos.y = m_Drill.y / 2;
m_PolyCorners.push_back( currpos ); // link to hole and begin hole
currpos.x -= m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y -= m_Drill.y;
m_PolyCorners.push_back( currpos );
currpos.x += m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y += m_Drill.y;
m_PolyCorners.push_back( currpos ); // close hole
m_PolyCorners.push_back( initialpos ); // link to outline
}
break;
case APT_LINE:
// Not used for flashed shapes
break;
case APT_RECT:
currpos.x = m_Size.x / 2;
currpos.y = m_Size.y / 2;
initialpos = currpos;
m_PolyCorners.push_back( currpos );
currpos.x -= m_Size.x;
m_PolyCorners.push_back( currpos );
currpos.y -= m_Size.y;
m_PolyCorners.push_back( currpos );
currpos.x += m_Size.x;
m_PolyCorners.push_back( currpos );
currpos.y += m_Size.y;
m_PolyCorners.push_back( currpos ); // close polygon
if( m_DrillShape == 1 ) // build a round hole
{
for( int ii = 0 ; ii <= SEGS_CNT; ii++ )
{
currpos.x = 0;
currpos.y = m_Drill.x / 2; // m_Drill.x / 2 is the radius of the hole
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
m_PolyCorners.push_back( currpos );
}
m_PolyCorners.push_back( initialpos ); // link to outline
}
if( m_DrillShape == 2 ) // Create rectangular hole
{
currpos.x = m_Drill.x / 2;
currpos.y = m_Drill.y / 2;
m_PolyCorners.push_back( currpos ); // link to hole and begin hole
currpos.x -= m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y -= m_Drill.y;
m_PolyCorners.push_back( currpos );
currpos.x += m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y += m_Drill.y;
m_PolyCorners.push_back( currpos ); // close hole
m_PolyCorners.push_back( initialpos ); // link to outline
}
break;
case APT_OVAL:
{
int delta, radius;
// we create an horizontal oval shape. then rotate if needed
if( m_Size.x > m_Size.y ) // horizontal oval
{
delta = (m_Size.x - m_Size.y) / 2;
radius = m_Size.y / 2;
}
else // vertical oval
{
delta = (m_Size.y - m_Size.x) / 2;
radius = m_Size.x / 2;
}
currpos.y = radius;
initialpos = currpos;
m_PolyCorners.push_back( currpos );
// build the right arc of the shape
unsigned ii = 0;
for( ; ii <= SEGS_CNT / 2; ii++ )
{
currpos = initialpos;
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
currpos.x += delta;
m_PolyCorners.push_back( currpos );
}
// build the left arc of the shape
for( ii = SEGS_CNT / 2; ii <= SEGS_CNT; ii++ )
{
currpos = initialpos;
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
currpos.x -= delta;
m_PolyCorners.push_back( currpos );
}
m_PolyCorners.push_back( initialpos ); // close outline
if( m_Size.y > m_Size.x ) // vertical oval, rotate polygon.
{
for( unsigned jj = 0; jj < m_PolyCorners.size(); jj++ )
RotatePoint( &m_PolyCorners[jj], 900 );
}
if( m_DrillShape == 1 ) // build a round hole
{
for( ii = 0 ; ii <= SEGS_CNT; ii++ )
{
currpos.x = 0;
currpos.y = m_Drill.x / 2; // m_Drill.x / 2 is the radius of the hole
RotatePoint( &currpos, ii * 3600 / SEGS_CNT );
m_PolyCorners.push_back( currpos );
}
m_PolyCorners.push_back( initialpos ); // link to outline
}
if( m_DrillShape == 2 ) // Create rectangular hole
{
currpos.x = m_Drill.x / 2;
currpos.y = m_Drill.y / 2;
m_PolyCorners.push_back( currpos ); // link to hole and begin hole
currpos.x -= m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y -= m_Drill.y;
m_PolyCorners.push_back( currpos );
currpos.x += m_Drill.x;
m_PolyCorners.push_back( currpos );
currpos.y += m_Drill.y;
m_PolyCorners.push_back( currpos ); // close hole
m_PolyCorners.push_back( initialpos ); // link to outline
}
}
break;
case APT_POLYGON:
// TODO
break;
case APT_MACRO:
// TODO
break;
}
}