Rename first layout tab across many DWG files (without opening each drawing)

Rename first layout tab across many DWG files (without opening each drawing)

Anonymous
Not applicable
1,125 Views
2 Replies
Message 1 of 3

Rename first layout tab across many DWG files (without opening each drawing)

Anonymous
Not applicable

I am attempting to use VB .NET to loop through a set of DWG files and rename the first layout tab in each.  

I started using the following code:

http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-5FA86EF3-DEFD-4256-BB1C-56DAC32BD868

 

Using this, plus the AcSmSheetSetMgr I can loop through each DWG file in a sheet set without issue.  I am then able to determine the first layout tab using the following code:


Dim sheet As AcSmSheet = item

acExDb = New Database(False, True)
acExDb.ReadDwgFile(sheet.GetLayout().GetFileName(), FileOpenMode.OpenForReadAndAllShare, True, "")

ed.WriteMessage("PROCESSING LAYOUT FOR SHEET: " + sheet.GetName().ToString() + " Filename: " + sheet.GetLayout().GetFileName() + vbCrLf)

Dim previousActiveDocument As Document = Application.DocumentManager.MdiActiveDocument

Dim layout As AcSmAcDbLayoutReference = sheet.GetLayout()
Dim sheetFileName As String = layout.GetFileName() 'the dwg


Dim dwgDoc As Document = Application.DocumentManager.Open(sheet.GetLayout().GetFileName())
Dim dwgDB As Database = dwgDoc.Database

Dim LayoutMgr As LayoutManager = LayoutManager.Current

Using dwgDoc.LockDocument()
Using acTrans As Transaction = dwgDB.TransactionManager.StartTransaction()
Dim lays As DBDictionary = acTrans.GetObject(dwgDB.LayoutDictionaryId, OpenMode.ForWrite) ' OpenMode.ForRead
dwgDoc.Editor.WriteMessage(vbCrLf & "Layouts:")
Dim isFirst As Boolean = True
For Each dbEntryItem As DBDictionaryEntry In lays
If Not dbEntryItem.Key = "Model" Then
If isFirst = True Then
LayoutMgr.RenameLayout(dbEntryItem.Key, sheet.GetName().ToString())
isFirst = False
End If
End If
Next
acTrans.Commit()
acTrans.Dispose()
End Using
End Using
dwgDB.Dispose()

'save it
dwgDoc.Database.SaveAs(sheetFileName, DwgVersion.Current)
'then close it
dwgDoc.CloseAndDiscard()

Application.DocumentManager.MdiActiveDocument = previousActiveDocument

 

Basically, using this code I can rename layout tabs, however this opens the DWG, then updates the layout tab, then saves/closes the DWG.  I am trying to perform this function in a loop for up to several hundred drawings, and as such this runs incredibly slowly and isn't really an ideal solution.  Is there a way to update the first layout tab without using the currently active document?  It appears the RenameLayout function in the LayerManager requires the DWG in question to be open.

 

Thanks

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

_gile
Consultant
Consultant
Accepted solution

Hi,

 

According to the suject title (I can't read the unformated VB code you posted, please, use the 'Insert Code button </>).

 

        public static List<string> RenameLayouts (IEnumerable<string> fileNames, string newName)
        {
            var errorFiles = new List<string>();
            var workingDb = HostApplicationServices.WorkingDatabase;
            foreach (string fileName in fileNames)
            {
                try
                {
                    using (var db = new Database(false, true))
                    {
                        db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, null);
                        string layoutName;
                        using (var tr = new OpenCloseTransaction())
                        {
                            layoutName = ((DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead))
                                .Cast<System.Collections.DictionaryEntry>()
                                .Select(e => (Layout)tr.GetObject((ObjectId)e.Value, OpenMode.ForRead))
                                .OrderBy(l => l.TabOrder)
                                .Select(l => l.LayoutName)
                                .First(n => n != "Model");
                        }
                        HostApplicationServices.WorkingDatabase = db;
                        var lm = LayoutManager.Current;
                        lm.RenameLayout(layoutName, newName);
                        db.SaveAs(fileName, DwgVersion.Current);
                    }
                }
                catch (System.Exception)
                {
                    errorFiles.Add(fileName);
                }
                finally
                {
                    HostApplicationServices.WorkingDatabase = workingDb;
                }
            }
            return errorFiles;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable
Accepted solution

Many thanks!  Apologies for the lack of formatting.  I'll give this a try.

0 Likes