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 865 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().

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