It has been a couple of very busy weeks, so I did not have much time for blogging. Plus I have been on training most of this week (you got to keep your skills sharp), so this is going to be a quick post. Still, I do not want to leave you without any information for two weeks, and this post will serve as an introduction for future posts.
As we saw during week 6 in the “COGO Point Bulk Operations” article, the COGO point object in itself is a very lightweight object, which only contains its graphical representation. A big part of representing a point is its label; therefore, the COGO point object, unlike other objects, exposes label properties right in the object itself.
The following example selects a COGO point object and displays its properties related to its label.
C#
[CommandMethod("CDS_ShowCogoPointLabelProperties")]
public void CDS_ShowCogoPointLabelProperties()
{
ObjectId pointId = selectCogoPoint();
if (pointId == ObjectId.Null)
{
return;
}
showLabelProperties(pointId);
}
private ObjectId selectCogoPoint()
{
PromptEntityOptions options = new PromptEntityOptions(
"Select COGO point: ");
options.SetRejectMessage("\nInvalid COGO point selected.");
options.AddAllowedClass(typeof(CogoPoint), true);
PromptEntityResult result = _editor.GetEntity(options);
if (result.Status == PromptStatus.OK)
{
return result.ObjectId;
}
return ObjectId.Null;
}
private void showLabelProperties(ObjectId pointId)
{
using (Transaction tr = startTransaction())
{
CogoPoint point = pointId.GetObject(OpenMode.ForRead)
as CogoPoint;
showLabelPropertiesFor(point);
}
}
private void showLabelPropertiesFor(CogoPoint point)
{
write("\nPoint Label Properties:");
write("\n- Style: " + getLabelStyleName(point.LabelStyleId));
write("\n- Style override: "
+ getLabelStyleName(point.LabelStyleIdOverride));
write("\n- Visible: " + point.IsLabelVisible.ToString());
write("\n- Location: " + point.LabelLocation.ToString());
write("\n- Rotation: " + point.LabelRotation.ToString());
write("\n- Dragged: " + point.IsLabelDragged.ToString());
write("\n- Pinned: " + point.IsLabelPinned.ToString());
}
private string getLabelStyleName(ObjectId id)
{
LabelStyle style = id.GetObject(OpenMode.ForRead) as LabelStyle;
return style.Name;
}
VB.NET
<CommandMethod("CDS_ShowCogoPointLabelProperties")> _
Public Sub CDS_ShowCogoPointLabelProperties()
Dim pointId As ObjectId = selectCogoPoint()
If pointId = ObjectId.Null Then
Return
End If
showLabelProperties(pointId)
End Sub
Private Function selectCogoPoint() As ObjectId
Dim options As New PromptEntityOptions("Select COGO point: ")
options.SetRejectMessage(vbLf & "Invalid COGO point selected.")
options.AddAllowedClass(GetType(CogoPoint), True)
Dim result As PromptEntityResult = _editor.GetEntity(options)
If result.Status = PromptStatus.OK Then
Return result.ObjectId
End If
Return ObjectId.Null
End Function
Private Sub showLabelProperties(pointId As ObjectId)
Using tr As Transaction = startTransaction()
Dim point As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForRead),
CogoPoint)
showLabelPropertiesFor(point)
End Using
End Sub
Private Sub showLabelPropertiesFor(point As CogoPoint)
write(vbLf & "Point Label Properties:")
write(vbLf & "- Style: " & getLabelStyleName(point.LabelStyleId))
write(vbLf & "- Style override: " _
& getLabelStyleName(point.LabelStyleIdOverride))
write(vbLf & "- Visible: " & point.IsLabelVisible.ToString())
write(vbLf & "- Location: " & point.LabelLocation.ToString())
write(vbLf & "- Rotation: " & point.LabelRotation.ToString())
write(vbLf & "- Dragged: " & point.IsLabelDragged.ToString())
write(vbLf & "- Pinned: " & point.IsLabelPinned.ToString())
End Sub
Private Function getLabelStyleName(id As ObjectId) As String
Dim style As LabelStyle = TryCast(id.GetObject(OpenMode.ForRead),
LabelStyle)
Return style.Name
End Function
The code, among other things, shows the label style name. There is also a label style override property, which returns the ‘ObjectId’ of the label style that overrides the one explicitly set in the COGO point. This property is read-only and comes from the primary point group to which the COGO point belongs. Of course, you have to state in the point group that you want to override the point label style. Here is an example of how to do that.
C#
[CommandMethod("CDS_OverrideLabelsForGroup")]
public void CDS_OverrideLabelsForGroup()
{
using (Transaction tr = startTransaction())
{
ObjectId pointGroupId = promptForPointGroup();
if (pointGroupId == ObjectId.Null)
{
return;
}
overrideLabelsForPointsIn(pointGroupId);
tr.Commit();
}
}
private ObjectId promptForPointGroup()
{
PromptResult result = _editor.GetString(
"\nEnter point group name: ");
if (result.Status == PromptStatus.OK)
{
return findGroup(result.StringResult);
}
return ObjectId.Null;
}
private void overrideLabelsForPointsIn(ObjectId pointGroupId)
{
PointGroup group = pointGroupId.GetObject(OpenMode.ForWrite)
as PointGroup;
group.IsPointLabelStyleOverridden = true;
}
private ObjectId findGroup(string name)
{
foreach (ObjectId id in _civildoc.PointGroups)
{
PointGroup group = id.GetObject(OpenMode.ForRead)
as PointGroup;
if (group.Name == name)
{
return id;
}
}
return ObjectId.Null;
}
VB.NET
<CommandMethod("CDS_OverrideLabelsForGroup")> _
Public Sub CDS_OverrideLabelsForGroup()
Using tr As Transaction = startTransaction()
Dim pointGroupId As ObjectId = promptForPointGroup()
If pointGroupId = ObjectId.Null Then
Return
End If
overrideLabelsForPointsIn(pointGroupId)
tr.Commit()
End Using
End Sub
Private Function promptForPointGroup() As ObjectId
Dim result As PromptResult = _editor.GetString(vbLf _
& "Enter point group name: ")
If result.Status = PromptStatus.OK Then
Return findGroup(result.StringResult)
End If
Return ObjectId.Null
End Function
Private Sub overrideLabelsForPointsIn(pointGroupId As ObjectId)
Dim group As PointGroup = TryCast(
pointGroupId.GetObject(OpenMode.ForWrite), PointGroup)
group.IsPointLabelStyleOverridden = True
End Sub
Private Function findGroup(name As String) As ObjectId
For Each id As ObjectId In _civildoc.PointGroups
Dim group As PointGroup = TryCast(id.GetObject(OpenMode.ForRead),
PointGroup)
If group.Name = name Then
Return id
End If
Next
Return ObjectId.Null
End Function
In a future post, we will look at the Labels API more in deep, since it is also part of the new functionality in Jay Peak.
Comments
You can follow this conversation by subscribing to the comment feed for this post.