Body Decomposition#
The Feature-Driven Pipeline ends by reading the defeatured part as a stock: one extrusion or one revolution the part was built on. Many parts are not one primitive, though. A plate with a pad standing on its corner is an extrusion with another extrusion on top, and no single reader reads it. Body decomposition is what the pipeline does then: it takes visible primitives off the part one at a time, the ones standing on faces of their own first and the largest first among those, until a stock reader reads what is left. What it read is the stock; what it took off are the body additions that go back on it.
Note
Prerequisites: Defeaturing Concepts and Feature-Driven Pipeline.
When it runs#
You do not call the decomposition yourself. BRepDefeaturerProcessor.run()
starts it after the features are removed, in two situations:
no stock reader reads the remainder at all, or
a reader reads it, but its reading leaves faces of the remainder unexplained. The extrusion reader will happily read a profile pair and the laterals between them on a plate with a pad, and leave the pad’s own walls out of its reading. The revolution reader reads a shaft with a bar across it as the shaft; it explains the shaft’s turned faces, its end caps and the shoulders square to its axis, but not the bar’s sides.
When every face of the remainder is explained by a reading, nothing happens and the result is what the pipeline always gave you.
The decomposition runs by default. To bound the time it may take, give the
processor a deadline, an instant on the time.perf_counter() clock by
which the whole run should be over; the decomposition gets half of the time
left when it starts:
import time
result = BRepDefeaturerProcessor(solid, deadline=time.perf_counter() + 30).run()
The deadline is checked before each candidate is tried and before each body is
read, so a cut already under way is finished; a decomposition stopped by it
ends on "ran_out".
The worked example#
The part below is a plate with a square pad standing on its corner and a
round lug running through its right end. The plate is 60 by 40 and 10 thick;
the pad, 10 by 10, stands 30 high in the corner where the plate’s left side
meets its back; the lug, a cylinder of radius 10, runs across the plate’s
width along its right end. Load it
(download plate_with_pad_and_lug.step,
in millimetres) to reproduce every number on this page.
from volmdlr.model import VolumeModel
from volmdlr_tools.graph.faces import AttributedAdjacencyGraph
from volmdlr_tools.shape_editing import BRepDefeaturerProcessor
solid = VolumeModel.from_step("plate_with_pad_and_lug.step", reading_unit="MM").primitives[0]
aag = AttributedAdjacencyGraph(solid, allow_smooth=False)
result = BRepDefeaturerProcessor(solid, aag=aag).run()
The pad stands on the corner on purpose. A pad standing mid-edge, flush with one side of the plate, is read by the feature stage as an addition and taken off there, before the stock stage runs; the decomposition never sees it. A pad on the corner splits no face of the plate in two, so the feature stage leaves it, and the stock stage receives the part as it is.
The part from three sides, each face numbered with the index the result reports.#
The part has nine faces. Their indices are the ones the result reports, so it is worth knowing which is which:
Face |
What it is |
|---|---|
0 |
The left side of the plate and of the pad, one L-shaped planar face (the pad’s outer wall is flush with the plate’s edge) |
1 |
The front face of the plate, merged with the lug’s end disc |
2 |
The bottom of the plate, up to the lug |
3 |
The back face of the plate and of the pad, one L-shaped planar face, merged with the lug’s other end disc |
4 |
The top of the pad |
5 |
The pad’s front wall, the one facing the plate’s middle |
6 |
The top of the plate, up to the lug; the pad’s footprint is cut out of its corner |
7 |
The lug’s cylindrical surface |
8 |
The pad’s inner wall, the one facing the plate’s centre along its length |
There are no holes, pockets or blends to remove, so the feature stage leaves the part as it is and the stock stage receives all nine faces.
Step 1 — no stock reader reads the part#
The extrusion reader needs a direction every face of the part runs along or stands across, and there is none: along the plate’s width the pad’s front wall stands in the way, along its length the pad’s inner wall does, and along the pad’s height the lug is round. The revolution reader needs an axis every face turns about, and only the lug has one. Nothing is read, which is the first of the two situations above, so the pipeline decomposes.
Step 2 — reading the candidates#
Each step of the decomposition reads every primitive it can see on the body as a candidate: every extrusion the prism reader finds on the body’s faces, and every revolution the revolution reader finds about the body’s axes. A reader closes a profile with chords where the faces it read do not close it, so some candidates are blocks and wedges no designer drew. On this part it sees nine.
The candidates are tried in an order, and the order matters, because the first one that can be cut off is the one taken:
A candidate standing on faces of its own comes first: every face it was read off lies within it, the way the pad’s two inner walls lie within the pad. One read off a face the body runs on past it, the way the plate’s side runs on past the pad, is as much a slice through the body as a thing on it, and comes after.
A candidate bigger than half the body is presumed to be the root, not an addition, and comes last: taking the biggest primitive off a body is taking the stock off it, which leaves the additions as the thing to read.
Among equals, the largest first, because the biggest thing you can take off is the one that gets you to a readable stock in the fewest steps.
At most eight candidates are tried in one step. In order, the ones this step has:
Kind |
Volume (mm³) |
Read off faces |
What it is |
|---|---|---|---|
revolved |
12 566 |
7 |
The lug, read as a revolution about its own axis. Its one face is its own. |
extrusion |
3 000 |
5, 8 |
The pad: its two inner walls, closed by two chords along the plate’s side and back into a 10 × 10 footprint, pushed 30 mm up from the plate’s top. Both faces are its own. |
extrusion |
4 000 |
0, 3 |
The column: the pad’s footprint pushed through the pad and the plate under it, read off the plate’s side and back, which run on past it |
extrusion |
3 500 |
0, 4, 8 |
The same column read along the plate’s width, off the left side, the pad’s top and its inner wall, closed by one chord |
extrusion |
3 500 |
3, 4, 5 |
The same column read along the plate’s length, off the back, the pad’s top and its front wall, closed by one chord |
The four candidates bigger than half the body (the plate-and-lug stock itself and three readings of the plate) come after these and are never reached.
Step 3 — refusing what is not a visible primitive#
A candidate is only removed when three checks pass, and the record keeps every refusal with its reason:
It must lie inside the body. The candidate is sampled on its surface and each sample must be on or inside the solid, within a tolerance scaled to the part. Every candidate tried on this part passes; the plate read along its height with a chord over the lug does not, but it is a presumed root and is never reached.
Cutting it off must merge a face away. Taking a primitive off a part must leave fewer faces than there were, otherwise it was not a visible primitive sitting on the body but a slice through it. The lug lies inside the body, but cutting the whole cylinder off leaves a round groove through the plate with as many faces as the lug had: nine faces where there were nine. It is refused as
no face merged away (9 before, 9 after).The cut must take off the candidate’s own volume. A pad with a hole down through it is read as a solid block; cutting that block off takes the hole with it, so the cut removes less material than the block holds, and adding the block back would fill the hole. Such a candidate is refused as
covers a void (removed …, candidate …).
The 3 000 mm³ pad passes all three checks. It lies inside the body, and cutting it off leaves a body with six faces: the plate with its lug. The column would have passed too, with eight faces left, and it is the order of step 2 that keeps it from being taken first and slicing the corner off the plate.
What the step leaves: the pad taken off, and the plate with its lug as the body to read next.#
Step 4 — the root#
The six-face body is offered to the stock readers again. The extrusion reader
reads it whole, off its front and back faces, and every one of its six faces
is explained. The decomposition ends on "root" after one step. If no
reader had read it, the next step would have read the candidates of the
six-face body and gone on.
The outcome is published on the result:
>>> result.stock_type
'extrusion'
>>> result.decomposition.ended
'root'
>>> [(s.kind, s.volume, s.faces_before, s.faces_after) for s in result.decomposition.steps]
[('extrusion', 3000.0, 9, 6)]
>>> [(r.kind, round(r.volume), r.reason) for r in result.decomposition.refusals]
[('revolved', 12566, 'no face merged away (9 before, 9 after)')]
Step 5 — lineage, back to the faces you gave it#
The stock is a new solid with its own face indices, and the pad was cut off a copy of the remainder. Neither is much use unless you can say which faces of your part each one accounts for. The result carries that in two fields.
stock_lineage maps each face of result.stock_shape to the input faces
behind it:
>>> result.stock_lineage
{0: (0,), 1: (1,), 2: (6,), 3: (3,), 4: (2,), 5: (7,)}
Six stock faces, six input faces: the plate’s sides, top, bottom and the lug. Notice faces 0 and 3: the plate’s side and the pad’s outer wall were one planar face in the input, and so were its back and the pad’s back wall. Both stay with the stock.
body_additions lists what was taken off, in the order it goes back on.
Every id it gives is a face of the input: the faces each addition owns, those
it was attached through, and among its own the end caps where it stops:
>>> addition, = result.body_additions
>>> addition.kind, addition.volume
('extrusion', 3000.0)
>>> addition.face_ids
(4, 5, 8)
>>> addition.interface_face_ids
(0, 3, 6)
>>> addition.end_face_ids
(4,)
The pad owns its top and its two inner walls, and its top is its end cap. Its interface faces are the input faces its cut touched: the plate’s side, back and top. Faces 0 and 3 are not in the addition’s faces even though the pad’s outer walls are part of them: an input face belongs to one step, and the stock claimed them first. Together, the stock’s six faces and the addition’s three cover all nine faces of the input exactly once.
The same answer is in result.claims, which every consumer of the pipeline
reads: the stock is claimed as an extrusion with heuristic_source
"base_extrusion", and each extrusion added back as one with
"body_addition", after the stock, so a face the stock owns stays with it. A
revolved addition, like a revolved stock, has no feature of its own; read it
from body_additions.
Step 6 — building it back#
The CAD reconstruction of volmdlr_build runs the same pipeline and turns
the stock and the additions into a build123d script: here the plate-and-lug
profile, a rectangle closed by an arc, extruded through the plate, and the
pad’s footprint extruded 30 mm up from the plate’s top. Its own documentation
describes the script and the report it writes.
How a decomposition ends#
result.decomposition is None when the stock reader explained every face
and nothing had to be decomposed. Otherwise its ended field tells you how
it went:
|
Meaning |
|---|---|
|
A stock reader read what was left. |
|
No candidate of the body could be removed: every one was refused. The stock is what the readers gave before the decomposition, if anything. |
|
The allowed number of steps was used up without a reader reading the body. |
|
The |
|
The body was not a solid the readers can read; |
|
Reading a body or asking a stock reader of it raised; |
Whatever the ending, steps and refusals are the full account of what
was tried, and result.defeatured_shape is the remainder the decomposition
started from, so you can inspect it when the ending is not "root".
See Also#
Feature-Driven Pipeline - The pipeline the decomposition is a stage of
Defeaturer - Manual, step-by-step defeaturing
Body Decomposition Algorithm - How the algorithm works, step by step, for developers