Skip to content

Commit

Permalink
Fix numerical imprecisions
Browse files Browse the repository at this point in the history
  • Loading branch information
yaqwsx committed Mar 8, 2022
1 parent bb788e9 commit e146259
Show file tree
Hide file tree
Showing 27 changed files with 72 additions and 32 deletions.
44 changes: 44 additions & 0 deletions doc/panelization.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ debugRenderPartitionLines(self)
Render partition line to the panel to be easily able to inspect them via
Pcbnew.

#### `getAuxiliaryOrigin`
```
getAuxiliaryOrigin(self)
```
None

#### `getGridOrigin`
```
getGridOrigin(self)
```
None

#### `getPrlFilepath`
```
getPrlFilepath(self, path=None)
Expand Down Expand Up @@ -347,6 +359,12 @@ inheritDesignSettings(self, board)
Inherit design settings from the given board specified by a filename or
a board

#### `inheritPageSize`
```
inheritPageSize(self, board)
```
Inherit page size from a board specified by a filename or a board

#### `inheritProperties`
```
inheritProperties(self, board)
Expand Down Expand Up @@ -496,6 +514,12 @@ setGridOrigin(self, point)
```
Set grid origin

#### `setPageSize`
```
setPageSize(self, size)
```
Set page size - either a string name (e.g., A4) or size in KiCAD units

#### `setProperties`
```
setProperties(self, properties)
Expand All @@ -520,6 +544,14 @@ setVCutLayer(self, layer)
```
Set layer on which the V-Cuts will be rendered

#### `translate`
```
translate(self, vec)
```
Translates the whole panel by vec. Such a feature can be useful to
specify the panel placement in the sheet. When we translate panel as the
last operation, none of the operations have to be placement-aware.

## Substrate class

This class represents a pice of substrate (with no components). Basically it is
Expand All @@ -529,6 +561,12 @@ a partition line for the substrate. Read more about partition lines in



#### `backToSource`
```
backToSource(self, point)
```
Return a point in the source form (if a reverse transformation was set)

#### `boundary`
```
boundary(self)
Expand Down Expand Up @@ -608,6 +646,12 @@ a tuple (None, None).
Returns a pair tab and cut outline. Add the tab it via union - batch
adding of geometry is more efficient.

#### `translate`
```
translate(self, vec)
```
Translate substrate by vec

#### `union`
```
union(self, other)
Expand Down
Binary file modified doc/resources/conn.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel14.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel15.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel17.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel18.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel20.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel21.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel22.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/resources/examplePanel9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 9 additions & 8 deletions kikit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
PKG_BASE = os.path.dirname(__file__)
KIKIT_LIB = os.path.join(PKG_BASE, "resources/kikit.pretty")
SHP_EPSILON = pcbnew.FromMM(0.001) # Common factor of enlarging substrates to
# cover up numerical imprecisions of Shapely
# cover up numerical imprecisions of Shapely

def fromDegrees(angle):
return angle * 10
Expand All @@ -37,13 +37,14 @@ def fitsIn(what, where):

def combineBoundingBoxes(a, b):
""" Retrun wxRect as a combination of source bounding boxes """
x = min(a.GetX(), b.GetX())
y = min(a.GetY(), b.GetY())
topLeft = wxPoint(x, y)
x = max(a.GetX() + a.GetWidth(), b.GetX() + b.GetWidth())
y = max(a.GetY() + a.GetHeight(), b.GetY() + b.GetHeight())
bottomRight = wxPoint(x, y)
return wxRect(topLeft, bottomRight)
x1 = min(a.GetX(), b.GetX())
y1 = min(a.GetY(), b.GetY())
x2 = max(a.GetX() + a.GetWidth(), b.GetX() + b.GetWidth())
y2 = max(a.GetY() + a.GetHeight(), b.GetY() + b.GetHeight())
# Beware that we cannot use the following code! It will add 1 to width and
# height. See https://github.com/wxWidgets/wxWidgets/blob/e43895e5317a1e82e295788264553d9839190337/src/common/gdicmn.cpp#L94-L114
# return wxRect(topLeft, bottomRight)
return wxRect(x1, y1, x2 - x1, y2 - y1)

def collectEdges(board, layerName, sourceArea=None):
""" Collect edges in sourceArea on given layer including footprints """
Expand Down
37 changes: 16 additions & 21 deletions kikit/panelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,10 @@ def f(point):

revertTransformation = makeRevertTransformation(rotationAngle, originPoint, translation)
try:
o = Substrate(edges, -bufferOutline,
s = Substrate(edges, 0,
revertTransformation=revertTransformation)
s = Substrate(edges, bufferOutline)
self.boardSubstrate.union(s)
self.substrates.append(o)
self.substrates.append(s)
self.substrates[-1].annotations = annotations
except substrate.PositionError as e:
point = undoTransformation(e.point, rotationAngle, originPoint, translation)
Expand Down Expand Up @@ -898,9 +897,8 @@ def makeFrame(self, width, hspace, vspace):
boards substrates and the frame. Return a tuple of vertical and
horizontal cuts.
"""
frameInnerRect = expandRect(shpBoxToRect(self.boardsBBox()),
hspace - SHP_EPSILON, vspace + SHP_EPSILON)
frameOuterRect = expandRect(frameInnerRect, width + SHP_EPSILON)
frameInnerRect = expandRect(shpBoxToRect(self.boardsBBox()), hspace, vspace)
frameOuterRect = expandRect(frameInnerRect, width)
outerRing = rectToRing(frameOuterRect)
innerRing = rectToRing(frameInnerRect)
polygon = Polygon(outerRing, [innerRing])
Expand Down Expand Up @@ -930,9 +928,8 @@ def makeRailsTb(self, thickness):
Adds a rail to top and bottom.
"""
minx, miny, maxx, maxy = self.panelBBox()
thickness -= SHP_EPSILON
topRail = box(minx, maxy - SHP_EPSILON, maxx, maxy + thickness)
bottomRail = box(minx, miny + SHP_EPSILON, maxx, miny - thickness)
topRail = box(minx, maxy, maxx, maxy + thickness)
bottomRail = box(minx, miny, maxx, miny - thickness)
self.appendSubstrate(topRail)
self.appendSubstrate(bottomRail)

Expand All @@ -941,9 +938,8 @@ def makeRailsLr(self, thickness):
Adds a rail to left and right.
"""
minx, miny, maxx, maxy = self.panelBBox()
thickness -= SHP_EPSILON
leftRail = box(minx - thickness + SHP_EPSILON, miny, minx, maxy)
rightRail = box(maxx - SHP_EPSILON, miny, maxx + thickness, maxy)
leftRail = box(minx - thickness, miny, minx, maxy)
rightRail = box(maxx, miny, maxx + thickness, maxy)
self.appendSubstrate(leftRail)
self.appendSubstrate(rightRail)

Expand Down Expand Up @@ -1225,8 +1221,8 @@ def buildFullTabs(self, framingOffsets):
outerBounds = self.substrates[0].partitionLine.bounds
for s in islice(self.substrates, 1, None):
outerBounds = shpBBoxMerge(outerBounds, s.partitionLine.bounds)
fill = box(*outerBounds).difference(bBoxes.buffer(SHP_EPSILON))
self.appendSubstrate(fill.buffer(SHP_EPSILON))
fill = box(*outerBounds).difference(bBoxes)
self.appendSubstrate(fill)

# Make the cuts from the bounding boxes of the PCB
substrateBoundaries = [linestringToSegments(rectToShpBox(s.boundingBox()).exterior)
Expand Down Expand Up @@ -1510,12 +1506,11 @@ def renderBackbone(self, vthickness, hthickness, vcut, hcut):
(x[0] - c * vthickness // 2, x[1]),
(x[0] + c * vthickness // 2, x[1])])
cuts.append(cut)
backbones = list([b.buffer(SHP_EPSILON, join_style=2) for b in pieces])
self.appendSubstrate(backbones)
self.appendSubstrate(pieces)
return cuts

def addCornerFillets(self, radius):
corners = self.panelCorners(-SHP_EPSILON, -SHP_EPSILON)
corners = self.panelCorners()
filletOrigins = self.panelCorners(radius, radius)
for corner, opposite in zip(corners, filletOrigins):
square = shapely.geometry.box(
Expand All @@ -1524,15 +1519,15 @@ def addCornerFillets(self, radius):
max(corner[0], opposite[0]),
max(corner[1], opposite[1])
)
filletCircle = Point(opposite).buffer(radius + SHP_EPSILON, resolution=16)
filletCircle = Point(opposite).buffer(radius, resolution=16)

cutShape = square.difference(filletCircle)
self.boardSubstrate.cut(cutShape)

def addCornerChamfers(self, size):
corners = self.panelCorners(-SHP_EPSILON, -SHP_EPSILON)
verticalStops = self.panelCorners(-SHP_EPSILON, size)
horizontalStops = self.panelCorners(size, -SHP_EPSILON)
corners = self.panelCorners()
verticalStops = self.panelCorners(0, size)
horizontalStops = self.panelCorners(size, 0)
for t, v, h in zip(corners, verticalStops, horizontalStops):
cutPoly = Polygon([t, v, h, t])
self.boardSubstrate.cut(cutPoly)
Expand Down
6 changes: 3 additions & 3 deletions kikit/substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ def millFillets(self, millRadius):
Add fillets to inner conernes which will be produced a by mill with
given radius.
"""
if millRadius < SHP_EPSILON:
EPS = fromMm(0.01)
RES = 32
if millRadius < EPS:
return
self.orient()
RES = 64
EPS = fromMm(0.01)
self.substrates = self.substrates.buffer(millRadius - EPS, resolution=RES) \
.buffer(-millRadius, resolution=RES) \
.buffer(EPS, resolution=RES)
Expand Down

0 comments on commit e146259

Please sign in to comment.