Author Topic: Problem setting TabOrder on Layouts  (Read 278 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16603
  • Superior Stupidity at its best
Problem setting TabOrder on Layouts
« on: May 26, 2017, 12:11:50 am »
Maybe I am not understanding what needs to be done to re-order layouts, but I'm pretty certain that what I am doing isn't working ....
Code - C#: [Select]
  1. public void moveLayout(string name, bool up)
  2. {
  3.    Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
  4.    Database acDB = acDoc.Database;
  5.    LayoutManager acLayoutMgr = LayoutManager.Current;
  6.    using (acDoc.LockDocument())
  7.    {
  8.        using (Transaction acTrans = acDB.TransactionManager.StartTransaction())
  9.        {
  10.            if (up)
  11.                ((Layout)acTrans.GetObject(acLayoutMgr.GetLayoutId(name), OpenMode.ForWrite)).TabOrder -= 1;
  12.            else
  13.                ((Layout)acTrans.GetObject(acLayoutMgr.GetLayoutId(name), OpenMode.ForWrite)).TabOrder += 1;
  14.            acTrans.Commit();
  15.            acDoc.Editor.Regen();
  16.        }
  17.    }
  18. }

I'm sure its something basic that I'm missing ... I seem to be having a forest/trees moment :-/
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 890
  • class keyThumper<T>:ILazy<T>
Re: Problem setting TabOrder on Layouts
« Reply #1 on: May 26, 2017, 01:15:34 am »

I can't make time to play, but
Have you seen Normans blog post
http://drive-cad-with-code.blogspot.com.au/2011/06/set-layout-in-order.html
called Kerry in my other life

Sometimes the question is more important than the answer.

Bryco

  • Water Moccasin
  • Posts: 1791
Re: Problem setting TabOrder on Layouts
« Reply #2 on: May 26, 2017, 11:21:42 am »
Do you have to make it current before changing the taborder, manually you have to.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16603
  • Superior Stupidity at its best
Re: Problem setting TabOrder on Layouts
« Reply #3 on: May 30, 2017, 11:30:32 pm »

I can't make time to play, but
Have you seen Normans blog post
http://drive-cad-with-code.blogspot.com.au/2011/06/set-layout-in-order.html

I've seen that post. I wouldn't be averse to doing what it suggests if I could be assured it works as advertised. Right now, I am wanting to reorder a single tab up or down in the taborder.
I do have an idea that I will try and see of that resolves the issue.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 890
  • class keyThumper<T>:ILazy<T>
Re: Problem setting TabOrder on Layouts
« Reply #4 on: May 31, 2017, 01:52:25 am »

What happens if you attempt setting 3 consecutive tabs as Taborder 2 ... that may give you an indicator why the code you are using is failing ...

I started to think about your issue and real life caught up with me, so ....

called Kerry in my other life

Sometimes the question is more important than the answer.

n.yuan

  • Bull Frog
  • Posts: 223
Re: Problem setting TabOrder on Layouts
« Reply #5 on: May 31, 2017, 11:18:43 am »

I can't make time to play, but
Have you seen Normans blog post
http://drive-cad-with-code.blogspot.com.au/2011/06/set-layout-in-order.html

I've seen that post. I wouldn't be averse to doing what it suggests if I could be assured it works as advertised. Right now, I am wanting to reorder a single tab up or down in the taborder.
I do have an idea that I will try and see of that resolves the issue.

Now that one of my old posts has been mentioned, let me chip in:

I think the reason of your code not working is because you simply try to change TabOrder of ONE layout, but did not try to change AFFECTED layout(s) (the ones before and/or after the one you change), which would result in duplicated TabOrder, thus exception when the Transaction is to commit. You see, there is always MODEL layout, which always has TabOrder=0. So, you cannot move layout with TabOrder=1 down. If the layout is has the max TabOrder, you cannot move it up. Therefore, when you say "re-order" layout tabs, it is actually SWAP TabOrder of 2 layouts: targeted one and the one before it (as long as it is not "MODEL"), or the one after it (as long as it is not the last one).

Once you understand this, things are really simple. Here is the code that works for me:

Code: [Select]
public static void SwapLayoutTabOrder(ObjectId lay1Id, ObjectId lay2Id)
        {
            using (var tran = lay1Id.Database.TransactionManager.StartTransaction())
            {
                var layout1 = (Layout)tran.GetObject(lay1Id, OpenMode.ForWrite);
                var layout2 = (Layout)tran.GetObject(lay2Id, OpenMode.ForWrite);

                var order1 = layout1.TabOrder;
                var order2 = layout2.TabOrder;

                layout1.TabOrder = order2;
                layout2.TabOrder = order1;

                tran.Commit();
            }
        }

The 2 arguments of the method are 2 layout's ObjectId, as long as none of them being "Model" layout.

I Just did a full demo-able project with UI to verify the code works as I expected. For the benefit of other possible readers to see the the code of entire project, I'll publish it in my blog later today, and post link here

n.yuan

  • Bull Frog
  • Posts: 223
Re: Problem setting TabOrder on Layouts
« Reply #6 on: May 31, 2017, 12:13:27 pm »

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 890
  • class keyThumper<T>:ILazy<T>
Re: Problem setting TabOrder on Layouts
« Reply #7 on: May 31, 2017, 08:04:13 pm »

Thanks Norman.

Very professional, as usual.

Regards,


called Kerry in my other life

Sometimes the question is more important than the answer.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16603
  • Superior Stupidity at its best
Re: Problem setting TabOrder on Layouts
« Reply #8 on: May 31, 2017, 11:44:41 pm »

I think the reason of your code not working is because you simply try to change TabOrder of ONE layout, but did not try to change AFFECTED layout(s) (the ones before and/or after the one you change), which would result in duplicated TabOrder, thus exception when the Transaction is to commit.

That's the issue that I had come to the conclusion was happening. I'll review your code and see how I can incorporate a similar approach in my project. I sincerely appreciate the assistance.

Incidentally, I was not getting any kind of error, it just did not work.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16603
  • Superior Stupidity at its best
Re: Problem setting TabOrder on Layouts
« Reply #9 on: June 05, 2017, 06:42:38 pm »
Setting TabOrder on all tabs affected resolved the issue.
Thanks for the help .. its amazing how a small thing can cause such a problem!

Now its off to figure out another thing that I have been dreading ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal