Message 1 of 11
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
My code can create a palette and add the PKT files via path.
I am using the Tool Palette Manager from the AcTcMgd DLL.
However, I'm unable to add text or a separator via C# code. Does anyone know how to do it?
[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
{
}
Solved! Go to Solution.