Click here to Skip to main content
15,885,952 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to load AutoCad from my programe. The code is:
Private Sub LoadAcad(ByVal fn As String)
      Dim acadApp As AcadApplication
      Dim acadDoc As AcadDocument
      Dim ProgId As String = "AutoCAD.Application"
      Try
         acadApp = GetObject(, ProgId)
         With acadApp
            .Visible = True
            acadDoc = .Documents.Open(fn, False)
          End With

I catch the error that AutoCad is not loaded so I load it
Catch ex1 As Exception
   Try
      acadApp = CreateObject(ProgId)
      With acadApp
         .Visible = True
         acadDoc = .Documents.Open(fn, False)
      End With

At this point AutoCad is loaded because I see a bit of it before it disappears, and it is there in the Windows Task Manager, but I don't know whether drawing "fn" was loaded. It then raises the exception "Cannot create ActiveX component."
Which is why I have captured the second error.
      Catch ex2 As Exception
         MsgBox(ex2.Message & vbCrLf & "Try Again")
      End Try
   End Try
End Sub


Now if I then execute this code again (from the form's button) it works. Except "fn" is always loaded read-only. I have checked it's attribues just before and it reports that it is Normal.

Any ideas?
Posted

It seems to me that the first attempt locks the file, so the 2nd attempt cannot open it normally (and thus defaults to readonly mode). You are doing something wrong here. Have you gone through their documentation here:

http://docs.autodesk.com/ACD/2011/ESP/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-43ef.htm[^]
 
Share this answer
 
Comments
Wendelius 10-Apr-11 10:37am    
Good catch, 5.
Nish Nishant 10-Apr-11 10:38am    
Thanks Mika.
Stuart Nathan 10-Apr-11 10:55am    
I remarked the first .Documents.Open in the getobject successful and the drawing fn had not been loaded because acadApp = CreateObject(ProgId) raises the error.
However it is a strong possibility that Autocad has locked the file, except there is no .dwl file present.
Also, I don't understand why Acad is in Task Manager - indeed I saw the Markup Set Manager for a moment.
Stuart Nathan 10-Apr-11 10:57am    
Finally, the code you point to is for acdbmg.dll, and that assumes your inProcess. I am using Autodesk.AutoCAD.Interop.dll
Nish Nishant 10-Apr-11 10:58am    
Some of the code samples were in VB.NET/C#. I suppose you could get some ideas from those.
You may try using GetRunningObjectTable[^] to get IRunningObjectTable[^] and see if you are able to get access to the running instance of AutoCad.

.Net version of IRunningObjectTable[^]

It might be worth a try :)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 10-Apr-11 12:40pm    
Good suggestion, my 5.
Espen Harlinn 10-Apr-11 12:41pm    
Thanks Nishant :)
I think the answer is that it takes a little time for the object to be made after the CreateObject statement.
So I created a timer and started it immediately after this statement.

In the timer event I use GetObject to return the Autocad object and it seems to work.

But whatever I do, the drawing is always opened as read-only.
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 17:51pm    
>> But whatever I do, the drawing is always opened as read-only
This may sound stupid, but have you checked the file attributes, and you are sure you have write access to that part of the file system?
Stuart Nathan 10-Apr-11 18:21pm    
Actually yes in code using FileAttributes - it returns Normal. Also properties are NOT read-only. I thought AutoCad locked files using .dwl files, but they aren't there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900