During one of my API trainings, a question came up: is it possible replace an alignment entity, let’s say an Arc, with a Spiral-Curve-Spiral? This can be interesting if we want to ‘validate’ the alignment against some localized standards.
The alignment object of Civil 3D .NET API has the Entities property, which is enumerable, but this may not return the items on the correct order. So we need to use its GetEntityByOrder method. Also, as the list is not indexed by its logical order, we have to use the proper Remove override that receives the entity (and not the index).
The following code show the basic idea: it searches for Arc entities with Radius smaller than 600 and replaces with Spiral-Curve-Spiral.
Private Sub ValidadeAlignment(ByVal align As Alignment)
' go through the alignment entities
' following the order
Dim entCount As Integer = align.Entities.Count
For i As Integer = 0 To entCount - 1
' access each alignment segment
Dim alignEnt As AlignmentEntity = _
align.Entities.GetEntityByOrder(i)
' on this sample, we're looking for simple
' arc entities to be replaces
Select Case alignEnt.EntityType
Case AlignmentEntityType.Arc
' downcast to Align Arc type
Dim arcEnt As AlignmentArc = alignEnt
' on this sample, let's focus on
' arcs with radius smaller than 600
If (arcEnt.Radius < 600) Then
' as the entity will be removed,
' we need to store its data now
Dim previousEnt As Integer = arcEnt.EntityBefore
Dim nextEnt As Integer = arcEnt.EntityAfter
Dim radius As Double = arcEnt.Radius
' ok, now remove the entity
align.Entities.Remove(arcEnt)
' and to replace with
' an SCS (spiral-curve-spiral) entity,
' first we need to calculate the parameters
' of the spirals, but let's assume a value here
Dim spiral1Param As Double = 20
Dim spiral2Param As Double = 20
' and notice that both previous and next
' entities are used here
align.Entities. _
AddFreeSCS(previousEnt, _
nextEnt, _
spiral1Param, _
spiral2Param, _
SpiralParamType.Length, _
radius, _
False, _
SpiralType.Clothoid)
End If
End Select
Next
End Sub