I want to override the dimension Text with keeping the tolerances as it is

I want to override the dimension Text with keeping the tolerances as it is

varad.keshatwar
Contributor Contributor
1,991 Views
8 Replies
Message 1 of 9

I want to override the dimension Text with keeping the tolerances as it is

varad.keshatwar
Contributor
Contributor

I want to override the dimension Text, with keeping the tolerances as it is. Is it possible?

varadkeshatwar_0-1699297820945.png

 

 

0 Likes
1,992 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

You can get the dimension MText contents from the BlockTableRecord of the Dimension and the measurement text from the dimension properties.

Here's an example you can start from.

        private static string GetDimensionContents(Dimension dimension) =>
            ((BlockTableRecord)dimension.DimBlockId.GetObject(OpenMode.ForRead))
            .Cast<ObjectId>()
            .Where(id => id.ObjectClass.Name == "AcDbMText")
            .Select(id => (MText)id.GetObject(OpenMode.ForRead))
            .First()
            .Contents;

        private static string GetMeasurementText(Dimension dimension) =>
            (dimension is LineAngularDimension2 || dimension is Point3AngularDimension) ?
            Converter.AngleToString(dimension.Measurement, (AngularUnitFormat)dimension.Dimaunit, dimension.Dimadec) :
            Converter.DistanceToString(dimension.Measurement, (DistanceUnitFormat)dimension.Dimlunit, dimension.Dimdec);

And then use Regex to replace the measurement part by another text.

dimension.DimensionText = Regex.Replace(
    GetDimensionContents(dimension),
    GetMeasurementText(dimension),
    replacementText);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

varad.keshatwar
Contributor
Contributor

Hii @_gile 
I'm using Stacked Dimension to override the tolerances. I'm overriding the text as follows
\A1;%%c316{\H0.5x;\S+0.00^-0.36;
by overriding dimension text as above, I get the following result

varadkeshatwar_1-1699883976998.png

 

0 Likes
Message 4 of 9

_gile
Consultant
Consultant

Sorry, I don't understand what you're trying to do.

Please post a 'before/after' example.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 9

a.kouchakzadeh
Advocate
Advocate

Giles, could you possible explain in a step by step manner how does thank LINQ work?

0 Likes
Message 6 of 9

_gile
Consultant
Consultant

@a.kouchakzadeh wrote:

Giles, could you possible explain in a step by step manner how does thank LINQ work?


        private static string GetDimensionContents(Dimension dimension) =>
            ((BlockTableRecord)dimension.DimBlockId.GetObject(OpenMode.ForRead))
            .Cast<ObjectId>()
            .Where(id => id.ObjectClass.Name == "AcDbMText")
            .Select(id => (MText)id.GetObject(OpenMode.ForRead))
            .First()
            .Contents;

stands for:

        private static string GetDimensionContents(Dimension dimension)
        {
            string contents = null;
            var btr = (BlockTableRecord)dimension.DimBlockId.GetObject(OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbMText")
                {
                    var mtext = (MText)id.GetObject(OpenMode.ForRead);
                    contents =  mtext.Contents;
                    break;
                }
            }
            return contents;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9

varad.keshatwar
Contributor
Contributor

@_gile 
Attaching the before and after images FYI
Before Image

varadkeshatwar_0-1700465695018.png

 


After Overriding the Text, I achieved the following results
After Image

varadkeshatwar_2-1700442831802.png

 

 



0 Likes
Message 8 of 9

_gile
Consultant
Consultant

It's still confusing. From what I see it is not "keeping the tolerance intact".

_gile_0-1700465621962.png

 

If I stick to your request: "override the dimension Text with keeping the tolerances as it is", my first answer is correct.

 

        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var promptEntityOptions = new PromptEntityOptions("\nSelect Dimension: ");
            promptEntityOptions.SetRejectMessage("\nSelected object is not a Dimension.");
            promptEntityOptions.AddAllowedClass(typeof(Dimension), false);
            var promptEntityResult = ed.GetEntity(promptEntityOptions);
            if (promptEntityResult.Status != PromptStatus.OK) return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var dimension = (Dimension)tr.GetObject(promptEntityResult.ObjectId, OpenMode.ForWrite);
                string currentMeasurementText = GetMeasurementText(dimension);

                var promptResult = ed.GetString(
                    $"\nCurrent mesurement = {currentMeasurementText}." +
                    "\nEnter the replacement text: ");
                if (promptResult.Status == PromptStatus.OK)
                {
                    string replacementText = promptResult.StringResult;
                    dimension.DimensionText = Regex.Replace(
                        GetDimensionContents(dimension),
                        currentMeasurementText,
                        replacementText);
                }
                tr.Commit();
            }
        }

        private static string GetDimensionContents(Dimension dimension) =>
            ((BlockTableRecord)dimension.DimBlockId.GetObject(OpenMode.ForRead))
            .Cast<ObjectId>()
            .Where(id => id.ObjectClass.Name == "AcDbMText")
            .Select(id => (MText)id.GetObject(OpenMode.ForRead))
            .First()
            .Contents;

        private static string GetMeasurementText(Dimension dimension) =>
            (dimension is LineAngularDimension2 || dimension is Point3AngularDimension) ?
            Converter.AngleToString(dimension.Measurement, (AngularUnitFormat)dimension.Dimaunit, dimension.Dimadec) :
            Converter.DistanceToString(dimension.Measurement, (DistanceUnitFormat)dimension.Dimlunit, dimension.Dimdec);

 

 

(view in My Videos)



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 9

varad.keshatwar
Contributor
Contributor

@_gile
There was Some mistake from my side, I actually attached some other image. I've updated the images please check

0 Likes