Hi All,
I have been working on getting a palette set of tools working with good success. A couple of problems I am having...
1) How do I close the paletteset when AutoCAD enters a zero document state? As it is now, the paletteset stays open with no drawing file open.
2) I have two palettes (tabs) in my paletteset. How can I switch between those two palettes with code? Or is it possible to create a button macro that opens the paletteset to a particular palette while another button opens the palettset to the other palette?
TIA!
Hi,
>> How can I switch between those two palettes with code?
myPS.Activate(0)
...would be the code to change to the first tab (Index = 0) of the object myPS (= type PaletteSet)
- alfred -
There probably is an event you can check otherwise you can use a timer.
if Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Count = 0 ...
As far as the palette set all you have to do is activate the postion.
ps.activate(TabPosition)
Thanks for those answers folks! I now have both working properly.
I have two more questions/problems with my palettes. I am hoping the answer to the first problem will resolve the second problem.
How do I stop the code from running in the palette if the user clicks the "X" on the palette? As it is now, even when the palette is closed, the code still runs. I am using the AddHandler .DocumentManager.DocumentActivated event to fire off the code when drawings are opened or switched. Unfortunately, this event continues to fire even after the palette has been closed.
The second problem I am having is that after the palette has been closed, once the palette has been reopened, it crashes AutoCAD. I am using the "Friend Shared myPaletteSet As Autodesk.AutoCAD.Windows.PaletteSet = Nothing" declaration to prevent multiple palettes, but cannot figure out why it crashes after a close and reopen. I would post the code, but it is pretty lengthy. If someone really wants to look at it, let me know and I will share.
Thanks for your expert advice!
Rob
You can add a check in your events / timers
if ps.visible = false then exit sub else run the code.
You should have something similar to this code for the palette.
Just remember you should never make your palette nothing once it's created.
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Runtime
Public Class Class2
' auto-enable our toolpalette for AutoCAD
Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
Friend Sub Initialize() Implements IExtensionApplication.Initialize
End Sub
Friend Sub Terminate() Implements IExtensionApplication.Terminate
End Sub
' ensure single instance of this app...
Friend Shared ps As Autodesk.AutoCAD.Windows.PaletteSet = Nothing
<CommandMethod("PS")> _
Public Sub buildPalette()
If ps Is Nothing Then
' use constructor with Guid so that we can save/load user data
' Guid is stored in the registry to keep the unique value.
ps = New Autodesk.AutoCAD.Windows.PaletteSet("PartList", New Guid("{94BEBD9F-DA71-46f3-9F56-1F5C6CF4C7E1}"))
Dim myCtrl As New Windows.Forms.UserControl
myCtrl.Name = "name"
ps.Add("Panel", myCtrl)
End If
ps.Visible = True
End Sub
End Class
Problem #2
Check if it exists but isdisposed (maybe the user closed it)
I use a boolean - something like this.
dim exists as boolean = true
if mypallette is nothing then
exists = false
end if
if mypallette.isdisposed = true
exists = true
end if
if exists = false then
'redefine the pallette
end if
mypallette.visible = true
Not quite right - sorry. If it is disposed it needs to be redefined.
dim exists as boolean = true
if mypallette is nothing then
exists = false
end if
if mypallette.isdisposed = true
exists = false
end if
if exists = false then
'redefine the pallette
end if
mypallette.visible = true
Hi,
your code can't run stable!, At least this situation
if mypallette is nothing then
exists = false
end if
if mypallette.isdisposed = true
exists = false
end if
If that is true: mypallette is nothing then the following if if mypallette.isdisposed ... will throw an exeption, because an object that is not defined can't be checked if it's disposed!
The shortest check would be [edited]:
Dim exists as Boolean = (mypallette isnot nothing) AndAlso (not mypallette.idDisposed)
And back to your question to watch the closing of the paletteset, try to declare your variable if the paletteset with "WithEvents" or define eventhandling with "AddHandler". I have not tried it now, but there are some Eventhandlers defined that may give you the info the paletteset closes.
HTH, - alfred -
Good catch Alfred. My pseudo code was wrong twice!
I should have pasted instead of typing on the fly.
Hi,
I also did an edit currently 😉
- alfred -
Thanks for the help again folks! I discovered that my crash was due to some bad code. I was trying to make a palette not visible instead of the paletteset.
I used Articad's suggestion to check the status of the palette first prior to running the code and this seems to work well. It seems like there should be a built in method to stop the code from running if the palette has been closed, but I guess that would be akin to a "net-unload" and from what I understand, you cannot do that (or at least not easily).
These palettesets are a little more complex than I thought they would be!
Thanks again for all the help.