.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trying to open dwg file

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
stevenelliott3894
1795 Views, 10 Replies

Trying to open dwg file

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!!

 

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
10 REPLIES 10
Message 2 of 11

 

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

 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 3 of 11

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

Drive CAD With Code

EESignature

Message 4 of 11
arcticad
in reply to: norman.yuan

norman.yuan,

That works even better 🙂

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 5 of 11

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  Smiley Sad

 

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 6 of 11

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.

 

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 7 of 11

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

 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 8 of 11

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.  Smiley Sad

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 9 of 11

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.

Dave O.                                                                  Sig-Logos32.png
Message 10 of 11

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!)

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 11 of 11

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!

 

 

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost