KiCad PCB EDA Suite
board_items_to_polygon_shape_transform.cpp File Reference
#include <vector>
#include <fctsys.h>
#include <bezier_curves.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 894 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().

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