Open multiple models, perform a transaction, save and close

pari_dhanakoti
Enthusiast

Open multiple models, perform a transaction, save and close

pari_dhanakoti
Enthusiast
Enthusiast

I am building a civil3D add-in that works great, when it accesses the active document, reads and sets properties. Now I am extending this add-in to work on multiple files. Basically, when the add-in is executed via NETLOAD, and the user gives the add-ins' command, it opens the file explorer allowing user to select multiple models.

 

Skeleton code to iterate through files selected in the files explorer, is below.

 

 if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 {
     // Iterate through the selected files
      foreach (string fileName in openFileDialog.GetFilenames())
      {
          try
          {
             // open the DWG file
             Document doc = Application.DocumentManager.Open(fileName, 
             false);
             // make it the active document
             Application.DocumentManager.MdiActiveDocument = doc;                        
             Document acadDoc = 
             AutoCADApplication.DocumentManager.MdiActiveDocument;
             Database db = acadDoc.Database;
             // Get the current document editor
             Editor editor = 
               AutoCADApplication.DocumentManager.MdiActiveDocument.Editor;
             // Write the file name to the editor
              editor.WriteMessage("\nExecuting PDSForTrack add-in on: " + 
              fileName);
              using (Transaction trans = 
                 db.TransactionManager.StartTransaction) 
              {
                    // Access the named dictionary
                    DBDictionary namedDict = (DBDictionary)
                                trans.GetObject(db.NamedObjectsDictionaryId, 
                                OpenMode.ForWrite);
                   DBDictionary propSetDefsDict = (DBDictionary)                               
                    trans.GetObject(namedDict.GetAt("AEC_PROPERTY_SET_DEFS"), 
                    OpenMode.ForWrite);

                   // Get Project_Parameters property set for PDS_Code
                    PropertySetDefinitionDetails                        
                            propSetDefnProjectParametersForPDS = 
                              GetPropertySetDefinition(trans, db, 
                                 Constants.PROPERTY_SET_PROJECT_PARAMETERS, 
                                  Constants.PROPERTY_PDS_CODE, errors);
                      //rest of the code, to read properties, set properties etc 
                     trans.Commit();
                   }
                db.SaveAs(acadDoc.Name, DwgVersion.Current);
                        
                // Close the document
                doc.CloseAndSave(doc.Name);
         }
         catch {
         }
      }
   }

 

However I am noticing following problems:

1. The GetPropertySetDefinition() [This is a function that I wrote to access the PROJECT_PARAMETERS property set from AEC_PROPERTY_SET_DEFS named dictionary]. This always returns null when accessing the document within the foreach loop like in the code above. However, only performing the action on the active document that is already opened (the one on which the NETLOAD command is loaded), it works (That is when there is no loop to go through each file and making it active document)

2. db.SaveAs(acadDoc.Name, DwgVersion.Current) always returns an error. it is very generic. The error code says "FileError" and source is "AcdbMgd"

 

This makes me think the Transaction object is somehow messed up.

My question is:

how do I make the add-in open a file that user selects in the file explorer, make it the active document and access the property sets etc on that document, write something, save and close it?

 

Any insights are appreciated!

 

 

 

0 Likes
Reply (1)

pari_dhanakoti
Enthusiast
Enthusiast
Accepted solution

Found out that  I had to change the working database to the newly opened doc's database everytime I switch to a new doc.

Also added CommandFlags.Session to the CommandMethod decorator. 

https://forums.autodesk.com/t5/net/change-activedocument/td-p/5464054 has more details.

 

0 Likes