.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Finding out when a custom PaletteSet is closed in AutoCAD using .NET code crash

10 REPLIES 10
Reply
Message 1 of 11
yaqiz
1552 Views, 10 Replies

Finding out when a custom PaletteSet is closed in AutoCAD using .NET code crash

Hi Guys,

 

I would like to do something when my paletteset is closed. I took the C# code from here

http://through-the-interface.typepad.com/through_the_interface/2011/12/finding-out-when-a-custom-pal...

 

I modified the code to VB.net

    Dim WithEvents ps As Autodesk.AutoCAD.Windows.PaletteSet

    <Autodesk.AutoCAD.Runtime.CommandMethod("psTest")> _
    Public Sub PSTest()
        If ps Is Nothing Then
            ps = New Autodesk.AutoCAD.Windows.PaletteSet("psTest")
            ps.Dock = Autodesk.AutoCAD.Windows.DockSides.Left
            ps.Visible = True
        End If
    End Sub

    Private Sub ps_StateChanged(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs) Handles ps.StateChanged
        If e.NewState = Autodesk.AutoCAD.Windows.StateEventIndex.Hide Then
            Application.DocumentManager.MdiActiveDocument.SendStringToExecute("CHECKPALETTESETCLOSE", True, True, False)
        End If
    End Sub

    <Autodesk.AutoCAD.Runtime.CommandMethod("CHECKPALETTESETCLOSE", CommandFlags.NoHistory)> _
    Public Sub CheckPaletteSetState()
        If Not ps Is Nothing And ps.Visible = False Then
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("closed")


        End If
    End Sub

My code crashed. If I catch exception it is eInvalidInput exception. I don't know what to do as it is a simple code segment I don't know hot to fix it.

 

Any ideas?

 

Thanks

10 REPLIES 10
Message 2 of 11
Alfred.NESWADBA
in reply to: yaqiz

Hi,

 

first guess: don't check only for "Is Nothing" but also for "ps.IsDisposed"

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 11
yaqiz
in reply to: Alfred.NESWADBA

Thanks for reply

 

the problem comes from here

    Private Sub ps_StateChanged(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs) Handles ps.StateChanged
        If e.NewState = Autodesk.AutoCAD.Windows.StateEventIndex.Hide Then
            Application.DocumentManager.MdiActiveDocument.Send​StringToExecute("CHECKPALETTESETCLOSE", True, True, False) '--here is the exception
        End If
    End Sub

 

Message 4 of 11
Alfred.NESWADBA
in reply to: yaqiz

Hi,

 

are you sure that you can run SendStringToExecute during this eventhandling? I fear NO ... to verify that: what happens if you send a _LINE-command?

PLUS: as it's a command defined by your app, why do you run this code with SendStringToExecute?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 11
yaqiz
in reply to: Alfred.NESWADBA

Thanks for reply. I don't know I took code from the post I found online(you can see in my main post). What I have done modify from c# to vb.net

 

I got reply from AutoCAD API support. They can replicate my issue. What I really want to do is to catch when user click paletteset "x" button rather than "-".

 

I don't know what to do

Message 6 of 11
Alfred.NESWADBA
in reply to: yaqiz

Hi,

 

in most cases when using events where I need to run SendCommand (or similar) I usually redirect the event to a position when AutoCAD get's quiescent.

So in your eventhandler create a new one using AddHandler that points to a sub handling then the reached quiesent-state for AutoCAD.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 11

From the next TTIF post after the one in the link above.


Something I should have mentioned about the reason for implementing a command, in the first place: the standard StateChanged event gets fired twice when undocking a palette set – once to hide it and once to display it again. This means we can’t assume a hide notification means the palette set is being closed – we really need to wait until the dust has settled before checking the state of the palette set in order to proclaim our verdict. Which is why we’re sending a command to be executed

 

Dave O.                                                                  Sig-Logos32.png
Message 8 of 11
yaqiz
in reply to: chiefbraincloud

Thanks for your reply.

 

Have you try to implement that code segement? Does that work as it should be?

Message 9 of 11
chiefbraincloud
in reply to: yaqiz

Sorry, I don't really like palettes, and they don't really fit in with the kind of automations I am writing.  So, no, I have not tried the code to verify it's operation.  I just replied to try and answer Alfred's question about why the code uses a SendStringToExecute.

Dave O.                                                                  Sig-Logos32.png
Message 10 of 11
DiningPhilosopher
in reply to: yaqiz

Message 11 of 11
DiningPhilosopher
in reply to: yaqiz

The solution Kean posted on his blog has a few problems.

 

One is that he assumes that there is an active document to send a command to, which may not be the case since the PaletteSet could be visible in 'zero-document state' (e.g., there are no documents open). In that case, kaboom.

 

Another problem, which affects most kludges that involve sending strings to the command line, is that the command that closes the paletteset could be scripted by LISP, and what follows that could be a prompt from LISP requesting some input. Unfortunately, Kean's solution will send its command string to the command line while the LISP script is waiting for the user to type a response, and the command string ends up satisfying the input request.

 

You can avoid all of the nonsense related to that solution, by trying this one (and remember that you can't assume there is any open documents when a PaletteSet is closed):

 

     Getting notified when a PaletteSet is hidden/closed

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost