Mr. Avis is on a roll. He not only co-author a previous post (Using Water-drops to Analyze Rainfall), he has now provided me with a very simple, easy to follow snippet of code that demonstrates how to create new elevation ranges in surfaces through the new .NET API. The command is very simple. It prompts the user for a TIN surface object and then calls a method to create the elevation regions. Here is the code:
C#
public void CDS_CreateElevationRanges() { AcadDb.ObjectId surfaceId = promptForTinSurface(); if (surfaceId == AcadDb.ObjectId.Null) { return; } using (AcadDb.Transaction tr = startTransaction()) { TinSurface surface = surfaceId.GetObject(AcadDb.OpenMode.ForWrite) as TinSurface; // Get the existing analysis, if any. SurfaceAnalysisElevationData[] data = surface.Analysis.GetElevationData(); _editor.WriteMessage("\nExisting analysis length: {0}", data.Length); SurfaceAnalysisElevationData[] newData = CreateElevationRegions(surface, 10, 100); surface.Analysis.SetElevationData(newData); tr.Commit(); } }
VB.NET
Public Sub CDS_CreateElevationRanges() Dim surfaceId As AcadDb.ObjectId = promptForTinSurface() If surfaceId = AcadDb.ObjectId.Null Then Return End If Using tr As AcadDb.Transaction = startTransaction() Dim surface As TinSurface = _ TryCast(surfaceId.GetObject(AcadDb.OpenMode.ForWrite), _ TinSurface) ' Get the existing analysis, if any. Dim data As SurfaceAnalysisElevationData() = _ surface.Analysis.GetElevationData() _editor.WriteMessage(vbLf & "Existing analysis length: {0}", _ data.Length) Dim newData As SurfaceAnalysisElevationData() = _ CreateElevationRegions(surface, 10, 100) surface.Analysis.SetElevationData(newData) tr.Commit() End Using End Sub
The nuts and bolts is the ‘CreateElevationRegions()’ method, which calculate the ranges based on the number of steps passed, creates the elevation data, and sets the color for each of the ranges. The implementation looks like this:
C#
private SurfaceAnalysisElevationData[] CreateElevationRegions( Surface surface, int steps, short startColor) { GeneralSurfaceProperties props = surface.GetGeneralProperties(); double minElevation = props.MinimumElevation; double maxElevation = props.MaximumElevation; double increment = (maxElevation - minElevation) / steps; SurfaceAnalysisElevationData[] newData = new SurfaceAnalysisElevationData[steps]; for (int i = 0; i < steps; i++) { Color color = Color.FromColorIndex(ColorMethod.ByLayer, (short)(100 + (i * 2))); newData[i] = new SurfaceAnalysisElevationData( minElevation + (increment * i), minElevation + (increment * (i + 1)), color); } return newData; }
VB.NET
Private Function CreateElevationRegions(surface As Surface, _ steps steps As Integer, _ startColor startColor As Short) _ As SurfaceAnalysisElevationData() Dim props As GeneralSurfaceProperties = _ surface.GetGeneralProperties() Dim minElevation As Double = props.MinimumElevation Dim maxElevation As Double = props.MaximumElevation Dim increment As Double = (maxElevation - minElevation) / steps Dim newData As SurfaceAnalysisElevationData() = _ New SurfaceAnalysisElevationData(steps - 1) {} For i As Integer = 0 To steps - 1 Dim color__1 As Color = Color.FromColorIndex( _ ColorMethod.ByLayer, CShort(100 + (i * 2))) newData(i) = New SurfaceAnalysisElevationData( _ minElevation + (increment * i), _ minElevation + (increment * (i + 1)), _ color__1) Next Return newData End Function
That’s it. Thanks again to Drew Avis for providing the code for this great example.
Guys
Thanks for another good example. I am currently working on some code for bounded surface volumes using some of your previous posts. But am still using the com boundvolume method has this be ported to .net I have searched the online help but found nothing.
Regards
Justin Ralston
http://c3dxtreme.blogspot.com/
Posted by: Justin Ralston | 10/15/2011 at 02:06 PM