Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET AutoCAD
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 10-Apr-11 5:18am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Mika Wendelius at 10-Apr-11 10:37am
   
Good catch, 5.
Nishant Sivakumar at 10-Apr-11 10:38am
   
Thanks Mika.
Stuart Nathan at 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 at 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
Nishant Sivakumar at 10-Apr-11 10:58am
   
Some of the code samples were in VB.NET/C#. I suppose you could get some ideas from those.
Stuart Nathan at 10-Apr-11 11:03am
   
I understand, and I have been right through the manual which by the way, is nowhere near as comprehensive as the R2000 manuals, and I get no help for my problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
 
Regards
Espen Harlinn
  Permalink  
Comments
Nishant Sivakumar at 10-Apr-11 12:40pm
   
Good suggestion, my 5.
Espen Harlinn at 10-Apr-11 12:41pm
   
Thanks Nishant :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Espen Harlinn at 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 at 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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 Andreas Gieriet 100
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100