.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rename Layouts

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
mgorecki
1281 Views, 13 Replies

Rename Layouts

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()

13 REPLIES 13
Message 2 of 14
fieldguy
in reply to: mgorecki

Do you commit the transaction? mytrans.commit() before mytrans.dispose().

Message 3 of 14
norman.yuan
in reply to: mgorecki

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

Drive CAD With Code

EESignature

Message 4 of 14
Anonymous
in reply to: 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

 

 

Message 5 of 14
mgorecki
in reply to: fieldguy

Slapping my forehead!!!!!  Oh man, I can't believe I just did that.  Thank you all for your responses.

 

Message 6 of 14
Anonymous
in reply to: mgorecki

Hi,

 

Please can you tell me how it is solved

 

Thanks,

 

Tamer Mourad

Message 7 of 14
Alfred.NESWADBA
in reply to: Anonymous

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 14
Anonymous
in reply to: Alfred.NESWADBA

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

Message 9 of 14
Alfred.NESWADBA
in reply to: Anonymous

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 10 of 14
Anonymous
in reply to: Alfred.NESWADBA

my code open several files however i will try it, but what is the purpose of this line

Message 11 of 14
Anonymous
in reply to: Anonymous

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

Message 12 of 14
Alfred.NESWADBA
in reply to: Anonymous

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 13 of 14
Anonymous
in reply to: Anonymous

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

Message 14 of 14
Alfred.NESWADBA
in reply to: Anonymous

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost