KiCad PCB EDA Suite
class_module.cpp File Reference

MODULE class implementation. More...

#include <fctsys.h>
#include <gr_basic.h>
#include <plotter.h>
#include <class_drawpanel.h>
#include <trigo.h>
#include <confirm.h>
#include <kicad_string.h>
#include <pcbnew.h>
#include <richio.h>
#include <filter_reader.h>
#include <macros.h>
#include <msgpanel.h>
#include <bitmaps.h>
#include <unordered_set>
#include <pcb_edit_frame.h>
#include <class_board.h>
#include <class_edge_mod.h>
#include <class_module.h>
#include <convert_basic_shapes_to_polygon.h>
#include <view/view.h>

Go to the source code of this file.

Functions

static double polygonArea (SHAPE_POLY_SET &aPolySet)
 
static void addRect (SHAPE_POLY_SET &aPolySet, wxRect aRect)
 
bool ConvertOutlineToPolygon (std::vector< DRAWSEGMENT * > &aSegList, SHAPE_POLY_SET &aPolygons, wxString *aErrorText, unsigned int aTolerance, wxPoint *aErrorLocation=nullptr)
 Function ConvertOutlineToPolygon build a polygon (with holes) from a DRAWSEGMENT list, which is expected to be a outline, therefore a closed main outline with perhaps closed inner outlines. More...
 

Detailed Description

MODULE class implementation.

TEXT_MODULE class implementation.

Definition in file class_module.cpp.

Function Documentation

static void addRect ( SHAPE_POLY_SET aPolySet,
wxRect  aRect 
)
static

Definition at line 1391 of file class_module.cpp.

References SHAPE_POLY_SET::Append(), and SHAPE_POLY_SET::NewOutline().

Referenced by MODULE::CoverageRatio().

1392 {
1393  aPolySet.NewOutline();
1394 
1395  aPolySet.Append( aRect.GetX(), aRect.GetY() );
1396  aPolySet.Append( aRect.GetX()+aRect.width, aRect.GetY() );
1397  aPolySet.Append( aRect.GetX()+aRect.width, aRect.GetY()+aRect.height );
1398  aPolySet.Append( aRect.GetX(), aRect.GetY()+aRect.height );
1399 }
int NewOutline()
Creates a new empty polygon in the set and returns its index
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
bool ConvertOutlineToPolygon ( std::vector< DRAWSEGMENT * > &  aSegList,
SHAPE_POLY_SET aPolygons,
wxString *  aErrorText,
unsigned int  aTolerance,
wxPoint *  aErrorLocation 
)

Function ConvertOutlineToPolygon build a polygon (with holes) from a DRAWSEGMENT list, which is expected to be a outline, therefore a closed main outline with perhaps closed inner outlines.

These closed inner outlines are considered as holes in the main outline

Parameters
aSegListthe initial list of drawsegments (only lines, circles and arcs).
aPolygonswill contain the complex polygon.
aToleranceis the max distance between points that is still accepted as connected (internal units)
aErrorTextis a wxString to return error message.
aErrorLocationis the optional position of the error in the outline

Definition at line 183 of file convert_drawsegment_list_to_polygon.cpp.

References PNS::angle(), SHAPE_POLY_SET::Append(), SHAPE_POLY_SET::CIterate(), close_enough(), close_st(), findPoint(), DRAWSEGMENT::GetAngle(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcStart(), GetArcToSegmentCount(), DRAWSEGMENT::GetBezierPoints(), DRAWSEGMENT::GetCenter(), DRAWSEGMENT::GetEnd(), MODULE::GetOrientation(), DRAWSEGMENT::GetParentModule(), DRAWSEGMENT::GetPolyShape(), DRAWSEGMENT::GetPosition(), MODULE::GetPosition(), DRAWSEGMENT::GetRadius(), DRAWSEGMENT::GetShape(), DRAWSEGMENT::GetStart(), DRAWSEGMENT::GetWidth(), i, MILLIMETRES, SHAPE_POLY_SET::NewHole(), SHAPE_POLY_SET::NewOutline(), DRAWSEGMENT::RebuildBezierToSegmentsPointsList(), reverse(), RotatePoint(), S_ARC, S_CIRCLE, S_CURVE, S_POLYGON, S_SEGMENT, BOARD_ITEM::ShowShape(), StringFromValue(), TransformCircleToPolygon(), and VECTOR2< T >::x.

Referenced by BuildBoardPolygonOutlines(), MODULE::BuildPolyCourtyard(), and MODULE::CoverageRatio().

185 {
186  if( aSegList.size() == 0 )
187  return true;
188 
189  wxString msg;
190 
191  // Make a working copy of aSegList, because the list is modified during calculations
192  std::vector< DRAWSEGMENT* > segList = aSegList;
193 
194  DRAWSEGMENT* graphic;
195  wxPoint prevPt;
196 
197  // Find edge point with minimum x, this should be in the outer polygon
198  // which will define the perimeter Edge.Cuts polygon.
199  wxPoint xmin = wxPoint( INT_MAX, 0 );
200  int xmini = 0;
201 
202  for( size_t i = 0; i < segList.size(); i++ )
203  {
204  graphic = (DRAWSEGMENT*) segList[i];
205 
206  switch( graphic->GetShape() )
207  {
208  case S_SEGMENT:
209  {
210  if( graphic->GetStart().x < xmin.x )
211  {
212  xmin = graphic->GetStart();
213  xmini = i;
214  }
215 
216  if( graphic->GetEnd().x < xmin.x )
217  {
218  xmin = graphic->GetEnd();
219  xmini = i;
220  }
221  }
222  break;
223 
224  case S_ARC:
225  // Freerouter does not yet understand arcs, so approximate
226  // an arc with a series of short lines and put those
227  // line segments into the !same! PATH.
228  {
229  wxPoint pstart = graphic->GetArcStart();
230  wxPoint center = graphic->GetCenter();
231  double angle = -graphic->GetAngle();
232  double radius = graphic->GetRadius();
233  int steps = GetArcToSegmentCount( radius, ARC_LOW_DEF, angle / 10.0 );
234  wxPoint pt;
235 
236  for( int step = 1; step<=steps; ++step )
237  {
238  double rotation = ( angle * step ) / steps;
239 
240  pt = pstart;
241 
242  RotatePoint( &pt, center, rotation );
243 
244  if( pt.x < xmin.x )
245  {
246  xmin = pt;
247  xmini = i;
248  }
249  }
250  }
251  break;
252 
253  case S_CIRCLE:
254  {
255  wxPoint pt = graphic->GetCenter();
256 
257  // pt has minimum x point
258  pt.x -= graphic->GetRadius();
259 
260  // when the radius <= 0, this is a mal-formed circle. Skip it
261  if( graphic->GetRadius() > 0 && pt.x < xmin.x )
262  {
263  xmin = pt;
264  xmini = i;
265  }
266  }
267  break;
268 
269  case S_CURVE:
270  {
271  graphic->RebuildBezierToSegmentsPointsList( graphic->GetWidth() );
272 
273  for( unsigned int jj = 0; jj < graphic->GetBezierPoints().size(); jj++ )
274  {
275  wxPoint pt = graphic->GetBezierPoints()[jj];
276 
277  if( pt.x < xmin.x )
278  {
279  xmin = pt;
280  xmini = i;
281  }
282  }
283  }
284  break;
285 
286  case S_POLYGON:
287  {
288  const auto poly = graphic->GetPolyShape();
289  MODULE* module = aSegList[0]->GetParentModule();
290  double orientation = module ? module->GetOrientation() : 0.0;
291  VECTOR2I offset = module ? module->GetPosition() : VECTOR2I( 0, 0 );
292 
293  for( auto iter = poly.CIterate(); iter; iter++ )
294  {
295  auto pt = *iter;
296  RotatePoint( pt, orientation );
297  pt += offset;
298 
299  if( pt.x < xmin.x )
300  {
301  xmin.x = pt.x;
302  xmin.y = pt.y;
303  xmini = i;
304  }
305  }
306  }
307  break;
308  default:
309  break;
310  }
311  }
312 
313  // Grab the left most point, assume its on the board's perimeter, and see if we
314  // can put enough graphics together by matching endpoints to formulate a cohesive
315  // polygon.
316 
317  graphic = (DRAWSEGMENT*) segList[xmini];
318 
319  // The first DRAWSEGMENT is in 'graphic', ok to remove it from 'items'
320  segList.erase( segList.begin() + xmini );
321 
322  // Output the Edge.Cuts perimeter as circle or polygon.
323  if( graphic->GetShape() == S_CIRCLE )
324  {
325  int steps = GetArcToSegmentCount( graphic->GetRadius(), ARC_LOW_DEF, 360.0 );
326  TransformCircleToPolygon( aPolygons, graphic->GetCenter(), graphic->GetRadius(), steps );
327  }
328  else if( graphic->GetShape() == S_POLYGON )
329  {
330  MODULE* module = graphic->GetParentModule(); // NULL for items not in footprints
331  double orientation = module ? module->GetOrientation() : 0.0;
332  VECTOR2I offset = module ? module->GetPosition() : VECTOR2I( 0, 0 );
333 
334  aPolygons.NewOutline();
335 
336  for( auto it = graphic->GetPolyShape().CIterate( 0 ); it; it++ )
337  {
338  auto pt = *it;
339  RotatePoint( pt, orientation );
340  pt += offset;
341  aPolygons.Append( pt );
342  }
343  }
344  else
345  {
346  // Polygon start point. Arbitrarily chosen end of the
347  // segment and build the poly from here.
348 
349  wxPoint startPt = wxPoint( graphic->GetEnd() );
350  prevPt = graphic->GetEnd();
351  aPolygons.NewOutline();
352  aPolygons.Append( prevPt );
353 
354  // Do not append the other end point yet of this 'graphic', this first
355  // 'graphic' might be an arc or a curve.
356 
357  for(;;)
358  {
359  switch( graphic->GetShape() )
360  {
361  case S_SEGMENT:
362  {
363  wxPoint nextPt;
364 
365  // Use the line segment end point furthest away from
366  // prevPt as we assume the other end to be ON prevPt or
367  // very close to it.
368 
369  if( close_st( prevPt, graphic->GetStart(), graphic->GetEnd() ) )
370  nextPt = graphic->GetEnd();
371  else
372  nextPt = graphic->GetStart();
373 
374  aPolygons.Append( nextPt );
375  prevPt = nextPt;
376  }
377  break;
378 
379  case S_ARC:
380  // We do not support arcs in polygons, so approximate
381  // an arc with a series of short lines and put those
382  // line segments into the !same! PATH.
383  {
384  wxPoint pstart = graphic->GetArcStart();
385  wxPoint pend = graphic->GetArcEnd();
386  wxPoint pcenter = graphic->GetCenter();
387  double angle = -graphic->GetAngle();
388  double radius = graphic->GetRadius();
389  int steps = GetArcToSegmentCount( radius, ARC_LOW_DEF, angle / 10.0 );
390 
391  if( !close_enough( prevPt, pstart, aTolerance ) )
392  {
393  wxASSERT( close_enough( prevPt, graphic->GetArcEnd(), aTolerance ) );
394 
395  angle = -angle;
396  std::swap( pstart, pend );
397  }
398 
399  wxPoint nextPt;
400 
401  for( int step = 1; step<=steps; ++step )
402  {
403  double rotation = ( angle * step ) / steps;
404  nextPt = pstart;
405  RotatePoint( &nextPt, pcenter, rotation );
406 
407  aPolygons.Append( nextPt );
408  }
409 
410  prevPt = nextPt;
411  }
412  break;
413 
414  case S_CURVE:
415  // We do not support Bezier curves in polygons, so approximate
416  // with a series of short lines and put those
417  // line segments into the !same! PATH.
418  {
419  wxPoint nextPt;
420  bool reverse = false;
421 
422  // Use the end point furthest away from
423  // prevPt as we assume the other end to be ON prevPt or
424  // very close to it.
425 
426  if( close_st( prevPt, graphic->GetStart(), graphic->GetEnd() ) )
427  nextPt = graphic->GetEnd();
428  else
429  {
430  nextPt = graphic->GetStart();
431  reverse = true;
432  }
433 
434  if( reverse )
435  {
436  for( int jj = graphic->GetBezierPoints().size()-1; jj >= 0; jj-- )
437  aPolygons.Append( graphic->GetBezierPoints()[jj] );
438  }
439  else
440  {
441  for( size_t jj = 0; jj < graphic->GetBezierPoints().size(); jj++ )
442  aPolygons.Append( graphic->GetBezierPoints()[jj] );
443  }
444 
445  prevPt = nextPt;
446  }
447  break;
448 
449  default:
450  if( aErrorText )
451  {
452  msg.Printf( "Unsupported DRAWSEGMENT type %s.",
453  BOARD_ITEM::ShowShape( graphic->GetShape() ) );
454 
455  *aErrorText << msg << "\n";
456  }
457 
458  if( aErrorLocation )
459  *aErrorLocation = graphic->GetPosition();
460 
461  return false;
462  }
463 
464  // Get next closest segment.
465 
466  graphic = findPoint( prevPt, segList, aTolerance );
467 
468  // If there are no more close segments, check if the board
469  // outline polygon can be closed.
470 
471  if( !graphic )
472  {
473  if( close_enough( startPt, prevPt, aTolerance ) )
474  {
475  // Close the polygon back to start point
476  // aPolygons.Append( startPt ); // not needed
477  }
478  else
479  {
480  if( aErrorText )
481  {
482  msg.Printf( _( "Unable to find segment with an endpoint of (%s, %s)." ),
483  StringFromValue( MILLIMETRES, prevPt.x, true ),
484  StringFromValue( MILLIMETRES, prevPt.y, true ) );
485 
486  *aErrorText << msg << "\n";
487  }
488 
489  if( aErrorLocation )
490  *aErrorLocation = prevPt;
491 
492  return false;
493  }
494  break;
495  }
496  }
497  }
498 
499  while( segList.size() )
500  {
501  // emit a signal layers keepout for every interior polygon left...
502  int hole = aPolygons.NewHole();
503 
504  graphic = (DRAWSEGMENT*) segList[0];
505  segList.erase( segList.begin() );
506 
507  // Both circles and polygons on the edge cuts layer are closed items that
508  // do not connect to other elements, so we process them independently
509  if( graphic->GetShape() == S_POLYGON )
510  {
511  MODULE* module = graphic->GetParentModule(); // NULL for items not in footprints
512  double orientation = module ? module->GetOrientation() : 0.0;
513  VECTOR2I offset = module ? module->GetPosition() : VECTOR2I( 0, 0 );
514 
515  for( auto it = graphic->GetPolyShape().CIterate(); it; it++ )
516  {
517  auto val = *it;
518  RotatePoint( val, orientation );
519  val += offset;
520 
521  aPolygons.Append( val, -1, hole );
522  }
523  }
524  else if( graphic->GetShape() == S_CIRCLE )
525  {
526  // make a circle by segments;
527  wxPoint center = graphic->GetCenter();
528  double angle = 3600.0;
529  wxPoint start = center;
530  int radius = graphic->GetRadius();
531  int steps = GetArcToSegmentCount( radius, ARC_LOW_DEF, 360.0 );
532  wxPoint nextPt;
533 
534  start.x += radius;
535 
536  for( int step = 0; step < steps; ++step )
537  {
538  double rotation = ( angle * step ) / steps;
539  nextPt = start;
540  RotatePoint( &nextPt.x, &nextPt.y, center.x, center.y, rotation );
541  aPolygons.Append( nextPt, -1, hole );
542  }
543  }
544  else
545  {
546  // Polygon start point. Arbitrarily chosen end of the
547  // segment and build the poly from here.
548 
549  wxPoint startPt( graphic->GetEnd() );
550  prevPt = graphic->GetEnd();
551  aPolygons.Append( prevPt, -1, hole );
552 
553  // do not append the other end point yet, this first 'graphic' might be an arc
554  for(;;)
555  {
556  switch( graphic->GetShape() )
557  {
558  case S_SEGMENT:
559  {
560  wxPoint nextPt;
561 
562  // Use the line segment end point furthest away from
563  // prevPt as we assume the other end to be ON prevPt or
564  // very close to it.
565 
566  if( close_st( prevPt, graphic->GetStart(), graphic->GetEnd() ) )
567  {
568  nextPt = graphic->GetEnd();
569  }
570  else
571  {
572  nextPt = graphic->GetStart();
573  }
574 
575  prevPt = nextPt;
576  aPolygons.Append( prevPt, -1, hole );
577  }
578  break;
579 
580  case S_ARC:
581  // Freerouter does not yet understand arcs, so approximate
582  // an arc with a series of short lines and put those
583  // line segments into the !same! PATH.
584  {
585  wxPoint pstart = graphic->GetArcStart();
586  wxPoint pend = graphic->GetArcEnd();
587  wxPoint pcenter = graphic->GetCenter();
588  double angle = -graphic->GetAngle();
589  int radius = graphic->GetRadius();
590  int steps = GetArcToSegmentCount( radius, ARC_LOW_DEF, angle / 10.0 );
591 
592  if( !close_enough( prevPt, pstart, aTolerance ) )
593  {
594  wxASSERT( close_enough( prevPt, graphic->GetArcEnd(), aTolerance ) );
595 
596  angle = -angle;
597  std::swap( pstart, pend );
598  }
599 
600  wxPoint nextPt;
601 
602  for( int step = 1; step <= steps; ++step )
603  {
604  double rotation = ( angle * step ) / steps;
605 
606  nextPt = pstart;
607  RotatePoint( &nextPt, pcenter, rotation );
608 
609  aPolygons.Append( nextPt, -1, hole );
610  }
611 
612  prevPt = nextPt;
613  }
614  break;
615 
616  case S_CURVE:
617  // We do not support Bezier curves in polygons, so approximate
618  // with a series of short lines and put those
619  // line segments into the !same! PATH.
620  {
621  wxPoint nextPt;
622  bool reverse = false;
623 
624  // Use the end point furthest away from
625  // prevPt as we assume the other end to be ON prevPt or
626  // very close to it.
627 
628  if( close_st( prevPt, graphic->GetStart(), graphic->GetEnd() ) )
629  nextPt = graphic->GetEnd();
630  else
631  {
632  nextPt = graphic->GetStart();
633  reverse = true;
634  }
635 
636  if( reverse )
637  {
638  for( int jj = graphic->GetBezierPoints().size()-1; jj >= 0; jj-- )
639  aPolygons.Append( graphic->GetBezierPoints()[jj], -1, hole );
640  }
641  else
642  {
643  for( size_t jj = 0; jj < graphic->GetBezierPoints().size(); jj++ )
644  aPolygons.Append( graphic->GetBezierPoints()[jj], -1, hole );
645  }
646 
647  prevPt = nextPt;
648  }
649  break;
650 
651  default:
652  if( aErrorText )
653  {
654  msg.Printf( "Unsupported DRAWSEGMENT type %s.",
655  BOARD_ITEM::ShowShape( graphic->GetShape() ) );
656 
657  *aErrorText << msg << "\n";
658  }
659 
660  if( aErrorLocation )
661  *aErrorLocation = graphic->GetPosition();
662 
663  return false;
664  }
665 
666  // Get next closest segment.
667 
668  graphic = findPoint( prevPt, segList, aTolerance );
669 
670  // If there are no more close segments, check if polygon
671  // can be closed.
672 
673  if( !graphic )
674  {
675  if( close_enough( startPt, prevPt, aTolerance ) )
676  {
677  // Close the polygon back to start point
678  // aPolygons.Append( startPt, -1, hole ); // not needed
679  }
680  else
681  {
682  if( aErrorText )
683  {
684  msg.Printf( _( "Unable to find segment with an endpoint of (%s, %s)." ),
685  StringFromValue( MILLIMETRES, prevPt.x, true ),
686  StringFromValue( MILLIMETRES, prevPt.y, true ) );
687 
688  *aErrorText << msg << "\n";
689  }
690 
691  if( aErrorLocation )
692  *aErrorLocation = prevPt;
693 
694  return false;
695  }
696  break;
697  }
698  }
699  }
700  }
701 
702  return true;
703 }
int NewHole(int aOutline=-1)
Creates a new hole in a given outline
static wxString ShowShape(STROKE_T aShape)
Function ShowShape converts the enum STROKE_T integer value to a wxString.
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
const wxPoint GetCenter() const override
Function GetCenter()
void TransformCircleToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aCircleToSegmentsCount)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines...
polygon (not yet used for tracks, but could be in microwave apps)
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments Has me...
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
const wxPoint GetPosition() const override
usual segment : line with rounded ends
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
bool close_st(const wxPoint &aReference, const wxPoint &aFirst, const wxPoint &aSecond)
Function close_st is a local method of qualifying if either the start of end point of a segment is cl...
const wxPoint & GetArcStart() const
STROKE_T GetShape() const
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
double GetOrientation() const
Definition: class_module.h:188
Arcs (with rounded ends)
SHAPE_POLY_SET & GetPolyShape()
Bezier Curve.
bool close_enough(const wxPoint &aLeft, const wxPoint &aRight, unsigned aLimit)
Function close_enough is a local and tunable method of qualifying the proximity of two points...
int NewOutline()
Creates a new empty polygon in the set and returns its index
const std::vector< wxPoint > & GetBezierPoints() const
double GetAngle() const
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
size_t i
Definition: json11.cpp:597
const wxPoint GetArcEnd() const
static DRAWSEGMENT * findPoint(const wxPoint &aPoint, std::vector< DRAWSEGMENT * > &aList, unsigned aLimit)
Searches for a DRAWSEGMENT matching a given end point or start point in a list, and if found...
static void reverse(privcurve_t *curve)
Definition: trace.cpp:1025
int GetWidth() const
wxString StringFromValue(EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:210
MODULE * GetParentModule() const
Function GetParentModule returns a pointer to the parent module, or NULL if DRAWSEGMENT does not belo...
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
const wxPoint GetPosition() const override
Definition: class_module.h:183
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
static double polygonArea ( SHAPE_POLY_SET aPolySet)
static

Definition at line 1375 of file class_module.cpp.

References SHAPE_LINE_CHAIN::Area(), SHAPE_POLY_SET::Outline(), SHAPE_POLY_SET::OutlineCount(), and SHAPE_LINE_CHAIN::SetClosed().

Referenced by MODULE::CoverageRatio().

1376 {
1377  double area = 0.0;
1378  for( int ii = 0; ii < aPolySet.OutlineCount(); ii++ )
1379  {
1380  SHAPE_LINE_CHAIN& outline = aPolySet.Outline( ii );
1381  // Ensure the curr outline is closed, to calculate area
1382  outline.SetClosed( true );
1383 
1384  area += outline.Area();
1385  }
1386 
1387  return area;
1388 }
int OutlineCount() const
Returns the number of outlines in the set
void SetClosed(bool aClosed)
Function SetClosed()
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
Class SHAPE_LINE_CHAIN.
double Area() const