Create Temporary Bodies with IModeler
Use IModeler to create geometry without features — the advanced SolidWorks API technique for fast mass property calculations, interference checks, and custom geometry operations.
public void TempBodyDemo(SldWorks swApp, ModelDoc2 model)
{
var modeler = (Modeler)swApp.GetModeler();
var mathUtil = (MathUtility)swApp.GetMathUtility();
// Create a temp cylinder (center, axis, radius, height)
double[] center = { 0, 0, 0 };
double[] axis = { 0, 1, 0 };
var body = (Body2)modeler.CreateBodyFromCyl(
new double[] {
center[0], center[1], center[2], // base center
axis[0], axis[1], axis[2], // axis direction
0.05, // radius (50mm)
0.10 // height (100mm)
});
// Get mass properties without creating a feature
int status = 0;
var props = (double[])body.GetMassProperties(
1, // density (1 kg/m^3 for volume only)
ref status);
Console.WriteLine($"Volume: {props[3]:F6} m^3");
// Boolean subtract: remove cylinder from existing body
var partDoc = (PartDoc)model;
var bodies = (object[])partDoc.GetBodies2(
(int)swBodyType_e.swSolidBody, true);
var mainBody = (Body2)bodies[0];
int err;
var results = (object[])mainBody.Operations2(
(int)swBodyOperationType_e.SWBODYCUT,
body, out err);
// Convert result to a real feature if needed
if (results != null && results.Length > 0)
partDoc.CreateFeatureFromBody3(
(Body2)results[0], false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify);
}What Are Temporary Bodies?
In the normal SolidWorks workflow, every piece of geometry is created through a feature — an extrude, a revolve, a fillet. Each feature appears in the Feature Manager tree. Temporary bodies bypass this entirely.
The IModeler interface provides 30+ methods to create bodies directly in memory:
- CreateBodyFromCyl — cylinder/tube
- CreateBodyFromBox — rectangular box
- CreateBodyFromCone — cone/frustum
- CreateSheetFromSurface — surface body from a mathematical surface
- CreateWireBody — wire body from curves
These bodies exist only in memory. They don't appear in the Feature Manager tree. They don't trigger rebuilds. They're fast.
Why Use Temporary Bodies?
- Performance: Creating a temp body is orders of magnitude faster than creating a feature and deleting it. For batch operations (calculating mass properties for 1000 configurations), this matters enormously
- Interference checking: Create a temp body representing a clearance zone, then boolean-intersect it with existing bodies to find collisions
- Custom validation: Build a bounding region and check if parts fit within it
- Geometry computation: Get volume, center of mass, or surface area without modifying the model
Boolean Operations
IBody2.Operations2 performs boolean operations between two bodies:
- SWBODYADD — union (combine two bodies into one)
- SWBODYCUT — subtract (remove one body from another)
- SWBODYINTERSECT — intersection (keep only the overlapping region)
The result is one or more new temporary bodies. The original bodies are not modified.
Converting to Features
If you want the temp body to become a permanent part of the model:
partDoc.CreateFeatureFromBody3(tempBody, false, opts);This adds the body as an "Imported" feature in the tree.
Tips
- Always access IModeler from the SldWorks application object, not from a document
- Temp bodies are garbage-collected when no references remain — hold a variable reference if you need them to persist
- The density parameter in GetMassProperties is in kg/m^3 — pass 1.0 to get raw volume, or the actual material density for real mass
- Temp bodies can be displayed visually using IBody2.Display3 for debugging — useful to verify your geometry is correct before boolean operations