Author Topic: Renaming Xref changing the layer display  (Read 3714 times)

0 Members and 1 Guest are viewing this topic.

Ajilal

  • Mosquito
  • Posts: 12
Renaming Xref changing the layer display
« on: January 02, 2013, 05:35:51 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.


Code: [Select]
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

TheMaster

  • Guest
Re: Renaming Xref changing the layer display
« Reply #1 on: January 03, 2013, 01:53:39 AM »
Not sure why you're reloading xrefs.

The name of the xref is the name of the block that represents it in the current drawing, so nothing has actually changed that would require the xref to be reloaded. The RENAME command renames xref-dependent symbols if you rename the xref block, so that the symbols always have the name of the xref block as the prefix before the vertical bar.

I'm not sure if renaming the block via the API does that as well, but my guess is that it does. I'm also not sure it's wise to reload xrefs within a transaction at all, if not within one in which you modify xref blocks.  You might try committing the transaction before trying to reload xrefs, if you actually need to reload them.

Ajilal

  • Mosquito
  • Posts: 12
Re: Renaming Xref changing the layer display
« Reply #2 on: January 03, 2013, 02:15:13 AM »
Hi,
Thanks for the reply.

Quote
Not sure why you're reloading xrefs.
If I am not reloading the xref via API , next time when I open the drawing the xref's will reload and the same thing is happening.
Also same thing if I am trying to reload the xref's via Xref Manager.

Quote
You might try committing the transaction before trying to reload xrefs, if you actually need to reload them.
I tried to reload the xref , after committing the transaction.
Still same result.

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Renaming Xref changing the layer display
« Reply #3 on: January 03, 2013, 03:15:55 AM »
I'm not a code type, however Autocad is going to reset the layers to their original state
unless you somehow also rename the layers to match the new XREF name....before you reload the xrefs
Be your Best


Michael Farrell
http://primeservicesglobal.com/