Skip to content

Commit 5e4ee16

Browse files
committed
Rotated parts get rotated bounding box
Issue #104
1 parent 531db45 commit 5e4ee16

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

InteractiveHtmlBom/ecad/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def to_component_dict(self):
6969
# type: () -> dict
7070
return {
7171
"pos": [self._x0, self._y0],
72+
"relpos": [0, 0],
7273
"size": [self._x1 - self._x0, self._y1 - self._y0],
74+
"angle": 0,
7375
}
7476

7577
def add(self, other):

InteractiveHtmlBom/ecad/kicad.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,19 @@ def parse_pad(self, pad):
281281
def parse_modules(self, pcb_modules):
282282
# type: (list) -> list
283283
modules = []
284-
for m in pcb_modules:
284+
for m in pcb_modules: # type: pcbnew.MODULE
285285
ref = m.GetReference()
286-
center = self.normalize(m.GetCenter())
287286

288287
# bounding box
289-
mrect = m.GetFootprintRect()
290-
mrect_pos = self.normalize(mrect.GetPosition())
291-
mrect_size = self.normalize(mrect.GetSize())
288+
m_copy = pcbnew.MODULE(m)
289+
m_copy.SetOrientation(0)
290+
m_copy.SetPosition(pcbnew.wxPoint(0, 0))
291+
mrect = m_copy.GetFootprintRect()
292292
bbox = {
293-
"pos": mrect_pos,
294-
"size": mrect_size
293+
"pos": self.normalize(m.GetPosition()),
294+
"relpos": self.normalize(mrect.GetPosition()),
295+
"size": self.normalize(mrect.GetSize()),
296+
"angle": m.GetOrientation() * 0.1,
295297
}
296298

297299
# graphical drawings
@@ -329,7 +331,6 @@ def parse_modules(self, pcb_modules):
329331
# add module
330332
modules.append({
331333
"ref": ref,
332-
"center": center,
333334
"bbox": bbox,
334335
"pads": pads,
335336
"drawings": drawings,

InteractiveHtmlBom/web/render.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,13 @@ function drawModule(ctx, layer, scalefactor, module, padcolor, outlinecolor, hig
270270
ctx.save();
271271
ctx.globalAlpha = 0.2;
272272
ctx.translate(...module.bbox.pos);
273+
ctx.rotate(deg2rad(-module.bbox.angle));
274+
ctx.translate(...module.bbox.relpos);
273275
ctx.fillStyle = padcolor;
274-
ctx.fillRect(
275-
0, 0,
276-
...module.bbox.size);
276+
ctx.fillRect(0, 0, ...module.bbox.size);
277277
ctx.globalAlpha = 1;
278278
ctx.strokeStyle = padcolor;
279-
ctx.strokeRect(
280-
0, 0,
281-
...module.bbox.size);
279+
ctx.strokeRect(0, 0, ...module.bbox.size);
282280
ctx.restore();
283281
}
284282
}
@@ -605,14 +603,19 @@ function netHitScan(layer, x, y) {
605603
return null;
606604
}
607605

606+
function pointWithinModuleBbox(x, y, bbox) {
607+
var v = [x - bbox.pos[0], y - bbox.pos[1]];
608+
v = rotateVector(v, bbox.angle);
609+
return bbox.relpos[0] <= v[0] && v[0] <= bbox.relpos[0] + bbox.size[0] &&
610+
bbox.relpos[1] <= v[1] && v[1] <= bbox.relpos[1] + bbox.size[1];
611+
}
612+
608613
function bboxHitScan(layer, x, y) {
609614
var result = [];
610615
for (var i = 0; i < pcbdata.modules.length; i++) {
611616
var module = pcbdata.modules[i];
612617
if (module.layer == layer) {
613-
var b = module.bbox;
614-
if (b.pos[0] <= x && b.pos[0] + b.size[0] >= x &&
615-
b.pos[1] <= y && b.pos[1] + b.size[1] >= y) {
618+
if (pointWithinModuleBbox(x, y, module.bbox)) {
616619
result.push(i);
617620
}
618621
}

0 commit comments

Comments
 (0)