Alignment Label set style start chainage

Alignment Label set style start chainage

yoitsarun
Advocate Advocate
3,959 Views
17 Replies
Message 1 of 18

Alignment Label set style start chainage

yoitsarun
Advocate
Advocate

Hi all,

I want to access start chainage and end chainage of Label set Style through Civil 3D .Net API.

Any ideas?

 

aruntr_1-1613284009107.png

I can import styles using code. but i didn't see option for start and end chainage:

ImportLabelSet(ObjectId labelSetStyleId);

 

 

 

 

0 Likes
Accepted solutions (1)
3,960 Views
17 Replies
Replies (17)
Message 2 of 18

Jeff_M
Consultant
Consultant

You can get the AlignmentLabelGroupIds from the alignment. Then get the RangeStart and RangeEnd from each of the LabelGroups.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 18

yoitsarun
Advocate
Advocate

Hi Jeff, do you have any sample for that. I'm stuck on this.

0 Likes
Message 4 of 18

Jeff_M
Consultant
Consultant

Here is one way:

    public class LabelSetRanges
    {
        public string LabelSet = string.Empty;
        public double RangeStart = double.NaN;
        public double RangeEnd = double.NaN;

        public LabelSetRanges()
        { }
    }
    public class LabelTools
    {
        public List<LabelSetRanges>RangeList (Alignment align)
        {
            var retval = new List<LabelSetRanges>();
            foreach(ObjectId id in align.GetAlignmentLabelGroupIds())
            {
                var labels = (AlignmentLabelGroup)id.Open(OpenMode.ForRead);
                var l = new LabelSetRanges();
                l.LabelSet = labels.Name;
                l.RangeEnd = labels.RangeEnd;
                l.RangeStart = labels.RangeEnd;
                labels.Close();
                retval.Add(l);
            }
            return retval;
        }
    }
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 5 of 18

yoitsarun
Advocate
Advocate

Thanks Jeff,

Sorry to ask again, how I can add your code to button click code below?

 private void BtnApply_Click(object sender, EventArgs e)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            CivilDocument doc = CivilApplication.ActiveDocument;
            Editor ed = acDoc.Editor;
            using (Transaction ts = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                foreach (string name in Alignchecklist.CheckedItems)
                {
                    var alignId = alignList[name];
                    Alignment myAlignment = ts.GetObject(alignId, OpenMode.ForWrite) as Alignment;
                    ObjectId styleID = CivilApplication.ActiveDocument.Styles.AlignmentStyles[AlignStyleCombo.SelectedIndex];
                    ObjectId alignmentLablesSetStyleId = CivilApplication.ActiveDocument.Styles.LabelSetStyles.AlignmentLabelSetStyles[AlignLablSetCombo.SelectedIndex];

                    AlignmentStyle myAlignmentStyle = ts.GetObject(styleID, OpenMode.ForWrite) as AlignmentStyle;
                    AlignmentLabelSetStyle myAlignmentStyleSet = ts.GetObject(alignmentLablesSetStyleId, OpenMode.ForWrite) as AlignmentLabelSetStyle;

                    var lineStyleId = CivilApplication.ActiveDocument.Styles.LabelStyles.AlignmentLabelStyles.LineLabelStyles[LineLabelStyleCombo.SelectedIndex];
                    var arcStyleId = CivilApplication.ActiveDocument.Styles.LabelStyles.AlignmentLabelStyles.CurveLabelStyles[CurveLabelStyleCombo.SelectedIndex];

                    var POBId = CivilApplication.ActiveDocument.Styles.LabelStyles.AlignmentLabelStyles.StationOffsetLabelStyles[POBcombobox.SelectedIndex];
                    var POEId = CivilApplication.ActiveDocument.Styles.LabelStyles.AlignmentLabelStyles.StationOffsetLabelStyles[POEcombobox.SelectedIndex];

                    var Markid = CivilApplication.ActiveDocument.Styles.MarkerStyles[0];
                    double s = myAlignment.StartingStation;
                    double end = myAlignment.EndingStation;
                    try
                    {
                        myAlignment.StyleId = myAlignmentStyle.Id;
                        myAlignment.ImportLabelSet(alignmentLablesSetStyleId);
                       
                        double eastingstart = 0;
                        double northingstart = 0;
                        double eastingend = 0;
                        double northingend = 0;

                        myAlignment.PointLocation(s, 0, ref eastingstart, ref northingstart);
                        myAlignment.PointLocation(end, 0, ref eastingend, ref northingend);
                        

                        Point2d startp = new Point2d(eastingstart, northingstart);
                        Point2d endp = new Point2d(eastingend, northingend);
                        StationOffsetLabel.Create(alignId, POBId, Markid, startp);
                        StationOffsetLabel.Create(alignId, POEId, Markid, endp);

                        double station = myAlignment.Length / 2;
                        var ent = myAlignment.Entities.EntityAtStation(station);
                        switch (ent.EntityType)
                        {
                            case AlignmentEntityType.Arc:
                                AlignmentCurveLabel.Create((AlignmentSubEntityArc)ent[0], arcStyleId);
                                break;

                            case AlignmentEntityType.Line:
                                AlignmentTangentLabel.Create((AlignmentSubEntityLine)ent[0], lineStyleId);
                                break;
                        }
                    }
                    catch (System.Exception)
                    {
                        System.Windows.MessageBox.Show("Error, Please check Alignemnt and Styles", "AUTO ALIGNMENT LABEL", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }                   
                }
                ts.Commit();
                System.Windows.MessageBox.Show("Alignment Styles Changed Successfully!", "AUTO ALIGNMENT LABEL", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

 

0 Likes
Message 6 of 18

Jeff_M
Consultant
Consultant

Does that not work? Do you get any errors?

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 7 of 18

yoitsarun
Advocate
Advocate

Hi Jeff,

I tried to add to my code. But your code is class method. I dont know how to bring that to my code. 

0 Likes
Message 8 of 18

Jeff_M
Consultant
Consultant

To use it, just create another Class module in the same Namespace then you can call it like so

var alignLabelSets = RangeList(myAlignment);

which will return a list of LabelSetRange objects. You can the loop through those like so:

foreach (LabelSetRange r in alignLabelSets)
{
   ed.WriteMessage("\nLabelSet named {0} goes from Station {1} to Station {2}.", r.Name, r.RangeStart.ToString("F2"), r.RangeEnd.ToString("F2"));
//or however else you intend to use the stations
}

 

I should note that since the Alignment object derives from the Curve object, you can use Curve properties on the Alignment. Thereby allowing you to save some processing time by changing this:

 

                        double eastingstart = 0;
                        double northingstart = 0;
                        double eastingend = 0;
                        double northingend = 0;

                        myAlignment.PointLocation(s, 0, ref eastingstart, ref northingstart);
                        myAlignment.PointLocation(end, 0, ref eastingend, ref northingend);
                        

                        Point2d startp = new Point2d(eastingstart, northingstart);
                        Point2d endp = new Point2d(eastingend, northingend);

 

to this:

                        Point2d startp = new Point2d(myAlignment.StartPoint.X, myAlignment.StartPoint.Y);
                        Point2d endp = new Point2d(myAlignment.EndPoint.X, myAlignment.EndPoint.Y);

 

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 9 of 18

yoitsarun
Advocate
Advocate

now iam getting below error,

object does not contain public instance definition for getenumerator

aruntr_0-1613569010492.png

may be I used your code wrongly?

0 Likes
Message 10 of 18

Jeff_M
Consultant
Consultant

Sorry for not testing, or proofreading, what I posted. The following code, placed in 2 Class modules, works for me.

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.Civil.DatabaseServices;
using System.Collections.Generic;

namespace Civil3D_Misc_Commands.AlignmentTools
{
    public class LabelSetRanges
    {
        public string LabelSet = string.Empty;
        public double RangeStart = double.NaN;
        public double RangeEnd = double.NaN;

        public LabelSetRanges()
        { }
    }
    public class LabelSetTools
    {
        public static List<LabelSetRanges> RangeList(Alignment align)
        {
            var retval = new List<LabelSetRanges>();
            foreach (ObjectId id in align.GetAlignmentLabelGroupIds())
            {
                var l = new LabelSetRanges();
                var labels = (AlignmentLabelGroup)id.Open(OpenMode.ForRead);
                try
                {
                    l.LabelSet = labels.DisplayName;
                    l.RangeEnd = labels.RangeEnd;
                    l.RangeStart = labels.RangeStart;
                    retval.Add(l);
                }
                catch { }
                labels.Close();

            }
            return retval;
        }

    }
}

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using System;


namespace Civil3D_Misc_Commands.AlignmentTools
{

    public class RangeTests
    {
        [CommandMethod("testingrangelist")]
        public void testingrangelist()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            foreach (ObjectId id in CivilApplication.ActiveDocument.GetAlignmentIds())
            {
                var myAlignment = (Alignment)id.Open(OpenMode.ForRead);
                var alignLabelSets = LabelSetTools.RangeList(myAlignment);
                try
                {
                    foreach (LabelSetRanges r in alignLabelSets)
                    {
                        ed.WriteMessage("\nLabelSet named {0} goes from Station {1} to Station {2}.", r.LabelSet, r.RangeStart.ToString("F2"), r.RangeEnd.ToString("F2"));
                        //or however else you intend to use the stations
                    }
                }
                catch { }
            }
        }
   }
}
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 11 of 18

yoitsarun
Advocate
Advocate

Thanks Jeff,

I tried this code. 

It is not changing in the Labelset properies.

aruntr_0-1613588661629.png

 

 var alignLabelSets = LabelSetTools.RangeList(myAlignment);
                        try
                        {
                            foreach (LabelSetRanges r in alignLabelSets)
                            {                                
                                r.RangeStart = 100;
                                r.RangeEnd = 200;
                                
                                ed.WriteMessage("\nLabelSet named {0} goes from Station {1} to Station {2}.", r.LabelSet, r.RangeStart.ToString("F2"), r.RangeEnd.ToString("F2"));
                                
                            }
                        }

                        catch 
                    { 
                    }

but message showing the changed chainages:

 

LabelSet named Alignment Geometry Point Label Group goes from Station 100.00 to Station 200.00.
LabelSet named Alignment Station Label Group goes from Station 100.00 to Station 200.00.
LabelSet named Alignment Station Label Group goes from Station 100.00 to Station 200.00

 

and the Labelset name is incorrect in the message.

0 Likes
Message 12 of 18

Jeff_M
Consultant
Consultant

The example I showed just GETS the values and prints them out. You didn't say you wanted to change them.

In your main code you can just do something like this (not tested but should work):

            foreach (ObjectId id in myAlignment.GetAlignmentLabelGroupIds())
            {
                var labels = (AlignmentLabelGroup)ts.GetObject(id, OpenMode.ForWrite);
                try
                {
                    //make sure you use valid values                  
                    labels.RangeStart = 100;
                    labels.RangeEnd = 200;
                }
                catch { }
             }

 

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 13 of 18

yoitsarun
Advocate
Advocate

Thanks Jeff,

I tried it but nothing happening. 

0 Likes
Message 14 of 18

Jeff_M
Consultant
Consultant
Please zip up your project and post it here.
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 15 of 18

yoitsarun
Advocate
Advocate

Jeff, zip file is attached.

0 Likes
Message 16 of 18

yoitsarun
Advocate
Advocate

some error, please find zip file here

0 Likes
Message 17 of 18

Jeff_M
Consultant
Consultant
Accepted solution

I know this is a work in progress so I won't comment on the other things that need to be addressed other than you should modify parts of the code to only run when the proper radio buttons are checked. These changes to the range portion now allows it to work:

                        foreach (ObjectId id in myAlignment.GetAlignmentLabelGroupIds())
                        {
                            var labels = (AlignmentLabelGroup)ts.GetObject(id, OpenMode.ForWrite);
                            try
                            {
                                labels.RangeStartFromFeature = false;
                                labels.RangeEndFromFeature = false;
                                labels.RangeStart = myAlignment.StartingStation + Convert.ToDouble(StartChTextbox.Text);
                                labels.RangeEnd = myAlignment.EndingStation - Convert.ToDouble(EndChTextbox.Text);
                                //ed.WriteMessage("\nLabelSet named {0} goes from Station {1} to Station {2}.", labels.DisplayName, labels.RangeStart.ToString("F2"), labels.RangeEnd.ToString("F2"));
                            }
                            catch { }
                        }
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 18 of 18

yoitsarun
Advocate
Advocate

Thanks Jeff, Finally it is working .

Sorry i wasted your time. I didnt mentioned what i want in the topic.

Many thanks for helping me.

Please guide me, I want to explore more in .net. 

 

0 Likes