Scale Drawing Dimensions for multiple drawing views – C# example

By Wayne Brill

This example shows how to scale drawing dimensions. All the dimensions will be scaled regarding on which drawing view they belong:

Here is the workflow:

Collect the dimensions related to a particular view

For each dimension, get the text origin in sheet space 
Compute text origin in view space 
Scale the text coordinates 
Compute the new scaled position in sheet space, and set it back to the dimensions

For example, you can use the procedure with a small scale factor (close to 1.0, let’s say 0.9), couple of times, at each time all the dimensions should come closer to the drawing view on which they are related. By doing this the alignment of all the dimensions will be kept. Also a scale factor greater than 1.0 will have the effect of stretching up the drawing dimensions.

 
Download ScaleDrawingDimensions

Here is the code from the example:

public void ReframeDimensions()
{
    try
    {
        DrawingDocument oDrawingDoc = 
 (DrawingDocument)ThisApplication.ActiveDocument; 
        Sheet oSheet = oDrawingDoc.ActiveSheet;
        double ScaleFactor = 0.5;
        foreach (DrawingView oDwgView in 
                             oSheet.DrawingViews)
        {
            //Collect Dimensions related to 
            //the current DrawingView
            ObjectCollection oObjCollection = 
    GetDrawingDimForView(oSheet, oDwgView.Name);
 
            // Iterate through all the drawing 
            // dimensions for this view
            foreach (object obj in oObjCollection)
            {
                DrawingDimension oDwgDimIterator = 
                          obj as DrawingDimension;
                DimensionText oDimensionText = 
                             oDwgDimIterator.Text;
 
                //Get text position in 
                //DrawingView space
                Point2d oPosInView = oDwgView.
   SheetToDrawingViewSpace(oDimensionText.Origin);
 
                //Scale the text coordinates by 
                //our scale factor
                Point2d oNewPosInView = 
                   ThisApplication.TransientGeometry.
            CreatePoint2d(oPosInView.X * ScaleFactor,
                         oPosInView.Y * ScaleFactor);
 
                //Convert new position in sheet space
                Point2d oNewPosInSheet = oDwgView.
              DrawingViewToSheetSpace(oNewPosInView);
 
                //Reframe dimension in sheet
              oDimensionText.Origin = oNewPosInSheet;
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
 
public ObjectCollection GetDrawingDimForView
                     (Sheet oSheet, string ViewName)
{
    ObjectCollection oDrawDimsForView = 
                   ThisApplication.TransientObjects.
                       CreateObjectCollection(null);
    DrawingDimensions oDrawDims = 
                           oSheet.DrawingDimensions;
    foreach (DrawingDimension oDrawDimIterator 
                                       in oDrawDims)
    {
        switch (oDrawDimIterator.Type)
        {
            case ObjectTypeEnum.
                      kLinearGeneralDimensionObject:
                {
                  LinearGeneralDimension oLinearDim = 
          oDrawDimIterator as LinearGeneralDimension;
                    DrawingCurve Curve = 
       oLinearDim.IntentOne.Geometry as DrawingCurve;
                    DrawingView DwgView = 
                         Curve.Parent as DrawingView;
                    if (string.Compare
                       (DwgView.Name, ViewName) == 0)
                    {
              oDrawDimsForView.Add(oDrawDimIterator);
                    }
                }
                break;
            case ObjectTypeEnum.
                        kRadiusGeneralDimensionObject:
                {
                   RadiusGeneralDimension oRadiusDim = 
           oDrawDimIterator as RadiusGeneralDimension;
              object obj = oRadiusDim.Intent.Geometry;
                    DrawingCurve Curve = 
           oRadiusDim.Intent.Geometry as DrawingCurve;
                    DrawingView DwgView = 
                          Curve.Parent as DrawingView;
                    if (string.Compare
                        (DwgView.Name, ViewName) == 0)
                    {
               oDrawDimsForView.Add(oDrawDimIterator);
                    }
                }
                break;
            case ObjectTypeEnum.
                      kDiameterGeneralDimensionObject:
                {
               DiameterGeneralDimension oDiameterDim = 
         oDrawDimIterator as DiameterGeneralDimension;
            object obj = oDiameterDim.Intent.Geometry;
                    DrawingCurve Curve = 
         oDiameterDim.Intent.Geometry as DrawingCurve;
                    DrawingView DwgView = 
                          Curve.Parent as DrawingView;
                    if (string.Compare(DwgView.Name, 
                                       ViewName) == 0)
                    {
               oDrawDimsForView.Add(oDrawDimIterator);
                    }
                }
                break;
            case ObjectTypeEnum.
                       kAngularGeneralDimensionObject:
                {
                AngularGeneralDimension oDiameterDim =
          oDrawDimIterator as AngularGeneralDimension;
                    DrawingCurve Curve = 
      oDiameterDim.IntentOne.Geometry as DrawingCurve;
                    DrawingView DwgView = 
                          Curve.Parent as DrawingView;
                    if (string.Compare(DwgView.Name, 
                                       ViewName) == 0)
                    {
               oDrawDimsForView.Add(oDrawDimIterator);
                    }
                }
                break;
 
            default:
                break;
        }
    }
    return oDrawDimsForView;
 
}

Comments

2 responses to “Scale Drawing Dimensions for multiple drawing views – C# example”

  1. How i can find AutDesk.Inventor.Application.dll? I am getting a compilation errors.
    Thanks
    Parthiv

  2. Wayne Brill Avatar
    Wayne Brill

    Hi Parthiv,
    Not sure about your question. Are you getting this error when you are building the example downloaded from this post? If you are talking about the Inventor API interop then see this directory:
    C:\Program Files\Autodesk\Inventor 2016\Bin\Public Assemblies
    If this related to something else please post the question on the Inventor customization forum. http://forums.autodesk.com/t5/inventor-customization/bd-p/120
    Thanks,
    Wayne

Leave a Reply to Wayne BrillCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading