.NET

## .NET

Active Contributor
Posts: 26
Registered: ‎01-16-2014
Message 1 of 4 (336 Views)

# CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

336 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.

*Expert Elite*
Posts: 8,898
Registered: ‎06-29-2007
Message 2 of 4 (323 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 -

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

# Re: CALCULATE LENGTH OF LINE AND ASSIGN NOMENCLATURE TO LINE

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

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: ")
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)
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
Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
btr.AppendEntity(dtext)

tr.Commit()
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()
{
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: ");
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
ObjectId lid = per.ObjectId;
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;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(dtext);

tr.Commit();
}
{
ed.WriteMessage(exs.Message + "\n" + exs.StackTrace);
}
}
}
#endregion```

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
Posts: 26
Registered: ‎01-16-2014
Message 4 of 4 (215 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.Closed = True
ms.AppendEntity(tra)
Dim line1 As Line = New Line(New Point3d(pt5.X, pt5.Y, 0), New Point3d(pt3.X, pt3.Y, 0))
ms.AppendEntity(line1)
tx.Commit()
End Using
End Using

### You are not logged in.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.