Last week, we took a look at how we could access some of the label properties for COGO points, and how we could override the label through the Point Group. But things get more interesting when you start creating your own label style that can be applied to objects. Today, we will take a look at how to add components to a label style. In my example, I will create a COGO point label style, but the concepts apply to all the label styles.
In the following example, we will create a new point label style named “Demo”. Then, we will add some components to demonstrate how they are created and customized. Here is the basic implementation of the command.
C#
[CommandMethod("CDS_CreateDemoPointLabelStyle")]
public void CDS_CreateDemoPointLabelStyle()
{
createPointLabelStyle("Demo");
}
private void createPointLabelStyle(string name)
{
ObjectId styleId = _pointLabelStyles.Add(name);
removeAllComponents(styleId);
customizeStyle(styleId);
}
private LabelStyleCollection _pointLabelStyles
{
get
{
return _civildoc.Styles.LabelStyles.PointLabelStyles.LabelStyles;
}
}
VB.NET
<CommandMethod("CDS_CreateDemoPointLabelStyle")> _
Public Sub CDS_CreateDemoPointLabelStyle()
createPointLabelStyle("Demo")
End Sub
Private Sub createPointLabelStyle(name As String)
Dim styleId As ObjectId = _pointLabelStyles.Add(name)
removeAllComponents(styleId)
customizeStyle(styleId)
End Sub
Private ReadOnly Property _pointLabelStyles() As LabelStyleCollection
Get
Return _civildoc.Styles.LabelStyles.PointLabelStyles.LabelStyles
End Get
End Property
When we create a new point label style, the object is initialized with three text components. We want to start from scratch, so the first thing that we do is remove them. Here is the implementation to remove the text components that get created by default.
C#
private void removeAllComponents(ObjectId styleId)
{
IEnumerable<string> componentNames = getTextComponentNames(styleId);
removeComponents(styleId, componentNames);
}
private IEnumerable<string> getTextComponentNames(ObjectId styleId)
{
List<string> names = new List<string>();
using (Transaction tr = startTransaction())
{
LabelStyle style = styleId.GetObject(OpenMode.ForRead)
as LabelStyle;
foreach (ObjectId id in style.GetComponents(
LabelStyleComponentType.Text))
{
LabelStyleComponent component =
id.GetObject(OpenMode.ForRead) as LabelStyleComponent;
names.Add(component.Name);
}
}
return names;
}
private void removeComponents(ObjectId styleId,
IEnumerable<string> componentNames)
{
using (Transaction tr = startTransaction())
{
LabelStyle style = styleId.GetObject(OpenMode.ForWrite)
as LabelStyle;
foreach (string name in componentNames)
{
style.RemoveComponent(name);
}
tr.Commit();
}
}
VB.NET
Private Sub removeAllComponents(styleId As ObjectId)
Dim componentNames As IEnumerable(Of String) = getTextComponentNames(
styleId)
removeComponents(styleId, componentNames)
End Sub
Private Function getTextComponentNames(styleId As ObjectId) _
As IEnumerable(Of String)
Dim names As New List(Of String)()
Using tr As Transaction = startTransaction()
Dim style As LabelStyle = TryCast(styleId.GetObject(OpenMode.ForRead),
LabelStyle)
For Each id As ObjectId In style.GetComponents(
LabelStyleComponentType.Text)
Dim component As LabelStyleComponent = TryCast(
id.GetObject(OpenMode.ForRead), LabelStyleComponent)
names.Add(component.Name)
Next
End Using
Return names
End Function
Private Sub removeComponents(styleId As ObjectId, componentNames _
As IEnumerable(Of String))
Using tr As Transaction = startTransaction()
Dim style As LabelStyle = TryCast(styleId.GetObject(OpenMode.ForWrite),
LabelStyle)
For Each name As String In componentNames
style.RemoveComponent(name)
Next
tr.Commit()
End Using
End Sub
After we clean-up the style, we add three components. The first one will be a leader/line that will go from the <feature>, in this case the point, to the text of the label. The following code shows how to create the line component.
C#
private void customizeStyle(ObjectId styleId)
{
using (Transaction tr = startTransaction())
{
addStyleComponents(styleId);
tr.Commit();
}
}
private void addStyleComponents(ObjectId styleId)
{
LabelStyle style = styleId.GetObject(OpenMode.ForWrite)
as LabelStyle;
addLeaderComponent(style);
addPointNumberComponent(style);
addLocationComponent(style);
}
private void addLeaderComponent(LabelStyle style)
{
ObjectId id = style.AddComponent("Leader",
LabelStyleComponentType.Line);
LabelStyleLineComponent component = id.GetObject(OpenMode.ForWrite)
as LabelStyleLineComponent;
component.General.StartAnchorPoint.Value = AnchorPointType.MiddleCenter;
}
VB.NET
Private Sub customizeStyle(styleId As ObjectId)
Using tr As Transaction = startTransaction()
addStyleComponents(styleId)
tr.Commit()
End Using
End Sub
Private Sub addStyleComponents(styleId As ObjectId)
Dim style As LabelStyle = TryCast(styleId.GetObject(OpenMode.ForWrite),
LabelStyle)
addLeaderComponent(style)
addPointNumberComponent(style)
addLocationComponent(style)
End Sub
Private Sub addLeaderComponent(style As LabelStyle)
Dim id As ObjectId = style.AddComponent("Leader",
LabelStyleComponentType.Line)
Dim component As LabelStyleLineComponent = TryCast(
id.GetObject(OpenMode.ForWrite), LabelStyleLineComponent)
component.General.StartAnchorPoint.Value = AnchorPointType.MiddleCenter
End Sub
We create two text components. The first one will show the point number; the second one will show the location of the point in the format (northing, easting, elevation). Here is the code.
C#
private void addPointNumberComponent(LabelStyle style)
{
ObjectId id = style.AddComponent("PN",
LabelStyleComponentType.Text);
LabelStyleTextComponent component = id.GetObject(OpenMode.ForWrite)
as LabelStyleTextComponent;
component.Text.Attachment.Value = LabelTextAttachmentType.MiddleLeft;
component.Text.Contents.Value = _pointNumber;
component.General.AnchorComponent.Value = "Leader";
component.General.AnchorLocation.Value = AnchorPointType.End;
}
private void addLocationComponent(LabelStyle style)
{
ObjectId id = style.AddComponent("Location",
LabelStyleComponentType.Text);
LabelStyleTextComponent component = id.GetObject(OpenMode.ForWrite)
as LabelStyleTextComponent;
component.Text.Attachment.Value = LabelTextAttachmentType.TopLeft;
string value = String.Format("({0}, {1}, {2})",
_northing, _easting, _elevation);
component.Text.Contents.Value = value;
component.General.AnchorComponent.Value = "PN";
component.General.AnchorLocation.Value = AnchorPointType.BottomLeft;
}
VB.NET
Private Sub addPointNumberComponent(style As LabelStyle)
Dim id As ObjectId = style.AddComponent("PN",
LabelStyleComponentType.Text)
Dim component As LabelStyleTextComponent = TryCast(
id.GetObject(OpenMode.ForWrite), LabelStyleTextComponent)
component.Text.Attachment.Value = LabelTextAttachmentType.MiddleLeft
component.Text.Contents.Value = _pointNumber
component.General.AnchorComponent.Value = "Leader"
component.General.AnchorLocation.Value = AnchorPointType.[End]
End Sub
Private Sub addLocationComponent(style As LabelStyle)
Dim id As ObjectId = style.AddComponent("Location",
LabelStyleComponentType.Text)
Dim component As LabelStyleTextComponent = TryCast(
id.GetObject(OpenMode.ForWrite), LabelStyleTextComponent)
component.Text.Attachment.Value = LabelTextAttachmentType.TopLeft
Dim value As String = [String].Format("({0}, {1}, {2})",
_northing, _easting, _elevation)
component.Text.Contents.Value = value
component.General.AnchorComponent.Value = "PN"
component.General.AnchorLocation.Value = AnchorPointType.BottomLeft
End Sub
The ‘LabelStyle’ class applies to all labels in Civil 3D; however, not every object supports every component type. When a component is added to the ‘LabelStyle’ the type is checked to see if its supported by the labeled object. If it is not supported, an exception will be thrown. Fortunately, the ‘LabelStyle’ object exposes a ‘IsSupporteType()’ method that allows you to check if the type is supported for a specific instance of ‘LabelStyle’.
In future posts, we will dive more into the Labels API, and we will see what things we can do with it. For now, you can start playing adding components, which is a good starting point. Don’t forget to download the full source from the Civilized Development repository.
Comments
You can follow this conversation by subscribing to the comment feed for this post.