Again, I have to thank Drew Avis for feeding me blog material. He sent me some code for a very handy utility that automates the process of turning display representations on and off in a style. In his sample, he uses the surface style, but this technique can be applied to any of the Civil 3D styles.
Style objects expose display components that can be turned on and off either of the plan and model views. This functionality is available through the style methods ‘GetDisplayStylePlan()’ and ‘GetDisplayStyleModel()’, and both return a ‘DisplayStyle’ object representing a display component which exposes a ‘Visible’ property to get and set the state. To access each of the supported display representations, the API provides an enum type for each of the individual styles.
In this example, we will use the surface style. The surface style is represented through the ‘SurfaceStyle’ class, and the supported display representations are defined in the ‘SurfaceDisplayStyleType’ enum. We create an array of ‘SurfaceDisplayStyleType’ values to store the display representations we will like to turn on. Then we will apply those settings to the style. (I omitted how the style object is accessed for clarity, but the full source code is available for download in C# and VB.NET versions).
C#
using (Transaction tr = startTransaction())
{
SurfaceStyle style = styleId.GetObject(OpenMode.ForWrite)
as SurfaceStyle;
SurfaceDisplayStyleType[] settings =
new SurfaceDisplayStyleType[]
{
SurfaceDisplayStyleType.MajorContour,
SurfaceDisplayStyleType.MinorContour,
SurfaceDisplayStyleType.Points,
SurfaceDisplayStyleType.Triangles
};
applySettings(style, settings);
tr.Commit();
}
VB.NET
Using tr As Transaction = startTransaction()
Dim style As SurfaceStyle =
TryCast(styleId.GetObject(OpenMode.ForWrite), SurfaceStyle)
Dim settings As SurfaceDisplayStyleType() =
New SurfaceDisplayStyleType() _
{
SurfaceDisplayStyleType.MajorContour,
SurfaceDisplayStyleType.MinorContour,
SurfaceDisplayStyleType.Points,
SurfaceDisplayStyleType.Triangles
}
applySettings(style, settings)
tr.Commit()
End Using
As you can see, we open the style for write and we create an array of ‘SurfaceDisplayStyleType’ values adding the representations we will like to turn on. The ‘applySettings()’ method is the one that does the work, and you can see the implementation here.
C#
private void applySettings(SurfaceStyle style,
IList<SurfaceDisplayStyleType> settings)
{
IEnumerable<SurfaceDisplayStyleType> displayTypes =
Enum.GetValues(typeof(SurfaceDisplayStyleType))
as IEnumerable<SurfaceDisplayStyleType>;
foreach (SurfaceDisplayStyleType displayType in displayTypes)
{
bool state = settings.Contains(displayType);
style.GetDisplayStylePlan(displayType).Visible = state;
style.GetDisplayStyleModel(displayType).Visible = state;
}
}
VB.NET
Private Sub applySettings(style As SurfaceStyle, _
settings As IList(Of SurfaceDisplayStyleType))
Dim displayTypes As IEnumerable(Of SurfaceDisplayStyleType) =
TryCast([Enum].GetValues(GetType(SurfaceDisplayStyleType)),
IEnumerable(Of SurfaceDisplayStyleType))
For Each displayType As SurfaceDisplayStyleType In displayTypes
Dim state As Boolean = settings.Contains(displayType)
style.GetDisplayStylePlan(displayType).Visible = state
style.GetDisplayStyleModel(displayType).Visible = state
Next
End Sub
The ‘applySettings()’ method retrieves all the enum values defined in ‘SurfaceDisplayStyleType’ and loops through them. If the value is contained in the settings collection, it means that we want to turn it on. We do so by getting the display representation (DisplayStyle) in plan and model and setting its ‘Visible’ property to true. If the value is not contained in the settings collection, the ‘state’ variable is set to false, and so it is the ‘Visible’ property of the display representation.
This is a very handy technique that can save a lot of time when several styles need to be modified, but it also demonstrates the use of the style objects and their display representations. Thanks again to Drew for providing me with this code.
P.S. You can download the entire source for Civilized Development from its BitBucket repository or by cloning the repository with mercurial using the command: “hg clone https://bitbucket.org/IsaacRodriguez/civilizeddevelopment”
Comments
You can follow this conversation by subscribing to the comment feed for this post.