KiCad PCB EDA Suite
board_items_to_polygon_shape_transform.cpp File Reference
#include <vector>
#include <fctsys.h>
#include <base_units.h>
#include <draw_graphic_text.h>
#include <pcbnew.h>
#include <pcb_edit_frame.h>
#include <trigo.h>
#include <class_board.h>
#include <class_pad.h>
#include <class_track.h>
#include <class_drawsegment.h>
#include <class_pcb_text.h>
#include <class_zone.h>
#include <class_module.h>
#include <class_edge_mod.h>
#include <convert_basic_shapes_to_polygon.h>
#include <geometry/geometry_utils.h>

Go to the source code of this file.

Classes

struct  TSEGM_2_POLY_PRMS
 

Functions

static void addTextSegmToPoly (int x0, int y0, int xf, int yf, void *aData)
 
void CreateThermalReliefPadPolygon (SHAPE_POLY_SET &aCornerBuffer, const D_PAD &aPad, int aThermalGap, int aCopperThickness, int aMinThicknessValue, int aCircleToSegmentsCount, double aCorrectionFactor, double aThermalRot)
 Function CreateThermalReliefPadPolygon Add holes around a pad to create a thermal relief copper thickness is min (dx/2, aCopperWitdh) or min (dy/2, aCopperWitdh) More...
 

Variables

TSEGM_2_POLY_PRMS prms
 
double s_error_max = Millimeter2iu( 0.02 )
 

Function Documentation

static void addTextSegmToPoly ( int  x0,
int  y0,
int  xf,
int  yf,
void *  aData 
)
static
void CreateThermalReliefPadPolygon ( SHAPE_POLY_SET aCornerBuffer,
const D_PAD aPad,
int  aThermalGap,
int  aCopperThickness,
int  aMinThicknessValue,
int  aCircleToSegmentsCount,
double  aCorrectionFactor,
double  aThermalRot 
)

Function CreateThermalReliefPadPolygon Add holes around a pad to create a thermal relief copper thickness is min (dx/2, aCopperWitdh) or min (dy/2, aCopperWitdh)

Parameters
aCornerBuffer= a buffer to store the polygon
aPad= the current pad used to create the thermal shape
aThermalGap= gap in thermal shape
aCopperThickness= stubs thickness in thermal shape
aMinThicknessValue= min copper thickness allowed
aCircleToSegmentsCount= the number of segments to approximate a circle
aCorrectionFactor= the correction to apply to circles radius to keep
aThermalRot= for rond pads the rotation of thermal stubs (450 usually for 45 deg.)

Definition at line 875 of file board_items_to_polygon_shape_transform.cpp.

References AddAngles(), PNS::angle(), SHAPE_POLY_SET::Append(), SHAPE_POLY_SET::BooleanSubtract(), delta, DIM, D_PAD::GetBoundingBox(), EDA_RECT::GetHeight(), D_PAD::GetOrientation(), D_PAD::GetShape(), D_PAD::GetSize(), EDA_RECT::GetWidth(), i, KiROUND(), max, min, SHAPE_POLY_SET::NewOutline(), PAD_SHAPE_CIRCLE, PAD_SHAPE_OVAL, PAD_SHAPE_RECT, PAD_SHAPE_ROUNDRECT, PAD_SHAPE_TRAPEZOID, SHAPE_POLY_SET::PM_FAST, SHAPE_POLY_SET::RemoveAllContours(), RotatePoint(), D_PAD::ShapePos(), SHAPE_POLY_SET::Simplify(), D_PAD::TransformShapeWithClearanceToPolygon(), wxPoint::x, and wxPoint::y.

Referenced by ZONE_FILLER::buildZoneFeatureHoleList().

883 {
884  wxPoint corner, corner_end;
885  wxPoint padShapePos = aPad.ShapePos(); // Note: for pad having a shape offset,
886  // the pad position is NOT the shape position
887  wxSize copper_thickness;
888 
889  double delta = 3600.0 / aCircleToSegmentsCount; // rot angle in 0.1 degree
890 
891  /* Keep in account the polygon outline thickness
892  * aThermalGap must be increased by aMinThicknessValue/2 because drawing external outline
893  * with a thickness of aMinThicknessValue will reduce gap by aMinThicknessValue/2
894  */
895  aThermalGap += aMinThicknessValue / 2;
896 
897  /* Keep in account the polygon outline thickness
898  * copper_thickness must be decreased by aMinThicknessValue because drawing outlines
899  * with a thickness of aMinThicknessValue will increase real thickness by aMinThicknessValue
900  */
901  aCopperThickness -= aMinThicknessValue;
902 
903  if( aCopperThickness < 0 )
904  aCopperThickness = 0;
905 
906  int dx = aPad.GetSize().x / 2;
907  int dy = aPad.GetSize().y / 2;
908 
909  copper_thickness.x = std::min( dx, aCopperThickness );
910  copper_thickness.y = std::min( dy, aCopperThickness );
911 
912  switch( aPad.GetShape() )
913  {
914  case PAD_SHAPE_CIRCLE: // Add 4 similar holes
915  {
916  /* we create 4 copper holes and put them in position 1, 2, 3 and 4
917  * here is the area of the rectangular pad + its thermal gap
918  * the 4 copper holes remove the copper in order to create the thermal gap
919  * 4 ------ 1
920  * | |
921  * | |
922  * | |
923  * | |
924  * 3 ------ 2
925  * holes 2, 3, 4 are the same as hole 1, rotated 90, 180, 270 deg
926  */
927 
928  // Build the hole pattern, for the hole in the X >0, Y > 0 plane:
929  // The pattern roughtly is a 90 deg arc pie
930  std::vector <wxPoint> corners_buffer;
931 
932  // Radius of outer arcs of the shape corrected for arc approximation by lines
933  int outer_radius = KiROUND( (dx + aThermalGap) * aCorrectionFactor );
934 
935  // Crosspoint of thermal spoke sides, the first point of polygon buffer
936  corners_buffer.push_back( wxPoint( copper_thickness.x / 2, copper_thickness.y / 2 ) );
937 
938  // Add an intermediate point on spoke sides, to allow a > 90 deg angle between side
939  // and first seg of arc approx
940  corner.x = copper_thickness.x / 2;
941  int y = outer_radius - (aThermalGap / 4);
942  corner.y = KiROUND( sqrt( ( (double) y * y - (double) corner.x * corner.x ) ) );
943 
944  if( aThermalRot != 0 )
945  corners_buffer.push_back( corner );
946 
947  // calculate the starting point of the outter arc
948  corner.x = copper_thickness.x / 2;
949 
950  corner.y = KiROUND( sqrt( ( (double) outer_radius * outer_radius ) -
951  ( (double) corner.x * corner.x ) ) );
952  RotatePoint( &corner, 90 ); // 9 degrees is the spoke fillet size
953 
954  // calculate the ending point of the outter arc
955  corner_end.x = corner.y;
956  corner_end.y = corner.x;
957 
958  // calculate intermediate points (y coordinate from corner.y to corner_end.y
959  while( (corner.y > corner_end.y) && (corner.x < corner_end.x) )
960  {
961  corners_buffer.push_back( corner );
962  RotatePoint( &corner, delta );
963  }
964 
965  corners_buffer.push_back( corner_end );
966 
967  /* add an intermediate point, to avoid angles < 90 deg between last arc approx line
968  * and radius line
969  */
970  corner.x = corners_buffer[1].y;
971  corner.y = corners_buffer[1].x;
972  corners_buffer.push_back( corner );
973 
974  // Now, add the 4 holes ( each is the pattern, rotated by 0, 90, 180 and 270 deg
975  // aThermalRot = 450 (45.0 degrees orientation) work fine.
976  double angle_pad = aPad.GetOrientation(); // Pad orientation
977  double th_angle = aThermalRot;
978 
979  for( unsigned ihole = 0; ihole < 4; ihole++ )
980  {
981  aCornerBuffer.NewOutline();
982 
983  for( unsigned ii = 0; ii < corners_buffer.size(); ii++ )
984  {
985  corner = corners_buffer[ii];
986  RotatePoint( &corner, th_angle + angle_pad ); // Rotate by segment angle and pad orientation
987  corner += padShapePos;
988  aCornerBuffer.Append( corner.x, corner.y );
989  }
990 
991  th_angle += 900; // Note: th_angle in in 0.1 deg.
992  }
993  }
994  break;
995 
996  case PAD_SHAPE_OVAL:
997  {
998  // Oval pad support along the lines of round and rectangular pads
999  std::vector <wxPoint> corners_buffer; // Polygon buffer as vector
1000 
1001  dx = (aPad.GetSize().x / 2) + aThermalGap; // Cutout radius x
1002  dy = (aPad.GetSize().y / 2) + aThermalGap; // Cutout radius y
1003 
1004  wxPoint shape_offset;
1005 
1006  // We want to calculate an oval shape with dx > dy.
1007  // if this is not the case, exchange dx and dy, and rotate the shape 90 deg.
1008  int supp_angle = 0;
1009 
1010  if( dx < dy )
1011  {
1012  std::swap( dx, dy );
1013  supp_angle = 900;
1014  std::swap( copper_thickness.x, copper_thickness.y );
1015  }
1016 
1017  int deltasize = dx - dy; // = distance between shape position and the 2 demi-circle ends centre
1018  // here we have dx > dy
1019  // Radius of outer arcs of the shape:
1020  int outer_radius = dy; // The radius of the outer arc is radius end + aThermalGap
1021 
1022  // Some coordinate fiddling, depending on the shape offset direction
1023  shape_offset = wxPoint( deltasize, 0 );
1024 
1025  // Crosspoint of thermal spoke sides, the first point of polygon buffer
1026  corner.x = copper_thickness.x / 2;
1027  corner.y = copper_thickness.y / 2;
1028  corners_buffer.push_back( corner );
1029 
1030  // Arc start point calculation, the intersecting point of cutout arc and thermal spoke edge
1031  // If copper thickness is more than shape offset, we need to calculate arc intercept point.
1032  if( copper_thickness.x > deltasize )
1033  {
1034  corner.x = copper_thickness.x / 2;
1035  corner.y = KiROUND( sqrt( ( (double) outer_radius * outer_radius ) -
1036  ( (double) ( corner.x - delta ) * ( corner.x - deltasize ) ) ) );
1037  corner.x -= deltasize;
1038 
1039  /* creates an intermediate point, to have a > 90 deg angle
1040  * between the side and the first segment of arc approximation
1041  */
1042  wxPoint intpoint = corner;
1043  intpoint.y -= aThermalGap / 4;
1044  corners_buffer.push_back( intpoint + shape_offset );
1045  RotatePoint( &corner, 90 ); // 9 degrees of thermal fillet
1046  }
1047  else
1048  {
1049  corner.x = copper_thickness.x / 2;
1050  corner.y = outer_radius;
1051  corners_buffer.push_back( corner );
1052  }
1053 
1054  // Add an intermediate point on spoke sides, to allow a > 90 deg angle between side
1055  // and first seg of arc approx
1056  wxPoint last_corner;
1057  last_corner.y = copper_thickness.y / 2;
1058  int px = outer_radius - (aThermalGap / 4);
1059  last_corner.x =
1060  KiROUND( sqrt( ( ( (double) px * px ) - (double) last_corner.y * last_corner.y ) ) );
1061 
1062  // Arc stop point calculation, the intersecting point of cutout arc and thermal spoke edge
1063  corner_end.y = copper_thickness.y / 2;
1064  corner_end.x =
1065  KiROUND( sqrt( ( (double) outer_radius *
1066  outer_radius ) - ( (double) corner_end.y * corner_end.y ) ) );
1067  RotatePoint( &corner_end, -90 ); // 9 degrees of thermal fillet
1068 
1069  // calculate intermediate arc points till limit is reached
1070  while( (corner.y > corner_end.y) && (corner.x < corner_end.x) )
1071  {
1072  corners_buffer.push_back( corner + shape_offset );
1073  RotatePoint( &corner, delta );
1074  }
1075 
1076  //corners_buffer.push_back(corner + shape_offset); // TODO: about one mil geometry error forms somewhere.
1077  corners_buffer.push_back( corner_end + shape_offset );
1078  corners_buffer.push_back( last_corner + shape_offset ); // Enabling the line above shows intersection point.
1079 
1080  /* Create 2 holes, rotated by pad rotation.
1081  */
1082  double angle = aPad.GetOrientation() + supp_angle;
1083 
1084  for( int irect = 0; irect < 2; irect++ )
1085  {
1086  aCornerBuffer.NewOutline();
1087  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1088  {
1089  wxPoint cpos = corners_buffer[ic];
1090  RotatePoint( &cpos, angle );
1091  cpos += padShapePos;
1092  aCornerBuffer.Append( cpos.x, cpos.y );
1093  }
1094 
1095  angle = AddAngles( angle, 1800 ); // this is calculate hole 3
1096  }
1097 
1098  // Create holes, that are the mirrored from the previous holes
1099  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1100  {
1101  wxPoint swap = corners_buffer[ic];
1102  swap.x = -swap.x;
1103  corners_buffer[ic] = swap;
1104  }
1105 
1106  // Now add corner 4 and 2 (2 is the corner 4 rotated by 180 deg
1107  angle = aPad.GetOrientation() + supp_angle;
1108 
1109  for( int irect = 0; irect < 2; irect++ )
1110  {
1111  aCornerBuffer.NewOutline();
1112 
1113  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1114  {
1115  wxPoint cpos = corners_buffer[ic];
1116  RotatePoint( &cpos, angle );
1117  cpos += padShapePos;
1118  aCornerBuffer.Append( cpos.x, cpos.y );
1119  }
1120 
1121  angle = AddAngles( angle, 1800 );
1122  }
1123  }
1124  break;
1125 
1126  case PAD_SHAPE_ROUNDRECT: // thermal shape is the same for round rect and rect.
1127  case PAD_SHAPE_RECT:
1128  {
1129  /* we create 4 copper holes and put them in position 1, 2, 3 and 4
1130  * here is the area of the rectangular pad + its thermal gap
1131  * the 4 copper holes remove the copper in order to create the thermal gap
1132  * 4 ------ 1
1133  * | |
1134  * | |
1135  * | |
1136  * | |
1137  * 3 ------ 2
1138  * hole 3 is the same as hole 1, rotated 180 deg
1139  * hole 4 is the same as hole 2, rotated 180 deg and is the same as hole 1, mirrored
1140  */
1141 
1142  // First, create a rectangular hole for position 1 :
1143  // 2 ------- 3
1144  // | |
1145  // | |
1146  // | |
1147  // 1 -------4
1148 
1149  // Modified rectangles with one corner rounded. TODO: merging with oval thermals
1150  // and possibly round too.
1151 
1152  std::vector <wxPoint> corners_buffer; // Polygon buffer as vector
1153 
1154  dx = (aPad.GetSize().x / 2) + aThermalGap; // Cutout radius x
1155  dy = (aPad.GetSize().y / 2) + aThermalGap; // Cutout radius y
1156 
1157  // The first point of polygon buffer is left lower corner, second the crosspoint of
1158  // thermal spoke sides, the third is upper right corner and the rest are rounding
1159  // vertices going anticlockwise. Note the inveted Y-axis in CG.
1160  corners_buffer.push_back( wxPoint( -dx, -(aThermalGap / 4 + copper_thickness.y / 2) ) ); // Adds small miters to zone
1161  corners_buffer.push_back( wxPoint( -(dx - aThermalGap / 4), -copper_thickness.y / 2 ) ); // fill and spoke corner
1162  corners_buffer.push_back( wxPoint( -copper_thickness.x / 2, -copper_thickness.y / 2 ) );
1163  corners_buffer.push_back( wxPoint( -copper_thickness.x / 2, -(dy - aThermalGap / 4) ) );
1164  corners_buffer.push_back( wxPoint( -(aThermalGap / 4 + copper_thickness.x / 2), -dy ) );
1165 
1166  double angle = aPad.GetOrientation();
1167  int rounding_radius = KiROUND( aThermalGap * aCorrectionFactor ); // Corner rounding radius
1168 
1169  for( int i = 0; i < aCircleToSegmentsCount / 4 + 1; i++ )
1170  {
1171  wxPoint corner_position = wxPoint( 0, -rounding_radius );
1172 
1173  // Start at half increment offset
1174  RotatePoint( &corner_position, 1800.0 / aCircleToSegmentsCount );
1175  double angle_pg = i * delta;
1176 
1177  RotatePoint( &corner_position, angle_pg ); // Rounding vector rotation
1178  corner_position -= aPad.GetSize() / 2; // Rounding vector + Pad corner offset
1179 
1180  corners_buffer.push_back( wxPoint( corner_position.x, corner_position.y ) );
1181  }
1182 
1183  for( int irect = 0; irect < 2; irect++ )
1184  {
1185  aCornerBuffer.NewOutline();
1186 
1187  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1188  {
1189  wxPoint cpos = corners_buffer[ic];
1190  RotatePoint( &cpos, angle ); // Rotate according to module orientation
1191  cpos += padShapePos; // Shift origin to position
1192  aCornerBuffer.Append( cpos.x, cpos.y );
1193  }
1194 
1195  angle = AddAngles( angle, 1800 ); // this is calculate hole 3
1196  }
1197 
1198  // Create holes, that are the mirrored from the previous holes
1199  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1200  {
1201  wxPoint swap = corners_buffer[ic];
1202  swap.x = -swap.x;
1203  corners_buffer[ic] = swap;
1204  }
1205 
1206  // Now add corner 4 and 2 (2 is the corner 4 rotated by 180 deg
1207  for( int irect = 0; irect < 2; irect++ )
1208  {
1209  aCornerBuffer.NewOutline();
1210 
1211  for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
1212  {
1213  wxPoint cpos = corners_buffer[ic];
1214  RotatePoint( &cpos, angle );
1215  cpos += padShapePos;
1216  aCornerBuffer.Append( cpos.x, cpos.y );
1217  }
1218 
1219  angle = AddAngles( angle, 1800 );
1220  }
1221  }
1222  break;
1223 
1224  case PAD_SHAPE_TRAPEZOID:
1225  {
1226  SHAPE_POLY_SET antipad; // The full antipad area
1227 
1228  // We need a length to build the stubs of the thermal reliefs
1229  // the value is not very important. The pad bounding box gives a reasonable value
1230  EDA_RECT bbox = aPad.GetBoundingBox();
1231  int stub_len = std::max( bbox.GetWidth(), bbox.GetHeight() );
1232 
1233  aPad.TransformShapeWithClearanceToPolygon( antipad, aThermalGap,
1234  aCircleToSegmentsCount, aCorrectionFactor );
1235 
1236  SHAPE_POLY_SET stub; // A basic stub ( a rectangle)
1237  SHAPE_POLY_SET stubs; // the full stubs shape
1238 
1239 
1240  // We now substract the stubs (connections to the copper zone)
1241  //ClipperLib::Clipper clip_engine;
1242  // Prepare a clipping transform
1243  //clip_engine.AddPath( antipad, ClipperLib::ptSubject, true );
1244 
1245  // Create stubs and add them to clipper engine
1246  wxPoint stubBuffer[4];
1247  stubBuffer[0].x = stub_len;
1248  stubBuffer[0].y = copper_thickness.y/2;
1249  stubBuffer[1] = stubBuffer[0];
1250  stubBuffer[1].y = -copper_thickness.y/2;
1251  stubBuffer[2] = stubBuffer[1];
1252  stubBuffer[2].x = -stub_len;
1253  stubBuffer[3] = stubBuffer[2];
1254  stubBuffer[3].y = copper_thickness.y/2;
1255 
1256  stub.NewOutline();
1257 
1258  for( unsigned ii = 0; ii < DIM( stubBuffer ); ii++ )
1259  {
1260  wxPoint cpos = stubBuffer[ii];
1261  RotatePoint( &cpos, aPad.GetOrientation() );
1262  cpos += padShapePos;
1263  stub.Append( cpos.x, cpos.y );
1264  }
1265 
1266  stubs.Append( stub );
1267 
1268  stubBuffer[0].y = stub_len;
1269  stubBuffer[0].x = copper_thickness.x/2;
1270  stubBuffer[1] = stubBuffer[0];
1271  stubBuffer[1].x = -copper_thickness.x/2;
1272  stubBuffer[2] = stubBuffer[1];
1273  stubBuffer[2].y = -stub_len;
1274  stubBuffer[3] = stubBuffer[2];
1275  stubBuffer[3].x = copper_thickness.x/2;
1276 
1277  stub.RemoveAllContours();
1278  stub.NewOutline();
1279 
1280  for( unsigned ii = 0; ii < DIM( stubBuffer ); ii++ )
1281  {
1282  wxPoint cpos = stubBuffer[ii];
1283  RotatePoint( &cpos, aPad.GetOrientation() );
1284  cpos += padShapePos;
1285  stub.Append( cpos.x, cpos.y );
1286  }
1287 
1288  stubs.Append( stub );
1290 
1291  antipad.BooleanSubtract( stubs, SHAPE_POLY_SET::PM_FAST );
1292  aCornerBuffer.Append( antipad );
1293 
1294  break;
1295  }
1296 
1297  default:
1298  ;
1299  }
1300 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:106
int GetHeight() const
Definition: eda_rect.h:118
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:216
static const int delta[8][2]
Definition: solve.cpp:112
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:288
Class SHAPE_POLY_SET.
const wxSize & GetSize() const
Definition: class_pad.h:269
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, int aClearanceValue, int aCircleToSegmentsCount, double aCorrectionFactor) const override
Function TransformShapeWithClearanceToPolygon Convert the pad shape to a closed polygon Used in filli...
int NewOutline()
Creates a new empty polygon in the set and returns its index
#define max(a, b)
Definition: auxiliary.h:86
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
size_t i
Definition: json11.cpp:597
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees, but soon degrees.
Definition: class_pad.h:382
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int GetWidth() const
Definition: eda_rect.h:117
wxPoint ShapePos() const
Definition: class_pad.cpp:500
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset difference For aFastMode meaning, see function booleanOp ...
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: class_pad.cpp:199
#define min(a, b)
Definition: auxiliary.h:85
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) ...

Variable Documentation

double s_error_max = Millimeter2iu( 0.02 )