Wednesday 29 August 2012

Code to Name Parcels after selected discharging Structure

Last week I wanted some code to quickly add a user property to a Parcel label to display what structure its flow was discharging too. I tried accessing user defined properties from the API but had no luck and requested an example form the ADN team which Partha posted here.

Seeing the job had to out the door, I wrote this code in the interim to use the Parcel Name and just add a counter on the end to avoid having Parcel name double ups which are not allowed.

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput

'Civil3d Com Imports
Imports AeccLandLib = Autodesk.AECC.Interop.Land

'Civil3d Imports
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.AECC.Interop.Land
Imports Autodesk.AutoCAD.Interop




Public Class ConnectParceltoStructure


<CommandMethod("xpConnectParceltoStructure")> _
Public Sub ConnectParceltoStructure()
Dim document As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = document.Editor
Dim db As Database = document.Database

Dim peo1 As New PromptEntityOptions(vbLf & "Select Catchment Parcel : ")

peo1.SetRejectMessage(vbLf & "Invalid selection...")
peo1.AddAllowedClass(GetType(Autodesk.Civil.Land.DatabaseServices.Parcel), True)

Dim pEntrs As PromptEntityResult = ed.GetEntity(peo1)
If PromptStatus.OK <> pEntrs.Status Then
Return
End If

Dim
ParcelId As ObjectId = pEntrs.ObjectId

Dim peo2 As New PromptEntityOptions(vbLf & "Select Structure to Link to : ")

peo2.SetRejectMessage(vbLf & "Invalid selection...")
peo2.AddAllowedClass(GetType(Autodesk.Civil.PipeNetwork.DatabaseServices.Structure), True)

pEntrs = ed.GetEntity(peo2)
If PromptStatus.OK <> pEntrs.Status Then
Return
End If

Dim
StructureId As ObjectId = pEntrs.ObjectId

Try
Using
trans As Transaction = db.TransactionManager.StartTransaction()
Dim oParcel As Autodesk.Civil.Land.DatabaseServices.Parcel = TryCast(trans.GetObject(ParcelId, OpenMode.ForRead), Entity)
Dim oStructure As Autodesk.Civil.PipeNetwork.DatabaseServices.Structure = TryCast(trans.GetObject(StructureId, OpenMode.ForRead), Entity)

oParcel.UpgradeOpen()


Dim StructureName As String = oStructure.Name

Dim ParcelDischargeStructure As String = oParcel.Name

'Check the all the Parcel Names in the site to see if name already exists
'if it does add counter to the end of name


Dim Count As Integer = 1
Dim x As String = "Writing"
Dim NewParcelName As String = StructureName

'Change the Parcel Name to the Structure Name
Do Until IsNothing(x)
Try
oParcel.Name = NewParcelName

'We have gotten to this line without firing an exception
' therefore written the structure name to the Parcel name set x to nothing to exit loop
x = Nothing

Catch
ex As System.Exception
'MsgBox("Error " & Err.Description & " - " & Err.Number & Convert.ToChar(10))
NewParcelName = StructureName & ("-(" & Count & ")")
Count = Count + 1
'Return
End Try


Loop

trans.Commit()
End Using
Catch
ex As System.Exception
ed.WriteMessage(ex.Message)
End Try
End Sub

End Class

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...