Horizontal or vertical control of dimensioning

Horizontal or vertical control of dimensioning

k005
Advisor Advisor
2,099 Views
10 Replies
Message 1 of 11

Horizontal or vertical control of dimensioning

k005
Advisor
Advisor

Hello friends, I need to create an if structure that checks whether the dimensioning is horizontal or vertical. How can I do this?


* Dimension selection includes only horizontal and vertical... and only one type of selection is made.

 

Thanks in advance to my helpful friend.

0 Likes
Accepted solutions (1)
2,100 Views
10 Replies
Replies (10)
Message 2 of 11

M_Kocyigit
Enthusiast
Enthusiast

To determine whether a dimension object is horizontal or vertical in AutoCAD using .NET, you can use the Dimension class or one of its derived types, such as AlignedDimension, and examine its properties.

Key Properties:

  1. Dimension.Normal: Indicates the plane of the dimension.
    For horizontal or vertical dimensions, the Normal vector will typically point along the Z-axis (e.g., (0, 0, 1)).

  2. AlignedDimension.XLine1Point and XLine2Point:
    These points define the endpoints of the dimension line.
    Compare the X and Y coordinates of these points to determine orientation.

Example:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

public class DimensionChecker
{
    [CommandMethod("CheckDimensionOrientation")]
    public void CheckDimensionOrientation()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;

        // Select a dimension
        PromptEntityOptions promptOptions = new PromptEntityOptions("\nSelect a dimension: ");
        promptOptions.SetRejectMessage("\nOnly dimensions are allowed.");
        promptOptions.AddAllowedClass(typeof(Dimension), exactMatch: false);
        PromptEntityResult promptResult = doc.Editor.GetEntity(promptOptions);

        if (promptResult.Status != PromptStatus.OK)
        {
            doc.Editor.WriteMessage("\nSelection canceled.");
            return;
        }

        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            // Open the selected dimension
            Dimension dim = trans.GetObject(promptResult.ObjectId, OpenMode.ForRead) as Dimension;

            if (dim is AlignedDimension alignedDim)
            {
                // Get the defining points of the dimension line
                Point3d point1 = alignedDim.XLine1Point;
                Point3d point2 = alignedDim.XLine2Point;

                // Determine if it's horizontal or vertical
                if (Math.Abs(point1.X - point2.X) < Tolerance.Global.EqualPoint)
                {
                    doc.Editor.WriteMessage("\nThe dimension is vertical.");
                }
                else if (Math.Abs(point1.Y - point2.Y) < Tolerance.Global.EqualPoint)
                {
                    doc.Editor.WriteMessage("\nThe dimension is horizontal.");
                }
                else
                {
                    doc.Editor.WriteMessage("\nThe dimension is neither strictly horizontal nor vertical.");
                }
            }
            else
            {
                doc.Editor.WriteMessage("\nThe selected dimension is not an AlignedDimension.");
            }

            trans.Commit();
        }
    }
}

 

 

Message 3 of 11

_gile
Consultant
Consultant

Hi,

Which type of dimension are you talking about? AlignedDimension or RotatedDimension? The second one has a Rotation property which is equal to 0 or PI radians if the dimension is 'horizontal'; PI/2 or 3PI/2 if the dimension is 'vertical'.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 11

k005
Advisor
Advisor

Thank you very much for the example.


"The selected dimension is not an AlignedDimension." This is the message I received.


There is only one situation:


Is it possible to proceed from the code I gave below?


I have to take this from the first selected dimension line.

 

 

 

 

 var selRes = ed.GetSelection();
 if (selRes.Status != PromptStatus.OK) return;

 using var tr = db.TransactionManager.StartTransaction();
 var dimensions = selRes.Value.GetObjectIds()
     .Select(id => tr.GetObject(id, OpenMode.ForRead) as Dimension)
     .Where(dim => dim != null)
     .ToList();

 if (!dimensions.Any())
 {
     ed.WriteMessage("\nSeçili nesneler arasında ölçü çizgisi bulunamadı.");
     return;
 }
   var firstDim = dimensions[0];

 

 

0 Likes
Message 5 of 11

k005
Advisor
Advisor

Hi,

 

I'm just talking about the horizontal and vertical dimension. ( 90° vertical) ( 0° horizontal)

normal dimensioning

0 Likes
Message 6 of 11

_gile
Consultant
Consultant

@k005  a écrit :

Hi,

 

I'm just talking about the horizontal and vertical dimension. ( 90° vertical) ( 0° horizontal)

normal dimensioning


This does not reply to my question. I'm talking about the AlignedDimension and RotatedDimension AutoCAD .NET classes.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 11

k005
Advisor
Advisor

This is the answer I can give for now.


There are two types of measurements. Horizontal - vertical.


Let me also add a picture.

 

firstDim.png

 

 

0 Likes
Message 8 of 11

_gile
Consultant
Consultant
Accepted solution

If the type of the dimension is AlignedDimension, as suggested by @M_Kocyigit, you can check XLine1Point and XLine2Point properties. If bothe have the same Y, the dimension is horizontal; If both have the same X, the dimension is vertical.

If the type of the dimension is RotatedDimension, you can check the Rotation property. If it's equal to 0 or PI, the dimension is horizontal; if it's equal to PI/2 or 3*PI/2, the dimension is vertical.

 

Please, read the provided links, to learn about the dimensions in AutoCAD .NET.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 11

k005
Advisor
Advisor

Ok. Thank you.

0 Likes
Message 10 of 11

M_Kocyigit
Enthusiast
Enthusiast

Hello @_gile,

Thank you very much for your valuable support and the additional suggestions.


Hello @k005,

Were we able to assist you? If yes, please click the "Accept" button for _gile so we can close this topic.


Best regards from Germany

0 Likes
Message 11 of 11

k005
Advisor
Advisor

@M_Kocyigit 

@_gile 

 

Thank you very much. Problem solved. I made another addition...

0 Likes