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
|
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
[Edit] - Added a check to disallow "Model" as either parameter.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; }
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
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
what the function what i'm trying to make should do is, rename the layoutname what ever name it is.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
The new layoutname will by given as byval.
Last edited by BlackBox; 2014-02-20 at 10:08 PM. Reason: Please use [CODE] Tags
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
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
Hi Blackbox,
i'm trying to understand you're code but i do not know the C#-code.
Greetings, Tori72
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
Hi Blackbox,
i've made the code YEAHHHHHH!!
This is the code which works for me.
Thanks for your help, you put me in the right direction.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
Greetings, Joop
Last edited by BlackBox; 2014-03-03 at 08:54 PM. Reason: Please use [CODE] Tags
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):
CheersCode:<CommandMethod("FOO")> _ Public Sub FOO() RenameLayout(Application.DocumentManager.MdiActiveDocument, "Layout1", "FOO") End Sub
"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
i used the same convert-site
so the code also works in C#? Nice
Greetings