Author Topic: Cannot switch to PaperSpace in code from behind modeless dialog  (Read 4420 times)

0 Members and 1 Guest are viewing this topic.

Patch61

  • Guest
I'm beginning to think AutoDesk does not want people to use the .Net interface using forms. It is nothing but trouble. Unfortunately, I have no choice.

I'm working on converting our custom Block Manager from VBA to .Net. The latest problem I've encountered is AutoCAD won't recognize commands to switch to paperspace (from modelspace) when called from code behind a modeless dialog. I have stepped through the running code and see nothing wrong. I try changing the Tilemode variable - no effect - AutoCAD just seems to ignore it. I tried using 'acDoc.Editor.SwitchToPaperSpace()' - AutoCAD gives me an error: eInvalidInput, as it would if I was already in PaperSpace.

If I call a 'toggle space' routine from a registered command using the command line, it functions like expected, so I know the codes are good. There must be something else involved.

Any ideas?

 
Code: [Select]
  Public Sub ChangeSpace(Optional ByVal ChangeType As String = "Toggle")
      '' Tilemode = 0 : PaperSpace tab is active
      ''     CVPort = 2 : ModelSpace is active in a viewport
      ''     CVPort <>2 : PaperSpace is active
      '' Tilemode = 1 : ModelSpace tab is active

      '' Get the current document
      Dim acDoc As Document = AcApp.DocumentManager.MdiActiveDocument
      '' Get the current values of CVPORT and TILEMODE
      Dim nCvports As Integer = AcApp.GetSystemVariable("CVPORT")
      Dim nTilemode As Integer = AcApp.GetSystemVariable("TILEMODE")
      If ChangeType = "Toggle" Then
         '' Check to see if the Model layout is active, TILEMODE is 1 when
         '' the Model layout is active
         If nTilemode = 0 Then
            '' CVPORT is 2 if Model space is active in a viewport      
            If nCvports = 2 Then
               acDoc.Editor.SwitchToPaperSpace()
            Else
               acDoc.Editor.SwitchToModelSpace()
            End If
         Else      '' Switch to the previous Paper space layout    
            'acDoc.Editor.SwitchToPaperSpace()
            AcApp.SetSystemVariable("TILEMODE", 0)
         End If
      ElseIf ChangeType = "Paper" Then
         If nTilemode = 0 Then
            If nCvports = 2 Then
               acDoc.Editor.SwitchToPaperSpace()
            End If
         Else
            AcApp.SetSystemVariable("TILEMODE", 0)
            'acDoc.Editor.SwitchToPaperSpace()
         End If
      Else   'ChangeType = "Model"
         If nTilemode = 0 Then
            If nCvports <> 2 Then
               acDoc.Editor.SwitchToModelSpace()
            End If
         Else
            AcApp.SetSystemVariable("TILEMODE", 1)
         End If
      End If
      nTilemode = AcApp.GetSystemVariable("TILEMODE")
   End Sub

This sample code from AutoDesk works from the command line:

Code: [Select]
     
<CommandMethod("Toggle")> _
      Public Sub ToggleSpace()
         '' Get the current document
         Dim acDoc As Document = acApp.DocumentManager.MdiActiveDocument

         '' Get the current values of CVPORT and TILEMODE
         Dim nCvports As Integer = acApp.GetSystemVariable("CVPORT")
         Dim nTilemode As Integer = acApp.GetSystemVariable("TILEMODE")

         '' Check to see if the Model layout is active, TILEMODE is 1 when
         '' the Model layout is active
         If nTilemode = 0 Then
            '' Check to see if Model space is active in a viewport,
            '' CVPORT is 2 if Model space is active
            If nCvports = 2 Then
               acDoc.Editor.SwitchToPaperSpace()
            Else
               acDoc.Editor.SwitchToModelSpace()
            End If
         Else
            '' Switch to the previous Paper space layout
            acApp.SetSystemVariable("TILEMODE", 0)
         End If
      End Sub
« Last Edit: March 23, 2011, 09:07:47 AM by Patch61 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Cannot switch to PaperSpace in code from behind modeless dialog
« Reply #1 on: March 23, 2011, 09:30:40 AM »

To give someone a chance to play with this : how about posting a full code solution including the form
.... trim it down if necessary.

I think it's a bit much to expect someone to re-do the work you've already done.
 ... and besides, Murphy's law says that the problem will become clearer when we see the code NOT posted.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Cannot switch to PaperSpace in code from behind modeless dialog
« Reply #2 on: March 23, 2011, 09:51:20 AM »
Hi,

From a modeless dialog, you have to lock the document before editing it

Code: [Select]
using System;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace ModelessDialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdPS_Click(object sender, EventArgs e)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            short tilemode = (short)AcAp.GetSystemVariable("TILEMODE");
            using (DocumentLock docLock = doc.LockDocument())
            {
                if (tilemode == 1)
                    AcAp.SetSystemVariable("TILEMODE", 0);
                else if ((short)AcAp.GetSystemVariable("CVPORT") != 1)
                    ed.SwitchToPaperSpace();
                else
                {
                    try { ed.SwitchToModelSpace(); }
                    catch { AcAp.SetSystemVariable("TILEMODE", 1); }
                }
            }
        }

        [CommandMethod("test")]
        public void test()
        {
            AcAp.ShowModelessDialog(this);
        }
    }
}
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Cannot switch to PaperSpace in code from behind modeless dialog
« Reply #3 on: March 23, 2011, 10:42:56 AM »

Yes gile, that's what I thought too ...
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Patch61

  • Guest
Re: Cannot switch to PaperSpace in code from behind modeless dialog
« Reply #4 on: March 23, 2011, 10:44:41 AM »
Gile:

Thanks! Locking the document did the trick. I didn't think changing spaces was 'editing' the drawing (doing it manually doesn't trigger the drawing as 'changed' in AutoCAD), so that is why I didn't think to try that to begin with. (Although this strikes me as strange that the command method works without the locking)

Kerry:

I understand what you are all about with your posts, but I was hoping somebody would recognize what I was doing wrong without me spending an hour or two putting together a short 'sample app'. In this case I was right. But I also see how your suggestions can be helpful in solving more stubborn problems. Sometimes a small problem is just a small problem.  :wink:
« Last Edit: March 23, 2011, 02:25:03 PM by Patch61 »