Toggle LWDISPLAY system variable

Toggle LWDISPLAY system variable

SRSDS
Advisor Advisor
2,680 Views
9 Replies
Message 1 of 10

Toggle LWDISPLAY system variable

SRSDS
Advisor
Advisor

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. 

 

0 Likes
Accepted solutions (1)
2,681 Views
9 Replies
Replies (9)
Message 2 of 10

Alfred.NESWADBA
Consultant
Consultant

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
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 10

SRSDS
Advisor
Advisor

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.

 

 

0 Likes
Message 4 of 10

Alfred.NESWADBA
Consultant
Consultant

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
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 10

SRSDS
Advisor
Advisor

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".

0 Likes
Message 6 of 10

Alfred.NESWADBA
Consultant
Consultant

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
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 10

Hallex
Advisor
Advisor

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
0 Likes
Message 8 of 10

SRSDS
Advisor
Advisor

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. 

0 Likes
Message 9 of 10

gopinath.taget
Alumni
Alumni
Accepted solution

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>

Message 10 of 10

SRSDS
Advisor
Advisor

That was the solution.

My thanks to you and also Alfred and & Hallex.

Sorry for the trouble.