Hi,
I am trying to rename all the xref's on the drawing with suffix "_renamed"
With the code shown below , I am able to do this.
But the problem I am facing is once the code renamed and reloaded the xref, the Xref-dependent layer properties are changing to Xref source layer properties.
Also the VISRETAIN variable is 1.
Please find the code below.
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Namespace ATK_XRF_RENAME
Public Class ATK_XRF_RENAME
<CommandMethod("ATK-XRF-RENAME-TEST")> _
Public Sub ATK_XRF_RENAME()
Dim DOC As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ED As Editor = DOC.Editor
Dim DB As Database = DOC.Database
Dim TM As Autodesk.AutoCAD.DatabaseServices.TransactionManager = DB.TransactionManager
Dim Xref_Rslvd_name_frm_Path As List(Of String) = New List(Of String)
Dim Xref_Rslvd_id_col As ObjectIdCollection = New ObjectIdCollection
Dim Xref_Not_Found_Name As List(Of String) = New List(Of String)
Dim Xref_Not_Found_id_col As ObjectIdCollection = New ObjectIdCollection
'Get the Xref Graph for the Current Drawing.
Dim Xref_Graph As XrefGraph = DB.GetHostDwgXrefGraph(True)
'If the Xref Graph contains one node then No Xref is attached and EXIT the program.
If Xref_Graph.NumNodes = 1 Then
ED.WriteMessage(vbCrLf + "Xref not found on this drawing")
Return
End If
'Get the Current Drawing name to check and avoid finding the same reference.
Dim Crnt_Dwg_Name As String = System.Convert.ToString(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGNAME"))
Crnt_Dwg_Name = Crnt_Dwg_Name.Substring(0, Crnt_Dwg_Name.Length - 4)
'Loop through each xref node to get the actual xref drawing name , not the screen name
For X = 0 To Xref_Graph.NumNodes - 1
Dim XNODE As XrefGraphNode = Xref_Graph.Node(X)
If XNODE.Name.ToLower <> Crnt_Dwg_Name.ToLower Then 'If not the current drawing itself.
If XNODE.IsNested = False Then 'If the xref is not Nested.
If XNODE.XrefStatus = XrefStatus.Resolved Then
Dim File_name_frm_Path As String = System.IO.Path.GetFileName(XNODE.Database.OriginalFileName)
File_name_frm_Path = File_name_frm_Path.Substring(0, File_name_frm_Path.Length - 4)
Xref_Rslvd_name_frm_Path.Add(File_name_frm_Path)
Xref_Rslvd_id_col.Add(XNODE.BlockTableRecordId)
Else
'If XNODE.XrefStatus = XrefStatus.FileNotFound Then
'Xref_Not_Found_Name.Add(XNODE.Name)
'Xref_Not_Found_id_col.Add(XNODE.BlockTableRecordId)
End If
End If
End If
Next
If Not Xref_Rslvd_id_col.Count = 0 Then
Using TR As Transaction = TM.StartTransaction
For Each ID As ObjectId In Xref_Rslvd_id_col
Dim obj As DBObject = TR.GetObject(ID, OpenMode.ForWrite)
Dim BLK As BlockTableRecord = DirectCast(obj, BlockTableRecord)
BLK.Name = BLK.Name + "_renamed"
Next
DB.ReloadXrefs(Xref_Rslvd_id_col)
TR.Commit()
End Using
End If
End Sub
End Class
End Namespace