<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Civil3D API c# extract label text in Civil 3D Customization Forum</title>
    <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/6988920#M12745</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to extract the text in the label,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its a civil 3D label (example pipe, structure, etc)&lt;/P&gt;
&lt;P&gt;I am able to get the style name and the text. But the issue is text which i get using he current code is all text so I am not able to get component wise text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check the attached screen grab,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;My label contains three components,&lt;/P&gt;
&lt;P&gt;KN, Top and Bot, I want to extract the text within each of those component.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to get that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My Code as follows,&lt;/P&gt;
&lt;PRE&gt;public static class LabelTextExtractor
    {
        public static string GetDisplayedLabelText(ObjectId labelId)
        {
            //if (labelId.ObjectClass.DxfName.ToUpper() != "AECC_GENERAL_SEGMENT_LABEL")
            //{
            //    throw new ArgumentException(
            //        "argument mismatch: not an \"AECC_GENERAL_SEGMENT_LABEL\"");
            //}

            StringBuilder lblText = new StringBuilder();

            using (var tran = labelId.Database.TransactionManager.StartTransaction())
            {
                var label = tran.GetObject(labelId, OpenMode.ForRead) as CivilDb.Label;
                if (label != null)
                {
                    bool changed = !label.Dragged &amp;amp;&amp;amp; label.AllowsDragging;
                    try
                    {
                        if (changed)
                        {
                            label.UpgradeOpen();
                            double delta = label.StartPoint.DistanceTo(label.EndPoint);
                            label.LabelLocation =
                                new Point3d(label.LabelLocation.X +
                                    delta, label.LabelLocation.Y +
                                    delta, label.LabelLocation.Z);
                        }

                        var dbObjs = FullExplode(label);
                        foreach (var obj in dbObjs)
                        {
                            if (obj.GetType() == typeof(DBText))
                            {
                                lblText.Append(" " + (obj as DBText).TextString);
                            }

                            obj.Dispose();
                        }
                    }
                    finally
                    {
                        if (changed) label.ResetLocation();
                    }
                }

                tran.Commit();
            }

            return lblText.ToString().Trim();
        }

        #region private methods

        private static List&amp;lt;CadDb.DBObject&amp;gt; FullExplode(CadDb.Entity ent)
        {
            // final result
            List&amp;lt;CadDb.DBObject&amp;gt; fullList = new List&amp;lt;CadDb.DBObject&amp;gt;();

            // explode the entity
            DBObjectCollection explodedObjects = new DBObjectCollection();
            ent.Explode(explodedObjects);
            foreach (CadDb.Entity explodedObj in explodedObjects)
            {
                // if the exploded entity is a blockref or mtext
                // then explode again
                if (explodedObj.GetType() == typeof(CadDb.BlockReference) ||
                    explodedObj.GetType() == typeof(CadDb.MText))
                {
                    fullList.AddRange(FullExplode(explodedObj));
                }
                else
                    fullList.Add(explodedObj);
            }
            return fullList;
        }

        #endregion
    }&lt;/PRE&gt;</description>
    <pubDate>Sat, 01 Apr 2017 10:05:23 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2017-04-01T10:05:23Z</dc:date>
    <item>
      <title>Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/6988920#M12745</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to extract the text in the label,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its a civil 3D label (example pipe, structure, etc)&lt;/P&gt;
&lt;P&gt;I am able to get the style name and the text. But the issue is text which i get using he current code is all text so I am not able to get component wise text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check the attached screen grab,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;My label contains three components,&lt;/P&gt;
&lt;P&gt;KN, Top and Bot, I want to extract the text within each of those component.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to get that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My Code as follows,&lt;/P&gt;
&lt;PRE&gt;public static class LabelTextExtractor
    {
        public static string GetDisplayedLabelText(ObjectId labelId)
        {
            //if (labelId.ObjectClass.DxfName.ToUpper() != "AECC_GENERAL_SEGMENT_LABEL")
            //{
            //    throw new ArgumentException(
            //        "argument mismatch: not an \"AECC_GENERAL_SEGMENT_LABEL\"");
            //}

            StringBuilder lblText = new StringBuilder();

            using (var tran = labelId.Database.TransactionManager.StartTransaction())
            {
                var label = tran.GetObject(labelId, OpenMode.ForRead) as CivilDb.Label;
                if (label != null)
                {
                    bool changed = !label.Dragged &amp;amp;&amp;amp; label.AllowsDragging;
                    try
                    {
                        if (changed)
                        {
                            label.UpgradeOpen();
                            double delta = label.StartPoint.DistanceTo(label.EndPoint);
                            label.LabelLocation =
                                new Point3d(label.LabelLocation.X +
                                    delta, label.LabelLocation.Y +
                                    delta, label.LabelLocation.Z);
                        }

                        var dbObjs = FullExplode(label);
                        foreach (var obj in dbObjs)
                        {
                            if (obj.GetType() == typeof(DBText))
                            {
                                lblText.Append(" " + (obj as DBText).TextString);
                            }

                            obj.Dispose();
                        }
                    }
                    finally
                    {
                        if (changed) label.ResetLocation();
                    }
                }

                tran.Commit();
            }

            return lblText.ToString().Trim();
        }

        #region private methods

        private static List&amp;lt;CadDb.DBObject&amp;gt; FullExplode(CadDb.Entity ent)
        {
            // final result
            List&amp;lt;CadDb.DBObject&amp;gt; fullList = new List&amp;lt;CadDb.DBObject&amp;gt;();

            // explode the entity
            DBObjectCollection explodedObjects = new DBObjectCollection();
            ent.Explode(explodedObjects);
            foreach (CadDb.Entity explodedObj in explodedObjects)
            {
                // if the exploded entity is a blockref or mtext
                // then explode again
                if (explodedObj.GetType() == typeof(CadDb.BlockReference) ||
                    explodedObj.GetType() == typeof(CadDb.MText))
                {
                    fullList.AddRange(FullExplode(explodedObj));
                }
                else
                    fullList.Add(explodedObj);
            }
            return fullList;
        }

        #endregion
    }&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Apr 2017 10:05:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/6988920#M12745</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-01T10:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/6992091#M12746</link>
      <description>&lt;P&gt;Haven't time to test the code but looks like going the right route. Basically have to explode them through the code, search the strings for "top, bot, etc" and store / use them however you like.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 16:10:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/6992091#M12746</guid>
      <dc:creator>brianchapmandesign</dc:creator>
      <dc:date>2017-04-03T16:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7005407#M12747</link>
      <description>&lt;P&gt;I was able to extract the component text by going through the label style. The following example prints to the command line the label's text component name and contents. Is this what you are looking to do?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VB.net&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("GetLabelComponents")&amp;gt; _
        Public Sub GetComp()
            Dim aDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = aDoc.Editor
            Dim db As Database = aDoc.Database
            Dim opts As New PromptEntityOptions(vbCrLf + "Select Label")
            opts.SetRejectMessage(vbCrLf + "Must be a Label")
            opts.AddAllowedClass(GetType(GeneralSegmentLabel), True)
            Dim res As PromptEntityResult = ed.GetEntity(opts)
            If res.Status = PromptStatus.OK Then
                Using trans As Transaction = db.TransactionManager.StartTransaction
                    Dim Lbl As GeneralSegmentLabel
                    Lbl = trans.GetObject(res.ObjectId, OpenMode.ForRead)
                    Dim LblStlId As ObjectId = Lbl.StyleId
                    Dim lblStl As Styles.LabelStyle
                    lblStl = trans.GetObject(LblStlId, OpenMode.ForRead)
                    Dim comps As New ObjectIdCollection
                    comps = lblStl.GetComponents(Styles.LabelStyleComponentType.Text)
                    For Each id As ObjectId In comps
                        Dim comp As Styles.LabelStyleTextComponent = trans.GetObject(id, OpenMode.ForRead)
                        ed.WriteMessage(vbCrLf + comp.Name)
                        ed.WriteMessage(vbCrLf + comp.Text.Contents.Value)
                    Next
                    trans.Commit()
                End Using
            End If
        End Sub&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Apr 2017 19:38:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7005407#M12747</guid>
      <dc:creator>hippe013</dc:creator>
      <dc:date>2017-04-09T19:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7005460#M12748</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1633394"&gt;@hippe013&lt;/a&gt;, the OP is looking to get the displayed contents of the label, not what the style defines. In the case of a distance label, it would return "123.54" not "&amp;lt;[General Overall Length(Uft|P2|RN|AP|Sn|OF)]&amp;gt;".&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2017 20:29:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7005460#M12748</guid>
      <dc:creator>Jeff_M</dc:creator>
      <dc:date>2017-04-09T20:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7006641#M12749</link>
      <description>&lt;P&gt;Thank you for the clarification Jeff. So, as it appears, the resulting text of the component string isn't exposed or implemented in the GeneralSegmentLabel Class.(?) That would be the reason they are exploding the object to get to the resulting text? I'll keep digging on my end.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 13:26:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7006641#M12749</guid>
      <dc:creator>hippe013</dc:creator>
      <dc:date>2017-04-10T13:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7007198#M12750</link>
      <description>Yes, hippe013, the values of the label are not exposed. You can use the GetTextComponentOverride() method which will return any added text. But any of the built in properties that return the values are still returned as the property formula and not the actual text.</description>
      <pubDate>Mon, 10 Apr 2017 17:04:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7007198#M12750</guid>
      <dc:creator>Jeff_M</dc:creator>
      <dc:date>2017-04-10T17:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7007884#M12751</link>
      <description>&lt;P&gt;Thanks Guys,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;I am able to do it,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. First I get the Draworder of the components,&lt;/P&gt;
&lt;P&gt;2. Get the object type of each of those components&lt;/P&gt;
&lt;P&gt;3. When I tried to explode individual components I add them to an array in the right order .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&lt;/P&gt;
&lt;P&gt;There are three types of text - Text component, Text for each component and another one is Reference text.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also some of these components may have multi-line text so when you exploded it, you need to concat that string or make sure to add them as single object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 22:16:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7007884#M12751</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-10T22:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7942873#M12752</link>
      <description>&lt;P&gt;Jeff,&lt;/P&gt;&lt;P&gt;So when you say the values of the labels are not exposed, does that mean you also cant override the text for a label? I have a post already asking; but I saw your reply here. If I have a surface label can I override the text for each component? Example, say I have a surface elevation label that has two text components, TC and BC. Can select that label and override the values that are there? If so, do you know how to do that. I would think it's possible because there is a command in Civil 3D to do just that. I can right click on a label and select Edit Label Text. Is this accessible through the API?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 20:43:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7942873#M12752</guid>
      <dc:creator>David_Prontnicki</dc:creator>
      <dc:date>2018-04-18T20:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7942884#M12753</link>
      <description>&lt;P&gt;Yes,&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/99495"&gt;@David_Prontnicki&lt;/a&gt;. I just replied to your other post.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 20:45:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/7942884#M12753</guid>
      <dc:creator>Jeff_M</dc:creator>
      <dc:date>2018-04-18T20:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/12354068#M12754</link>
      <description>&lt;P&gt;very thanck for you code&amp;nbsp;&lt;/P&gt;&lt;P&gt;without this i was anable to continue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Nov 2023 16:24:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/12354068#M12754</guid>
      <dc:creator>granat1</dc:creator>
      <dc:date>2023-11-04T16:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: Civil3D API c# extract label text</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/12354776#M12755</link>
      <description>&lt;P&gt;&lt;EM&gt;SPOT SURFACE LEVEL&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;CAN I CHANGE A TEXT COMPOSANTTO REFERENCE TO AN AUTHER SURFACE ?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;WHERE IS WRITE THE REFERENCE SURFACE NAME?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;I HAVE A LABEL THAT SHOW THE LEVEL OF A SECOND SURFACE BY REFERENCE TEXT&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;CAN I ALSO CHANGE HIM ? WHERE IS HE IN THE DATA BASE ? NOT ONLY THE NAME OR THE&amp;nbsp; FORMAT&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;TANCK YOU&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Nov 2023 07:00:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/civil3d-api-c-extract-label-text/m-p/12354776#M12755</guid>
      <dc:creator>granat1</dc:creator>
      <dc:date>2023-11-05T07:00:25Z</dc:date>
    </item>
  </channel>
</rss>

