Results 1 to 10 of 10

Thread: Renaming Layoutname

  1. #1
    Member
    Join Date
    2013-02
    Posts
    31
    Login to Give a bone
    0

    Default Renaming Layoutname

    Hello everyone,

    i'm trying to create a code to rename the layoutname. Does someone have an idea how i can make this in .net-code?

    Greetings, Tori72

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Code:
            public bool RenameLayout(Document doc, string oldLayoutName, string newLayoutName)
            {
                bool ok = false;
                Database db = doc.Database;
    
                if (oldLayoutName != newLayoutName &&
                    "MODEL" != oldLayoutName.ToUpper() &&
                    "MODEL" != newLayoutName.ToUpper())
                {
                    using (Transaction tr =
                            db.TransactionManager.StartOpenCloseTransaction())
                    {
                        DBDictionary layoutDictionary =
                            (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
    
                        if (layoutDictionary.Contains(oldLayoutName) &&
                            !layoutDictionary.Contains(newLayoutName))
                            ok = true;
    
                        tr.Commit();
                    }
                }
    
                if (ok)
                {
                    LayoutManager layoutManager = LayoutManager.Current;
                    layoutManager.RenameLayout(oldLayoutName, newLayoutName);
    
                    doc.Editor.Regen();
                }
    
                return ok;
            }
    [Edit] - Added a check to disallow "Model" as either parameter.
    Last edited by BlackBox; 2014-02-20 at 09:32 PM.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Member
    Join Date
    2013-02
    Posts
    31
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Hi BlackBox,

    thanks for posting the code.
    I'm trying to understand this code but i'm new with programming in vb.net.
    But i'm breaking almost my head to understand what you're writing.

    This is the part of code i'm writing

    Code:
            Public Function RenameLayout(ByVal NieuweLayoutNaam As String)
    
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim dwg As Database = doc.Database
    
            'Vergrendelen document
            Using doc.LockDocument
    
                '' Start de transactie
                Using Transactie As Transaction = dwg.TransactionManager.StartTransaction()
                    Try
                        '' Open de blocktable om te lezen
                        Dim BlockTable As BlockTable
                        BlockTable = Transactie.GetObject(dwg.BlockTableId, OpenMode.ForRead)
    
                        '' Open de blocktablerecord paperspace om te schrijven
                        Dim BlockTableRec As BlockTableRecord
                        BlockTableRec = Transactie.GetObject(BlockTable(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
    
    
    
                      
    
    
                    
    
    
                        ''Opslaan van de wijzigingen en het verwijderen van de transactie
                            Transactie.Commit()
                    Catch
                        MsgBox("Er zit een foutje in mdl     ")
                    End Try
                End Using
            End Using
        End Function
    what the function what i'm trying to make should do is, rename the layoutname what ever name it is.
    The new layoutname will by given as byval.
    Last edited by BlackBox; 2014-02-20 at 10:08 PM. Reason: Please use [CODE] Tags

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    No worries; we all start somewhere.

    You need to account for a few things, to avoid raising an exception. The first is that the newLayoutName isn't the same as the oldLayoutName (this won't raise an exception, but would be a waste of time to evaluate), and that neither is "Model." Once you've done that you wrap a Transaction in a using statement, to access the LayoutDictionary to confirm that the oldLayoutName exists, and the newLayoutName does not. If ok at this point, you access the current LayoutManager, and rename the oldLayoutName with the newLayoutName.

    Separately, you don't technically need to add a try / catch / finally block within the using statement, as once compiled the code is added for you.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  5. #5
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Just following up... Were you able to get what you needed from the C# code above?
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  6. #6
    Member
    Join Date
    2013-02
    Posts
    31
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Hi Blackbox,

    i'm trying to understand you're code but i do not know the C#-code.

    Greetings, Tori72

  7. #7
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Quote Originally Posted by ukemi72362725 View Post
    i'm trying to understand you're code but i do not know the C#-code.
    If there's a line specifically that you're trying to understand, let me know?

    Both C# and VB.NET operate on the AutoCAD Object Model, so the syntax is essentially the same, with exception (no pun intended) to casting, permissions, etc. as a generalization.

    You might consider using a free web-based code converter.

    HTH
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  8. #8
    Member
    Join Date
    2013-02
    Posts
    31
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Hi Blackbox,

    i've made the code YEAHHHHHH!!

    This is the code which works for me.

    Code:
            Public Sub OpvragenEnWijzigingenLayoutnaam(ByVal LayoutNaamNieuw As String)
            'Deze sub vraagt en wijzigt de layoutnaam van de current layout
    
            'Reserveren geheugenplaatsen
            Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim Dwg As Database = Doc.Database
    
            'Vergrendel het document
            Using Doc.LockDocument
    
                'Start transactie
                Using Transactie As Transaction = Dwg.TransactionManager.StartTransaction()
                    Try
    
                        '' Reference the Layout Manager
                        Dim LayoutManager As LayoutManager
                        LayoutManager = LayoutManager.Current
    
                        '' Opvragen van de huidige layoutnaam
                        Dim LayoutNaam As Layout
                        Dim LayoutNaamOud As String
                        LayoutNaam = Transactie.GetObject(LayoutManager.GetLayoutId(LayoutManager.CurrentLayout), OpenMode.ForRead)
                        LayoutNaamOud = LayoutNaam.LayoutName
    
                        'Wijzig Layoutnaam
                        Dim LayoutNieuw As New Layout
                        LayoutNieuw = Transactie.GetObject(LayoutManager.GetLayoutId(LayoutManager.CurrentLayout), OpenMode.ForWrite)
                        LayoutManager.RenameLayout(LayoutNaamOud, LayoutNaamNieuw)
                        LayoutNieuw.UpgradeOpen()
    
                        'Voltooi transactie
                        Transactie.Commit()
    
                        'Opvangen en melding bij een fout
                    Catch ex As Exception
                        MsgBox("Er ging iets fout in VeranderenLayoutnaam: " & vbCrLf & ex.Message)
                    End Try
                End Using
            End Using
        End Sub
    Thanks for your help, you put me in the right direction.

    Greetings, Joop
    Last edited by BlackBox; 2014-03-03 at 08:54 PM. Reason: Please use [CODE] Tags

  9. #9
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    Quote Originally Posted by ukemi72362725 View Post
    Hi Blackbox,

    i've made the code YEAHHHHHH!!

    This is the code which works for me.

    Code:
            Public Sub OpvragenEnWijzigingenLayoutnaam(ByVal LayoutNaamNieuw As String)
            'Deze sub vraagt en wijzigt de layoutnaam van de current layout
    
            'Reserveren geheugenplaatsen
            Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim Dwg As Database = Doc.Database
    
            'Vergrendel het document
            Using Doc.LockDocument
    
                'Start transactie
                Using Transactie As Transaction = Dwg.TransactionManager.StartTransaction()
                    Try
    
                        '' Reference the Layout Manager
                        Dim LayoutManager As LayoutManager
                        LayoutManager = LayoutManager.Current
    
                        '' Opvragen van de huidige layoutnaam
                        Dim LayoutNaam As Layout
                        Dim LayoutNaamOud As String
                        LayoutNaam = Transactie.GetObject(LayoutManager.GetLayoutId(LayoutManager.CurrentLayout), OpenMode.ForRead)
                        LayoutNaamOud = LayoutNaam.LayoutName
    
                        'Wijzig Layoutnaam
                        Dim LayoutNieuw As New Layout
                        LayoutNieuw = Transactie.GetObject(LayoutManager.GetLayoutId(LayoutManager.CurrentLayout), OpenMode.ForWrite)
                        LayoutManager.RenameLayout(LayoutNaamOud, LayoutNaamNieuw)
                        LayoutNieuw.UpgradeOpen()
    
                        'Voltooi transactie
                        Transactie.Commit()
    
                        'Opvangen en melding bij een fout
                    Catch ex As Exception
                        MsgBox("Er ging iets fout in VeranderenLayoutnaam: " & vbCrLf & ex.Message)
                    End Try
                End Using
            End Using
        End Sub
    Thanks for your help, you put me in the right direction.

    Greetings, Joop
    You're very welcome; I'm happy to help.

    FWIW - I used this website to convert the C# code above into VB.NET and come up with this, which tests successfully in AutoCAD 2014:

    Code:
            Public Function RenameLayout(doc As Document, oldLayoutName As String, newLayoutName As String) As Boolean
                Dim ok As Boolean = False
                Dim db As Database = doc.Database
    
                If oldLayoutName <> newLayoutName AndAlso "MODEL" <> oldLayoutName.ToUpper() AndAlso "MODEL" <> newLayoutName.ToUpper() Then
                    Using tr As Transaction = db.TransactionManager.StartOpenCloseTransaction()
                        Dim layoutDictionary As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
    
                        If layoutDictionary.Contains(oldLayoutName) AndAlso Not layoutDictionary.Contains(newLayoutName) Then
                            ok = True
                        End If
    
                        tr.Commit()
                    End Using
                End If
    
                If ok Then
                    Dim layoutManager As LayoutManager = LayoutManager.Current
                    layoutManager.RenameLayout(oldLayoutName, newLayoutName)
    
                    doc.Editor.Regen()
                End If
    
                Return ok
            End Function


    ... Sample command (assumes that "Layout1" exists):

    Code:
            <CommandMethod("FOO")> _
            Public Sub FOO()
                RenameLayout(Application.DocumentManager.MdiActiveDocument, "Layout1", "FOO")
            End Sub
    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  10. #10
    Member
    Join Date
    2013-02
    Posts
    31
    Login to Give a bone
    0

    Default Re: Renaming Layoutname

    i used the same convert-site
    so the code also works in C#? Nice

    Greetings

Similar Threads

  1. renaming layers
    By cad2335 in forum AutoCAD General
    Replies: 1
    Last Post: 2015-03-23, 06:38 PM
  2. BLOCK RENAMING
    By Wish List System in forum AutoCAD Wish List
    Replies: 2
    Last Post: 2012-08-08, 05:54 PM
  3. Renaming Sub-Folders
    By jbayne in forum ACA General
    Replies: 1
    Last Post: 2008-03-07, 06:35 PM
  4. Renaming Layers
    By Opie in forum AutoCAD Tips & Tricks
    Replies: 13
    Last Post: 2005-12-13, 10:28 AM
  5. Renaming *U-Blocks :
    By jfoster.72059 in forum ACA General
    Replies: 7
    Last Post: 2005-10-25, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •