Reduce decimal value to 4 digits

Reduce decimal value to 4 digits

J-Rocks
Collaborator Collaborator
831 Views
8 Replies
Message 1 of 9

Reduce decimal value to 4 digits

J-Rocks
Collaborator
Collaborator

Hello everyone.

 

HAPPY NEW YEAR TO ALL OF YOU. Heart

 

I am writing lengths of lines into a single text , but the probem is that the value of lengths is many digits and I would like it to be 2 or 4

 

how to reduce the decimal numbers to a specific number of digits?

 

Many thanks.

0 Likes
Accepted solutions (1)
832 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant
Accepted solution

Hi,

 

If you want to round the length double value

 

Math.Round(length, 4)

 

If you just want the string to display 4 digits without changin the double value:

 

length.ToString("0.0000")


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 9

J-Rocks
Collaborator
Collaborator

Thank you .

 

I needed the first answer. Smiley Wink

 

I am also using your polar method to get the rotation of lines to add single text to each line object, it seems it doesn't work. can you please advise on this ?

 

if (ss.Status == PromptStatus.OK)
                {
                  foreach (SelectedObject obj in ss.Value)
                    {     
                            ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                            len = ent.Length;
                            dis += len;
                            p1 = ent.StartPoint;
                            p2 = ent.EndPoint;
                            p3 = Polar (p1,ent.Angle, (len / 2.0))// IT IS NOT AS PER LINE'S ROTATION
                            txt = new DBText();
                            txt.SetDatabaseDefaults();
                            txt.Position = p3
                            txt.TextString = Math.Round(len, 4).ToString();
                            txt.Height = 0.2;
                            blkrec.AppendEntity(txt);
                            trans.AddNewlyCreatedDBObject(txt, true);    
                        }
                        trans.Commit();
                    }  
0 Likes
Message 4 of 9

_gile
Consultant
Consultant

I don't know why the Polar method isn't working (can you show the code you're using).

 

you can also simply get the middle of the line like so:

p3 = p1 + (p2 - p1) / 2.0;

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 9

J-Rocks
Collaborator
Collaborator

Hi gile

 

This is the codes that I wrote and the rotation of text is always 0.0 and it is not following each line's angle.

 

If you have any comment about the codes please let me know.

 

Thanks.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;

namespace SecondExample
{
    public class Class1
    {
        [CommandMethod("LineLengths", CommandFlags.UsePickSet)]
        public void GetlineLengths()
        {
            Editor edit = Application.DocumentManager.MdiActiveDocument.Editor;
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database Dtb = acDoc.Database;
            using (Transaction trans = Dtb.TransactionManager.StartTransaction())
            {
                double len, dis = 0.0 ;
                Line ent;
                Point3d p1, p2, p3;
                DBText txt;
                try
                {
                BlockTable blktbl = trans.GetObject(Dtb.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord blkrec = trans.GetObject(Dtb.CurrentSpaceId, OpenMode.ForWrite)as BlockTableRecord;
                TypedValue[] tv = new TypedValue [] { new TypedValue(0, "LINE") };
                SelectionFilter fltr = new SelectionFilter(tv);
                PromptSelectionResult ss = edit.GetSelection(fltr);
                if (ss.Status == PromptStatus.OK)
                {
                  foreach (SelectedObject obj in ss.Value)
                    {     
                            ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                            len = ent.Length;
                            dis += len;
                            p1 = ent.StartPoint;
                            p2 = ent.EndPoint;
                            p3 = Polar (p1,ent.Angle, (len / 2.0));
                            txt = new DBText();
                            txt.SetDatabaseDefaults();
                            txt.Position = p3;
                            txt.TextString = Math.Round(len, 4).ToString();
                            txt.Height = 0.2;
                            blkrec.AppendEntity(txt);
                            trans.AddNewlyCreatedDBObject(txt, true);    
                        }
                        trans.Commit();
                    }  
                if (dis > 0.0)
                    {
                        edit.WriteMessage("Total lengths : " + dis.ToString());
                    }
                }
                catch (System.Exception exp)
                {
                    edit.WriteMessage(exp.Message);
                    trans.Abort();
                }
            } 
        }
        /// <summary>
        /// Defines a point with polar coordinates from a base point.
        /// </summary>
        /// <param name="basePt">The base point.</param>
        /// <param name="angle">The angle (radians) about the X axis.</param>
        /// <param name="distance">The distance from the base point.</param>
        /// <returns>The new 3d point.</returns>
        public Point3d Polar(Point3d basePt, double angle, double distance)
        {
            return new Point3d(
                basePt.X + (distance * Math.Cos(angle)),
                basePt.Y + (distance * Math.Sin(angle)),
                basePt.Z);
        }     
    }
}

 
0 Likes
Message 6 of 9

kerry_w_brown
Advisor
Advisor

 

You aren't actually setting the txt.Rotation.

You will need something like this

 

txt.Rotation = ent.Angle;

You'll find this code does exactly what it's told ... unfortunately.

 

The next task will be to change the text rotation angle so that it's 'readable' from the right and bottom only ...

if the line runs from right to left, so will the text 🙂

 

If you need a helping hand, just yell.

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 7 of 9

_gile
Consultant
Consultant

Hi,

 

Kerry is right.

The Polar function works as expect and have nothing to do with the text rotation.

 

Try like this:

 

                if (ss.Status == PromptStatus.OK)
                {
                    foreach (SelectedObject obj in ss.Value)
                    {
                        ent = (Line)trans.GetObject(obj.ObjectId, OpenMode.ForRead);
                        len = ent.Length;
                        dis += len;
                        p1 = ent.StartPoint;
                        p2 = ent.EndPoint;
                        p3 = p1 + (p2 - p1) / 2.0;
                        txt = new DBText();
                        txt.SetDatabaseDefaults();

                        // set the text position
                        txt.Position = p3;
                        // or set the text justification and position
                        //txt.Justify = AttachmentPoint.BottomCenter;
                        //txt.AlignmentPoint = p3;

                        // set the text rotation
                        var rot = ent.Angle;
                        if (Math.Cos(rot) < 0.0)
                            rot += Math.PI;
                        txt.Rotation = rot;

                        txt.TextString = Math.Round(len, 4).ToString();
                        txt.Height = 0.2;
                        blkrec.AppendEntity(txt);
                        trans.AddNewlyCreatedDBObject(txt, true);
                    }
                    trans.Commit();
                }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 9

J-Rocks
Collaborator
Collaborator

@KerryBrown wrote:

 

You aren't actually setting the txt.Rotation.

You will need something like this

 

txt.Rotation = ent.Angle;

You'll find this code does exactly what it's told ... unfortunately.

 

The next task will be to change the text rotation angle so that it's 'readable' from the right and bottom only ...

if the line runs from right to left, so will the text 🙂

 

If you need a helping hand, just yell.

 


Awesome Smiley Very Happy

 

One more thing about the rotation , some texts become in reverse direction ( from Right to Left ) Smiley Surprised and some on oblique lines become reversed also , need ro wrok on this problem if that is possible please.

 

Thank you.

0 Likes
Message 9 of 9

J-Rocks
Collaborator
Collaborator

@_gile wrote:

Hi,

 

Kerry is right.

The Polar function works as expect and have nothing to do with the text rotation.

 

Try like this:

 

                if (ss.Status == PromptStatus.OK)
                {
                    foreach (SelectedObject obj in ss.Value)
                    {
                        ent = (Line)trans.GetObject(obj.ObjectId, OpenMode.ForRead);
                        len = ent.Length;
                        dis += len;
                        p1 = ent.StartPoint;
                        p2 = ent.EndPoint;
                        p3 = p1 + (p2 - p1) / 2.0;
                        txt = new DBText();
                        txt.SetDatabaseDefaults();

                        // set the text position
                        txt.Position = p3;
                        // or set the text justification and position
                        //txt.Justify = AttachmentPoint.BottomCenter;
                        //txt.AlignmentPoint = p3;

                        // set the text rotation
                        var rot = ent.Angle;
                        if (Math.Cos(rot) < 0.0)
                            rot += Math.PI;
                        txt.Rotation = rot;

                        txt.TextString = Math.Round(len, 4).ToString();
                        txt.Height = 0.2;
                        blkrec.AppendEntity(txt);
                        trans.AddNewlyCreatedDBObject(txt, true);
                    }
                    trans.Commit();
                }

You replied to my question before asking it Smiley Happy how awesome guys you are!

 

That is great now , I have learned a lot from this program .

 

Thank you so much gile and Kerry.

 

0 Likes