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

Go to the source code of this file.

Functions

static void addTextSegmToPoly (int x0, int y0, int xf, int yf)
 
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

static int s_textWidth
 
static int s_textCircle2SegmentCount
 
static SHAPE_POLY_SETs_cornerBuffer
 

Function Documentation

static void addTextSegmToPoly ( int  x0,
int  y0,
int  xf,
int  yf 
)
static

Definition at line 58 of file board_items_to_polygon_shape_transform.cpp.

References s_textCircle2SegmentCount, s_textWidth, and TransformRoundedEndsSegmentToPolygon().

Referenced by MODULE::TransformGraphicShapesWithClearanceToPolygonSet(), MODULE::TransformGraphicTextWithClearanceToPolygonSet(), and TEXTE_PCB::TransformShapeWithClearanceToPolygonSet().

59 {
61  wxPoint( x0, y0), wxPoint( xf, yf ),
63 }
void TransformRoundedEndsSegmentToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aStart, wxPoint aEnd, int aCircleToSegmentsCount, int aWidth)
Function TransformRoundedEndsSegmentToPolygon convert a segment with rounded ends to a polygon Conver...
static SHAPE_POLY_SET * s_cornerBuffer
static int s_textCircle2SegmentCount
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 829 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(), 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().

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

SHAPE_POLY_SET* s_cornerBuffer
static

Definition at line 55 of file board_items_to_polygon_shape_transform.cpp.