.NET

Reply
Mentor
SRSDS
Posts: 247
Registered: ‎04-15-2011
Message 1 of 10 (412 Views)
Accepted Solution

Toggle LWDISPLAY system variable

412 Views, 9 Replies
10-31-2011 07:40 PM

Hi,

Can someone suggest why I would get an eInvalidInput error when toggling  a system variable.

I've tried a few things which is why it's 4 lines instead of 1.

 

        Dim lwdisplay As Object = Application.GetSystemVariable("LWDISPLAY")
        If CShort(lwdisplay) = 0 Then
            Application.SetSystemVariable("LWDISPLAY", CShort(1))
        End If

  It generally works but every so often produces an error and I can't figure out why. 

 

Hi,

 

If it works in the Iniitialize component and not in a palette, it usually means your having a problem with the contexts (application/document contexts). Can you please try locking the active document in the palette code before you try to change the variable?

 

<code>

using(DocumentLock lk = Application.DocumentManager.MdiActiveDocument.LockDocument())

{

    // Do your thing

}

</code>

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 2 of 10 (398 Views)

Re: Toggle LWDISPLAY system variable

10-31-2011 11:40 PM in reply to: SRSDS

Hi,

 

at least the question is the line ... in which line does your code throw an exception?

 

And for doing that in one line ... use this (but it's not faster and it's not easier to read):

   <Runtime.CommandMethod("ADESK_ToggleLWDisp")> _
   Public Shared Sub ADESK_ToggleLWShow()
      Application.SetSystemVariable("LWDISPLAY", Math.Abs(CType(Application.GetSystemVariable("LWDISPLAY"), Integer) - 1))
   End Sub

 this command I have tried (many times) with 2010/32bit, 2010/64bit, 2012/64bit and I could not reproduce a crash. The difference might be that I run that from a command?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
SRSDS
Posts: 247
Registered: ‎04-15-2011
Message 3 of 10 (342 Views)

Re: Toggle LWDISPLAY system variable

12-08-2011 12:37 PM in reply to: Alfred.NESWADBA

Hi Alfred,

 

I must have forgotten to look into this but have rediscoverd the problem.

 

Even using 

 

        Dim lwdisplay As Object = Application.GetSystemVariable("LWDISPLAY")
        If CShort(lwdisplay) = 0 Then
            Application.SetSystemVariable("LWDISPLAY", Math.Abs(CType(Application.GetSystemVariable("LWDISPLAY"), Integer) - 1))
        End If

 I still get an eInvalidInput error.

 

Seems like a basic toggle. 2011/32bit here.

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 4 of 10 (340 Views)

Re: Toggle LWDISPLAY system variable

12-08-2011 12:42 PM in reply to: SRSDS

Hi,

 

.... your code is not the same I wrote, your code just turns ON LWDISPLAY but never OFF.

 

What line does crash? What message do you get as Exception.Message? Have you tried my command exactly in the way I prepared it?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
SRSDS
Posts: 247
Registered: ‎04-15-2011
Message 5 of 10 (336 Views)

Re: Toggle LWDISPLAY system variable

12-08-2011 01:21 PM in reply to: Alfred.NESWADBA

It should do the same I would have thought. If 0 the abs(-1) should turn it on.

 

 

            Try
                Application.SetSystemVariable("LWDISPLAY", CInt(1))
            Catch
                Try
                    Application.SetSystemVariable("LWDISPLAY", CShort(1))
                Catch
                    Try
                        Application.SetSystemVariable("LWDISPLAY", CBool(1))
                    Catch
                        MsgBox("?")
                    End Try
                End Try
            End Try

 I get either eInvalidInput or "Value does not fall within the expected range".

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 6 of 10 (334 Views)

Re: Toggle LWDISPLAY system variable

12-08-2011 01:50 PM in reply to: SRSDS

Hi,

 

>> It should do the same I would have thought. If 0 the abs(-1) should turn it on

My code did toogle LWDISPLAY, if it's 0 then I turn it to 1, if it's one I change it to 0.

Your previous code had an if LWDISPLAY = 0 then included, so if LWDISPLAY was 1 there was never changed back to 0.

 

However, that's not the problem at the moment. Your code never works? Let me know what AutoCAD you exactly have now:

  • AutoCAD .... or vertical Product (like Mech, Map3D, Civil3D, ...)
  • what release?
  • 32bit/64bit?
  • what servicepack is installed
  • what version of VisualStudio do you have?
  • what Framework does your App use?

...maybe we get any info that gives us a chance to work on/to check.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 10 (320 Views)

Re: Toggle LWDISPLAY system variable

12-09-2011 05:14 AM in reply to: SRSDS

How about avoid using VBA-like types

        Public Sub togglelwdisplay()


            Dim lwdisplay As Short = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("LWDISPLAY")


            If lwdisplay = 0 Then


                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("LWDISPLAY", 1)


            Else


                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("LWDISPLAY", 0)


            End If


        End Sub

 

 VBA-like types:

 

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
SRSDS
Posts: 247
Registered: ‎04-15-2011
Message 8 of 10 (310 Views)

Re: Toggle LWDISPLAY system variable

12-09-2011 06:55 AM in reply to: Hallex

I think it's got something to do with using code in a palette.

It works fine when I move it to  the initialize component.

 

Attached is a VERY simple example of what I'm experiencing if someone's got time to look. 

ADN Support Specialist
gopinath.taget
Posts: 123
Registered: ‎08-15-2007
Message 9 of 10 (301 Views)

Re: Toggle LWDISPLAY system variable

12-09-2011 10:28 AM in reply to: SRSDS

Hi,

 

If it works in the Iniitialize component and not in a palette, it usually means your having a problem with the contexts (application/document contexts). Can you please try locking the active document in the palette code before you try to change the variable?

 

<code>

using(DocumentLock lk = Application.DocumentManager.MdiActiveDocument.LockDocument())

{

    // Do your thing

}

</code>

Mentor
SRSDS
Posts: 247
Registered: ‎04-15-2011
Message 10 of 10 (295 Views)

Re: Toggle LWDISPLAY system variable

12-09-2011 11:33 AM in reply to: SRSDS

That was the solution.

My thanks to you and also Alfred and & Hallex.

Sorry for the trouble.

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.