Changing Current Layout

Changing Current Layout

GeeHaa
Collaborator Collaborator
6,760 Views
14 Replies
Message 1 of 15

Changing Current Layout

GeeHaa
Collaborator
Collaborator
Hi,

Can anyone tell me why this code doesn't Change the current layout.

Dim acLayoutMgr As LayoutManager
acLayoutMgr = LayoutManager.Current
acLayoutMgr.CurrentLayout = "Model"

Thanks in Advance
0 Likes
6,761 Views
14 Replies
Replies (14)
Message 2 of 15

chiefbraincloud
Collaborator
Collaborator
The code I have is intended to activate a newly created layout tab, and ran in a command that has to be run from the Model tab, so I had to test it to see if it would activate the model tab and it worked fine. So the problem is not the code you posted.

Perhaps it has to do with where the code is running from (ie from code in a form class), but you'll have to provide more info for anyone to help.

If the code is as it is posted (always activates the Model tab, never any other tab) then you could try setting the system variable TILEMODE to 1 and see if that works.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 15

arcticad
Advisor
Advisor
try adding the session flag

Autodesk.AutoCAD.Runtime.CommandMethod("CommandName", Autodesk.AutoCAD.Runtime.CommandFlags.Session)
---------------------------



(defun botsbuildbots() (botsbuildbots))
0 Likes
Message 4 of 15

GeeHaa
Collaborator
Collaborator
Thanks for the response. I have command flags.session in there but for some reason it still doesn't change layouts. I was running it from a form so I tried putting it in the command routine,But it still doesn't change. It doesn't give an error either. Aside from the imports here is the code leading up to the change.

CommandMethod("PCL", CommandFlags.Session)
Public Sub PCL()

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

Dim acLayoutMgr As LayoutManager
acLayoutMgr = LayoutManager.Current
acLayoutMgr.CurrentLayout = "Model"
MsgBox("Current Layout = " + acLayoutMgr.CurrentLayout)

current layout is still "Layout1"

Thanks again.
0 Likes
Message 5 of 15

Hallex
Advisor
Advisor
Use Try..Catch to find a problem easily, i.e.
{code}
Public Sub PCL()
Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Using doclock As DocumentLock = acDoc.LockDocument
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Try
Dim acLayoutMgr As LayoutManager
acLayoutMgr = LayoutManager.Current
acLayoutMgr.CurrentLayout = "Model"

MsgBox("Current Layout = " + acLayoutMgr.CurrentLayout)
acTrans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message & vbCr & ex.StackTrace)
End Try

End Using
End Using
End Sub
{code}

~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 6 of 15

Anonymous
Not applicable
I found it rolled back if you don't commit the transaction

If you leave out the commit, the layout changes, displays the layout name, and then checking okay rolls back
0 Likes
Message 7 of 15

GeeHaa
Collaborator
Collaborator
Locking the document did the trick. It changes the layout immediately even before the commit. I'm curious though, does Using Doclock lock the document just for this transaction and release it with the end Using statement?
0 Likes
Message 8 of 15

chiefbraincloud
Collaborator
Collaborator
Yes. the DocumentLock is disposed at the End Using.

You actually don't need a transaction to do this at all. You need the DocumentLock because you are running it from a form (which is why I mentioned form code earlier), but my code works fine without either a transaction or a DocumentLock, running from a CommandMethod in a Module.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 9 of 15

Anonymous
Not applicable

Hi,

 

I still cannot understand the solution, i tried in many ways but it couldnt work. Can you please clarify much more

 

Thanks,

 

Tamer Mourad

0 Likes
Message 10 of 15

fieldguy
Advisor
Advisor

Show us what you have so far.

0 Likes
Message 11 of 15

Anonymous
Not applicable

Here it is

 

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

EndUsingEndUsing

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

0 Likes
Message 12 of 15

fieldguy
Advisor
Advisor

I assume you have this working from the posts here and in the other thread. Unless there is more to this, you don't need the database or a transaction.  You also don't need an editor that I can see.  This will do nothing if layout1 does not exist.

 

Using doclock

 LayoutManager.Current.CurrentLayout = "Layout1"

End Using

 

If you do need the editor, declare it after you open the currentfile, like "dim adedo as editor = doc.editor".

 

Command flags session is described in the developers guide - a good page to bookmark.  (http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a919382...)

 

If you have to do more batch file processing do a google search for the openfiledialog class. 

0 Likes
Message 13 of 15

Anonymous
Not applicable

Hi,

 

I do need the editor and transaction for the rest of the program, however when i added this line next to the command method it worked

 

Autodesk.AutoCAD.Runtime.

CommandFlags.Session

 

Do you have any idea what does this line do?

By the way the hyperlink you attached is linking to something out of what i am asking for. R u sure from this bookmark

 

Thanks,

 

Tamer Mourad

0 Likes
Message 14 of 15

fieldguy
Advisor
Advisor

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

 

There is lots of help available on the internet.  The main idea is to search first.

0 Likes
Message 15 of 15

Anonymous
Not applicable

Thanks

 

 

0 Likes