Absolute Path to Relative Path

Absolute Path to Relative Path

David_Prontnicki
Collaborator Collaborator
483 Views
1 Reply
Message 1 of 2

Absolute Path to Relative Path

David_Prontnicki
Collaborator
Collaborator

I found an old post on devBlog about changing absolute paths to relative paths for XREFs. Seems straight forward but I keep getting a eFileAccessEr during the Reload Xref's portion. I will eventually turn this into a batch processor to run on all drawing in a folder but for now I'm just trying to get the core of it done.

 

As you will see I take the full xref drawing name and path from the ReadDwgFile drawing file. For Example: R:\Projects\2018\18001808A\Survey\dwg\ASBLT\18001808A-BaseFile.dwg (This is the xref in the ReadDwgFile)

 

Then in this example I want it to set the relative path to .\Survey\dwg\ASBLT\18001808A-BaseFile.dwg

So it will start at that top folder and look down for the xref file. 

 

The program sets the new paths during debugging but when it gets to reloading them, that's where the error appears. 

 

 

<Autodesk.AutoCAD.Runtime.CommandMethod("ChangeXRefPath")>
    Public Sub ChangeXRefPathMethod()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim collection As ObjectIdCollection = New ObjectIdCollection()

        Using db As Database = New Database(False, True)

            db.ReadDwgFile("R:\Projects\2018\18001808A\Survey\dwg\ASBLT\18001808A-FENCE As Built.dwg", FileOpenMode.OpenForReadAndWriteNoShare, False, "")

            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 Then

                        btr.UpgradeOpen()
                        Dim oldPath As String = btr.PathName

                        Dim oldPathArray As String() = oldPath.Split("\"c)
                        Dim oldPathRoot As String = oldPathArray(0) & "\" & oldPathArray(1) & "\" & oldPathArray(2) & "\" & oldPathArray(3)

                        Dim newPath As String = oldPath.Replace(oldPathRoot, ".")
                        btr.PathName = newPath
                        collection.Add(btrId)
                        ed.WriteMessage(String.Format("{0}Old Path : {1} New Path : {2}", Environment.NewLine, oldPath, newPath))

                    End If

                Next

                tr.Commit()

            End Using

            If collection.Count > 0 Then

                db.ReloadXrefs(collection) '<--- errors here

            End If

            db.SaveAs(db.OriginalFileName, True, db.OriginalFileVersion, db.SecurityParameters)

        End Using

    End Sub

 

 

484 Views
1 Reply
Reply (1)
Message 2 of 2

Alexander.Rivilis
Mentor
Mentor

@David_Prontnicki 

Relative path for "R:\Projects\2018\18001808A\Survey\dwg\ASBLT\18001808A-FENCE As Built.dwg" will be "R:\Projects\2018\18001808A\Survey\dwg\ASBLT\Survey\dwg\ASBLT\18001808A-BaseFile.dwg"

Are you sure that you have file with such path?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes