As those who frequent this board probably know by now, I'm a beginner starting out with programming for AutoCAD.
I'm trying to write a plug-in that involves operations with TIN surfaces. I tried to follow the example from the Developer's guide just to see if I could get accessing a TIN surface to work, but without success, and now I'm stumped.
Here's my code, which is supposed prompt the user to pick a surface, and then simply spit out the name of the surface that was selected:
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices
<Assembly: CommandClass(GetType(surfacetest.MyCommands))>
Namespace surfacetest
Public Class MyCommands
<CommandMethod("SurfName")> _
Public Sub MyCommand() ' This method can have any name
Dim exground As Autodesk.Civil.DatabaseServices.TinSurface
Dim options As PromptEntityOptions
Dim result As PromptEntityResult
Dim myeditor As Editor
Dim surfaceID As ObjectId
Dim surfname As String
myeditor = Application.DocumentManager.MdiActiveDocument.Editor
options = New PromptEntityOptions(vbNullString & "Select existing ground surface")
options.SetRejectMessage(vbNullString & "The selected object is not a TIN Surface.")
options.AddAllowedClass(GetType(Autodesk.Civil.DatabaseServices.TinSurface), True)
result = myeditor.GetEntity(options)
If (result.Status = PromptStatus.OK) Then
surfaceID = result.ObjectId
Else
surfaceID = ObjectId.Null
Exit Sub
End If
exground = surfaceID.GetObject(OpenMode.ForRead)
surfname = exground.Name
myeditor.WriteMessage(vbNullString & "You have selected the surface: " & surfname)
End Sub
End Class
End Namespace
The plug-in is loading just fine and the initial prompt comes up, but whenever I actually select a surface after being prompted, I get a fatal error that causes AutoCAD to close. Anybody have any ideas about what's going wrong?
Solved! Go to Solution.
Solved by Jeff_M. Go to Solution.
You need a Transaction in which to use object.GetObject().
Before tackling the C3D API you might want to run through the AutoCAD .NET tutorials that are available. Doing so will help you understand why/when you need transactions. My First AutoCAD Plugin
MyDb As Database = Application.DocumentManager.MdiActiveDocument.Database
Using MyTrans as Transaction = MyDb.TransactionManager.StartTransaction()
''your code to GetObject and work with it
''all done, Commit the Transaction
MyTrans.Commit()
End Using
Forgive me if there are typos up there, this was done on the fly and I don't normally do VB at all.
You will get many example code snippet using Transaction, PromptEntityOptions and .NET in our IM DevBlog -
http://adndevblog.typepad.com/infrastructure/
Thanks,
Partha
I had my attention diverted from this for a little while because I had to work on some other stuff, and then I apparently had some other issues with my code that kept it from working for a little while...but it's finally working thanks to your input.
Thank you!