C#: Specific Document for the Document Object

C#: Specific Document for the Document Object

Anonymous
Not applicable
1,070 Views
3 Replies
Message 1 of 4

C#: Specific Document for the Document Object

Anonymous
Not applicable

Hi,

 

I'm trying to access the database object of specific drawings. Since I'm creating a list of all of the documents that a CAD user has open (for demonstration purpose only):

        public void getDWGList()
        {
            DocumentCollection docs = Application.DocumentManager;

            foreach (Document doc in docs)
            {
                string currentDwg = doc.Name;
                dwgList.Add(currentDwg);
                dwgCount++;

I WILL CALL A FUTURE METHOD HERE WITH THE VARIABLE CURRENTDWG } }

 

I want to iterate through each drawing and collect some information, which I will store in another list.. How do I specify the document below?

 

 

Application.DocumentManager.CURRENTDWG-VARIABLE-FROM-ABOVE.Database;

 

 

0 Likes
Accepted solutions (1)
1,071 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

What about using a Dictionary<string, Database> instead of a List<string>?

This way you can retrieve a document database from its name.

 

        public Dictionary<string, Database> GetDocumentsDatabase()
        {
            DocumentCollection docs = Application.DocumentManager;
            var dict = new Dictionary<string, Database>();
            foreach (Document doc in docs)
            {
                dict[doc.Name] = doc.Database;
            }
return dict; }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

Anonymous
Not applicable

I'm sorry but I'm having trouble understanding how this gives me an advantage over list, is it because I have to break the object statement to place my variable (Drawing Name), so I could control the entire ending like so with a dictionary statement? With my end goal in mind, I do see an advantage in the association capabilities of dictionary, however.

Application.DocumentManager.CURRENTDWGVAR.DATABASEVAR;

 

With your code, how would the syntax look for the above when I am trying to access an open drawing's database from my dictionary to retrieve information?

 

My end goal is to to get each drawing name, access each drawing database, and retrieve the REV# from the title block attribute. So there would be an association between the drawing name and rev number which I will use throughout my code to accomplish various tasks. I have a similar method in LISP that accomplishes these tasks, however cannot deliver the future functionality I am trying to achieve.

 

I'm new to C# so trying to take it one step at a time. I really appreciate the feedback & suggestion.

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

Sorry I'm not sure to understand what you want to achieve.

You can access to the databases of the documents directly in your loop:

foreach (Document doc in docs)
{
string currentDwg = doc.Name;
Database currentDb = doc.Database;
// do what you want here with the document name and document database
}

But as you were storing the document names in an external list, I thaught it would be interesting to store both the name and database of each document in a dictionary so that you can retrieve each document database using its name.

after doing:

var docsNameAndDatabase = GetDocumentsDatabase();

you can get any document database by name:

docsNameAndDatabase[currentDwgVar] returns databaseVar



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes