Gerbview: more code cleanup. remove duplicate code.

This commit is contained in:
jean-pierre charras 2023-02-10 16:10:29 +01:00
parent 00374f0a75
commit 79d246b8fe
4 changed files with 68 additions and 111 deletions

View File

@ -55,7 +55,7 @@ static VECTOR2I mapPt( double x, double y, bool isMetric )
} }
bool AM_PRIMITIVE::IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) const bool AM_PRIMITIVE::IsAMPrimitiveExposureOn( const D_CODE* aDcode ) const
{ {
/* /*
* Some but not all primitives use the first parameter as an exposure control. * Some but not all primitives use the first parameter as an exposure control.
@ -75,12 +75,11 @@ bool AM_PRIMITIVE::IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) co
case AMP_OUTLINE: case AMP_OUTLINE:
case AMP_POLYGON: case AMP_POLYGON:
// All have an exposure parameter and can return a value (0 or 1) // All have an exposure parameter and can return a value (0 or 1)
return m_Params[0].GetValue( aParent->GetDcodeDescr() ) != 0; return m_Params[0].GetValue( aDcode ) != 0;
break; break;
case AMP_THERMAL: // Exposure is always on case AMP_THERMAL: // Exposure is always on
case AMP_MOIRE: // Exposure is always on case AMP_MOIRE: // Exposure is always on
case AMP_EOF:
case AMP_UNKNOWN: case AMP_UNKNOWN:
default: default:
return 1; // All have no exposure parameter and are always 0N return true return 1; // All have no exposure parameter and are always 0N return true
@ -93,29 +92,15 @@ bool AM_PRIMITIVE::IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) co
const int seg_per_circle = 64; // Number of segments to approximate a circle const int seg_per_circle = 64; // Number of segments to approximate a circle
void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent, SHAPE_POLY_SET& aShapeBuffer, void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const D_CODE* aDcode,
const VECTOR2I& aShapePos ) SHAPE_POLY_SET& aShapeBuffer )
{ {
#define TO_POLY_SHAPE \
{ \
if( polybuffer.size() > 1 ) \
{ \
aShapeBuffer.NewOutline(); \
\
for( unsigned jj = 0; jj < polybuffer.size(); jj++ ) \
aShapeBuffer.Append( polybuffer[jj].x, polybuffer[jj].y ); \
\
aShapeBuffer.Append( polybuffer[0].x, polybuffer[0].y ); \
} \
}
// Draw the primitive shape for flashed items. // Draw the primitive shape for flashed items.
// Create a static buffer to avoid a lot of memory reallocation. // Create a static buffer to avoid a lot of memory reallocation.
static std::vector<VECTOR2I> polybuffer; static std::vector<VECTOR2I> polybuffer;
polybuffer.clear(); polybuffer.clear();
VECTOR2I curPos = aShapePos; const D_CODE* tool = aDcode;
D_CODE* tool = aParent->GetDcodeDescr();
switch( m_Primitive_id ) switch( m_Primitive_id )
{ {
@ -127,7 +112,7 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* <rotation> is a optional parameter: rotation from origin. * <rotation> is a optional parameter: rotation from origin.
* type is not stored in parameters list, so the first parameter is exposure * type is not stored in parameters list, so the first parameter is exposure
*/ */
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
// shape rotation (if any): // shape rotation (if any):
if( m_Params.size() >= 5 ) if( m_Params.size() >= 5 )
@ -141,15 +126,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
} }
} }
// Move to current position:
for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
}
TO_POLY_SHAPE;
break; break;
} }
@ -165,7 +141,7 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* type (2), exposure, width, start.x, start.y, end.x, end.y, rotation * type (2), exposure, width, start.x, start.y, end.x, end.y, rotation
* type is not stored in parameters list, so the first parameter is exposure * type is not stored in parameters list, so the first parameter is exposure
*/ */
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
// shape rotation: // shape rotation:
EDA_ANGLE rotation( m_Params[6].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[6].GetValue( tool ), DEGREES_T );
@ -176,14 +152,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
} }
// Move to current position:
for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
}
TO_POLY_SHAPE;
break; break;
} }
@ -197,7 +165,7 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* type (21), exposure, ,width, height, center pos.x, center pos.y, rotation * type (21), exposure, ,width, height, center pos.x, center pos.y, rotation
* type is not stored in parameters list, so the first parameter is exposure * type is not stored in parameters list, so the first parameter is exposure
*/ */
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
// shape rotation: // shape rotation:
EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T );
@ -208,14 +176,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
} }
// Move to current position:
for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
}
TO_POLY_SHAPE;
break; break;
} }
@ -226,7 +186,7 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* type (22), exposure, ,width, height, corner pos.x, corner pos.y, rotation * type (22), exposure, ,width, height, corner pos.x, corner pos.y, rotation
* type is not stored in parameters list, so the first parameter is exposure * type is not stored in parameters list, so the first parameter is exposure
*/ */
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
// shape rotation: // shape rotation:
EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T );
@ -236,15 +196,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
for( unsigned ii = 0; ii < polybuffer.size(); ii++ ) for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
} }
// Move to current position:
for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
}
TO_POLY_SHAPE;
break; break;
} }
@ -259,8 +210,9 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* on. * on.
*/ */
std::vector<VECTOR2I> subshape_poly; std::vector<VECTOR2I> subshape_poly;
curPos += mapPt( m_Params[0].GetValue( tool ), m_Params[1].GetValue( tool ), m_GerbMetric ); VECTOR2I center( mapPt( m_Params[0].GetValue( tool ),
ConvertShapeToPolygon( aParent, subshape_poly ); m_Params[1].GetValue( tool ), m_GerbMetric ) );
ConvertShapeToPolygon( tool, subshape_poly );
// shape rotation: // shape rotation:
EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T );
@ -275,14 +227,18 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
for( unsigned jj = 0; jj < polybuffer.size(); jj++ ) for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
RotatePoint( polybuffer[jj], -sub_rotation ); RotatePoint( polybuffer[jj], -sub_rotation );
// Move to current position: // Move to center position given by the tool:
for( unsigned jj = 0; jj < polybuffer.size(); jj++ ) for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
{ {
polybuffer[jj] += curPos; polybuffer[jj] += center;
polybuffer[jj] = aParent->GetABPosition( polybuffer[jj] );
} }
TO_POLY_SHAPE; aShapeBuffer.NewOutline();
for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
aShapeBuffer.Append( polybuffer[jj] );
aShapeBuffer.Append( polybuffer[0] );
} }
} }
break; break;
@ -293,8 +249,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* The moire primitive is a cross hair centered on concentric rings (annuli). * The moire primitive is a cross hair centered on concentric rings (annuli).
* Exposure is always on. * Exposure is always on.
*/ */
curPos += mapPt( m_Params[0].GetValue( tool ), m_Params[1].GetValue( tool ),
m_GerbMetric );
/* Generated by an aperture macro declaration like: /* Generated by an aperture macro declaration like:
* "6,0,0,0.125,.01,0.01,3,0.003,0.150,0" * "6,0,0,0.125,.01,0.01,3,0.003,0.150,0"
@ -307,8 +261,9 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
int gap = scaletoIU( m_Params[4].GetValue( tool ), m_GerbMetric ); int gap = scaletoIU( m_Params[4].GetValue( tool ), m_GerbMetric );
int numCircles = KiROUND( m_Params[5].GetValue( tool ) ); int numCircles = KiROUND( m_Params[5].GetValue( tool ) );
// Draw circles: // Draw circles @ position pos.x, pos.y given by the tool:
VECTOR2I center = aParent->GetABPosition( curPos ); VECTOR2I center( mapPt( m_Params[0].GetValue( tool ), m_Params[1].GetValue( tool ),
m_GerbMetric ) );
// adjust outerDiam by this on each nested circle // adjust outerDiam by this on each nested circle
int diamAdjust = ( gap + penThickness ) * 2; int diamAdjust = ( gap + penThickness ) * 2;
@ -334,21 +289,17 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
} }
// Draw the cross: // Draw the cross:
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
EDA_ANGLE rotation( m_Params[8].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[8].GetValue( tool ), DEGREES_T );
for( unsigned ii = 0; ii < polybuffer.size(); ii++ ) for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{ {
// shape rotation: // move crossair shape to center and rotate shape:
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
polybuffer[ii] += center;
// Move to current position:
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
} }
TO_POLY_SHAPE;
break; break;
} }
@ -405,14 +356,6 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
} }
// Move to current position:
for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{
polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
}
TO_POLY_SHAPE;
break; break;
} }
@ -427,41 +370,45 @@ void AM_PRIMITIVE::ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
* type(5), exposure, vertices count, pox.x, pos.y, diameter, rotation * type(5), exposure, vertices count, pox.x, pos.y, diameter, rotation
* type is not stored in parameters list, so the first parameter is exposure * type is not stored in parameters list, so the first parameter is exposure
*/ */
curPos += mapPt( m_Params[2].GetValue( tool ), m_Params[3].GetValue( tool ), m_GerbMetric ); VECTOR2I curPos( mapPt( m_Params[2].GetValue( tool ), m_Params[3].GetValue( tool ), m_GerbMetric ) );
// Creates the shape: // Creates the shape:
ConvertShapeToPolygon( aParent, polybuffer ); ConvertShapeToPolygon( tool, polybuffer );
// rotate polygon and move it to the actual position // rotate polygon
EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T ); EDA_ANGLE rotation( m_Params[5].GetValue( tool ), DEGREES_T );
for( unsigned ii = 0; ii < polybuffer.size(); ii++ ) for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
{ {
RotatePoint( polybuffer[ii], -rotation ); RotatePoint( polybuffer[ii], -rotation );
polybuffer[ii] += curPos; polybuffer[ii] += curPos;
polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
} }
TO_POLY_SHAPE;
break; break;
} }
case AMP_EOF: case AMP_COMMENT:
// not yet supported, waiting for you.
break;
case AMP_UNKNOWN: case AMP_UNKNOWN:
default:
break; break;
} }
if( polybuffer.size() > 1 ) // a valid polygon has more than 1 corner
{
aShapeBuffer.NewOutline();
for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
aShapeBuffer.Append( polybuffer[jj] );
// Close the shape:
aShapeBuffer.Append( polybuffer[0] );
}
} }
void AM_PRIMITIVE::ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent, void AM_PRIMITIVE::ConvertShapeToPolygon( const D_CODE* aDcode,
std::vector<VECTOR2I>& aBuffer ) std::vector<VECTOR2I>& aBuffer )
{ {
D_CODE* tool = aParent->GetDcodeDescr(); const D_CODE* tool = aDcode;
switch( m_Primitive_id ) switch( m_Primitive_id )
{ {
@ -688,7 +635,6 @@ void AM_PRIMITIVE::ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
case AMP_COMMENT: case AMP_COMMENT:
case AMP_UNKNOWN: case AMP_UNKNOWN:
case AMP_EOF:
break; break;
} }
} }

View File

@ -75,11 +75,10 @@ enum AM_PRIMITIVE_ID {
AMP_LINE20 = 20, // Same as AMP_LINE2 AMP_LINE20 = 20, // Same as AMP_LINE2
AMP_LINE_CENTER = 21, // Rectangle. (height, width and center pos + rotation) AMP_LINE_CENTER = 21, // Rectangle. (height, width and center pos + rotation)
AMP_LINE_LOWER_LEFT = 22, // Rectangle. (height, width and left bottom corner pos + rotation) AMP_LINE_LOWER_LEFT = 22, // Rectangle. (height, width and left bottom corner pos + rotation)
AMP_EOF = 3, // End Of File marker: not really a shape
AMP_OUTLINE = 4, // Free polyline (n corners + rotation) AMP_OUTLINE = 4, // Free polyline (n corners + rotation)
AMP_POLYGON = 5, // Closed regular polygon(diameter, number of vertices (3 to 10), AMP_POLYGON = 5, // Closed regular polygon(diameter, number of vertices (3 to 10),
// rotation) // rotation)
AMP_MOIRE = 6, // A cross hair with n concentric circles + rotation AMP_MOIRE = 6, // A cross hair with n concentric circles + rotation (deprecated in 2021)
AMP_THERMAL = 7 // Thermal shape (pos, outer and inner diameter, cross hair AMP_THERMAL = 7 // Thermal shape (pos, outer and inner diameter, cross hair
// thickness + rotation) // thickness + rotation)
}; };
@ -118,7 +117,7 @@ public:
* In a aperture macro shape, a basic primitive with exposure off is a hole in the shape * In a aperture macro shape, a basic primitive with exposure off is a hole in the shape
* it is NOT a negative shape * it is NOT a negative shape
*/ */
bool IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) const; bool IsAMPrimitiveExposureOn( const D_CODE* aDcode ) const;
/** /**
* Generate the polygonal shape of the primitive shape of an aperture * Generate the polygonal shape of the primitive shape of an aperture
@ -128,9 +127,8 @@ public:
* @param aShapeBuffer is a SHAPE_POLY_SET to put the shape converted to a polygon. * @param aShapeBuffer is a SHAPE_POLY_SET to put the shape converted to a polygon.
* @param aShapePos is the actual shape position. * @param aShapePos is the actual shape position.
*/ */
void ConvertBasicShapeToPolygon( const GERBER_DRAW_ITEM* aParent, void ConvertBasicShapeToPolygon( const D_CODE* aDcode,
SHAPE_POLY_SET& aShapeBuffer, SHAPE_POLY_SET& aShapeBuffer );
const VECTOR2I& aShapePos );
private: private:
/** /**
@ -143,7 +141,7 @@ private:
* converted because circles are very easy to draw (no rotation problem) so convert * converted because circles are very easy to draw (no rotation problem) so convert
* them in polygons and draw them as polygons is not a good idea. * them in polygons and draw them as polygons is not a good idea.
*/ */
void ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent, std::vector<VECTOR2I>& aBuffer ); void ConvertShapeToPolygon( const D_CODE* aDcode, std::vector<VECTOR2I>& aBuffer );
}; };

View File

@ -30,6 +30,7 @@
#include <gerbview.h> #include <gerbview.h>
#include <aperture_macro.h> #include <aperture_macro.h>
#include <gerber_draw_item.h>
void APERTURE_MACRO::AddPrimitiveToList( AM_PRIMITIVE& aPrimitive ) void APERTURE_MACRO::AddPrimitiveToList( AM_PRIMITIVE& aPrimitive )
@ -56,19 +57,20 @@ SHAPE_POLY_SET* APERTURE_MACRO::GetApertureMacroShape( const GERBER_DRAW_ITEM* a
SHAPE_POLY_SET holeBuffer; SHAPE_POLY_SET holeBuffer;
m_shape.RemoveAllContours(); m_shape.RemoveAllContours();
D_CODE * dcode = aParent->GetDcodeDescr();
for( AM_PRIMITIVE& prim_macro : m_primitivesList ) for( AM_PRIMITIVE& prim_macro : m_primitivesList )
{ {
if( prim_macro.m_Primitive_id == AMP_COMMENT ) if( prim_macro.m_Primitive_id == AMP_COMMENT )
continue; continue;
if( prim_macro.IsAMPrimitiveExposureOn( aParent ) ) if( prim_macro.IsAMPrimitiveExposureOn( dcode ) )
{ {
prim_macro.ConvertBasicShapeToPolygon( aParent, m_shape, aShapePos ); prim_macro.ConvertBasicShapeToPolygon( dcode, m_shape );
} }
else else
{ {
prim_macro.ConvertBasicShapeToPolygon( aParent, holeBuffer, aShapePos ); prim_macro.ConvertBasicShapeToPolygon( dcode, holeBuffer );
if( holeBuffer.OutlineCount() ) // we have a new hole in shape: remove the hole if( holeBuffer.OutlineCount() ) // we have a new hole in shape: remove the hole
{ {
@ -86,6 +88,21 @@ SHAPE_POLY_SET* APERTURE_MACRO::GetApertureMacroShape( const GERBER_DRAW_ITEM* a
// (i.e link holes by overlapping edges) // (i.e link holes by overlapping edges)
m_shape.Fracture( SHAPE_POLY_SET::PM_FAST ); m_shape.Fracture( SHAPE_POLY_SET::PM_FAST );
// Move m_shape to the actual draw position:
for( int icnt = 0; icnt < m_shape.OutlineCount(); icnt++ )
{
SHAPE_LINE_CHAIN& outline = m_shape.Outline( icnt );
for( int jj = 0; jj < outline.PointCount(); jj++ )
{
VECTOR2I point = outline.CPoint( jj );
point += aShapePos;
point = aParent->GetABPosition( point );
outline.SetPoint( jj, point );
}
}
return &m_shape; return &m_shape;
} }

View File

@ -1068,10 +1068,6 @@ bool GERBER_FILE_IMAGE::ReadApertureMacro( char *aBuff, unsigned int aBuffSize,
paramCount = 6; paramCount = 6;
break; break;
case AMP_EOF:
paramCount = 0;
break;
case AMP_OUTLINE: case AMP_OUTLINE:
paramCount = 4; // partial count. other parameters are vertices and rotation paramCount = 4; // partial count. other parameters are vertices and rotation
// Second parameter is vertice (coordinate pairs) count. // Second parameter is vertice (coordinate pairs) count.