I am converting a VBA app to .Net and I have very nearly run out of hair to pull out. Now, the simplest thing is holding me up: opening a drawing file. Everything was fine until I added the code to open a file (adapted straight from the .Net Developer's Guide). When execution hits the open code, it gives me an error of 'Invalid Execution Context'. Huh?
Imports
AcApp = Autodesk.AutoCAD.ApplicationServices.Application
Public Sub OpenDwg(ByVal strFileName As String)
Dim acDocMgr As DocumentCollection = AcApp.DocumentManager
If (File.Exists(strFileName)) Then
acDocMgr.Open(strFileName, False) <--- This is the code flagged as error
Else
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
End If
End Sub
I have tried every permutation of this code I can think of, including fully qualifying everything. I always get the same error. When I check the details, it shows me an error code of -2145386296.
UGH! I hate this crappy API!!
Solved! Go to Solution.
Solved by stevenelliott3894. Go to Solution.
Imports Autodesk.AutoCAD.ApplicationServices Public Class Class1 Function OpenFile(ByVal FullPath As String, ByRef myDocument As Autodesk.AutoCAD.ApplicationServices.Document, ByVal bolReadOnly As Boolean) As Boolean Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim rtnValue As Boolean Try myDocument = acDocMgr.Open(FullPath, bolReadOnly) rtnValue = True Catch ex As Exception rtnValue = False End Try Return rtnValue End Function Function CloseFile() As Boolean Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim rtnValue As Boolean Try acDocMgr.MdiActiveDocument.CloseAndDiscard() rtnValue = True Catch ex As Exception rtnValue = False End Try Return rtnValue End Function End Class
Where the Sub is called from? From CommandMeth, right?
By default (e.g. without setting a CommandFlags value in CommandMethod attribute, the command is in the scope of drawing context. There is why you get this exception. You need to set a Session flag:
<CommandMethod("OpenDwg",CommandFlags.Session)> _
public Sub OpenMyDrawing()
...
OpenDwg("C:\mydwgs\theDrawingFile.dwg",False)
...
End Sub
Public Sub OpenDwg(ByVal strFileName As String)
Dim acDocMgr As DocumentCollection = AcApp.DocumentManager
If (File.Exists(strFileName)) Then
acDocMgr.Open(strFileName, False)
Else
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
End If
End Sub
Norman Yuan
norman.yuan,
That works even better 🙂
Thanks, guys, but both methods provided above give me the exact same error I was getting before.
I do seem to remember, before this part was written, coming across some setting (or option, etc.) that basically referenced some part of my app to the document, and while checking it out, I saw that I could set it to the session (or app?). Since I thought that it made more sense to work from the autocad app instead of a document (drawing), I changed it. That may or may not be related to this, but I bet it is. I just don't remember exactly what it was or where I came across it.
Not happy
Yes, something is messed up. Now other parts of my app aren't working. I got an 'eLockViolation' error trying to create a new layer. This worked fine before.
You should always lock the drawing.
Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.ApplicationServices Public Class Class2 Sub test() Using db As Database = HostApplicationServices.WorkingDatabase() Using tr As Transaction = db.TransactionManager.StartTransaction() Dim myDWG As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Using lock As DocumentLock = myDWG.LockDocument ' run code End Using End Using End Using End Sub End Class
Thanks for responding again.
It worked before without the locking code. None of the examples (from AutoDesk and others) show that.
Anyway, it doesn't change anything when I add that, either.
This is killing me. I am already almost a week overdue.
If you are running code in the session context, you always have to manage your own document locks.
When you are running code in the document context, frequently they are not necessary, unless you are running code from a form.
Thanks for the help. Is that documented? I'm having quite a difficult time with the lack of decent documentation.
Nevertheless, all of this has not cleared up my original problem... getting the 'Invalid Execution Context' when trying to open an existing drawing (in Acad 2010).
Any further help would be appreciated!! (pretty soon I won't have anymore hair left to pull out!)
Ok, the solution to the 'Invalid Execution Context' is: You cannot open a drawing from a modal form. I switched it to modeless and no more error!
Locking the document seems to have fixed the eLock violation error when trying to create a layer.
Thanks for all the help, guys!