Accessing Child Object Style using .NET API

By Partha Sarkar

In this
particular example we will see how to access Civil 3D Surface Spot Elevation
Child label style. Suppose there is a Surface Spot Elevation Child label style
named "Foot Meter_Child" in a Civil 3D DWG file and we try to use the
following VB.NET code snippet to find out the number of Spot Elevation label
style in the same DWG file, child label styles won't be shown or counted as
shown in the screenshot below –

Dim surfaceSpotElevnLblStyle As LabelStyleCollection = civilDoc.Styles.LabelStyles.SurfaceLabelStyles.SpotElevationLabelStyles

MsgBox("This DWG File has : " + surfaceSpotElevnLblStyle.Count.ToString + " Surface Spot Elevn Label Styles !")

 

Child_Label_Style_01

 

Above code
snippet shows the count of 'Spot Elevation' labels as two omitting the child label
style.  So how do we access Child
Object Style using .NET API ?

We can access
child label style from the parent label style's ObjectId using [index]. Here is
the relevant VB.NET code snippet –

 

Try 
 
    Dim surfaceSpotElevnLblStyle As LabelStyleCollection = civilDoc.Styles.LabelStyles.SurfaceLabelStyles.SpotElevationLabelStyles
    'MsgBox("This DWG File has : " + surfaceSpotElevnLblStyle.Count.ToString + " Surface Spot Elevn Label Styles !") 
 
    Dim surfaceLblStyleId As ObjectId = Nothing              
    Dim osurfaceElevnLabelStyle As LabelStyle  = Nothing
 
    For Each surfaceLblStyleId In surfaceSpotElevnLblStyle 
      osurfaceElevnLabelStyle = trans.GetObject(surfaceLblStyleId, OpenMode.ForRead) 
      MsgBox("Style Name : " + osurfaceElevnLabelStyle.Name.ToString + "  " + "Children Style Count : " + osurfaceElevnLabelStyle.ChildrenCount.ToString) 
      If (osurfaceElevnLabelStyle.ChildrenCount >0) then               
          Dim childLblStyleId As ObjectId =   osurfaceElevnLabelStyle(0) 
          Dim childLblStyle As LabelStyle = trans.GetObject(childLblStyleId, OpenMode.ForRead) 
          MsgBox("Child Style Name :" + childLblStyle.Name.ToString)                           
 
      End If
    Next         
 
    trans.Commit()         
Catch ex As Exception 
    ed.WriteMessage("Exception Message is : " + ex.Message.ToString()) 
Exit Sub
End Try

Child_Label_Style_02

Hope this is
useful to you!


Comments

4 responses to “Accessing Child Object Style using .NET API”

  1. Thank you so much for posting this Partha!! Very Helpful as the child styles can be confusing, and not apparent as to where they can be found.

  2. You are welcome Steve !
    Cheers,
    Partha
    ADN

  3. Does anyone know a way for a “find and replace” type routine for text manually added to labels? In this case I have over 100 note labels that I have to manually edit. These notes were street CL intersections that were Sta xx+xx.xx B Street. Now we have all the street names and need to change them.

  4. Hi Erick –
    You might have to iterate through the note labels and handle the replacement in your custom routine.
    Thanks,
    Partha

Leave a Reply to Erick CCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading