KiCad PCB EDA Suite
class_board.cpp File Reference

BOARD class functions. More...

#include <limits.h>
#include <algorithm>
#include <fctsys.h>
#include <common.h>
#include <kicad_string.h>
#include <wxBasePcbFrame.h>
#include <msgpanel.h>
#include <pcb_netlist.h>
#include <reporter.h>
#include <base_units.h>
#include <ratsnest_data.h>
#include <ratsnest_viewitem.h>
#include <worksheet_viewitem.h>
#include <pcbnew.h>
#include <colors_selection.h>
#include <collectors.h>
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_zone.h>
#include <class_marker_pcb.h>
#include <class_drawsegment.h>
#include <class_pcb_text.h>
#include <class_mire.h>
#include <class_dimension.h>
#include <connectivity.h>
#include <stdio.h>

Go to the source code of this file.

Functions

static void removeTrack (TRACKS *aList, TRACK *aOneToRemove)
 Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector. More...
 
static void otherEnd (const TRACK &aTrack, const wxPoint &aNotThisEnd, wxPoint *aOtherEnd)
 
static int find_vias_and_tracks_at (TRACKS &at_next, TRACKS &in_net, LSET &lset, const wxPoint &next)
 Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which excludes vias. More...
 
static void checkConnectedTo (BOARD *aBoard, TRACKS *aList, const TRACKS &aTracksInNet, const wxPoint &aGoal, const wxPoint &aStart, TRACK *aFirstTrack)
 Function checkConnectedTo returns if aTracksInNet contains a copper pathway to aGoal when starting with aFirstTrack. More...
 
static bool sortNetsByNodes (const NETINFO_ITEM *a, const NETINFO_ITEM *b)
 
static bool sortNetsByNames (const NETINFO_ITEM *a, const NETINFO_ITEM *b)
 
bool sortPadsByXthenYCoord (D_PAD *const &ref, D_PAD *const &comp)
 Function SortPadsByXCoord is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value. More...
 
bool BuildBoardPolygonOutlines (BOARD *aBoard, SHAPE_POLY_SET &aOutlines, wxString *aErrorText)
 

Detailed Description

BOARD class functions.

Definition in file class_board.cpp.

Function Documentation

bool BuildBoardPolygonOutlines ( BOARD aBoard,
SHAPE_POLY_SET aOutlines,
wxString *  aErrorText 
)

Definition at line 585 of file convert_drawsegment_list_to_polygon.cpp.

References SHAPE_POLY_SET::Append(), PCB_TYPE_COLLECTOR::Collect(), BOARD::ComputeBoundingBox(), ConvertOutlineToPolygon(), Edge_Cuts, EOT, BOARD::GetBoardEdgesBoundingBox(), COLLECTOR::GetCount(), EDA_RECT::GetEnd(), EDA_RECT::GetHeight(), GetLayer(), EDA_RECT::GetOrigin(), EDA_RECT::GetWidth(), EDA_RECT::Inflate(), SHAPE_POLY_SET::NewOutline(), SHAPE_POLY_SET::OutlineCount(), PCB_LINE_T, PCB_MODULE_EDGE_T, SHAPE_POLY_SET::RemoveAllContours(), wxPoint::x, and wxPoint::y.

Referenced by BOARD::GetBoardPolygonOutlines().

588 {
589  PCB_TYPE_COLLECTOR items;
590 
591  // Get all the DRAWSEGMENTS and module graphics into 'items',
592  // then keep only those on layer == Edge_Cuts.
593  static const KICAD_T scan_graphics[] = { PCB_LINE_T, PCB_MODULE_EDGE_T, EOT };
594  items.Collect( aBoard, scan_graphics );
595 
596  // Make a working copy of aSegList, because the list is modified during calculations
597  std::vector< DRAWSEGMENT* > segList;
598 
599  for( int ii = 0; ii < items.GetCount(); ii++ )
600  {
601  if( items[ii]->GetLayer() == Edge_Cuts )
602  segList.push_back( static_cast< DRAWSEGMENT* >( items[ii] ) );
603  }
604 
605  const int STEPS = 36; // for a segmentation of an arc of 360 degrees
606  bool success = ConvertOutlineToPolygon( segList, aOutlines, STEPS, aErrorText );
607 
608  if( !success || !aOutlines.OutlineCount() )
609  {
610  // Creates a valid polygon outline is not possible.
611  // So uses the board edge cuts bounding box to create a
612  // rectangular outline
613  // When no edge cuts items, build a contour
614  // from global bounding box
615 
616  EDA_RECT bbbox = aBoard->GetBoardEdgesBoundingBox();
617 
618  // If null area, uses the global bounding box.
619  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
620  bbbox = aBoard->ComputeBoundingBox();
621 
622  // Ensure non null area. If happen, gives a minimal size.
623  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
624  bbbox.Inflate( Millimeter2iu( 1.0 ) );
625 
626  aOutlines.RemoveAllContours();
627  aOutlines.NewOutline();
628 
629  wxPoint corner;
630  aOutlines.Append( bbbox.GetOrigin() );
631 
632  corner.x = bbbox.GetOrigin().x;
633  corner.y = bbbox.GetEnd().y;
634  aOutlines.Append( corner );
635 
636  aOutlines.Append( bbbox.GetEnd() );
637 
638  corner.x = bbbox.GetEnd().x;
639  corner.y = bbbox.GetOrigin().y;
640  aOutlines.Append( corner );
641  }
642 
643  return success;
644 }
int GetCount() const
Function GetCount returns the number of objects in the list.
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
int GetHeight() const
int OutlineCount() const
Returns the number of outlines in the set
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:106
search types array terminator (End Of Types)
Definition: typeinfo.h:94
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:90
bool ConvertOutlineToPolygon(std::vector< DRAWSEGMENT * > &aSegList, SHAPE_POLY_SET &aPolygons, int aSegmentsByCircle, wxString *aErrorText)
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.
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:811
const wxPoint & GetOrigin() const
int NewOutline()
Creates a new empty polygon in the set and returns its index
void Collect(BOARD_ITEM *aBoard, const KICAD_T aScanList[])
Function Collect scans a BOARD_ITEM using this class's Inspector method, which does the collection...
Definition: collectors.cpp:488
const wxPoint GetEnd() const
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
Class EDA_RECT handles the component boundary box.
int GetWidth() const
static bool GetLayer(MODEL_VRML &aModel, LAYER_NUM layer, VRML_LAYER **vlayer)
Class PCB_TYPE_COLLECTOR merely gathers up all BOARD_ITEMs of a given set of KICAD_T type(s)...
Definition: collectors.h:593
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:103
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
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 void checkConnectedTo ( BOARD aBoard,
TRACKS *  aList,
const TRACKS &  aTracksInNet,
const wxPoint aGoal,
const wxPoint aStart,
TRACK aFirstTrack 
)
static

Function checkConnectedTo returns if aTracksInNet contains a copper pathway to aGoal when starting with aFirstTrack.

aFirstTrack should have one end situated on aStart, and the traversal testing begins from the other end of aFirstTrack.

The function throws an exception instead of returning bool so that detailed information can be provided about a possible failure in the track layout.

Exceptions
IO_ERROR- if points are not connected, with text saying why.

Definition at line 276 of file class_board.cpp.

References find_vias_and_tracks_at(), BOARD_ITEM::FormatInternalUnits(), BOARD_ITEM::GetLayer(), BOARD::GetPadFast(), next(), otherEnd(), removeTrack(), StrPrintf(), and THROW_IO_ERROR.

Referenced by BOARD::TracksInNetBetweenPoints().

278 {
279  TRACKS in_net = aTracksInNet; // copy source list so the copy can be modified
280  wxPoint next;
281 
282  otherEnd( *aFirstTrack, aStart, &next );
283 
284  aList->push_back( aFirstTrack );
285  removeTrack( &in_net, aFirstTrack );
286 
287  LSET lset( aFirstTrack->GetLayer() );
288 
289  while( in_net.size() )
290  {
291  if( next == aGoal )
292  return; // success
293 
294  // Want an exact match on the position of next, i.e. pad at next,
295  // not a forgiving HitTest() with tolerance type of match, otherwise the overall
296  // algorithm will not work. GetPadFast() is an exact match as I write this.
297  if( aBoard->GetPadFast( next, lset ) )
298  {
299  std::string m = StrPrintf(
300  "intervening pad at:(xy %s) between start:(xy %s) and goal:(xy %s)",
301  BOARD_ITEM::FormatInternalUnits( next ).c_str(),
302  BOARD_ITEM::FormatInternalUnits( aStart ).c_str(),
303  BOARD_ITEM::FormatInternalUnits( aGoal ).c_str()
304  );
305  THROW_IO_ERROR( m );
306  }
307 
308  int track_count = find_vias_and_tracks_at( *aList, in_net, lset, next );
309 
310  if( track_count != 1 )
311  {
312  std::string m = StrPrintf(
313  "found %d tracks intersecting at (xy %s), exactly 2 would be acceptable.",
314  track_count + aList->size() == 1 ? 1 : 0,
315  BOARD_ITEM::FormatInternalUnits( next ).c_str()
316  );
317  THROW_IO_ERROR( m );
318  }
319 
320  // reduce lset down to the layer that the last track at 'next' is on.
321  lset = aList->back()->GetLayerSet();
322 
323  otherEnd( *aList->back(), next, &next );
324  }
325 
326  std::string m = StrPrintf(
327  "not enough tracks connecting start:(xy %s) and goal:(xy %s).",
328  BOARD_ITEM::FormatInternalUnits( aStart ).c_str(),
329  BOARD_ITEM::FormatInternalUnits( aGoal ).c_str()
330  );
331  THROW_IO_ERROR( m );
332 }
CITER next(CITER it)
Definition: ptree.cpp:130
static void removeTrack(TRACKS *aList, TRACK *aOneToRemove)
Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector.
D_PAD * GetPadFast(const wxPoint &aPosition, LSET aLayerMask)
Function GetPadFast return pad found at aPosition on aLayerMask using the fast search method...
static void otherEnd(const TRACK &aTrack, const wxPoint &aNotThisEnd, wxPoint *aOtherEnd)
static int find_vias_and_tracks_at(TRACKS &at_next, TRACKS &in_net, LSET &lset, const wxPoint &next)
Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which e...
static std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from board internal units to a string appropriate for wr...
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:75
Class LSET is a set of PCB_LAYER_IDs.
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
static int find_vias_and_tracks_at ( TRACKS &  at_next,
TRACKS &  in_net,
LSET lset,
const wxPoint next 
)
static

Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which excludes vias.

Definition at line 224 of file class_board.cpp.

References TRACK::GetEnd(), BOARD_ITEM::GetLayerSet(), TRACK::GetStart(), next(), PCB_VIA_T, and EDA_ITEM::Type().

Referenced by checkConnectedTo().

225 {
226  // first find all vias (in this net) at 'next' location, and expand LSET with each
227  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); )
228  {
229  TRACK* t = *it;
230 
231  if( t->Type() == PCB_VIA_T && (t->GetLayerSet() & lset).any() &&
232  ( t->GetStart() == next || t->GetEnd() == next ) )
233  {
234  lset |= t->GetLayerSet();
235  at_next.push_back( t );
236  it = in_net.erase( it );
237  }
238  else
239  ++it;
240  }
241 
242  int track_count = 0;
243 
244  // with expanded lset, find all tracks with an end on any of the layers in lset
245  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); /* iterates in the loop body */ )
246  {
247  TRACK* t = *it;
248 
249  if( ( t->GetLayerSet() & lset ).any() && ( t->GetStart() == next || t->GetEnd() == next ) )
250  {
251  at_next.push_back( t );
252  it = in_net.erase( it );
253  ++track_count;
254  }
255  else
256  {
257  ++it;
258  }
259  }
260 
261  return track_count;
262 }
CITER next(CITER it)
Definition: ptree.cpp:130
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
const wxPoint & GetEnd() const
Definition: class_track.h:118
const wxPoint & GetStart() const
Definition: class_track.h:121
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:108
static void otherEnd ( const TRACK aTrack,
const wxPoint aNotThisEnd,
wxPoint aOtherEnd 
)
static

Definition at line 206 of file class_board.cpp.

References TRACK::GetEnd(), and TRACK::GetStart().

Referenced by checkConnectedTo().

207 {
208  if( aTrack.GetStart() == aNotThisEnd )
209  {
210  *aOtherEnd = aTrack.GetEnd();
211  }
212  else
213  {
214  wxASSERT( aTrack.GetEnd() == aNotThisEnd );
215  *aOtherEnd = aTrack.GetStart();
216  }
217 }
const wxPoint & GetEnd() const
Definition: class_track.h:118
const wxPoint & GetStart() const
Definition: class_track.h:121
static void removeTrack ( TRACKS *  aList,
TRACK aOneToRemove 
)
static

Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector.

Definition at line 200 of file class_board.cpp.

Referenced by checkConnectedTo().

201 {
202  aList->erase( std::remove( aList->begin(), aList->end(), aOneToRemove ), aList->end() );
203 }
static bool sortNetsByNames ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1448 of file class_board.cpp.

References NETINFO_ITEM::GetNetname().

Referenced by BOARD::SortedNetnamesList().

1449 {
1450  return a->GetNetname() < b->GetNetname();
1451 }
const wxString & GetNetname() const
Function GetNetname.
static bool sortNetsByNodes ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1435 of file class_board.cpp.

References BOARD::GetConnectivity(), NETINFO_ITEM::GetNet(), NETINFO_ITEM::GetNetname(), and NETINFO_ITEM::GetParent().

Referenced by BOARD::SortedNetnamesList().

1436 {
1437  auto connectivity = a->GetParent()->GetConnectivity();
1438  int countA = connectivity->GetPadCount( a->GetNet() );
1439  int countB = connectivity->GetPadCount( b->GetNet() );
1440 
1441  if( countA == countB )
1442  return a->GetNetname() < b->GetNetname();
1443  else
1444  return countB < countA;
1445 }
int GetNet() const
Function GetNet.
BOARD * GetParent() const
const wxString & GetNetname() const
Function GetNetname.
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
bool sortPadsByXthenYCoord ( D_PAD *const &  ref,
D_PAD *const &  comp 
)

Function SortPadsByXCoord is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value.

This function is used to build ordered pads lists

Definition at line 1748 of file class_board.cpp.

References D_PAD::GetPosition(), wxPoint::x, and wxPoint::y.

Referenced by DRAG_LIST::BuildDragListe(), and BOARD::GetSortedPadListByXthenYCoord().

1749 {
1750  if( ref->GetPosition().x == comp->GetPosition().x )
1751  return ref->GetPosition().y < comp->GetPosition().y;
1752  return ref->GetPosition().x < comp->GetPosition().x;
1753 }
const wxPoint & GetPosition() const override
Definition: class_pad.h:170