It has been a long time since my last post due to, go figure, all the work we are doing for our next release. It is very hard to keep the weekly post and do my actual work during times like this. I still want to complete the series and I ask you to be patient if I miss some week. There are some more topics to cover, which I think are very interesting.
This week I will like to talk about extracting a grid from a TIN surface. This functionality exists in the application and has been exposed through the API. The API for this functionality is very simple. You just need to get a TIN surface and invoke its ‘ExtractGridded()’ method. The method takes a ‘SurfaceExtractionSettingsType’, which can have a value of ‘Model’ or ‘Plan’. This directly corresponds to 2d or 3d (We use ‘Model’ in the example, which is 3d).
Once the polylines that create the grid are extracted, you can manipulate them as any other object. In the example, we just change their color so you can distinguish them, but you can do whichever manipulation you like. Some people will not want to keep the polylines around, in which case, you can extract the information you want and not commit the transaction, so the polylines are not added to the drawing.
Here is the entire source, which you can also get from the Civilized Development repository at BitBucket.
C#
[CommandMethod("CDS_ExtractGrid")]
public void CDS_ExtractGrid()
{
ObjectId surfaceId = promptForTinSurface();
if (surfaceId == ObjectId.Null)
{
write("\nNo TIN Surface selected.");
return;
}
using (Transaction tr = startTransaction())
{
TinSurface surface = surfaceId.GetObject(OpenMode.ForRead)
as TinSurface;
ObjectIdCollection ids = surface.ExtractGridded(
SurfaceExtractionSettingsType.Model);
foreach (ObjectId id in ids)
{
Polyline3d polyline =
id.GetObject(OpenMode.ForWrite) as Polyline3d;
if (polyline != null)
{
using (polyline)
{
polyline.Color =
AutoCAD.Colors.Color.FromRgb(255, 0, 0);
}
}
}
tr.Commit();
}
}
private ObjectId promptForTinSurface()
{
PromptEntityOptions options = new PromptEntityOptions(
"\nSelect a TIN Surface: ");
options.SetRejectMessage(
"\nThe selected object is not a TIN Surface.");
options.AddAllowedClass(typeof(TinSurface), true);
PromptEntityResult result = _editor.GetEntity(options);
if (result.Status == PromptStatus.OK)
{
return result.ObjectId;
}
return ObjectId.Null; // Indicating error.
}
VB.NET
<CommandMethod("CDS_ExtractGrid")> _
Public Sub CDS_ExtractGrid()
Dim surfaceId As ObjectId = promptForTinSurface()
If surfaceId = ObjectId.Null Then
write(vbLf & "No TIN Surface selected.")
Return
End If
Using tr As Transaction = startTransaction()
Dim surface As TinSurface =
TryCast(surfaceId.GetObject(OpenMode.ForRead), TinSurface)
Dim ids As ObjectIdCollection = surface.ExtractGridded(
SurfaceExtractionSettingsType.Model)
For Each id As ObjectId In ids
Dim polyline As Polyline3d =
TryCast(id.GetObject(OpenMode.ForWrite), Polyline3d)
If polyline IsNot Nothing Then
Using polyline
polyline.Color = AutoCAD.Colors.Color.FromRgb(255, 0, 0)
End Using
End If
Next
tr.Commit()
End Using
End Sub
Private Function promptForTinSurface() As ObjectId
Dim options As New PromptEntityOptions(vbLf & _"Select a TIN Surface: ")
options.SetRejectMessage(vbLf & _
"The selected object is not a TIN Surface.")
options.AddAllowedClass(GetType(TinSurface), True)
Dim result As PromptEntityResult = _editor.GetEntity(options)
If result.Status = PromptStatus.OK Then
' Everything is cool; we return the selected
' surface ObjectId.
Return result.ObjectId
End If
Return ObjectId.Null
' Indicating error.
End Function
Comments
You can follow this conversation by subscribing to the comment feed for this post.