Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 67020

Re: distance between the dimension line and witness line endpoint

$
0
0

I have not tested myself whether this works, but the approach suggested by Gemini looks quite sensible to me:

    

You're looking to control the gap between the dimension line and the witness line endpoint—often called the "extension line offset" or "witness line gap." While the Revit API doesn't directly expose a settable property for this, we can get the value and use some clever manipulation to effectively set it. It involves working with dimension segments and their geometry.

Getting the Current Gap:

The key is to access the DimensionSegment of the dimension. Each dimension has one or more segments, and these segments define the geometry, including the witness lines.

C#
 
// Assuming 'dimension' is your Dimension element.foreach (DimensionSegment segment in dimension.Segments)
{// Get the start point of the witness line.
    XYZ witnessLineStart = segment.WitnessLineStart;// Get the point on the dimension line corresponding to the witness line.
    XYZ dimensionLinePoint = segment.DimensionLinePoint;// Calculate the distance.double gap = witnessLineStart.DistanceTo(dimensionLinePoint);// 'gap' now holds the distance between the witness line and dimension line.
    TaskDialog.TaskDialog.Show("Gap Distance", gap.ToString()); // Display the gap.
}

"Setting" the Gap (The Workaround):

Since we can't directly set the gap, the most effective approach is to move the dimension text. This might seem counterintuitive, but by carefully adjusting the dimension text position, we indirectly control the perceived gap. Here's the strategy:

  1. Get the Current Gap and Dimension Text Position: Use the code above to get the current gap. Also, get the XYZlocation of the dimension text.

  2. Calculate the New Text Position: Based on the desired gap and the dimension's orientation, calculate where the dimension text should be moved. This requires vector math.

  3. Move the Dimension Text: Use the Dimension.MoveText() method to relocate the dimension text.

Code Example (Setting the Gap):

C#
 
// ... (Get the dimension and desiredGap - the new gap value)foreach (DimensionSegment segment in dimension.Segments)
{
    XYZ witnessLineStart = segment.WitnessLineStart;
    XYZ dimensionLinePoint = segment.DimensionLinePoint;double currentGap = witnessLineStart.DistanceTo(dimensionLinePoint);// 1. Get the current dimension text position.
    XYZ textPosition = dimension.TextPosition;// 2. Calculate the translation vector.
    XYZ translationVector = GetTranslationVector(witnessLineStart, dimensionLinePoint, currentGap, desiredGap);// 3. Calculate the new text position.
    XYZ newTextPosition = textPosition.Add(translationVector);// 4. Move the dimension text.using (Transaction t = new Transaction(doc, "Set Dimension Gap"))
    {
        t.Start();
        dimension.MoveText(newTextPosition);
        t.Commit();
    }
}//Helper function to calculate the translation vectorprivate XYZ GetTranslationVector(XYZ witnessLineStart, XYZ dimensionLinePoint, double currentGap, double desiredGap)
{// Vector from dimension line point to witness line start.
    XYZ gapVector = witnessLineStart.Subtract(dimensionLinePoint).Normalize();// Calculate the difference in gaps.double gapDifference = desiredGap - currentGap;// Scale the gap vector by the difference.
    XYZ translationVector = gapVector.Multiply(gapDifference);return translationVector;
}

Explanation and Crucial Points:

  • GetTranslationVector(): This helper function calculates the vector by which the text needs to be moved. The direction is determined from the dimension line point to the witness line start, and the magnitude is the difference between the desired and current gaps.
  • Transaction: Modifying the dimension requires a transaction.
  • Dimension Orientation: The direction of the translation vector is crucial. The provided code assumes a typical dimension orientation. You may need to adjust the vector calculation based on the specific dimension type (aligned, linear, etc.) and its orientation in 3D space. Consider using the dimension.Orientation property for more robust handling.
  • Limitations: This approach indirectly sets the gap by moving the text. The actual witness line geometry remains unchanged. It provides a visual adjustment, but it's not a true geometric modification of the witness line itself.
  • Alternative (More Complex): You could explore creating entirely new dimensions with the desired geometry. This is significantly more complex and generally not recommended unless absolutely necessary. It involves deleting the existing dimension and creating a new one with adjusted witness line lengths.

This method provides a practical workaround to control the perceived gap between the dimension line and witness line endpoint. Remember to thoroughly test and adapt the code to your specific dimension types and orientations for the best results. If you have any more questions, just let me know!

  

 


Viewing all articles
Browse latest Browse all 67020

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>