Preventing Duplicate Dimensions for Identical Lengths in Revit API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, All
I am working on a custom dimensioning tool and I need to ensure that dimensions are not created multiple times for edges with the same length. Specifically, I want to skip any duplicate dimensions for edges that have already been processed based on their length.
Here’s the approach I am currently using:
Tracking Dimension Lengths: I use a HashSet<string> to track the unique lengths of dimensions that have already been created. The key for this HashSet is the length of the dimension, which is represented as a string.
Checking for Duplicate Dimensions: Before creating a new dimension, I generate a unique key for the dimension based on the length of the dimension line (dimensionLine.Length). If this key already exists in the HashSet, I skip creating the dimension for that edge, thus preventing duplicates.
Here’s the relevant code snippet:
// Track processed locations to avoid duplicate dimensions
HashSet<string> processedDimensions = new HashSet<string>();
// Iterate through each edge and create dimensions
foreach (Edge edge in edges)
{
Curve curve = edge.AsCurve();
if (curve is Line || curve is Arc)
{
XYZ startPoint = curve.GetEndPoint(0);
XYZ endPoint = curve.GetEndPoint(1);
// Skip if start and end points are identical (duplicate edge)
if (startPoint.IsAlmostEqualTo(endPoint))
{
continue; // Skip dimensioning for this edge
}
// Create the dimension direction (from the start to the end point)
XYZ dimensionDirection = endPoint - startPoint;
// If the direction is too small or zero, skip this dimension
if (dimensionDirection.IsZeroLength())
{
continue;
}
// Apply offset in the correct direction (cross product for orientation)
XYZ offsetVector = view.ViewDirection.CrossProduct(dimensionDirection).Normalize() * offsetDistance;
Line dimensionLine = Line.CreateBound(startPoint + offsetVector, endPoint + offsetVector);
// Generate a unique key for this dimension based on the start and end point coordinates
string dimensionKey = $"{dimensionLine.Length.ToString()}";
// Skip if this dimension has already been processed (avoid duplicate dimensions)
if (processedDimensions.Contains(dimensionKey))
{
continue; // Skip if the dimension for this edge has already been created
}
try
{
// Initialize the ReferenceArray for the edge
ReferenceArray refArray = new ReferenceArray();
// Add the references for the start and end points of the edge
refArray.Append(edge.GetEndPointReference(0)); // Start point reference
refArray.Append(edge.GetEndPointReference(1)); // End point reference
// Ensure the ReferenceArray contains at least two references (for valid dimension)
if (refArray.Size < 2)
{
//TaskDialog.Show("Error", "Not enough references for dimension creation.");
continue;
}
// Create the dimension with the calculated dimension line
doc.Create.NewDimension(view, dimensionLine, refArray);
// Mark this edge as processed by adding the unique key
processedDimensions.Add(dimensionKey);
}
catch (Exception ex)
{
// If dimension creation fails, log the error
dimensionCreationFailed = true;
// TaskDialog.Show("Error", $"Failed to create dimension: {ex.Message}");
}
}
}
Expected Outcome:
- If the same length is encountered again (for a different edge or the same edge), the dimension creation is skipped.
- This avoids creating multiple dimensions for edges with identical lengths.
Thank you in advance for your help!