GetAcadDocument

GetAcadDocument

Pfilc
Enthusiast Enthusiast
3,261 Views
6 Replies
Message 1 of 7

GetAcadDocument

Pfilc
Enthusiast
Enthusiast

I'm having trouble trying to get the GetAcadDocument method to work.

 

2012 code:

ThisDrawing = DocumentManager.MdiActiveDocument.AcadDocument

 

2013:

ThisDrawing = DocumentExtension.GetAcadDocument(Application.DocumentManager.MdiActiveDocument)

 

Produces a run time error.  I know just enough .net to brute force my app to do what I want and would appreciate any help.

 

Thanks,

Phil

0 Likes
3,262 Views
6 Replies
Replies (6)
Message 2 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

for me it's running (it used time to find the alternative to .AcadDocument but with the method same to yours it's working for me. How is ThisDrawing declared at your code, what runtime-error do you get exactly?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 7

Pfilc
Enthusiast
Enthusiast

Thanks for the response Alfred...confirming the code was ok made me look for a more fundamental problem and I realized I had a bad reference.

0 Likes
Message 4 of 7

Anonymous
Not applicable

Greetings, I have the same problem you had, I would like you to help me solve mine, with some questions
. How did you fix it?
- Versiond and vb.net What did you use?
- What version of AutoCAD are you?
- What references did you use?

I await your reply, thanks.

0 Likes
Message 5 of 7

_gile
Consultant
Consultant

Hi,

 

Just a little precision about GetAcadDocument() method.

 

It's an extension method which extents the Document type (defining extension isn't exactly the same with C# or VB).

 

Extension methods can be called both way (assuming doc = MdiActivedocument):

 

- as static (Shared) methods as shown upper:

ThisDrawing = DocumentExension.GetAcadDocument(doc)

 

- as instance methods of the extended type:

ThisDrawing = doc.GetAcadDocument():

 

The second one will directly work with C# because DocumentExtension is a class of the ApplicationServices namespace.

But with VB extension methods have to be defined in a Module, so, the DocumentExtension module have to be imported for this behavior to be available.

Imports Autodesk.AutoCAD.ApplicationServices.DocumentExtension



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 7

Anonymous
Not applicable

Gilles,

I am moving over my VB app to C#. 

 

I used this in VB

 

Public ReadOnly Property ThisDrawing() as AcadDocument

     Get

          Return

               Core.Application.DocumentManager.MdiActiveDocument.GetAcadDocument()

     End Get

End Property

 

then in a different sub

ThisDrawing.ObjectSnapMode = False  

 

 

However I couldn't do this in C# so I tried:

 

private readonly Document roDocument = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;

 

public object ThisDrawing
{
     get
     {
          return roDocument.GetAcadDocument();
     }
}

 

then in a different sub

ThisDrawing.ObjectSnapMode = False   - however ObjectSnapMode does not come up.

 

Do you have any ideas?

0 Likes
Message 7 of 7

norman.yuan
Mentor
Mentor

Your C# code

 

public object ThisDrawing
{
     get
     {
          return roDocument.GetAcadDocument();
     }
}

 

is not exactly equivalent to the original VB.NET code:

 

Public ReadOnly Property ThisDrawing() as AcadDocument

     Get

          Return

               Core.Application.DocumentManager.MdiActiveDocument.GetAcadDocument()

     End Get

End Property

 

In the VB.NET code, the property ThisDrawing is a type of AcadDocument (you must had reference to AutoCAD's COM API), while in the C# code, the same property ThisDrawing is type of object, thus you cannot have code like ThisDrawing.ObjectSnapMode=..... To make the C# code the same as VB.NET, you need to add reference to AutoCAD COM API and expose the property ThisDrawing as AcadDocument.

 

BTW, unless it is really necessary, in most cases, there is no need to mix COM APIs in the .NET API development, especially the VBA style of "ThisDrawing" shown in various code examples available on the net is bad-taste code used in AutoCAD .NET API programming, IMO.

 

Norman Yuan

Drive CAD With Code

EESignature