TheSwamp

Code Red => .NET => Topic started by: MickD on June 20, 2006, 06:15:36 PM

Title: A very simple dockable palette application
Post by: MickD on June 20, 2006, 06:15:36 PM
For those new to C# and the arx .net api I thought I'd post a very simple dockable palette application to get you started.
So here goes -

1) Create a new class library application, add references to the 2 AutoCAD managed dll's, the Systym.Windows.Forms.dll and the System.Drawing dll.

2) Create 2 new UserControls, call them what you like but here I've used default names.

3) To each UserControl, add 1 button, to each button add some code to handle the click event (tip. double click the control in the ide)

4) Create a command to load up our palette and show it in AutoCAD.

5) Compile, load and give it a run.

Below is the typical code used to set up and load your palette, I've attached the solution (VS2002) for AutoCAD 2006, I'm sure it would take very little to compile in 2007.

Code: [Select]
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections;
using System.Data;
using System.Data.OleDb;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows.ToolPalette;
namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
private Autodesk.AutoCAD.Windows.PaletteSet ps;
[CommandMethod("MyPalette")]
public void CreateMyPalette()
{
if(ps == null)
{
ps = new Autodesk.AutoCAD.Windows.PaletteSet("My Palette");
ps.Style = Autodesk.AutoCAD.Windows.PaletteSetStyles.ShowPropertiesMenu
| Autodesk.AutoCAD.Windows.PaletteSetStyles.ShowAutoHideButton
| Autodesk.AutoCAD.Windows.PaletteSetStyles.ShowCloseButton;
ps.Opacity = 90;
ps.Size = new System.Drawing.Size(250,400);
ps.MinimumSize = new System.Drawing.Size(225, 400);
//ctrl.Dock = System.Windows.Forms.DockStyle.Fill;
ps.Add("My Controls 1", new UserControl1());
ps.Add("My Controls 2", new UserControl2());
ps.Dock = Autodesk.AutoCAD.Windows.DockSides.None;
//You may or may not need the below code for list/combo boxes to retain focus.
//ps.KeepFocus = true;
ps.Visible = true;
}
else
{
ps.Visible = true;
}
}
}
}

Some typical code for the button event handler -
Code: [Select]
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Activated Button 1 on User control 1!");
}

You may also need Document locking for times when you need to go to and from the editor, I'll post some typical code later, absorb this for now and if you have any questions, ask away :)
Title: Re: A very simple dockable palette application
Post by: Bob Wahr on June 20, 2006, 07:15:41 PM
Thanks Mick.  I can actually follow it and it looks useful to boot.
Title: Re: A very simple dockable palette application
Post by: LE on June 20, 2006, 07:44:37 PM
Very good Sir... once I got get! my Vegetable 8 Juice new VS 2005 (V8) and A2K7 this week, I give it a try out.

 :lol:
Title: Re: A very simple dockable palette application
Post by: Kerry on June 20, 2006, 09:51:47 PM
Bookmarked Mick ... Thanks. !
I'll have a look when the fires go out.
Title: Re: A very simple dockable palette application
Post by: TR on June 21, 2006, 09:32:51 AM
Very nice. This should be quite useful.
Title: Re: A very simple dockable palette application
Post by: MickD on June 21, 2006, 05:48:04 PM
Very nice. This should be quite useful.

Yep, I thought it might be, there are a few threads here where people are discussing dropping there current UI dev tools and thinking of going to .net.
This just might be the extra prod in the ribs they require  :evil:
Title: Re: A very simple dockable palette application
Post by: LE on June 21, 2006, 05:53:08 PM
Mick;

So in other words, is no way to do this in plain jane arx?....  :-(

Thanks.
Title: Re: A very simple dockable palette application
Post by: MickD on June 21, 2006, 06:10:33 PM
Yes Luis, there is, in fact you 'may' be able to do much more but no where near as easily as you can with .net.
Have a look at the sdk about palettes in arx, it's a COM & MFC nightmare. Although the palette set .net api is just a thin wrapper around the MFC portion it's still light years easier to use which is exactly what we need for 'customisation'.

I actually love C++ - when it's pure C++ - but it is more often a mix of C and C++ especially when it comes to working with the win32 api, your other option is to use MFC...

<edit>
Sorry Luis, after re-reading, the first few words were definitely not meant to 'sound' the way they looked  :oops:
</edit>
Title: Re: A very simple dockable palette application
Post by: Draftek on June 21, 2006, 10:27:15 PM
I agree. It will be much easier to just expose your arx app to managed c# to write the UI's.
Title: Re: A very simple dockable palette application
Post by: It's Alive! on April 17, 2010, 10:01:42 PM
Yes Luis, there is, in fact you 'may' be able to do much more but no where near as easily as you can with .net.
Have a look at the sdk about palettes in arx, it's a COM & MFC nightmare. Although the palette set .net api is just a thin wrapper around the MFC portion it's still light years easier to use which is exactly what we need for 'customisation'.

It's this kind of talk that stopped me from ever trying, I hate COM in C++ and I don't particularly like nightmares either. After being traumatized for so many years and having spent so long in "C++ ain't bad" therapy, I finally worked the courage to give it try, while chewing on a tablespoon of coffee grounds, I managed to figure it out.  Geez, what a piece of cake. Add a CAdUiPaletteSet class, add couple of CAdUiPalette classes, make a command.

Code: [Select]
static void ExtTools_doit(void)
  {
    if(!gExtPaletteSet)
    {
      gExtPaletteSet = new CExtPaletteSet;
      CRect rect(0, 0, 250, 500);
      gExtPaletteSet->Create(_T("Palette"),
        WS_OVERLAPPED | WS_DLGFRAME, rect, acedGetAcadFrame());

      gBlockPalette = new CBlockPalette();
      gDummyPalette = new CAdUiPalette();

      gBlockPalette->Create(WS_CHILD |
        WS_VISIBLE,_T("Palette 1"),gExtPaletteSet,PS_EDIT_NAME);
      gExtPaletteSet->AddPalette(gBlockPalette);

      gDummyPalette->Create(WS_CHILD |
        WS_VISIBLE,_T("Palette 2"),gExtPaletteSet, PS_EDIT_NAME);
      gExtPaletteSet->AddPalette(gDummyPalette);

      gExtPaletteSet->EnableDocking(CBRS_ALIGN_ANY);
    }
    gExtPaletteSet->RestoreControlBar();
    CMDIFrameWnd* pAcadFrame = acedGetAcadFrame();
    pAcadFrame->ShowControlBar(gExtPaletteSet, TRUE, FALSE);
  }

voila ...

@ Mick, you do know I'm kidding right?  :laugh:
Title: Re: A very simple dockable palette application
Post by: sinc on April 18, 2010, 09:40:15 AM
Note that if you add a GUID to your call that creates the Palette set, Autocad will remember the location and size of the palette, even when you shut down and restart Autocad.
Title: Re: A very simple dockable palette application
Post by: It's Alive! on April 19, 2010, 02:43:20 AM
You're right! Thank you  8-)