.NET

.NET

Reply
Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 1 of 4 (393 Views)
Accepted Solution

CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

393 Views, 3 Replies
01-25-2014 12:07 AM

HI,

 

I WANT TO CALCULATE LENGTH OF LINE FROM ITS START AND END POINT. AN D ALSO NOMENCLATURE FOR LINE. HOPE FOR PROMPT REPLY.

 

 

Next time please do not shout and upload the picture

to imagine what you exactly need

Here you go:

#Region "Place text above line"
' by Tony Tanzillo
Public Shared Function PolarPoint(basepoint As Point3d, angle As Double, distance As Double) As Point3d
Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)
End Function
<CommandMethod("LBL", CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
Public Sub TestLabeling()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim mtx As Matrix3d = ed.CurrentUserCoordinateSystem
Dim peo As New PromptEntityOptions(vbLf & "Select a line: ")
peo.SetRejectMessage(vbLf & "Selected object is not a line, try again: ")
peo.AddAllowedClass(GetType(Autodesk.AutoCAD.DatabaseServices.Line), False)
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status <> PromptStatus.OK Then
Return
End If
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim lid As ObjectId = per.ObjectId
Dim en As Entity = DirectCast(tr.GetObject(lid, OpenMode.ForRead), Entity)
Dim ln As Autodesk.AutoCAD.DatabaseServices.Line = TryCast(en, Autodesk.AutoCAD.DatabaseServices.Line)
If ln Is Nothing Then
Return
End If
Dim mp As Point3d = ln.GetPointAtParameter(ln.EndParam / 2).TransformBy(mtx)
Dim ang As Double = ln.Angle
If (ang > Math.PI / 2) AndAlso (ang < Math.PI * 1.5) Then
ang = ang + Math.PI
End If
Dim tp As Point3d = PolarPoint(mp, ang + Math.PI / 2, db.Textsize).TransformBy(mtx)
Dim dtext As New DBText
dtext.SetDatabaseDefaults()
dtext.TextString = String.Format("{0:f" + db.Dimdec.ToString() + "}", ln.Length) '<---change text string to your needs
dtext.Position = tp
dtext.HorizontalMode = TextHorizontalMode.TextMid
dtext.VerticalMode = TextVerticalMode.TextBottom
dtext.Rotation = ang
dtext.AlignmentPoint = tp
dtext.AdjustAlignment(db)
Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
btr.AppendEntity(dtext)
tr.AddNewlyCreatedDBObject(dtext, True)
tr.Commit()
Catch exs As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(exs.Message & vbLf & exs.StackTrace)
End Try
End Using
End Sub
#End Region

 C#

 #region "Place text above line"
// by Tony Tanzillo
public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
{
return new Point3d(
basepoint.X + (distance * Math.Cos(angle)),
basepoint.Y + (distance * Math.Sin(angle)),
basepoint.Z);
}
[CommandMethod("LTXT", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void TestLabeling()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Matrix3d mtx = ed.CurrentUserCoordinateSystem;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a line: ");
peo.SetRejectMessage("\nSelected object is not a line, try again: ");
peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Line), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
ObjectId lid = per.ObjectId;
Entity en = (Entity)tr.GetObject(lid, OpenMode.ForRead);
Autodesk.AutoCAD.DatabaseServices.Line ln = en as Autodesk.AutoCAD.DatabaseServices.Line;
if (ln == null)
{
return;
}
Point3d mp = ln.GetPointAtParameter(ln.EndParam / 2).TransformBy(mtx);
double ang = ln.Angle;
if ((ang > Math.PI / 2) && (ang < Math.PI * 1.5))
{
ang = ang + Math.PI;
}
Point3d tp = PolarPoint(mp, ang + Math.PI / 2, db.Textsize).TransformBy(mtx);
DBText dtext = new DBText();
dtext.SetDatabaseDefaults();
dtext.TextString = string.Format("{0:f" + db.Dimdec.ToString() + "}", ln.Length);
//<---change text string to your needs
dtext.Position = tp;
dtext.HorizontalMode = TextHorizontalMode.TextMid;
dtext.VerticalMode = TextVerticalMode.TextBottom;
dtext.Rotation = ang;
dtext.AlignmentPoint = tp;
dtext.AdjustAlignment(db);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(dtext);
tr.AddNewlyCreatedDBObject(dtext, true);
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception exs)
{
ed.WriteMessage(exs.Message + "\n" + exs.StackTrace);
}
}
}
#endregion

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,605
Registered: ‎06-29-2007
Message 2 of 4 (380 Views)

Re: CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

01-25-2014 04:44 AM in reply to: bvyas89

Hi,

 

why calculate the length of a line if you can read the value from the line's property ".Length".

 

>> ALSO NOMENCLATURE FOR LINE

Do you mean the syntax for how to create a line?

There are a lot of samples in www a sample is available directly in the docs >>>here<<<.

 

HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 4 (367 Views)

Re: CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

01-25-2014 06:03 AM in reply to: bvyas89

Next time please do not shout and upload the picture

to imagine what you exactly need

Here you go:

#Region "Place text above line"
        ' by Tony Tanzillo
        Public Shared Function PolarPoint(basepoint As Point3d, angle As Double, distance As Double) As Point3d
            Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)
        End Function

        <CommandMethod("LBL", CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
        Public Sub TestLabeling()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim mtx As Matrix3d = ed.CurrentUserCoordinateSystem
            Dim peo As New PromptEntityOptions(vbLf & "Select a line: ")
            peo.SetRejectMessage(vbLf & "Selected object is not a line, try again: ")
            peo.AddAllowedClass(GetType(Autodesk.AutoCAD.DatabaseServices.Line), False)
            Dim per As PromptEntityResult = ed.GetEntity(peo)
            If per.Status <> PromptStatus.OK Then
                Return
            End If
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Try
                    Dim lid As ObjectId = per.ObjectId
                    Dim en As Entity = DirectCast(tr.GetObject(lid, OpenMode.ForRead), Entity)
                    Dim ln As Autodesk.AutoCAD.DatabaseServices.Line = TryCast(en, Autodesk.AutoCAD.DatabaseServices.Line)
                    If ln Is Nothing Then
                        Return
                    End If

                    Dim mp As Point3d = ln.GetPointAtParameter(ln.EndParam / 2).TransformBy(mtx)
                    Dim ang As Double = ln.Angle

                    If (ang > Math.PI / 2) AndAlso (ang < Math.PI * 1.5) Then
                        ang = ang + Math.PI
                    End If
                    Dim tp As Point3d = PolarPoint(mp, ang + Math.PI / 2, db.Textsize).TransformBy(mtx)
                    Dim dtext As New DBText
                    dtext.SetDatabaseDefaults()
                    dtext.TextString = String.Format("{0:f" + db.Dimdec.ToString() + "}", ln.Length)    '<---change text string to your needs
                    dtext.Position = tp
                    dtext.HorizontalMode = TextHorizontalMode.TextMid
                    dtext.VerticalMode = TextVerticalMode.TextBottom
                    dtext.Rotation = ang
                    dtext.AlignmentPoint = tp
                    dtext.AdjustAlignment(db)
                    Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                    btr.AppendEntity(dtext)
                    tr.AddNewlyCreatedDBObject(dtext, True)

                    tr.Commit()
                Catch exs As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage(exs.Message & vbLf & exs.StackTrace)
                End Try
            End Using
        End Sub
#End Region

 C#

        #region "Place text above line"
        // by Tony Tanzillo
        public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
        {
            return new Point3d(
            basepoint.X + (distance * Math.Cos(angle)),
            basepoint.Y + (distance * Math.Sin(angle)),
            basepoint.Z);
        }

        [CommandMethod("LTXT", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void TestLabeling()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Matrix3d mtx = ed.CurrentUserCoordinateSystem;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a line: ");
            peo.SetRejectMessage("\nSelected object is not a line, try again: ");
            peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Line), false);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
            {
                return;
            }
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    ObjectId lid = per.ObjectId;
                    Entity en = (Entity)tr.GetObject(lid, OpenMode.ForRead);
                    Autodesk.AutoCAD.DatabaseServices.Line ln = en as Autodesk.AutoCAD.DatabaseServices.Line;
                    if (ln == null)
                    {
                        return;
                    }
                   
                    Point3d mp = ln.GetPointAtParameter(ln.EndParam / 2).TransformBy(mtx);
                    double ang = ln.Angle;

                    if ((ang > Math.PI / 2) && (ang < Math.PI * 1.5))
                    {
                        ang = ang + Math.PI;
                    }
                    Point3d tp = PolarPoint(mp, ang + Math.PI / 2, db.Textsize).TransformBy(mtx);
                    DBText dtext = new DBText();
                    dtext.SetDatabaseDefaults();
                    dtext.TextString = string.Format("{0:f" + db.Dimdec.ToString() + "}", ln.Length);
                    //<---change text string to your needs
                    dtext.Position = tp;
                    dtext.HorizontalMode = TextHorizontalMode.TextMid;
                    dtext.VerticalMode = TextVerticalMode.TextBottom;
                    dtext.Rotation = ang;
                    dtext.AlignmentPoint = tp;
                    dtext.AdjustAlignment(db);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    btr.AppendEntity(dtext);
                    tr.AddNewlyCreatedDBObject(dtext, true);

                    tr.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception exs)
                {
                    ed.WriteMessage(exs.Message + "\n" + exs.StackTrace);
                }
            }
        }
        #endregion

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 4 of 4 (272 Views)

Re: CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

03-11-2014 02:48 AM in reply to: Hallex

Hello Mr. hallex,


Can you please help me. I want to assign Dtext or label to each polyline while drawing it or after drawing it. how can i do that. Please  find the dwg attach herewith for reference.

 

code using to draw polyline is as below:

 

'get current document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
'lock document as using modeless form
Using docLock As DocumentLock = doc.LockDocument
Using tx As Transaction = db.TransactionManager.StartTransaction()
'open blocktable
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
'open model space for write
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'converte text value
Dim uWidth As Double = CDbl(TextBox1.Text)
Dim base As Double = CDbl(TextBox2.Text)
Dim tHeight As Double = CDbl(TextBox3.Text)
Dim Xx As Double = ((base - uWidth) / 2)
' Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt2 As Point2d = New Point2d(base, 0)
Dim pt3 As Point2d = New Point2d(base - Xx, tHeight)
Dim pt4 As Point2d = New Point2d(0, 0)
Dim pt5 As Point2d = New Point2d(0 + Xx, tHeight)
Dim pt6 As Point2d = New Point2d(base, 0)
'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)
tra.AddVertexAt(1, pt2, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.AddVertexAt(3, pt4, 0, 0, 0)
tra.AddVertexAt(4, pt5, 0, 0, 0)
tra.AddVertexAt(5, pt6, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
Dim line1 As Line = New Line(New Point3d(pt5.X, pt5.Y, 0), New Point3d(pt3.X, pt3.Y, 0))
ms.AppendEntity(line1)
tx.AddNewlyCreatedDBObject(line1, True)
tx.Commit()
End Using
End Using

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.