From 7566aa2824e237b49307fffd2ead42fd0ab9518a Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Fri, 13 Jan 2012 19:35:46 +0100 Subject: [PATCH] Code cleaning and better comments. Gebview: fix issue in export to pcbnew (Track arc shapes are now exported, approximated by segments) --- gerbview/export_to_pcbnew.cpp | 23 +++-- pcbnew/zones_test_and_combine_areas.cpp | 8 +- polygon/PolyLine.cpp | 111 +++++++++--------------- polygon/PolyLine.h | 23 +++-- 4 files changed, 69 insertions(+), 96 deletions(-) diff --git a/gerbview/export_to_pcbnew.cpp b/gerbview/export_to_pcbnew.cpp index a4580937b7..7aca2fe892 100644 --- a/gerbview/export_to_pcbnew.cpp +++ b/gerbview/export_to_pcbnew.cpp @@ -326,35 +326,35 @@ void GBR_TO_PCB_EXPORTER::export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer ) { -#if 0 // TODO: does not work in all cases, so needs some work double a = atan2( (double)( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ), (double)( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) ); double b = atan2( (double)( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ), (double)( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) ); - int arc_angle = wxRound( ( (a - b) / M_PI * 1800.0 ) ); wxPoint start = aGbrItem->m_Start; wxPoint end = aGbrItem->m_End; /* Because Pcbnew does not know arcs in tracks, - * approximate arc by segments (16 segment per 360 deg) + * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg) + * The arc is drawn in an anticlockwise direction from the start point to the end point. */ - #define DELTA 3600/16 + #define SEG_COUNT_CIRCLE 16 + #define DELTA_ANGLE 2*M_PI/SEG_COUNT_CIRCLE - if( arc_angle < 0 ) - { - NEGATE( arc_angle ); - EXCHG( start, end ); - } + // calculate the number of segments from a to b. + // we want CNT_PER_360 segments fo a circle + if( a > b ) + b += 2*M_PI; wxPoint curr_start = start; - for( int rot = DELTA; rot < (arc_angle - DELTA); rot += DELTA ) + int ii = 1; + for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ ) { TRACK * newtrack = new TRACK( m_pcb ); newtrack->SetLayer( aLayer ); newtrack->m_Start = curr_start; wxPoint curr_end = start; - RotatePoint( &curr_end, aGbrItem->m_ArcCentre, rot ); + RotatePoint( &curr_end, aGbrItem->m_ArcCentre, -(int)(DELTA_ANGLE * ii * 1800 / M_PI) ); newtrack->m_End = curr_end; newtrack->m_Width = aGbrItem->m_Size.x; // Reverse Y axis: @@ -376,7 +376,6 @@ void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, NEGATE( newtrack->m_End.y ); m_pcb->Add( newtrack ); } -#endif } diff --git a/pcbnew/zones_test_and_combine_areas.cpp b/pcbnew/zones_test_and_combine_areas.cpp index 842bbbc4a1..a9b664612d 100644 --- a/pcbnew/zones_test_and_combine_areas.cpp +++ b/pcbnew/zones_test_and_combine_areas.cpp @@ -320,7 +320,7 @@ int BOARD::ClipAreaPolygon( PICKED_ITEMS_LIST * aNewZonesList, //** if( test == 1 ) { std::vector* pa = new std::vector; - curr_polygon->Undraw(); + curr_polygon->UnHatch(); int n_poly = aCurrArea->m_Poly->NormalizeAreaOutlines( pa, bRetainArcs ); // i.e if clipping has created some polygons, we must add these new copper areas. @@ -339,12 +339,12 @@ int BOARD::ClipAreaPolygon( PICKED_ITEMS_LIST * aNewZonesList, // and replace it with a poly from NormalizeAreaOutlines delete NewArea->m_Poly; NewArea->m_Poly = new_p; - NewArea->m_Poly->Draw(); + NewArea->m_Poly->Hatch(); NewArea->utility = 1; } } - curr_polygon->Draw(); + curr_polygon->Hatch(); delete pa; } @@ -901,7 +901,7 @@ int BOARD::CombineAreas( PICKED_ITEMS_LIST* aDeletedList, ZONE_CONTAINER* area_r area_ref->utility = 1; area_ref->m_Poly->RestoreArcs( &arc_array1 ); area_ref->m_Poly->RestoreArcs( &arc_array2 ); - area_ref->m_Poly->Draw(); + area_ref->m_Poly->Hatch(); delete booleng; return 1; } diff --git a/polygon/PolyLine.cpp b/polygon/PolyLine.cpp index c109da85eb..cf40aec209 100644 --- a/polygon/PolyLine.cpp +++ b/polygon/PolyLine.cpp @@ -5,12 +5,12 @@ // #include #include +#include #include "fctsys.h" -#include "config.h" +#include "config.h" // to define KICAD_NANOMETRE #include "PolyLine.h" -#include "gr_basic.h" #include "bezier_curves.h" #include "polygon_test_point_inside.h" @@ -44,7 +44,7 @@ CPolyLine::CPolyLine() // CPolyLine::~CPolyLine() { - Undraw(); + UnHatch(); if( m_Kbool_Poly_Engine ) delete m_Kbool_Poly_Engine; } @@ -81,7 +81,7 @@ int CPolyLine::NormalizeWithKbool( std::vector * aExtraPolyList, boo else MakeKboolPoly( -1, -1, NULL ); - Undraw(); + UnHatch(); /* now, recreate polys * if more than one outside contour are found, extra CPolyLines will be created @@ -649,7 +649,7 @@ int CPolyLine::RestoreArcs( std::vector * arc_array, std::vectorUndraw(); + poly->UnHatch(); for( int ic = 0; icGetNumCorners(); ic++ ) poly->SetUtility( ic, 0 ); @@ -805,7 +805,7 @@ void CPolyLine::Start( int layer, int x, int y, int hatch ) // void CPolyLine::AppendCorner( int x, int y, int style, bool bDraw ) { - Undraw(); + UnHatch(); CPolyPt poly_pt( x, y ); poly_pt.end_contour = FALSE; @@ -815,7 +815,7 @@ void CPolyLine::AppendCorner( int x, int y, int style, bool bDraw ) if( corner.size() > 0 && !corner[corner.size() - 1].end_contour ) side_style[corner.size() - 1] = style; if( bDraw ) - Draw(); + Hatch(); } @@ -827,11 +827,11 @@ void CPolyLine::Close( int style, bool bDraw ) { wxASSERT( 0 ); } - Undraw(); + UnHatch(); side_style[corner.size() - 1] = style; corner[corner.size() - 1].end_contour = TRUE; if( bDraw ) - Draw(); + Hatch(); } @@ -839,10 +839,10 @@ void CPolyLine::Close( int style, bool bDraw ) // void CPolyLine::MoveCorner( int ic, int x, int y ) { - Undraw(); + UnHatch(); corner[ic].x = x; corner[ic].y = y; - Draw(); + Hatch(); } @@ -850,7 +850,7 @@ void CPolyLine::MoveCorner( int ic, int x, int y ) // void CPolyLine::DeleteCorner( int ic, bool bDraw ) { - Undraw(); + UnHatch(); int icont = GetContour( ic ); int istart = GetContourStart( icont ); int iend = GetContourEnd( icont ); @@ -878,7 +878,7 @@ void CPolyLine::DeleteCorner( int ic, bool bDraw ) RemoveContour( icont ); } if( bDraw ) - Draw(); + Hatch(); } @@ -892,7 +892,7 @@ void CPolyLine::RemoveContour( int icont ) * remove a contour only if there is more than 1 contour */ { - Undraw(); + UnHatch(); int istart = GetContourStart( icont ); int iend = GetContourEnd( icont ); @@ -916,7 +916,7 @@ void CPolyLine::RemoveContour( int icont ) side_style.erase( side_style.begin() + ic ); } } - Draw(); + Hatch(); } @@ -1140,7 +1140,7 @@ void CPolyLine::RemoveAllContours( void ) */ void CPolyLine::InsertCorner( int ic, int x, int y ) { - Undraw(); + UnHatch(); if( (unsigned) (ic) >= corner.size() ) { corner.push_back( CPolyPt( x, y ) ); @@ -1160,31 +1160,15 @@ void CPolyLine::InsertCorner( int ic, int x, int y ) corner[ic].end_contour = FALSE; } } - Draw(); + Hatch(); } // undraw polyline by removing all graphic elements from display list // -void CPolyLine::Undraw() +void CPolyLine::UnHatch() { m_HatchLines.clear(); - bDrawn = FALSE; -} - - -// draw polyline by adding all graphics to display list -// if side style is ARC_CW or ARC_CCW but endpoints are not angled, -// convert to STRAIGHT -// -void CPolyLine::Draw() -{ - // first, undraw if necessary - if( bDrawn ) - Undraw(); - - Hatch(); - bDrawn = TRUE; } @@ -1226,10 +1210,10 @@ CRect CPolyLine::GetCornerBounds() r.right = r.top = INT_MIN; for( unsigned i = 0; i 2 ) - { - for( unsigned istart = 0; istart < (pointbuffer.size() - 1); istart++ ) - { - int max_x = INT_MIN; - int imax = INT_MIN; - for( unsigned i = istart; i < pointbuffer.size(); i++ ) - { - if( pointbuffer[i].x > max_x ) - { - max_x = pointbuffer[i].x; - imax = i; - } - } + sort( pointbuffer.begin(), pointbuffer.end(), sort_ends_by_descending_X ); - CPoint temp = pointbuffer[istart]; - pointbuffer[istart] = pointbuffer[imax]; - pointbuffer[imax] = temp; - } - } - - // creates lines + // creates lines or short segments inside the complex polygon for( unsigned ip = 0; ip < pointbuffer.size(); ip += 2 ) { double dx = pointbuffer[ip + 1].x - pointbuffer[ip].x; @@ -1609,7 +1584,7 @@ bool CPolyLine::TestPointInside( int px, int py ) // void CPolyLine::Copy( CPolyLine* src ) { - Undraw(); + UnHatch(); m_HatchStyle = src->m_HatchStyle; // copy corners, using vector copy corner = src->corner; @@ -1636,19 +1611,19 @@ bool CPolyLine::IsCutoutContour( int icont ) void CPolyLine::MoveOrigin( int x_off, int y_off ) { - Undraw(); + UnHatch(); for( int ic = 0; ic < GetNumCorners(); ic++ ) { SetX( ic, GetX( ic ) + x_off ); SetY( ic, GetY( ic ) + y_off ); } - Draw(); + Hatch(); } // Set various parameters: -// the calling function should Undraw() before calling them, +// the calling function should UnHatch() before calling them, // and Draw() after // void CPolyLine::SetX( int ic, int x ) diff --git a/polygon/PolyLine.h b/polygon/PolyLine.h index aa30eeede7..2d9b01e793 100644 --- a/polygon/PolyLine.h +++ b/polygon/PolyLine.h @@ -140,10 +140,11 @@ public: void RemoveAllContours( void ); - // drawing functions - void Undraw(); - void Draw(); + // Remove or create hatch + void UnHatch(); void Hatch(); + + // Transform functions void MoveOrigin( int x_off, int y_off ); // misc. functions @@ -174,7 +175,7 @@ public: int GetSideStyle( int is ); int GetHatchStyle() { return m_HatchStyle; } - void SetHatch( int hatch ) { Undraw(); m_HatchStyle = hatch; Draw(); }; + void SetHatch( int hatch ) { m_HatchStyle = hatch; Hatch(); }; void SetX( int ic, int x ); void SetY( int ic, int y ); void SetEndContour( int ic, bool end_contour ); @@ -246,24 +247,22 @@ public: */ void FreeKboolEngine( ) { delete m_Kbool_Poly_Engine; m_Kbool_Poly_Engine = NULL; } + // Bezier Support + void AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3); + void AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); + private: int m_layer; // layer to draw on int m_Width; // lines width when drawing. Provided but not really used int utility; + Bool_Engine* m_Kbool_Poly_Engine; // polygons set in kbool engine data + public: std::vector corner; // array of points for corners std::vector side_style; // array of styles for sides int m_HatchStyle; // hatch style, see enum above std::vector m_HatchLines; // hatch lines -private: - Bool_Engine* m_Kbool_Poly_Engine; // polygons set in kbool engine data - bool bDrawn; - - // Bezier Support -public: - void AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3); - void AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); }; #endif // #ifndef POLYLINE_H