Divide and Conquer

Last week I presented at the Local User Group here TBAUG. I taught on Civil3D Project Workflow and we focused on how to divide and conquer. This is a philosophy that existed before Civil3D when we separated our work and xref’d our files together to create sheet files.

The same mentality should be applied to software development. Say, for example, that you are building a subroutine that will display a list of all the alignments in the drawing and then let the user to select an alignment and create surface profiles from said alignment. Now you COULD build all the code to create a list of all the alignments in the drawing OR better yet you could build a separate function that will return a list of alignment names. The function is better since now you can re-use the code and when you tweak the one function (the alignment’s list function) you are improving every routine that calls that function.

Don’t just stop there but also divide your Visual Studio Projects and package them all into one solution. This way you can reuse the function now not only with one software package but multiple software packages.

So whether you are coding, working with Civil3D, or just plain AutoCAD – remember to Divide and Conquer.

Oh, I suppose you want some code….

The following is a quick and dirty way to get a collection of alignment object data. I created a simple object data class that is useful to store for any Civil3D object because it is a bad practice to keep objects open from a transaction.

 

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

 

Imports Autodesk.Civil

Imports Autodesk.Civil.Land.DatabaseServices

 

Public Class Alignments

    ‘Gets a collection of ObjectData regarding all the alignments in the drawing.

    ‘Optionally set if to list referenced alignments or not

    Public Function GetAlignmentsList(ByVal allowDrefs As Boolean) As Generic.List(Of ObjectData)

        Dim objIdCol As ObjectIdCollection = ApplicationServices.CivilApplication.ActiveDocument.GetAlignmentIds

        ‘Get the alinId’s

 

        ‘If no alignments

        If objIdCol Is Nothing Then Return Nothing

        If objIdCol.Count = 0 Then Return Nothing

 

        ‘If there are alignments iterate through alignments

        Dim alin As Alignment

        Dim alinCol As New Generic.List(Of ObjectData)

        Dim alinData As ObjectData

        Using lck As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument

            Using db As Database = HostApplicationServices.WorkingDatabase

                Using trans As Transaction = db.TransactionManager.StartTransaction

                    For Each objId As ObjectId In objIdCol

                        alin = trans.GetObject(objId, OpenMode.ForRead)

                        alinData = New ObjectData(objId, alin.Name, alin.Description)

                        alinCol.Add(alinData)

                    Next

                End Using

            End Using

        End Using

        If alinCol Is Nothing Then Return Nothing

        If alinCol.Count = 0 Then Return Nothing

        Return alinCol

    End Function

End Class

Public Class ObjectData

    Private sName, sDesc As String

    Private sId As ObjectId

    Private cObjs() As Generic.List(Of ObjectData)

 

    Public Sub New(ByVal objId As ObjectId, ByVal objName As String, ByVal objDesc As String)

        MyBase.New()

        sName = objName

        sDesc = objDesc

        sId = objId

    End Sub

 

    Public Property ObjectName() As String

        Get

            ObjectName = sName

        End Get

        Set(ByVal value As String)

            sName = value

        End Set

    End Property

 

    Public Property ObjectDescription() As String

        Get

            ObjectDescription = sDesc

        End Get

        Set(ByVal value As String)

            sDesc = value

        End Set

    End Property

 

    Public Property Id() As ObjectId

        Get

            Id = sId

        End Get

        Set(ByVal value As ObjectId)

            sId = value

        End Set

    End Property

 

    Public Property ChildObjects() As Generic.List(Of ObjectData)()

        Get

            ChildObjects = cObjs

        End Get

        Set(ByVal value As Generic.List(Of ObjectData)())

            cObjs = value

        End Set

    End Property

End Class

One comment

  1. […] we divide and conquer some of these […]