Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can i add labels to a Polyline by .NET?

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
mehdi-guida
7553 Views, 25 Replies

How can i add labels to a Polyline by .NET?

Hi, How can i add labels to a Polyline by .NET?

For example i have a Polyline and i want to add labels to Polyline segments.

25 REPLIES 25
Message 2 of 26
_gile
in reply to: mehdi-guida

Hi,

 

You can use the Overrule API.

See this topic.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 26
Jeff_M
in reply to: mehdi-guida

What type of label?
Jeff_M, also a frequent Swamper
EESignature
Message 4 of 26
mehdi-guida
in reply to: _gile

Thank you, many codes were in that topic.

I tested the first one, it put a text on the middle of  each segment.

I am looking for a label instead of text.

does that topic have some thing for associate labels to polyline segments?

Message 5 of 26
mehdi-guida
in reply to: Jeff_M

General segment label

Message 6 of 26
mehdi-guida
in reply to: mehdi-guida

I can add general segment label to one line, arc or feature line. But i need to add label to all of segments of a polyline.

Message 7 of 26
_gile
in reply to: mehdi-guida

You should elaborate on what you mean with "segment label" or better show the code you use to "add general segment label to one line, arc or feature line."

I suspect you're using some vertical, not vanilla AutoCAD..



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 26
mehdi-guida
in reply to: _gile

Actually i have some parcels, Alignment and feature line in my drawing. I have to label(tag) all their segments (image attached).

I used GeneralSegmentLabel.Create(object id , rate) for labeling. It works perfect for single segment only. my alignments include many lines and arcs.So i decided to label their base curve (for my drawing always are Polyline). but i don't know how to label a polyline in .NET. If you have better suggestion , guide me please.

Message 9 of 26
Jeff_M
in reply to: mehdi-guida

            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var entsel = ed.GetEntity("\nSelect a polyline: ");
            if (entsel.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var p = (Polyline)tr.GetObject(entsel.ObjectId, OpenMode.ForRead);
                for (int i = (int)p.StartParam; i < p.EndParam; i++)
                {
                    GeneralSegmentLabel.Create(p.ObjectId, i + 0.5);
                }
                tr.Commit();
            }
Jeff_M, also a frequent Swamper
EESignature
Message 10 of 26
mehdi-guida
in reply to: Jeff_M

THANK YOU SO MUCH,    WORKS PERFECT :)))

EVERY ONE NEED A TEACHER LIKE YOU. :))

Message 11 of 26
mehdi-guida
in reply to: Jeff_M

Thank you again for code you sent.

Can i used this method for a Basecurve of a civil 3d object?

for example i have an alignment. can i add labels(by your method) to alignment basecurve instead of an individual polyline?

Message 12 of 26
Jeff_M
in reply to: mehdi-guida

No, the Alignment.BaseCurve is not a member of the database...it has a null ObjectId. You would need to use the AlignmentCurveLabel.Create() and AlignmentTangentLabel.Create() methods.

Jeff_M, also a frequent Swamper
EESignature
Message 13 of 26
mehdi-guida
in reply to: Jeff_M

Thank you

what about parcels and feature lines?

Do they have something similar methods like AlignmentCurveLabel.Create() and AlignmentTangentLabel.Create() for Labeling their segments?

Message 14 of 26
mehdi-guida
in reply to: Jeff_M

I tried to use your method for alignment labeling.

but i got this error . Would you please help me to fix that? (error image attached)

 

filterlist = new TypedValue[1];
filterlist[0] = new TypedValue(0, "AECC_ALIGNMENT");
filter = new SelectionFilter(filterlist);
Promptentity = editor.SelectAll(filter);
sval = Promptentity.Value;
CivilDocument civildoc = CivilApplication.ActiveDocument;
ObjectId curvelblstid = civildoc.Styles.LabelStyles.GeneralCurveLabelStyles["ericsCurveStandard"];
ObjectId linelblstid = civildoc.Styles.LabelStyles.GeneralCurveLabelStyles["ericsLineStandard"];
if (sval != null)
{
         foreach (ObjectId j in sval.GetObjectIds())
             {
                 Autodesk.AutoCAD.DatabaseServices.DBObject dbobj = ts.GetObject(j, OpenMode.ForRead);
                 Autodesk.Civil.DatabaseServices.Entity prs = dbobj as Autodesk.Civil.DatabaseServices.Entity;
                 Polyline v = prs.BaseCurve as Polyline;
                 for (int i = 0; i < v.NumberOfVertices; i++)
                   {
                       SegmentType sgtn = v.GetSegmentType(i);
                      if (sgtn == SegmentType.Arc)
                        {
                            AlignmentArc item = v.GetArcSegment2dAt(i);
                            AlignmentCurveLabel.Create(item, curvelblstid);
                        }
                    if (sgtn == SegmentType.Line)
                      {
                          AlignmentLine item = v.GetLineSegment2dAt(i);
                          AlignmentTangentLabel.Create(item, linelblstid);
                      }
             }
        }
}

 

Message 15 of 26
Jeff_M
in reply to: mehdi-guida

Try this:

        [CommandMethod("TestCreateAlignLabels")]
        public void createalignlabels()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var filterlist = new TypedValue[1];
            filterlist[0] = new TypedValue(0, "AECC_ALIGNMENT");
            var filter = new SelectionFilter(filterlist);
            var Promptentity = doc.Editor.SelectAll(filter);
            var sval = Promptentity.Value;
            CivilDocument civildoc = CivilApplication.ActiveDocument;
            ObjectId curvelblstid = civildoc.Styles.LabelStyles.GeneralCurveLabelStyles[0];
            ObjectId linelblstid = civildoc.Styles.LabelStyles.GeneralLineLabelStyles[0];
            if (sval != null)
            {
                using (Transaction ts = doc.Database.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId j in sval.GetObjectIds())
                    {
                        Alignment prs = (Alignment)ts.GetObject(j, OpenMode.ForRead);
                        foreach(AlignmentEntity seg in prs.Entities)
                        {
                            switch (seg.EntityType)
                            {
                                //this example does not take into account a spiral 
                                case AlignmentEntityType.Arc:
                                    var arcent = (AlignmentCurve)seg;
                                    AlignmentCurveLabel.Create((AlignmentSubEntityArc)arcent[0], curvelblstid);
                                    break;
                                case AlignmentEntityType.Line:
                                    AlignmentTangentLabel.Create((AlignmentLine)seg, linelblstid);
                                    break;
                            }
                        }
                    }
                    ts.Commit();
                }
            }
        }
Jeff_M, also a frequent Swamper
EESignature
Message 16 of 26
Jeff_M
in reply to: mehdi-guida


@mehdi-guida wrote:

what about parcels and feature lines?

 


The GeneralSegmentLabel.Create() can be used with lines, arcs, polylines, and featurelines.

For parcels, I'm not seeing any way to add those labels.

Jeff_M, also a frequent Swamper
EESignature
Message 17 of 26
mehdi-guida
in reply to: Jeff_M

Thank you SO MUCH.

I checked it up , i worked perfect.

what about parcels and feature lines?

Do they have something similar methods like AlignmentCurveLabel.Create() and AlignmentTangentLabel.Create() for Labeling their segments?

Message 18 of 26
Jeff_M
in reply to: mehdi-guida

@mehdi-guida for the ParcelSegment labels, there is a method in the COM API to do this. Using the dynamic Type you could likely accomplish what you need. I don't have time right now to look further into it.

Jeff_M, also a frequent Swamper
EESignature
Message 19 of 26
mehdi-guida
in reply to: Jeff_M

Thank you sir

You help me a lot today. 🙂

Message 20 of 26
mehdi-guida
in reply to: Jeff_M

Hi, Three weeks ago you did me a favor and sent me this code.

It was really perfect.

I have question about this code.

Why it doesn't work for closed Polylines?

And how can i change it to works for closed Polylines?

 

Thank you

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report