Skip to content

Commit

Permalink
Rotated parts get rotated bounding box
Browse files Browse the repository at this point in the history
Issue #104
  • Loading branch information
qu1ck committed Oct 21, 2019
1 parent 531db45 commit 5e4ee16
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
2 changes: 2 additions & 0 deletions InteractiveHtmlBom/ecad/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def to_component_dict(self):
# type: () -> dict
return {
"pos": [self._x0, self._y0],
"relpos": [0, 0],
"size": [self._x1 - self._x0, self._y1 - self._y0],
"angle": 0,
}

def add(self, other):
Expand Down
17 changes: 9 additions & 8 deletions InteractiveHtmlBom/ecad/kicad.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,19 @@ def parse_pad(self, pad):
def parse_modules(self, pcb_modules):
# type: (list) -> list
modules = []
for m in pcb_modules:
for m in pcb_modules: # type: pcbnew.MODULE
ref = m.GetReference()
center = self.normalize(m.GetCenter())

# bounding box
mrect = m.GetFootprintRect()
mrect_pos = self.normalize(mrect.GetPosition())
mrect_size = self.normalize(mrect.GetSize())
m_copy = pcbnew.MODULE(m)
m_copy.SetOrientation(0)
m_copy.SetPosition(pcbnew.wxPoint(0, 0))
mrect = m_copy.GetFootprintRect()
bbox = {
"pos": mrect_pos,
"size": mrect_size
"pos": self.normalize(m.GetPosition()),
"relpos": self.normalize(mrect.GetPosition()),
"size": self.normalize(mrect.GetSize()),
"angle": m.GetOrientation() * 0.1,
}

# graphical drawings
Expand Down Expand Up @@ -329,7 +331,6 @@ def parse_modules(self, pcb_modules):
# add module
modules.append({
"ref": ref,
"center": center,
"bbox": bbox,
"pads": pads,
"drawings": drawings,
Expand Down
21 changes: 12 additions & 9 deletions InteractiveHtmlBom/web/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,13 @@ function drawModule(ctx, layer, scalefactor, module, padcolor, outlinecolor, hig
ctx.save();
ctx.globalAlpha = 0.2;
ctx.translate(...module.bbox.pos);
ctx.rotate(deg2rad(-module.bbox.angle));
ctx.translate(...module.bbox.relpos);
ctx.fillStyle = padcolor;
ctx.fillRect(
0, 0,
...module.bbox.size);
ctx.fillRect(0, 0, ...module.bbox.size);
ctx.globalAlpha = 1;
ctx.strokeStyle = padcolor;
ctx.strokeRect(
0, 0,
...module.bbox.size);
ctx.strokeRect(0, 0, ...module.bbox.size);
ctx.restore();
}
}
Expand Down Expand Up @@ -605,14 +603,19 @@ function netHitScan(layer, x, y) {
return null;
}

function pointWithinModuleBbox(x, y, bbox) {
var v = [x - bbox.pos[0], y - bbox.pos[1]];
v = rotateVector(v, bbox.angle);
return bbox.relpos[0] <= v[0] && v[0] <= bbox.relpos[0] + bbox.size[0] &&
bbox.relpos[1] <= v[1] && v[1] <= bbox.relpos[1] + bbox.size[1];
}

function bboxHitScan(layer, x, y) {
var result = [];
for (var i = 0; i < pcbdata.modules.length; i++) {
var module = pcbdata.modules[i];
if (module.layer == layer) {
var b = module.bbox;
if (b.pos[0] <= x && b.pos[0] + b.size[0] >= x &&
b.pos[1] <= y && b.pos[1] + b.size[1] >= y) {
if (pointWithinModuleBbox(x, y, module.bbox)) {
result.push(i);
}
}
Expand Down

0 comments on commit 5e4ee16

Please sign in to comment.