Replacing Alignment Entities

By Augusto Goncalves

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

   &#1
60; 
End Select

    Next

  End Sub


Comments

4 responses to “Replacing Alignment Entities”

  1. joantopo Avatar
    joantopo

    After this line:
    “For i As Integer = 0 To entCount – 1”
    you should start with “Try{…”
    Because the “GetEntityByOrder” could throw an exception due to:
    This function behaves like the GUI in that disconnected alignment entities are not considered.

  2. You’re right, we just prefer to avoid error checking to keep code clean/small for blog posts, but glad you pointed out.
    Cheers,
    Augusto Goncalves

  3. joantopo Avatar
    joantopo

    I have a question about replacing alignment entities.
    I know that we can retain the ObjectId in AutoCAD API if we remove it and we replace it to another object, but the ID of the alignment entities is an Integer value.
    Can we stablish an old Id entity to a new alignment entity?

  4. Hi,
    I don’t believe you can change this ID, it’s read-only and not a parameter while creating the align entity. That’s where this approach comes in: remove and replace ensuring the previous and next remains.
    Regards,
    Augusto Goncalves

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading