.NET

Reply
Valued Mentor
Ajilal.Vijayan
Posts: 439
Registered: ‎01-12-2008
Message 1 of 5 (379 Views)

Renaming Xref changing the layer display

379 Views, 4 Replies
01-02-2013 03:07 AM

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

 

*Expert Elite*
pendean
Posts: 22,446
Registered: ‎11-06-2003
Message 2 of 5 (344 Views)

Re: Renaming Xref changing the layer display

01-02-2013 05:35 AM in reply to: Ajilal.Vijayan

>>>...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...<<<

 

That's how AutoCAD works: a core function/feature: you probably did not notice, but Xref-Dependent block names changed too.

 

May I ask what is the exact purpose of tagging an XREF as "renamed"? How is that critical to your workflow? Perhaps other ways are available.

Dean Saadallah Blog | Facebook | Google+ | Twitter | PINS
Valued Mentor
Ajilal.Vijayan
Posts: 439
Registered: ‎01-12-2008
Message 3 of 5 (338 Views)

Re: Renaming Xref changing the layer display

01-02-2013 05:53 AM in reply to: pendean

Hi,

Thanks for the reply.

 

I have a spread sheet with two columns named Old Xref name and New Xref name.

My program looking for a match with the Old Xref name column, and if its find one , will rename to the new name corresponding to the New Xref name found in the spreadsheet.

 

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 4 of 5 (303 Views)

Re: Renaming Xref changing the layer display

01-02-2013 11:55 PM in reply to: Ajilal.Vijayan

See my reply on theswamp.org

Valued Mentor
Ajilal.Vijayan
Posts: 439
Registered: ‎01-12-2008
Message 5 of 5 (299 Views)

Re: Renaming Xref changing the layer display

01-03-2013 12:08 AM in reply to: Ajilal.Vijayan

Hi,

 

I tried as per the reply , but no luck.

Please see my reply in Swamp.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.