GERBER_PLOTTER: small change in code to manage the current selected aperture.

Avoid using an iterator to store this aperture because iterators can be invalided
when modifying a list.
Enhancements in gerber_placefile_writer.
This commit is contained in:
jean-pierre charras 2019-11-03 20:23:31 +01:00
parent 56525296ba
commit 51ed01d765
3 changed files with 100 additions and 101 deletions

View File

@ -47,7 +47,7 @@ GERBER_PLOTTER::GERBER_PLOTTER()
{ {
workFile = NULL; workFile = NULL;
finalFile = NULL; finalFile = NULL;
currentAperture = apertures.end(); m_currentApertureIdx = -1;
m_apertureAttribute = 0; m_apertureAttribute = 0;
// number of digits after the point (number of digits of the mantissa // number of digits after the point (number of digits of the mantissa
@ -65,7 +65,7 @@ GERBER_PLOTTER::GERBER_PLOTTER()
void GERBER_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil, void GERBER_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
double aScale, bool aMirror ) double aScale, bool aMirror )
{ {
wxASSERT( aMirror == false ); wxASSERT( aMirror == false );
m_plotMirror = false; m_plotMirror = false;
@ -101,8 +101,7 @@ void GERBER_PLOTTER::SetGerberCoordinatesFormat( int aResolution, bool aUseInche
void GERBER_PLOTTER::emitDcode( const DPOINT& pt, int dcode ) void GERBER_PLOTTER::emitDcode( const DPOINT& pt, int dcode )
{ {
fprintf( outputFile, "X%dY%dD%02d*\n", fprintf( outputFile, "X%dY%dD%02d*\n", KiROUND( pt.x ), KiROUND( pt.y ), dcode );
KiROUND( pt.x ), KiROUND( pt.y ), dcode );
} }
void GERBER_PLOTTER::ClearAllAttributes() void GERBER_PLOTTER::ClearAllAttributes()
@ -282,7 +281,7 @@ bool GERBER_PLOTTER::EndPlot()
void GERBER_PLOTTER::SetDefaultLineWidth( int width ) void GERBER_PLOTTER::SetDefaultLineWidth( int width )
{ {
defaultPenWidth = width; defaultPenWidth = width;
currentAperture = apertures.end(); m_currentApertureIdx = -1;
} }
@ -306,23 +305,20 @@ void GERBER_PLOTTER::SetCurrentLineWidth( int width, void* aData )
} }
std::vector<APERTURE>::iterator GERBER_PLOTTER::getAperture( const wxSize& aSize, int GERBER_PLOTTER::GetOrCreateAperture( const wxSize& aSize,
APERTURE::APERTURE_TYPE aType, int aApertureAttribute ) APERTURE::APERTURE_TYPE aType, int aApertureAttribute )
{ {
int last_D_code = 9; int last_D_code = 9;
// Search an existing aperture // Search an existing aperture
std::vector<APERTURE>::iterator tool = apertures.begin(); for( int idx = 0; idx < (int)m_apertures.size(); ++idx )
while( tool != apertures.end() )
{ {
APERTURE* tool = &m_apertures[idx];
last_D_code = tool->m_DCode; last_D_code = tool->m_DCode;
if( (tool->m_Type == aType) && (tool->m_Size == aSize) && if( (tool->m_Type == aType) && (tool->m_Size == aSize) &&
(tool->m_ApertureAttribute == aApertureAttribute) ) (tool->m_ApertureAttribute == aApertureAttribute) )
return tool; return idx;
++tool;
} }
// Allocate a new aperture // Allocate a new aperture
@ -332,9 +328,9 @@ std::vector<APERTURE>::iterator GERBER_PLOTTER::getAperture( const wxSize& aSize
new_tool.m_DCode = last_D_code + 1; new_tool.m_DCode = last_D_code + 1;
new_tool.m_ApertureAttribute = aApertureAttribute; new_tool.m_ApertureAttribute = aApertureAttribute;
apertures.push_back( new_tool ); m_apertures.push_back( new_tool );
return apertures.end() - 1; return m_apertures.size() - 1;
} }
@ -342,20 +338,20 @@ void GERBER_PLOTTER::selectAperture( const wxSize& aSize,
APERTURE::APERTURE_TYPE aType, APERTURE::APERTURE_TYPE aType,
int aApertureAttribute ) int aApertureAttribute )
{ {
bool change = ( currentAperture == apertures.end() ) || bool change = ( m_currentApertureIdx < 0 ) ||
( currentAperture->m_Type != aType ) || ( m_apertures[m_currentApertureIdx].m_Type != aType ) ||
( currentAperture->m_Size != aSize ); ( m_apertures[m_currentApertureIdx].m_Size != aSize );
if( !m_useNetAttributes ) if( !m_useNetAttributes )
aApertureAttribute = 0; aApertureAttribute = 0;
else else if( !change )
change = change || ( currentAperture->m_ApertureAttribute != aApertureAttribute ); change = m_apertures[m_currentApertureIdx].m_ApertureAttribute != aApertureAttribute;
if( change ) if( change )
{ {
// Pick an existing aperture or create a new one // Pick an existing aperture or create a new one
currentAperture = getAperture( aSize, aType, aApertureAttribute ); m_currentApertureIdx = GetOrCreateAperture( aSize, aType, aApertureAttribute );
fprintf( outputFile, "D%d*\n", currentAperture->m_DCode ); fprintf( outputFile, "D%d*\n", m_apertures[m_currentApertureIdx].m_DCode );
} }
} }
@ -387,8 +383,7 @@ void GERBER_PLOTTER::writeApertureList()
useX1StructuredComment = true; useX1StructuredComment = true;
// Init // Init
for( std::vector<APERTURE>::iterator tool = apertures.begin(); for( APERTURE& tool : m_apertures )
tool != apertures.end(); ++tool )
{ {
// apertude sizes are in inch or mm, regardless the // apertude sizes are in inch or mm, regardless the
// coordinates format // coordinates format
@ -397,7 +392,7 @@ void GERBER_PLOTTER::writeApertureList()
if(! m_gerberUnitInch ) if(! m_gerberUnitInch )
fscale *= 25.4; // size in mm fscale *= 25.4; // size in mm
int attribute = tool->m_ApertureAttribute; int attribute = tool.m_ApertureAttribute;
if( attribute != m_apertureAttribute ) if( attribute != m_apertureAttribute )
{ {
@ -406,7 +401,7 @@ void GERBER_PLOTTER::writeApertureList()
useX1StructuredComment ).c_str(), outputFile ); useX1StructuredComment ).c_str(), outputFile );
} }
char* text = cbuf + sprintf( cbuf, "%%ADD%d", tool->m_DCode ); char* text = cbuf + sprintf( cbuf, "%%ADD%d", tool.m_DCode );
/* Please note: the Gerber specs for mass parameters say that /* Please note: the Gerber specs for mass parameters say that
exponential syntax is *not* allowed and the decimal point should exponential syntax is *not* allowed and the decimal point should
@ -415,24 +410,24 @@ void GERBER_PLOTTER::writeApertureList()
can't remove trailing zeros but thats not a problem, since nothing can't remove trailing zeros but thats not a problem, since nothing
forbid it (the file is only slightly longer) */ forbid it (the file is only slightly longer) */
switch( tool->m_Type ) switch( tool.m_Type )
{ {
case APERTURE::AT_CIRCLE: case APERTURE::AT_CIRCLE:
sprintf( text, "C,%#f*%%\n", tool->GetDiameter() * fscale ); sprintf( text, "C,%#f*%%\n", tool.GetDiameter() * fscale );
break; break;
case APERTURE::AT_RECT: case APERTURE::AT_RECT:
sprintf( text, "R,%#fX%#f*%%\n", tool->m_Size.x * fscale, sprintf( text, "R,%#fX%#f*%%\n", tool.m_Size.x * fscale,
tool->m_Size.y * fscale ); tool.m_Size.y * fscale );
break; break;
case APERTURE::AT_PLOTTING: case APERTURE::AT_PLOTTING:
sprintf( text, "C,%#f*%%\n", tool->m_Size.x * fscale ); sprintf( text, "C,%#f*%%\n", tool.m_Size.x * fscale );
break; break;
case APERTURE::AT_OVAL: case APERTURE::AT_OVAL:
sprintf( text, "O,%#fX%#f*%%\n", tool->m_Size.x * fscale, sprintf( text, "O,%#fX%#f*%%\n", tool.m_Size.x * fscale,
tool->m_Size.y * fscale ); tool.m_Size.y * fscale );
break; break;
case APERTURE::AT_REGULAR_POLY: case APERTURE::AT_REGULAR_POLY:
@ -446,8 +441,8 @@ void GERBER_PLOTTER::writeApertureList()
case APERTURE::AT_REGULAR_POLY10: case APERTURE::AT_REGULAR_POLY10:
case APERTURE::AT_REGULAR_POLY11: case APERTURE::AT_REGULAR_POLY11:
case APERTURE::AT_REGULAR_POLY12: case APERTURE::AT_REGULAR_POLY12:
sprintf( text, "P,%#fX%dX%#f*%%\n", tool->GetDiameter() * fscale, sprintf( text, "P,%#fX%dX%#f*%%\n", tool.GetDiameter() * fscale,
tool->GetVerticeCount(), tool->GetRotation() ); tool.GetVerticeCount(), tool.GetRotation() );
break; break;
} }
@ -695,7 +690,7 @@ void GERBER_PLOTTER::ThickRect( const wxPoint& p1, const wxPoint& p2, int width,
wxPoint offsetp1( p1.x - (width - currentPenWidth) / 2, wxPoint offsetp1( p1.x - (width - currentPenWidth) / 2,
p1.y - (width - currentPenWidth) / 2 ); p1.y - (width - currentPenWidth) / 2 );
wxPoint offsetp2( p2.x + (width - currentPenWidth) / 2, wxPoint offsetp2( p2.x + (width - currentPenWidth) / 2,
p2.y + (width - currentPenWidth) / 2 ); p2.y + (width - currentPenWidth) / 2 );
Rect( offsetp1, offsetp2, NO_FILL, -1 ); Rect( offsetp1, offsetp2, NO_FILL, -1 );
offsetp1.x += (width - currentPenWidth); offsetp1.x += (width - currentPenWidth);
offsetp1.y += (width - currentPenWidth); offsetp1.y += (width - currentPenWidth);
@ -858,27 +853,27 @@ void GERBER_PLOTTER::FlashPadRect( const wxPoint& pos, const wxSize& aSize,
break; break;
default: // plot pad shape as polygon default: // plot pad shape as polygon
{ {
// XXX to do: use an aperture macro to declare the rotated pad // XXX to do: use an aperture macro to declare the rotated pad
wxPoint coord[4]; wxPoint coord[4];
// coord[0] is assumed the lower left // coord[0] is assumed the lower left
// coord[1] is assumed the upper left // coord[1] is assumed the upper left
// coord[2] is assumed the upper right // coord[2] is assumed the upper right
// coord[3] is assumed the lower right // coord[3] is assumed the lower right
/* Trace the outline. */ /* Trace the outline. */
coord[0].x = -size.x/2; // lower left coord[0].x = -size.x/2; // lower left
coord[0].y = size.y/2; coord[0].y = size.y/2;
coord[1].x = -size.x/2; // upper left coord[1].x = -size.x/2; // upper left
coord[1].y = -size.y/2; coord[1].y = -size.y/2;
coord[2].x = size.x/2; // upper right coord[2].x = size.x/2; // upper right
coord[2].y = -size.y/2; coord[2].y = -size.y/2;
coord[3].x = size.x/2; // lower right coord[3].x = size.x/2; // lower right
coord[3].y = size.y/2; coord[3].y = size.y/2;
FlashPadTrapez( pos, coord, orient, trace_mode, aData ); FlashPadTrapez( pos, coord, orient, trace_mode, aData );
} }
break; break;
} }
} }

View File

@ -1281,6 +1281,17 @@ public:
*/ */
void ClearAllAttributes(); void ClearAllAttributes();
/**
* @return a index to the aperture in aperture list which meets the size and type of tool
* if the aperture does not exist, it is created and entered in aperture list
* @param aSize = the size of tool
* @param aType = the type ( shape ) of tool
* @param aApertureAttribute = an aperture attribute of the tool (a tool can have onlu one attribute)
* 0 = no specific attribute
*/
int GetOrCreateAperture( const wxSize& aSize,
APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
protected: protected:
/** /**
* Pick an existing aperture or create a new one, matching the * Pick an existing aperture or create a new one, matching the
@ -1321,20 +1332,8 @@ protected:
*/ */
void clearNetAttribute(); void clearNetAttribute();
// Remove all attributes from object attributes dictionary (TO. and TA commands) // the attributes dictionary created/modifed by %TO, attached to objects, when they are created
/** // by D01, D03, G36/G37 commands
* Function getAperture returns a reference to the aperture which meets the size anf type of tool
* if the aperture does not exist, it is created and entered in aperture list
* @param aSize = the size of tool
* @param aType = the type ( shape ) of tool
* @param aApertureAttribute = an aperture attribute of the tool (a tool can have onlu one attribute)
* 0 = no specific attribute
*/
std::vector<APERTURE>::iterator getAperture( const wxSize& aSize,
APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
// the attributes dictionary created/modifed by %TO, attached the objects, when they are created
// by D01, D03 G36/G37 commands
// standard attributes are .P, .C and .N // standard attributes are .P, .C and .N
// this is used by gerber readers when creating a new object. Cleared by %TD command // this is used by gerber readers when creating a new object. Cleared by %TD command
// Note: m_objectAttributesDictionnary can store more than one attribute // Note: m_objectAttributesDictionnary can store more than one attribute
@ -1354,8 +1353,8 @@ protected:
*/ */
void writeApertureList(); void writeApertureList();
std::vector<APERTURE> apertures; std::vector<APERTURE> m_apertures; // The list of available apertures
std::vector<APERTURE>::iterator currentAperture; int m_currentApertureIdx; // The index of the current aperture in m_apertures
bool m_gerberUnitInch; // true if the gerber units are inches, false for mm bool m_gerberUnitInch; // true if the gerber units are inches, false for mm
int m_gerberUnitFmt; // number of digits in mantissa. int m_gerberUnitFmt; // number of digits in mantissa.

View File

@ -109,31 +109,22 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
plotter.StartPlot(); plotter.StartPlot();
if( aIncludeBrdEdges ) // Some tools in P&P files have the type and size defined.
{ // they are position flash (round), pad1 flash (diamond), other pads flash (round)
brd_plotter.SetLayerSet( LSET( Edge_Cuts ) ); // and component outline thickness (polyline)
int flash_position_shape_diam = Millimeter2iu( 0.3 ); // defined size for position shape (circle)
// Plot edge layer and graphic items int pad1_mark_size = Millimeter2iu( 0.36 ); // defined size for pad 1 position (diamond)
brd_plotter.PlotBoardGraphicItems(); int other_pads_mark_size = 0; // defined size for position shape (circle)
int line_thickness = Millimeter2iu( 0.1 ); // defined size for component outlines
// Draw footprint other graphic items:
for( MODULE* footprint : fp_list )
{
for( auto item : footprint->GraphicalItems() )
{
if( item->Type() == PCB_MODULE_EDGE_T && item->GetLayer() == Edge_Cuts )
brd_plotter.Plot_1_EdgeModule( (EDGE_MODULE*) item );
}
}
}
brd_plotter.SetLayerSet( LSET( aLayer ) ); brd_plotter.SetLayerSet( LSET( aLayer ) );
int cmp_count = 0; int cmp_count = 0;
bool allowUtf8 = true; bool allowUtf8 = true;
// Plot components data: position, outlines, pad1 and other pads.
for( MODULE* footprint : fp_list ) for( MODULE* footprint : fp_list )
{ {
// Manage the aperture attributes: in drill files 3 attributes can be used: // Manage the aperture attribute component position:
GBR_METADATA gbr_metadata; GBR_METADATA gbr_metadata;
gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_POSITION ); gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_POSITION );
@ -178,8 +169,7 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
wxPoint flash_pos = footprint->GetPosition() + m_offset; wxPoint flash_pos = footprint->GetPosition() + m_offset;
int flash_diam = Millimeter2iu( 0.3 ); // arbitrary but reasonable value plotter.FlashPadCircle( flash_pos, flash_position_shape_diam, FILLED, &gbr_metadata );
plotter.FlashPadCircle( flash_pos, flash_diam, FILLED, &gbr_metadata );
gbr_metadata.m_NetlistMetadata.ClearExtraData(); gbr_metadata.m_NetlistMetadata.ClearExtraData();
// Now some extra metadata is output, avoid blindly clearing the full metadata list // Now some extra metadata is output, avoid blindly clearing the full metadata list
@ -191,7 +181,6 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
if( footprint->BuildPolyCourtyard() ) if( footprint->BuildPolyCourtyard() )
{ {
int thickness = Millimeter2iu( 0.1 ); // arbitrary but reasonable value
gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_COURTYARD ); gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_COURTYARD );
SHAPE_POLY_SET& courtyard = aLayer == B_Cu ? SHAPE_POLY_SET& courtyard = aLayer == B_Cu ?
@ -207,13 +196,12 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
poly.Move( m_offset ); poly.Move( m_offset );
useFpPadsBbox = false; useFpPadsBbox = false;
plotter.PLOTTER::PlotPoly( poly, NO_FILL, thickness, &gbr_metadata ); plotter.PLOTTER::PlotPoly( poly, NO_FILL, line_thickness, &gbr_metadata );
} }
} }
if( useFpPadsBbox ) if( useFpPadsBbox )
{ {
int thickness = Millimeter2iu( 0.1 ); // arbitrary but reasonable value
gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_FOOTPRINT ); gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_FOOTPRINT );
// bbox of fp pads, pos 0, rot 0, non flipped // bbox of fp pads, pos 0, rot 0, non flipped
@ -232,7 +220,7 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
poly.Rotate( -footprint->GetOrientationRadians(), VECTOR2I( 0, 0 ) ); poly.Rotate( -footprint->GetOrientationRadians(), VECTOR2I( 0, 0 ) );
poly.Move( footprint->GetPosition() + m_offset ); poly.Move( footprint->GetPosition() + m_offset );
plotter.PLOTTER::PlotPoly( poly, NO_FILL, thickness, &gbr_metadata ); plotter.PLOTTER::PlotPoly( poly, NO_FILL, line_thickness, &gbr_metadata );
} }
std::vector<D_PAD*>pad_key_list; std::vector<D_PAD*>pad_key_list;
@ -249,11 +237,9 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
gbr_metadata.SetPadName( pad1->GetName() ); gbr_metadata.SetPadName( pad1->GetName() );
gbr_metadata.SetNetAttribType( GBR_NETLIST_METADATA::GBR_NETINFO_PAD ); gbr_metadata.SetNetAttribType( GBR_NETLIST_METADATA::GBR_NETINFO_PAD );
// Flashes a diamond at pad position: use a slightly bigger size than the // Flashes a diamond at pad position:
// round spot to be able to see these 2 shapes when drawn at the same location plotter.FlashRegularPolygon( pad1->GetPosition() + m_offset, pad1_mark_size,
int mark_size = (flash_diam*6)/5; 4, 0.0, FILLED, &gbr_metadata );
plotter.FlashRegularPolygon( pad1->GetPosition() + m_offset, mark_size, 4,
0.0, FILLED, &gbr_metadata );
} }
} }
@ -288,8 +274,7 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
gbr_metadata.SetPadName( pad->GetName() ); gbr_metadata.SetPadName( pad->GetName() );
// Flashes a round, 0 sized round shape at pad position // Flashes a round, 0 sized round shape at pad position
int mark_size = 0; plotter.FlashPadCircle( pad->GetPosition() + m_offset, other_pads_mark_size,
plotter.FlashPadCircle( pad->GetPosition() + m_offset, mark_size,
FILLED, &gbr_metadata ); FILLED, &gbr_metadata );
} }
} }
@ -299,6 +284,26 @@ int PLACEFILE_GERBER_WRITER::CreatePlaceFile( wxString& aFullFilename,
cmp_count++; cmp_count++;
} }
// Plot board outlines, if requested
if( aIncludeBrdEdges )
{
brd_plotter.SetLayerSet( LSET( Edge_Cuts ) );
// Plot edge layer and graphic items
brd_plotter.PlotBoardGraphicItems();
// Draw footprint other graphic items:
for( MODULE* footprint : fp_list )
{
for( auto item : footprint->GraphicalItems() )
{
if( item->Type() == PCB_MODULE_EDGE_T && item->GetLayer() == Edge_Cuts )
brd_plotter.Plot_1_EdgeModule( (EDGE_MODULE*) item );
}
}
}
plotter.EndPlot(); plotter.EndPlot();
return cmp_count; return cmp_count;