Add Profile Label Set To Profile

Add Profile Label Set To Profile

y.awwad
Enthusiast Enthusiast
1,718 Views
6 Replies
Message 1 of 7

Add Profile Label Set To Profile

y.awwad
Enthusiast
Enthusiast

 

[CommandMethod("Test")]
public void Profileview()
{
    var doc = AcadApp.Application.DocumentManager.MdiActiveDocument;
    var ed = doc.Editor;
    var DB = doc.Database;
    var CivilDoc = CivilApp.CivilApplication.ActiveDocument;

    selectionResult = ed.GetSelection(new SelectionFilter(new TypedValue[1] { new TypedValue((int)DxfCode.Start, "AECC_PROFILE_VIEW") }));
    if (selectionResult.Status != PromptStatus.OK)
    {
        ed.WriteMessage("\nNo Profile Views Selected.");
        return;
    }

    SelectionSet selectionSet = selectionResult.Value;
    ObjectId[] profileviewIds = selectionSet.GetObjectIds();

    using (Transaction trans = DB.TransactionManager.StartTransaction())
    {
        ObjectId LabelSetID;
        foreach (ObjectId labelid in CivilDoc.Styles.LabelSetStyles.ProfileLabelSetStyles)
        {
            ProfileLabelSetStyle lblset = trans.GetObject(labelid, OpenMode.ForRead) as ProfileLabelSetStyle;
            string labelname = lblset.Name;
            if (labelname == "Complete Label Set")
            {
                LabelSetID = labelid;
            }
        }

        foreach (ObjectId profileviewId in profileviewIds)
        {
            if (profileviewId.ObjectClass.DxfName == "AECC_PROFILE_VIEW")
            {
                ProfileView profileview = trans.GetObject(profileviewId, OpenMode.ForWrite) as ProfileView;
                ObjectId alignmentId = profileview.AlignmentId;
                Alignment alignment = trans.GetObject(alignmentId, OpenMode.ForRead) as Alignment;
                foreach (ObjectId profileId in alignment.GetProfileIds())
                {
                    Profile profile = trans.GetObject(profileId, OpenMode.ForWrite) as Profile;
                    if (profile.ProfileType == ProfileType.FG)
                    {
                        //Profile Label Set
                    }
                }
            }
        }
        trans.Commit();
        trans.Dispose();
    }
}

 

 

 

 

 

0 Likes
Accepted solutions (2)
1,719 Views
6 Replies
Replies (6)
Message 2 of 7

Jeff_M
Consultant
Consultant
Accepted solution

@y.awwad You will need to loop through the LabelSetStyle and create each of the label style types individually. I have an extension method I use for this.

        public static void ApplyLabelSet(this Profile profile, Transaction tr, ObjectId labelSetId, ProfileView pView, bool deleteExisting = true)
        {
            var profId = profile.ObjectId;
            var pviewId = pView.ObjectId;
            if (deleteExisting)
            {
                RXClass lblgrpClass = ProfileLabelGroup.GetClass(typeof(ProfileLabelGroup));
                var lblgrpIds = ProfileLabelGroup.GetAvailableLabelGroupIds(lblgrpClass, pviewId, profId, true);
                foreach (ObjectId id in lblgrpIds)
                {
                    try
                    {
                        var grp = tr.GetObject(id, OpenMode.ForWrite) as ProfileLabelGroup;
                        if(grp != null)
                            grp.Erase();
                    }
                    catch { }
                }
            }
            var pvStyle = (ProfileViewStyle)tr.GetObject(pView.StyleId, OpenMode.ForRead);

            var lblset = (ProfileLabelSetStyle)tr.GetObject(labelSetId, OpenMode.ForRead);
            for (int j = lblset.Count - 1; j > -1; j--)//use reverse order so they show up the same in the ProfileLabels, except it's not working
            {
                ProfileLabelSetItem itm = lblset[j];
                var lblId = ObjectId.Null;
                var lbltype = itm.LabelStyleType;
                switch (lbltype)
                {
                    case LabelStyleType.ProfileCrestCurve:
                        lblId = ProfileCrestCurveLabelGroup.Create(pviewId, profId, itm.LabelStyleId);
                        var crestcrv = (ProfileCrestCurveLabelGroup)tr.GetObject(lblId, OpenMode.ForWrite);
                        //crestcrv.DefaultDimensionAnchorOpt
                        break;
                    case LabelStyleType.ProfileGradeBreaks:
                        lblId = ProfilePVILabelGroup.Create(pviewId, profId, itm.LabelStyleId);
                        break;
                    case LabelStyleType.ProfileHorizontalGeometryPoint:
                        lblId = ProfileHorizontalGeometryPointLabelGroup.Create(pviewId, profId, itm.LabelStyleId);
                        var grp = (ProfileHorizontalGeometryPointLabelGroup)tr.GetObject(lblId, OpenMode.ForWrite);
                        var geopoints = grp.GetGeometryPointsOptions();
                        var geopoints2 = itm.GetLabeledAlignmentGeometryPoints();
                        geopoints.UnSelectAll();
                        foreach (var pair in geopoints2)
                        {
                            geopoints[pair.Key].Selected = true;
                        }
                        grp.SetGeometryPointsOptions(geopoints);
                        break;
                    case LabelStyleType.ProfileLine:
                        lblId = ProfileLineLabelGroup.Create(pviewId, profId, itm.LabelStyleId);
                        break;
                    case LabelStyleType.ProfileMajorStation:
                        lblId = ProfileStationLabelGroup.CreateMajor(pviewId, profId, itm.LabelStyleId, pvStyle.BottomAxis.MajorTickStyle.Interval);
                        break;
                    case LabelStyleType.ProfileMinorStation:
                        lblId = ProfileMinorStationLabelGroup.Create(pviewId, itm.LabelStyleId, pvStyle.BottomAxis.MinorTickStyle.Interval);
                        break;
                    case LabelStyleType.ProfileSagCurve:
                        lblId = ProfileSagCurveLabelGroup.Create(pviewId, profId, itm.LabelStyleId);
                        break;
                }
                if (lblId == ObjectId.Null)
                    continue;
                try
                {
                    var grpitm = (ProfileLabelGroup)tr.GetObject(lblId, OpenMode.ForWrite);
                    grpitm.DefaultDimensionAnchorOption = (Autodesk.Civil.DimensionAnchorOptionType)itm.DimensionAnchorOption;
                    grpitm.DefaultDimensionAnchorValue = itm.DimensionAnchorValue;
                }
                catch (System.Exception e)
                {
                    var msg = e.Message;
                }
            }
        }
Jeff_M, also a frequent Swamper
EESignature
Message 3 of 7

y.awwad
Enthusiast
Enthusiast

When I try to build DLL I get an error
Extension method must be defined in a non-generic static class

0 Likes
Message 4 of 7

Jeff_M
Consultant
Consultant
Accepted solution

As the error message says, you need to place the extension method in a static class:

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;

namespace MyUtilities.Extensions
{
    public static partial class CivilProfileExtensions
    {
        ///that extension method goes here
    }
}

To use it you will need a using statement to the namespace with the extension(s) and then call it like so:

prof.ApplyLabelSet(tr, labelSetId, pview);

 

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

y.awwad
Enthusiast
Enthusiast

@Jeff_M 

It worked like a charm 😍💙

I'm so grateful for your incredible solution, thank you from the bottom of my heart. 😘

 

0 Likes
Message 6 of 7

rymen_kyro
Explorer
Explorer

How can i use this sir?

0 Likes
Message 7 of 7

Jeff_M
Consultant
Consultant

@rymen_kyro message #4 explains how to use it.

Jeff_M, also a frequent Swamper
EESignature
0 Likes