Preventing Duplicate Dimensions for Identical Lengths in Revit API

Preventing Duplicate Dimensions for Identical Lengths in Revit API

DesignGroup01
Enthusiast Enthusiast
182 Views
1 Reply
Message 1 of 2

Preventing Duplicate Dimensions for Identical Lengths in Revit API

DesignGroup01
Enthusiast
Enthusiast

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:

  1. 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.

  2. 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!

0 Likes
183 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni

I totally agree with the idea of creating a unique key for each dimension and avoiding creation of duplicate dimensions for the same edge. However, I am very doubtful about basing that key on the dimension length alone. Certainly completely different edges may well have the same length in a model, so that cannot generally be used to differentiate them. A more reliable key to differentiate between dimensioning for different edges could probably be generated by using the stable representation of the references used. Here are some further thoughts on generating your own key:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes