How to add a title and separator in the Tool Palette Manager using C#

How to add a title and separator in the Tool Palette Manager using C#

artillis_prado
Enthusiast Enthusiast
1,062 Views
10 Replies
Message 1 of 11

How to add a title and separator in the Tool Palette Manager using C#

artillis_prado
Enthusiast
Enthusiast

My code can create a palette and add the PKT files via path.
I am using the Tool Palette Manager from the AcTcMgd DLL.


artillis_prado_0-1711989110665.png


However, I'm unable to add text or a separator via C# code. Does anyone know how to do it?

artillis_prado_1-1711989167770.png


 

            [CommandMethod("QSAVEAS")]
            public void QuickSaveAs()
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;

                // If this is the first time run...

                if (_path == "" || _base == "")
                {
                    // Ask the user for a base file location

                    PromptSaveFileOptions opts =
                      new PromptSaveFileOptions(
                        "Select location to save first drawing file"
                      );
                    opts.Filter = "Drawing (*.dwg)|*.dwg";
                    PromptFileNameResult pr =
                      ed.GetFileNameForSave(opts);

                    // Delete the file, if it exists
                    // (may be a problem if the file is in use)

                    if (File.Exists(pr.StringResult))
                    {
                        try
                        {
                            File.Delete(pr.StringResult);
                        }
                        catch { }
                    }

                    if (pr.Status == PromptStatus.OK)
                    {
                        // If a file was selected, and it contains a path...

                        if (pr.StringResult.Contains(pthSep))
                        {
                            // Separate the path from the file name

                            int idx = pr.StringResult.LastIndexOf(pthSep);
                            _path =
                              pr.StringResult.Substring(0, idx);
                            string fullname =
                              pr.StringResult.Substring(idx + 1);

                            // If the path has an extension (this should always
                            // be the case), extract the base file name

                            if (fullname.Contains(extSep))
                            {
                                _base =
                                  fullname.Substring(
                                    0,
                                    fullname.LastIndexOf(extSep)
                                  );

                                // Create folders for our icons and our scripts

                                Directory.CreateDirectory(
                                  _path + pthSep + bmpLoc
                                );
                                Directory.CreateDirectory(
                                  _path + pthSep + scrLoc
                                );
                            }
                        }
                    }
                }

                // Assuming the path and name were set appropriately...

                if (_path != "" && _base != "")
                {
                    string name = _base;

                    // Add our suffix if not the first time run

                    if (_count > 0)
                        name += sfxSep + _count.ToString();

                    // Our drawing is located in the base path

                    string dwgPath = _path + pthSep + name + dwgExt;

                    // While our script is in a sub-folder

                    string scrPath =
                      _path + pthSep + scrLoc + pthSep + name + scrExt;

                    // Create a dummy script, so we can make sure we pick
                    // up the contents in our dummy execute command

                    File.WriteAllText(
                      scrPath,
                      "This is a dummy script for " + name + "."
                    );

                    // Now we want to save our drawing and use the image
                    // for our tool icon

                    // Using either COM or .NET doesn't generate a
                    // thumbnail in the resultant file (or its Database)

                    // .NET:
                    // db.SaveAs(dwgPath, false, DwgVersion.Current, null);

                    // COM:
                    // AcadDocument adoc = (AcadDocument)doc.AcadDocument;
                    // adoc.SaveAs(dwgPath, AcSaveAsType.acNative, null);

                    // So we'll send commands to the command-line
                    // We'll use LISP, to avoid having to set FILEDIA to 0

                    object ocmd = Application.GetSystemVariable("CMDECHO");
                    string dwgPath2 = dwgPath.Replace(pthSep, lspSep);
                    string scrPath2 = scrPath.Replace(pthSep, lspSep);

                    string c1 =
                      "(setvar \"CMDECHO\" 0)" +
                      "(command \"_.SAVEAS\" \"\" \"" + dwgPath2 + "\")";
                    string c2 =
                      "(setvar \"CMDECHO\" " + ocmd.ToString() + ")" +
                      "(tp-create \"" + name + "\" \"" + scrPath2 + "\")" +
                      "(princ) ";
                    string cmd = c1 + c2;

                    if (cmd.Length <= 255)
                    {
                        doc.SendStringToExecute(cmd, false, false, false);
                    }
                    else
                    {
                        doc.SendStringToExecute(c1 + " ", false, false, false);
                        doc.SendStringToExecute(c2, false, false, false);
                    }

                    // Print a confirmation message for the DWG save
                    // (which actually gets displayed before the queued
                    // string gets executed, but anyway)

                    ed.WriteMessage("\nSaved to: \"" + dwgPath + "\"");

                    _count++;
                }
            }

            // Our LISP-registered continuation function to create a
            // command tool on our tool palette

            [LispFunction("TP-CREATE")]
            public ResultBuffer CreateToolPaletteCommand(
              ResultBuffer rb
            )
            {
                const int RTSTR = 5005;

                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;

                if (rb == null)
                {
                    ed.WriteMessage("\nError: too few arguments.");
                }
                else
                {
                    // We're only interested in the first two arguments

                    Array args = rb.AsArray();
                    if (args.Length != 2)
                    {
                        ed.WriteMessage(
                          "\nError: wrong number of arguments."
                        );
                    }
                    else
                    {
                        // First argument is the name, second is the path
                        // to the script

                        TypedValue tv1 = (TypedValue)args.GetValue(0);
                        TypedValue tv2 = (TypedValue)args.GetValue(1);

                        if (tv1 != null && tv1.TypeCode == RTSTR &&
                            tv2 != null && tv2.TypeCode == RTSTR)
                        {
                            string name = Convert.ToString(tv1.Value);
                            string lspScrPath = Convert.ToString(tv2.Value);
                            string scrPath =
                              lspScrPath.Replace(lspSep, pthSep);
                            bool success =
                              CreateCommand(doc.Database, name, scrPath);
                              //CreateCommand(doc.Database, name, scrPath);
                              //CreateCommand(doc.Database, name, scrPath);
                        return
                              (success ?
                                new ResultBuffer(
                                  new TypedValue(RTSTR, tv1.Value)
                                )
                                : null);
                        }
                    }
                }
                return null;
            }

            // Function to add a command tool to
            // Function to add a command tool to our tool palette to
            // execute the script

            private bool CreateCommand(
              Database db,
              string name,
              string scrPath
            )
            {
                const string catName = "ScriptCatalog";
                const string palName = "ToolsPalette";

                ToolPaletteManager tpm = ToolPaletteManager.Manager;

                // Get the GUID of our dummy custom tool

                Type t = typeof(DummyTool);
                GuidAttribute ga =
                  (GuidAttribute)t.GetCustomAttributes(
                    typeof(GuidAttribute), false)[0];
                Guid g = new Guid(ga.Value);

                // Instantiate our dummy tool - this will allow us to use
                // its helper functions

                DummyTool tool = new DummyTool();
                Catalog cat;
                Autodesk.AutoCAD.Windows.ToolPalette.Palette pal = null;

            // First we check whether our GUID is in a catalog

            CatalogItem ci = tpm.StockToolCatalogs.Find(g);
                if (ci != null)
                {
                    // If it is, search each catalog for our palette

                    foreach (CatalogItem ci2 in tpm.Catalogs)
                    {
                        for (int i = 0; i < ci2.ChildCount; i++)
                        {
                            CatalogItem ci3 = ci2.GetChild(i);
                            if (ci3 != null && ci3.Name == palName)
                            {
                                pal = ci3 as Autodesk.AutoCAD.Windows.ToolPalette.Palette;
                                break;
                            }
                        }
                        if (pal != null)
                            break;
                    }
                }

                // If we didn't find our palette, create it

                if (pal == null)
                {
                    cat = tool.CreateStockTool(catName);
                    pal = tool.CreatePalette(cat, palName);
                }

            // To add our command tool instance we need an icon

            List<string> listaPKT = new List<string> { "Ferrovias_Surperestrutura 1", "Ferrovias_InfraEstrutura 1"};
            List<string> listpath = new List<string>() { "C:\\Users\\pc\\music\\file 1.pkt", "C:\\Users\\pc\\music\\file 2.pkt" };

            int cont = 0;
            foreach (string namePKT in listaPKT)
            {
                ImageInfo ii = new ImageInfo();
                ii.Size = new System.Drawing.Size(65, 65);

                // And then we use our dummy tool to create the
                // command tool


                tool.CreateCommandTool(
                  pal,
                  namePKT,
                  ii,
                  "_IMPORTPKT\"" + listpath[cont] + "\""
                );
                cont += 1;
            }
            // Finally we reload the catalogs to display the change

            tpm.LoadCatalogs();

                return true;
            }

            [Guid("3B725500-0451-4081-A1BB-B37CE6A65767")]
            [Tool("MyDummyTool", "IDB_TOOL")]
            [ClassInterface(ClassInterfaceType.AutoDual)]
            public class DummyTool : CustomToolBase
            {

            }

 

 

0 Likes
Accepted solutions (3)
1,063 Views
10 Replies
Replies (10)
Message 2 of 11

ActivistInvestor
Mentor
Mentor
Accepted solution

What release of AutoCAD are you using that forces you to resort to sending lisp Expressions to the command line?

Message 3 of 11

ActivistInvestor
Mentor
Mentor

That was a question but it definitely didn't solve your problem. There should be a property named ToolType that can be assigned the value ToolType.Seperator or ToolType.Text.

0 Likes
Message 4 of 11

artillis_prado
Enthusiast
Enthusiast

2024

0 Likes
Message 5 of 11

ActivistInvestor
Mentor
Mentor
Accepted solution

@artillis_prado wrote:

2024


Are you sure?

 

 

// Using either COM or .NET doesn't generate a
// thumbnail in the resultant file (or its Database)

// COM:
// AcadDocument adoc = (AcadDocument)doc.AcadDocument;
// adoc.SaveAs(dwgPath, AcSaveAsType.acNative, null);

 

The above code has not worked since AutoCAD 2012.  After that release, there is no AcadDocument property (It was replaced with the GetAcadDocument() extension method).

 

AutoCAD 2024 doesn't require sending LISP expressions to the command line to execute commands. You can use the Editor's Command() method to do that more directly.

 

About your original problem, to create a separator or text, you have to create an Autodesk.AutoCAD.Windows.ToolPalette.Tool, and set its ToolType property to ToolTip.Seperator or ToolType.Text.

 

 

0 Likes
Message 6 of 11

artillis_prado
Enthusiast
Enthusiast
How do I add it to the palette? My question is how to add it.
0 Likes
Message 7 of 11

ActivistInvestor
Mentor
Mentor

First, You should have mentioned that the code you posted is from a 2009 Through The Interface blog post.

 

You add items using the AddChild() method.

0 Likes
Message 8 of 11

artillis_prado4J852
Explorer
Explorer

I realized that it accepts catalog as a parameter, and not other parameters I am experienced with the autocad api, so these doubts

0 Likes
Message 9 of 11

artillis_prado4J852
Explorer
Explorer
I realized that it accepts catalog as a parameter, and not other parameters I am experienced with the autocad api, so these doubts
0 Likes
Message 10 of 11

ActivistInvestor
Mentor
Mentor
Accepted solution

CreateTool() and CreateCommandTool() both create and return a Tool object, and adds it to a Palette.

 

 

0 Likes
Message 11 of 11

artillis_prado
Enthusiast
Enthusiast

But, so you know an example of what it would be like using Tooltype, to add text or separator
I think these commands are not useful for ToolType, because both commands you mentioned ask for an image

0 Likes