i want to change path of nested xrefs if it is unloaded, but my code only works to the parent xrefs only.
It works if we open dwg one by one manually (not batch mode).
Please help me to fix this, or give me solution to open all dwgs one by one as a batch process and run this code and close the dwg.
Private Sub button_Publish_Click(sender As Object, e As EventArgs) Handles button_Publish.Click Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor If openFile1.FileNames.Length > 0 Then Dim strFile As String For Each strFile In openFile1.FileNames Using db As Database = New Database(False, True) db.ReadDwgFile(strFile, FileOpenMode.OpenForReadAndAllShare, False, Nothing) Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim collection As ObjectIdCollection = New ObjectIdCollection() Using tr As Transaction = db.TransactionManager.StartTransaction() Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) For Each btrId As ObjectId In bt Dim btr As BlockTableRecord = TryCast(tr.GetObject(btrId, OpenMode.ForRead), BlockTableRecord) If btr.IsFromExternalReference And btr.IsUnloaded Then Collection.Add(btrId) btr.UpgradeOpen() Dim oldPath As String = btr.PathName Dim newpath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Temp.dwg") btr.PathName = newpath ed.WriteMessage(String.Format("{0}Old Path : {1} New Path : {2}", Environment.NewLine, oldPath, newpath)) End If Next If Collection.Count > 0 Then db.ReloadXrefs(Collection) End If tr.Commit() End Using db.SaveAs(db.OriginalFileName, True, db.OriginalFileVersion, db.SecurityParameters) End Using Next End If End Sub
Solved! Go to Solution.
i want to change path of nested xrefs if it is unloaded, but my code only works to the parent xrefs only.
It works if we open dwg one by one manually (not batch mode).
Please help me to fix this, or give me solution to open all dwgs one by one as a batch process and run this code and close the dwg.
Private Sub button_Publish_Click(sender As Object, e As EventArgs) Handles button_Publish.Click Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor If openFile1.FileNames.Length > 0 Then Dim strFile As String For Each strFile In openFile1.FileNames Using db As Database = New Database(False, True) db.ReadDwgFile(strFile, FileOpenMode.OpenForReadAndAllShare, False, Nothing) Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim collection As ObjectIdCollection = New ObjectIdCollection() Using tr As Transaction = db.TransactionManager.StartTransaction() Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) For Each btrId As ObjectId In bt Dim btr As BlockTableRecord = TryCast(tr.GetObject(btrId, OpenMode.ForRead), BlockTableRecord) If btr.IsFromExternalReference And btr.IsUnloaded Then Collection.Add(btrId) btr.UpgradeOpen() Dim oldPath As String = btr.PathName Dim newpath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Temp.dwg") btr.PathName = newpath ed.WriteMessage(String.Format("{0}Old Path : {1} New Path : {2}", Environment.NewLine, oldPath, newpath)) End If Next If Collection.Count > 0 Then db.ReloadXrefs(Collection) End If tr.Commit() End Using db.SaveAs(db.OriginalFileName, True, db.OriginalFileVersion, db.SecurityParameters) End Using Next End If End Sub
Solved! Go to Solution.
Solved by aruntr. Go to Solution.
Thanks norman.yuan for this code
using System.Collections.Generic; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(BatchWorkTest.MyCommands))] namespace BatchWorkTest { public class MyCommands { [CommandMethod("BatchDwg", CommandFlags.Session )] public static void RunCommand() { var dwg = CadApp.DocumentManager.MdiActiveDocument; var ed = dwg.Editor; var fileNames = GetFileNames(); if (fileNames==null) { ed.WriteMessage("\n*Cancel*"); return; } ProcessDwgs(fileNames); } private static IEnumerable GetFileNames() { IEnumerable files = null; using (var dialog = new System.Windows.Forms.OpenFileDialog { DefaultExt = ".dwg", Multiselect = true, Filter = "dwg files|*.dwg", }) { if (dialog.ShowDialog()== System.Windows.Forms.DialogResult.OK) { files = dialog.FileNames; } } return files; } private static void ProcessDwgs(IEnumerable dwgFiles) { var dwgCol = CadApp.DocumentManager; foreach (var dwgFile in dwgFiles) { // Since the CommnadFlags.Session flag is set // The newly opened drawing becomes MdiActiveDocument automatically var dwg = dwgCol.Open(dwgFile, false, null); using (var lck = dwg.LockDocument()) { DoSomethingInDwg(dwg); } // Save and close dwg.Database.SaveAs(dwg.Name, true, DwgVersion.Current, null); dwg.CloseAndDiscard(); } } private static void DoSomethingInDwg(Document dwg) { //CadApp.ShowAlertDialog($"Processing drawing:\n{dwg.Name}"); //change 2 user system variables dwg.Database.Useri1 = 1; dwg.Database.Useri2 = 2; } } }
Thanks norman.yuan for this code
using System.Collections.Generic; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(BatchWorkTest.MyCommands))] namespace BatchWorkTest { public class MyCommands { [CommandMethod("BatchDwg", CommandFlags.Session )] public static void RunCommand() { var dwg = CadApp.DocumentManager.MdiActiveDocument; var ed = dwg.Editor; var fileNames = GetFileNames(); if (fileNames==null) { ed.WriteMessage("\n*Cancel*"); return; } ProcessDwgs(fileNames); } private static IEnumerable GetFileNames() { IEnumerable files = null; using (var dialog = new System.Windows.Forms.OpenFileDialog { DefaultExt = ".dwg", Multiselect = true, Filter = "dwg files|*.dwg", }) { if (dialog.ShowDialog()== System.Windows.Forms.DialogResult.OK) { files = dialog.FileNames; } } return files; } private static void ProcessDwgs(IEnumerable dwgFiles) { var dwgCol = CadApp.DocumentManager; foreach (var dwgFile in dwgFiles) { // Since the CommnadFlags.Session flag is set // The newly opened drawing becomes MdiActiveDocument automatically var dwg = dwgCol.Open(dwgFile, false, null); using (var lck = dwg.LockDocument()) { DoSomethingInDwg(dwg); } // Save and close dwg.Database.SaveAs(dwg.Name, true, DwgVersion.Current, null); dwg.CloseAndDiscard(); } } private static void DoSomethingInDwg(Document dwg) { //CadApp.ShowAlertDialog($"Processing drawing:\n{dwg.Name}"); //change 2 user system variables dwg.Database.Useri1 = 1; dwg.Database.Useri2 = 2; } } }
Can't find what you're looking for? Ask the community or share your knowledge.