Shift all positions so the bounding-rect centre lands on the origin. Keeps every item on the grid by snapping the shift to grid_size. Called before saving so the file always loads with content near (0,0).
(self, grid_size: int = 5)
| 503 | model_defs=model_defs, properties=properties) |
| 504 | |
| 505 | def normalize_origin(self, grid_size: int = 5) -> None: |
| 506 | """Shift all positions so the bounding-rect centre lands on the origin. |
| 507 | |
| 508 | Keeps every item on the grid by snapping the shift to grid_size. |
| 509 | Called before saving so the file always loads with content near (0,0). |
| 510 | """ |
| 511 | xs, ys = [], [] |
| 512 | for c in self.components: |
| 513 | xs.append(c.x); ys.append(c.y) |
| 514 | for w in self.wires: |
| 515 | for px, py in w.points: |
| 516 | xs.append(px); ys.append(py) |
| 517 | for j in self.junctions: |
| 518 | xs.append(j.x); ys.append(j.y) |
| 519 | for item in (*self.free_texts, *self.hyperlinks, *self.commands, |
| 520 | *self.libs, *self.images, *self.latex_fragments, |
| 521 | *self.parameters, *self.analysis_items, *self.model_defs): |
| 522 | xs.append(item.x); ys.append(item.y) |
| 523 | for s in self.shapes: |
| 524 | xs.append(s.x); ys.append(s.y) |
| 525 | if self.border is not None: |
| 526 | xs.append(self.border.x); ys.append(self.border.y) |
| 527 | |
| 528 | if not xs: |
| 529 | return |
| 530 | |
| 531 | cx = (min(xs) + max(xs)) / 2 |
| 532 | cy = (min(ys) + max(ys)) / 2 |
| 533 | dx = round(cx / grid_size) * grid_size |
| 534 | dy = round(cy / grid_size) * grid_size |
| 535 | if dx == 0 and dy == 0: |
| 536 | return |
| 537 | |
| 538 | for c in self.components: |
| 539 | c.x -= dx; c.y -= dy |
| 540 | for w in self.wires: |
| 541 | w.points = [(px - dx, py - dy) for px, py in w.points] |
| 542 | for j in self.junctions: |
| 543 | j.x -= dx; j.y -= dy |
| 544 | for item in (*self.free_texts, *self.hyperlinks, *self.commands, |
| 545 | *self.libs, *self.images, *self.latex_fragments, |
| 546 | *self.parameters, *self.analysis_items, *self.model_defs): |
| 547 | item.x -= dx; item.y -= dy |
| 548 | for s in self.shapes: |
| 549 | s.x -= dx; s.y -= dy |
| 550 | if self.border is not None: |
| 551 | self.border.x -= dx; self.border.y -= dy |
| 552 | |
| 553 | def save(self, path: Path) -> None: |
| 554 | self.normalize_origin() |