I have drawings with layout tabs called "P1", "P2", "P3".......
I'm writing a program that will either add or delete a layout, then re-number the layouts.
I'm having trouble renaming the layouts. This is my code. When stepping through it, the variables are changing as expected, but in the end, the layout tabs (which should be called "XP1", "XP2", "XP3") have not changed at all.
Can someone show me why it's not working? Thanks.
Dim myDWG AsAutodesk.AutoCAD.ApplicationServices.Document
Dim myDB AsAutodesk.AutoCAD.DatabaseServices.Database
Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager
Dim myTrans AsTransaction
myDWG = DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction
Dim myBT AsBlockTable
Dim myBTR AsBlockTableRecord
Dim myBTE AsSymbolTableEnumerator
myBT = myDB.BlockTableId.GetObject(OpenMode.ForRead)
myBTE = myBT.GetEnumerator
While myBTE.MoveNext
myBTR = myBTE.Current.GetObject(OpenMode.ForRead)
If myBTR.IsLayout Then
If Not myBTR.Name = "*Model_Space" Then
Dim LayoutMgr As LayoutManager = LayoutManager.Current
Dim layoutObject As Layout = myTrans.GetObject(myBTR.LayoutId, OpenMode.ForRead)
' Get the layout's name
Dim layoutName As String= layoutObject.LayoutName
Dim newLayoutName AsString
newLayoutName = "X" & layoutName
LayoutMgr.RenameLayout(layoutName, newLayoutName)
myDWG.Editor.Regen()
End If
End If
End While
myTrans.Dispose()
myTransMan.Dispose()
Solved! Go to Solution.
Solved by fieldguy. Go to Solution.
You need to commit the transaction before the transaction is disposed;
....
End While
''Commit the transaction, or any change you done
''inside the transaction will be lost
myTrans.Commit()
myTrans.Dispose()
Norman Yuan
Just put the Transaction in a using block and make sure you commit it then be done with it.
When you use a 'Using' block and build it then look at it with ildasm it creates a try catch finally block under the covers and disposes it in the finally block
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
Hi,
without looking to the code you have the thread-opening + a checkmark for the solution, so:
a) the TO has presented his code
b) user fieldguy has mentioned that transaction has to be commited
...for you that means take the code plus do a transaction.commit before the transaction get's disposed.
- alfred -
Hi Alfred,
It is not working 😞 , here is the code
<CommandMethod("t13")>
PublicSubT13()
'Change the drawing Number
MsgBox("Please place all the sheet under the this path C:\x and create an empty folder under the C:\1 that drawings will be placed in")
Dim direc AsString = "C:\x"Dim filecount AsInteger = System.IO.Directory.GetFiles(direc, "*.dwg").Length
Dim arrfile() AsString = System.IO.Directory.GetFiles(direc, "*.dwg")
Dim val AsInteger = InputBox("Please Enter the value you want to add to the text")
Dim adedo AsEditor = Application.DocumentManager.MdiActiveDocument.Editor
Try
Dim i AsInteger= 0
For i = 0 To filecount - 1
Dim currentFile AsString= arrfile(i)
Dim doc AsDocument = Application.DocumentManager.Open(currentFile, False)
Dim db AsDatabase= doc.Database
Dim newfil AsString= Replace(currentFile, Mid(Right(currentFile, 7), 1, 3), Mid(Right(currentFile, 7), 1, 3) + val)
newfil = Replace(newfil,
"C:\x\", "C:\1\")
Dim doclock AsDocumentLock= doc.LockDocument
Usingdoclock
Dim trans AsTransaction= db.TransactionManager.StartTransaction()
Using(trans)
Dim ly1 AsLayoutManager = LayoutManager.Current
LayoutManager.Current.CurrentLayout = "Layout1"
trans.Commit()
EndUsing
EndUsing
doc.Database.SaveAs(newfil,
True, DwgVersion.Current, doc.Database.SecurityParameters)
doc.CloseAndDiscard()
Next
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.Message & vbLf & ex.StackTrace)
Finally
MsgBox("Done")
EndTry
EndSub
Hi,
am I right (I have not tested your code) that you run through multiple files but you don't see them get opened?
I would guess so and that is the reason why this statement does not work:
LayoutManager.Current.CurrentLayout = "Layout1"
In your case (I imagine) there is always the same drawing current, when you started you command.
Try to change the first line (command-method) to this (and let us know if this works).
<CommandMethod("t13", Autodesk.AutoCAD.Runtime.CommandFlags.Session)>
If that does not work, let us know "what does not work", exceptions or .... or ...?
- alfred -
my code open several files however i will try it, but what is the purpose of this line
it worked however it open the dialog of AutocadMapmessage, i have to close one by one how can we solve this out?
Also what does the line we added used for
Thanks,
Tamer Mourad
Hi,
>> it open the dialog of AutocadMapmessage
And the message is ...?
Map3D messages could sometime be avoided by setting CMDDIA to 0, you also could try to set EXPERT to 5, but as long as we are guessing it could be anything.
- alfred -
Thanks Alfred
it worked after setting the CMDDIA to 0
But i cant understand what does it means adding this line
Autodesk.AutoCAD.Runtime.
CommandFlags.Session
Can you please explain
Regards,
Tamer Mourad
Hi,
>> But i cant understand what does it means adding this line
>> Autodesk.AutoCAD.Runtime.CommandFlags.Session
Looking into the help file arxmgd.chm chapter "Autodesk.AutoCAD.Runtime.CommandFlags Enumeration"(part of the ObjectARX-kit >>>here<<<) I come to this description:
The command will be run in the application execution context rather than the current document context, with the different capabilities and limitations that entails. It should be used sparingly.
- alfred -