Defeaturer#
The high-level, chainable API for defeaturing CAD models.
Note
Prerequisites: Defeaturing Concepts
This tutorial assumes you understand the basics of defeaturing, including the distinction between soft and hard features. If you haven’t read the concepts section, we recommend starting there.
Tip
Tutorial Data File: ANC101.brep
Using the Defeaturer#
The Defeaturer class is the recommended entry point for defeaturing operations. It provides a simple, chainable API for removing features from CAD models while automatically tracking the history of modifications.
Quick Start#
Here’s a minimal example to get you started:
from volmdlr.shapes import Solid
from volmdlr_tools.shape_editing import Defeaturer
# Load your model
solid = Solid.from_brep("data/brep/ANC101.brep")
# Remove all cavities and blends
result = (
Defeaturer(solid)
.remove_cavities()
.remove_blends(max_radius=5.0)
.result
)
In just a few lines, you’ve removed holes, pockets, and fillets/chamfers from your model.
Step-by-Step Tutorial#
Step 1: Create a Defeaturer#
Start by wrapping your solid in a Defeaturer instance:
from volmdlr.shapes import Solid
from volmdlr_tools.shape_editing import Defeaturer
# Load a model
solid = Solid.from_brep("data/brep/ANC101.brep")
# Create the defeaturer
defeaturer = Defeaturer(solid)
# Check initial state
print(f"Original shape has {defeaturer.face_count} faces")
The Defeaturer keeps track of:
The original shape (never modified)
The current shape (updated after each operation)
A history of all operations performed
Step 2: Remove Features#
The Defeaturer provides several methods to remove different types of features.
Removing Cavities#
Cavities include holes, pockets, and other concave features that cut into the solid:
defeaturer.remove_cavities()
print(f"After cavity removal: {defeaturer.face_count} faces")
This method uses the FeatureProcessor internally to identify all cavities, then removes them.
Removing Blends (Fillets and Chamfers)#
Blends are the rounded or beveled edges that smooth transitions between faces:
defeaturer.remove_blends(max_radius=10.0)
Parameters:
max_radius: Only remove blends with radius smaller than this value. Larger blends are preserved. Default is0.5.incremental: IfTrue, removes blends one at a time, re-analyzing after each removal. This is slower but more robust for complex geometries. Default isFalse.check_validity: IfTrue, validates the result after each blend removal. Default isFalse.
Removing Specific Features#
If you know exactly which features to remove, you can extract them first using FeatureProcessor and pass them directly:
from volmdlr_tools.features import FeatureProcessor
# Extract features from the shape
processor = FeatureProcessor(shape=defeaturer.shape)
processor.extract_cavities()
# Remove only the first 3 cavities
defeaturer.remove_features(processor.cavities[:3])
Removing by Face Indices#
For fine-grained control, you can remove faces by their indices:
# Remove faces 5, 6, and 7
defeaturer.remove_by_face_indices([5, 6, 7])
Face indices are 0-based and always refer to the faces of the original shape, however many removals came before.
Faces are first tried as a cut lying inside the faces around it, which can be taken off on its own, before the
general removal. A cut that reaches the outer boundary of the faces around it, such as a step or a slot open at an
edge, is only taken off by the general removal: pass isolated=False to go straight to it.
defeaturer.remove_by_face_indices([5, 6, 7], isolated=False)
Filling a cut back#
Defeaturer.fill_feature(feature, aag, face_indices=None) takes a cut off by adding back the material it cut
away. The feature, and the face adjacency graph it was read from, belong to the original shape; the material is
added to the current one. A pocket, slot or step is filled along the way it was cut, and a shoulder turned on a shaft
by turning its section back about the shaft’s axis; holes are not filled. face_indices are the faces of the
original shape the fill has to take off, all of the feature’s faces when not given.
from volmdlr.shapes import Solid
from volmdlr_tools.features import FeatureProcessor
from volmdlr_tools.shape_editing import Defeaturer
solid = Solid.from_brep("data/brep/ANC101.brep")
processor = FeatureProcessor(shape=solid)
processor.extract_interacting_features()
# The pocket with the fewest faces
pocket = min(processor.pockets, key=lambda pocket: len(pocket.nodes))
print(type(pocket).__name__, sorted(pocket.nodes))
# Pocket [10, 11, 47]
defeaturer = Defeaturer(solid).fill_feature(pocket, processor.aag)
print(f"{defeaturer.original_face_count} faces -> {defeaturer.face_count} faces")
# 87 faces -> 83 faces
print(f"volume: {solid.volume():.0f} -> {defeaturer.result.volume():.0f}")
# volume: 8182350 -> 8200752
step = defeaturer.history[-1]
print(step.operation, sorted(step.removed_face_indices), step.faces_before, step.faces_after)
# fill_feature [10, 11, 47] 87 83
Left: the pocket filled in the example, in orange. Right: the part after the fill.#
The shape is left as it was, and no step is recorded, when the cut cannot be filled, when the fill would leave
some of the faces it has to take off standing, or when it would take off faces outside the cut as well. A fill that
is taken is recorded in history as a step whose operation is "fill_feature".
Removing a feature by deleting its faces#
FaceDeletionRemover (in volmdlr_tools.shape_editing.face_deletion) removes one feature, given its faces, on its
own. It drops the feature’s faces and lets the faces around it grow back over the opening, each on its own surface,
until they meet. It takes:
a cut whose rim runs over two or more faces: a slot or groove open at an edge, a rebate on a corner, a step;
a cut lying wholly inside the faces around it: a pocket sunk into a top, a hole straight through;
an addition: a lug or an arm standing flush on a block, a boss standing on a face. The faces it stood on close under it.
The result is accepted only if it is one valid solid with the topology the removal should leave; anything else is
refused. The Defeaturer does not use this remover yet: call it yourself.
IsolatedFeatureRemover also takes off a feature lying wholly inside the faces around it. Use it when only the part
is wanted and its faces are to be meshed for display, as the Defeaturer does. Use FaceDeletionRemover when the
feature’s volume is wanted too, when the removal must be checked to leave one valid solid, or when the feature’s rim
runs over two or more faces, which IsolatedFeatureRemover does not take.
from volmdlr.shapes import Solid
from volmdlr_tools.features import FeatureProcessor
from volmdlr_tools.shape_editing.face_deletion import FaceDeletionRemover
solid = Solid.from_brep("data/brep/ANC101.brep")
processor = FeatureProcessor(shape=solid)
processor.extract_cavities()
# The cavity with the most faces, the one with the lowest face index on a tie
pocket = min(processor.cavities, key=lambda cavity: (-len(cavity.nodes), min(cavity.nodes)))
print(sorted(pocket.nodes))
# [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 55, 56]
removal = FaceDeletionRemover(solid, aag=processor.aag).remove(pocket.nodes)
print(removal.removed, removal.added)
# True False
print(f"{len(solid.faces)} faces -> {len(removal.result.faces)} faces")
# 87 faces -> 74 faces
print(f"feature volume: {removal.feature_volume.volume():.1f}")
# feature volume: 21611.6
print(f"part volume: {solid.volume():.0f} -> {removal.result.volume():.0f}")
# part volume: 8182350 -> 8203962
Left: the cavity removed in the example, in blue. Right: the part after the removal, the end face closed over it.#
FaceDeletionRemover(shape, aag=None) takes the part and, optionally, its face adjacency graph (made from the part
when not given). Face indices are 0-based, as everywhere in the package.
remove(face_indices)returns aFaceDeletion.remove_features(face_indices)returnsTruewhen the feature was removed, like the other removers; the part without it is then inresult(Nonewhen refused).
A FaceDeletion holds:
Field |
Meaning |
|---|---|
|
the part without the feature, or |
|
the material the feature took away (a cut) or added (an addition); |
|
why |
|
|
|
the step that refused, or |
|
why that step refused; empty when the feature was removed |
|
a |
|
|
The feature volume is a by-product: a removal whose result passes every check stands even when the volume does not
close into one valid solid, as where a face closes across one of the feature’s own edges, so that the material
taken and the material given back are not one piece. feature_volume is then None, feature_volume_problem
says why, and a warning is logged.
A refusal comes back as a FaceDeletion, not as an exception, and so does a failure of the geometry kernel while
the feature is removed. Any other error is a fault of the remover and is raised:
refusal = FaceDeletionRemover(solid).remove([1000])
print(refusal.removed, refusal.refused_at, refusal.reason)
# False loop the feature's faces are not faces of the part
Inside the remover, each step refuses by raising FaceDeletionRefusedError(step, reason), which carries the same
step and reason. The steps, as RemovalStep values, are: "loop" (reading the opening the feature leaves),
"chords" (closing it across edges the feature interrupts), "extension" (carrying edges across to an opposite
face, which also refuses a new edge of no finite extent), "rewrite" (rebuilding the faces around the feature),
"feature_volume" (walking round the caps that close the feature’s volume) and "validation" (checking the result).
Step 3: Get Results#
After performing operations, access the modified shape:
# Get the modified shape
result = defeaturer.result # or defeaturer.shape
# Compare face counts
print(f"Original: {defeaturer.original_face_count} faces")
print(f"Current: {defeaturer.face_count} faces")
print(f"Faces removed: {defeaturer.original_face_count - defeaturer.face_count}")
You can also get a formatted summary:
print(defeaturer.summary())
Output:
Defeaturing Summary
========================================
Original faces: 156
Current faces: 42
Operations: 2
1. remove_cavities
Features removed: 8
Faces: 156 -> 98
2. remove_blends
Features removed: 12
Faces: 98 -> 42
Parameters: max_radius=5.0, incremental=False
The Fluent Interface#
All Defeaturer methods return self, enabling method chaining (the fluent pattern):
result = (
Defeaturer(solid)
.remove_cavities()
.remove_blends(max_radius=2.0)
.remove_by_face_indices([10, 11])
.result
)
This produces the same result as calling each method separately but is more concise.
Inspecting Operation History#
Every operation is recorded in the history list:
for step in defeaturer.history:
print(f"Operation: {step.operation}")
print(f" Features removed: {step.features_removed}")
print(f" Faces: {step.faces_before} -> {step.faces_after}")
if step.parameters:
print(f" Parameters: {step.parameters}")
Each DefeaturingStep contains:
operation: Name of the operation (e.g.,"remove_cavities")features_removed: Number of features removedfaces_before/faces_after: Face counts before and afterparameters: Any parameters passed to the operationremoved_face_indices: Indices of faces that were removed
Querying Shape Evolution#
For detailed tracking of what happened to specific faces, use the history_graph:
# Faces of the original shape are named by their index
face_id = 0
# Check if it was deleted
if defeaturer.is_face_deleted(face_id):
print("Face was deleted")
else:
# Get where it ended up in the current shape
final_faces = defeaturer.get_last_modified(face_id)
print(f"Face evolved into {len(final_faces)} face(s)")
For more details, see the History Tracking section.
Resetting#
To start over with the original shape:
defeaturer.reset()
print(f"Back to {defeaturer.face_count} faces") # Same as original
This clears all modifications and history.
Complete Example#
Here’s a full working example:
from volmdlr.shapes import Solid
from volmdlr_tools.shape_editing import Defeaturer
# Load a complex part
solid = Solid.from_brep("data/brep/ANC101.brep")
print(f"Loaded model with {len(list(solid.faces))} faces")
# Create defeaturer
defeaturer = Defeaturer(solid)
# Remove features step by step
defeaturer.remove_cavities()
print(f"After cavity removal: {defeaturer.face_count} faces")
defeaturer.remove_blends(max_radius=3.0)
print(f"After blend removal: {defeaturer.face_count} faces")
# Get the simplified result
simplified = defeaturer.result
# Print summary
print("\n" + defeaturer.summary())
# Check what happened to a specific face
evolution = defeaturer.get_face_evolution(10)
print(f"\nFace 10 evolution:")
print(f" Deleted: {evolution['deleted']}")
print(f" Modified: {evolution['modified']}")
print(f" Operations: {evolution['operations']}")
Tips and Best Practices#
Start with cavities: Removing cavities first often simplifies blend removal.
Use appropriate
max_radius: Settingmax_radiustoo high may remove structural features; too low may miss decorative fillets.Try incremental mode for complex parts: If single-pass blend removal fails, try
incremental=True:defeaturer.remove_blends(max_radius=5.0, incremental=True)
Check the summary: Always review
defeaturer.summary()to verify the operation performed as expected.Preserve the original: The
original_shapeproperty always gives you the unmodified input.
Next Steps#
History Tracking: Learn to trace shape evolution through operations
Concepts: Understand soft vs hard features and the defeaturing strategy
See Also#
Defeaturing Concepts - Understanding defeaturing fundamentals
History Tracking - Tracking shape evolution
Feature Processor - Extracting features to remove