Add Assembly Mates Programmatically
Use the SolidWorks API to add coincident, distance, and angle mates in assemblies. Covers the confusing selection mark system that trips up most developers.
public void AddCoincidentMate(AssemblyDoc assy)
{
var model = (ModelDoc2)assy;
var ext = model.Extension;
// Clear any existing selection
model.ClearSelection2(true);
// Select first face (mark = 1 for mate entity 1)
ext.SelectByID2("", "FACE",
0.05, 0.0, 0.0, // point on face
false, 1, // mark = 1
null, (int)swSelectOption_e.swSelectOptionDefault);
// Select second face (mark = 1 for mate entity 2)
ext.SelectByID2("", "FACE",
0.10, 0.0, 0.0, // point on face
true, 1, // Append=true, mark = 1
null, (int)swSelectOption_e.swSelectOptionDefault);
// Add coincident mate
int errors;
var mate = assy.AddMate5(
(int)swMateType_e.swMateCOINCIDENT,
(int)swMateAlign_e.swMateAlignALIGNED,
false, // flip
0, 0, 0, // distance, distanceAbsUpperLimit, distanceAbsLowerLimit
0, 0, 0, // angle, angleAbsUpperLimit, angleAbsLowerLimit
0, 0, 0, // forceAttrValue, forceAttrUpperLimit, forceAttrLowerLimit
out errors);
if (errors != 0) Console.WriteLine($"Mate error: {errors}");
}The Selection Mark System
The hardest part of adding mates via the API isn't the AddMate call — it's the selection marks. Before calling AddMate, you must select entities (faces, edges, vertices, planes) and tag them with numeric marks that tell SolidWorks which entity plays which role.
For standard mates (coincident, parallel, perpendicular, distance, angle):
- Mark = 1: Mate entity 1 (first face/edge/plane)
- Mark = 1: Mate entity 2 (second face/edge/plane — yes, same mark value)
The second selection must have Append = true to add to the existing selection set.
Mate Types and Parameters
| Mate Type | swMateType_e Value | Key Parameters |
|---|---|---|
| Coincident | swMateCOINCIDENT | alignment only |
| Distance | swMateDISTANCE | distance value |
| Angle | swMateANGLE | angle in radians |
| Concentric | swMateCONCENTRIC | alignment |
| Parallel | swMatePARALLEL | alignment |
| Perpendicular | swMatePERPENDICULAR | alignment |
| Tangent | swMateTANGENT | alignment |
Mate Alignment
- swMateAlignALIGNED — face normals point in the same direction
- swMateAlignANTI_ALIGNED — face normals point in opposite directions (most common for coincident mates — faces touching)
For coincident mates where you want faces touching (like putting a bolt head flat against a surface), use ANTI_ALIGNED.
Selecting by Component Name
Instead of selecting by coordinates (fragile), select by component and face name:
ext.SelectByID2("Face<1>@Bracket-1@Assembly",
"FACE", 0, 0, 0, false, 1, null, 0);The format is: EntityName@ComponentName@AssemblyName
Troubleshooting
- Error = 1: Over-defined. The mate conflicts with existing constraints
- Error = 2: Inconsistent. The geometry doesn't support this mate type
- Mate adds but component doesn't move: Check alignment — you may need ANTI_ALIGNED instead of ALIGNED
- Nothing happens: Verify your selections have the correct marks. Call SelectionManager.GetSelectedObjectCount() to confirm entities are selected