.NET

Reply
Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 1 of 10 (274 Views)
Accepted Solution

Assign nomenclature to each polyline.

274 Views, 9 Replies
03-10-2014 01:05 AM

Hello

 

I want to assign nomenclature to each polyline while drawing it. Nomenclature can vary. I know how to label line with dtext. but confused with polyine.

please Find Attach dwg for better understanding.

 

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 / 2, tHeight)
Dim pt3 As Point2d = New Point2d(base, 0)
Dim pt4 As Point2d = New Point2d(base - Xx, tHeight)
Dim pt5 As Point2d = New Point2d(0 + Xx, tHeight)

'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)

'wanna label here

?????
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, pt1, 0, 0, 0)
tra.AddVertexAt(6, pt3, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)

tx.Commit()
End Using
End Using

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 2 of 10 (229 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 02:36 AM in reply to: bvyas89

Hello

with below code iam able to assign label to line:

 

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);
}

 

//labeling
Matrix3d mtx = ed.CurrentUserCoordinateSystem;
Point3d mp = acLine.GetPointAtParameter(acLine.EndParam / 2).TransformBy(mtx);
double ang = acLine.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) + 100)).TransformBy(mtx);
DBText dtext = new DBText();
dtext.SetDatabaseDefaults();
dtext.TextString = string.Format("{0:f" + db.Dimdec.ToString() + "}", acLine.Length);
//<---change text string to your needs
dtext.Position = tp;
dtext.Height = 120;
dtext.HorizontalMode = TextHorizontalMode.TextMid;
dtext.VerticalMode = TextVerticalMode.TextBottom;
dtext.Rotation = ang;
dtext.AlignmentPoint = tp;
dtext.AdjustAlignment(db);
BlockTableRecord btr = (BlockTableRecord)tx.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(dtext);
tx.AddNewlyCreatedDBObject(dtext, true);

 

 

But now i want to label polyline issue it is single plyline.

Mentor
mzakiralam
Posts: 230
Registered: ‎11-09-2012
Message 3 of 10 (226 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 02:43 AM in reply to: bvyas89

HI,

Okay, I got your point now. As you know how to draw text for a line so it will be easier for you now. What you need to do just cast the polyline then you will be able to get all vertices of that polyline. If you get all vertex then I think your problem will be solved. Please see below code snippet which will help you to get the polyline and its vertices.

 

 <CommandMethod("tst")> Public Sub TestPoly()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
        Dim peo As PromptEntityOptions = New PromptEntityOptions(vbLf & "Select a polyline to label it:")
        Dim per As PromptEntityResult = ed.GetEntity(peo)
        Using tx As Transaction = db.TransactionManager.StartTransaction()
            Dim ent As Entity = tx.GetObject(per.ObjectId, OpenMode.ForRead)
            If TypeOf ent Is Polyline Then
                Dim pl As Polyline = TryCast(ent, Polyline)
                Dim vn As Integer = pl.NumberOfVertices
                For i = 0 To vn - 2
                    'get adjacent vertices of a polyline
                    Dim pt1 As Point2d = pl.GetPoint2dAt(i)
                    Dim pt2 As Point2d = pl.GetPoint2dAt(i + 1)
                    'do what you need
                Next
            End If
            tx.Commit()
        End Using
    End Sub

 

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 4 of 10 (223 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 02:54 AM in reply to: bvyas89
is it possible to explode Polyline and treat each line as input for above code
Mentor
mzakiralam
Posts: 230
Registered: ‎11-09-2012
Message 5 of 10 (219 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 03:02 AM in reply to: bvyas89

if you want to explode a entity , you can have a look on below link:

 

http://through-the-interface.typepad.com/through_the_interface/2011/02/exploding-autocad-objects-usi...

 

But I would not recommend you to do so. But I do not understand, if you have a vertex why don't you add a label around there?

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 6 of 10 (212 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 03:35 AM in reply to: mzakiralam
How can i add label to each vertex. i want to label each line so i think to go with previous method. can i change possition of label if assign to vertex
Mentor
mzakiralam
Posts: 230
Registered: ‎11-09-2012
Message 7 of 10 (204 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 04:57 AM in reply to: bvyas89

please try this code. I did a trick. I create a temporary line to get its angle. but did not add it to model space.

 

 <CommandMethod("tst")> Public Sub TestPolyline()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
        Dim peo As PromptEntityOptions = New PromptEntityOptions("Select a polyline:")
        Dim per As PromptEntityResult = ed.GetEntity(peo)
        If per.Status <> PromptStatus.OK Then
            Return
        End If
        Using tx As Transaction = db.TransactionManager.StartTransaction()
            Dim ent As Entity = tx.GetObject(per.ObjectId, OpenMode.ForRead)
            If TypeOf ent Is Polyline Then
                Dim pl As Polyline = TryCast(ent, Polyline)
                Dim vn As Integer = pl.NumberOfVertices
                For i = 0 To vn - 2
                    Dim pt1 As Point2d = pl.GetPoint2dAt(i)
                    Dim pt2 As Point2d = pl.GetPoint2dAt(i + 1)
                    Dim ln As Line = New Line(New Point3d(pt1.X, pt1.Y, 0), New Point3d(pt2.X, pt2.Y, 0))
                    Dim ang As Double = ln.Angle
                    Dim dtxt As DBText = New DBText()
                    dtxt.Position = New Point3d((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2, 0)
                    dtxt.Height = 2.5
                    dtxt.TextString = "label" & i
                    dtxt.Rotation = ang

                    Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
                    Dim btr As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                    btr.AppendEntity(dtxt)
                    tx.AddNewlyCreatedDBObject(dtxt, True)
                Next
            End If
            tx.Commit()
        End Using
    End Sub

 

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 8 of 10 (198 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 05:58 AM in reply to: mzakiralam
Thank you .its working for my requirement well. But now issue is i want to do this for all poly-lines without selection from user. Is it possible ?
Mentor
mzakiralam
Posts: 230
Registered: ‎11-09-2012
Message 9 of 10 (189 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 06:55 AM in reply to: bvyas89

HI , it is possible. Just see my code. In one place I had to cast the polyline from selection.

 

Dim pl As Polyline = TryCast(ent, Polyline)

 

In your case, when you create a polyline, just use that polyline to get all that vertices which I did later. Please see my below code, where I will create a polyline and later add laber for that.

 

 <CommandMethod("tst")> Public Sub TestPoly()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
       
        Using tx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            'add polyline
            Dim pl As Polyline = New Polyline()
            pl.AddVertexAt(0, New Point2d(0, 0), 0, 0, 0)
            pl.AddVertexAt(1, New Point2d(5, 0), 0, 0, 0)
            pl.AddVertexAt(2, New Point2d(5, 5), 0, 0, 0)
            pl.AddVertexAt(3, New Point2d(0, 0), 0, 0, 0)
            btr.AppendEntity(pl)
            tx.AddNewlyCreatedDBObject(pl, True)
            'add label for that polyline
            Dim vn As Integer = pl.NumberOfVertices
            For i = 0 To vn - 2
                Dim pt1 As Point2d = pl.GetPoint2dAt(i)
                Dim pt2 As Point2d = pl.GetPoint2dAt(i + 1)
                Dim ln As Line = New Line(New Point3d(pt1.X, pt1.Y, 0), New Point3d(pt2.X, pt2.Y, 0))
                Dim ang As Double = ln.Angle
                Dim dtxt As DBText = New DBText()
                dtxt.Position = New Point3d((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2, 0)
                dtxt.Height = 0.1
                dtxt.TextString = "label" & i
                dtxt.Rotation = ang
                btr.AppendEntity(dtxt)
                tx.AddNewlyCreatedDBObject(dtxt, True)
            Next
            tx.Commit()
        End Using
    End Sub

 

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 10 of 10 (162 Views)

Re: Assign nomenclature to each polyline.

03-12-2014 09:48 PM in reply to: mzakiralam

Thank you so much... its what exactly i want... Your code always help me... Thank you once again.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community