This topic came up recently in the discussion forum. The developer wanted to display a custom form on double clicking of block reference that were created by his application but let AutoCAD perform the usual block editing for other block references.
Here is a sample code that modifies the macro for the block double click using the CUI API and makes it invoke a custom command called “mybedit”. From the “mybedit” custom command, we can check if the block reference is the one that we are interested in. If yes, we do our custom action such as showing our custom form. If not, we set the pick first selection and then invoke the AutoCAD “bedit”.
// Requires reference to AcCui.dll
using Autodesk.AutoCAD.Customization;
[CommandMethod("ChangeMacroCUI")]
static public void ChangeMacroCUI()
{
Document activeDoc
= Application.DocumentManager.MdiActiveDocument;
Editor ed = activeDoc.Editor;
// Retrieve the location of, and open the ACAD Main CUI File
string mainCuiFile =
(string)Application.GetSystemVariable("MENUNAME");
mainCuiFile += ".cuix";
CustomizationSection cs = new CustomizationSection(mainCuiFile);
DoubleClickAction blockDoubleClickAction = null;
foreach (DoubleClickAction dca in
cs.MenuGroup.DoubleClickActions)
{
if (dca.Name.Equals("Block"))
{
blockDoubleClickAction = dca;
break;
}
}
if (blockDoubleClickAction != null)
{
// Change the double click macro to call our command
blockDoubleClickAction.DoubleClickCmd.MenuMacroReference.macro.Command
= "$M=$(if,$(and,$(>,$(getvar,blockeditlock),0)),^C^C_properties,^C^C_MyBEdit)";
// Save our changes
if (cs.IsModified)
cs.Save();
}
}
[CommandMethod("MyBEdit", CommandFlags.UsePickSet)]
static public void MyBeditCommand()
{
Document activeDoc
= Application.DocumentManager.MdiActiveDocument;
Editor ed = activeDoc.Editor;
String RegAppName = "MyApp";
PromptSelectionResult result = ed.GetSelection();
if (result.Status == PromptStatus.OK)
{
SelectionSet ss = result.Value;
foreach (SelectedObject so in ss)
{
bool isMyBlockRef = false;
Database db = activeDoc.Database;
using (Transaction tr
= db.TransactionManager.StartTransaction())
{
Entity ent
= tr.GetObject(
so.ObjectId,
OpenMode.ForRead
) as Entity;
// Lets check for XData to identify if we need
// to show our dialog
if (ent.GetXDataForApplication(RegAppName) != null)
{
isMyBlockRef = true;
}
tr.Commit();
}
if(isMyBlockRef)
{
// Custom action such as showing our form.
// Just as a demo we use the alert dialog
Application.ShowAlertDialog
("Custom action such as showing our form.");
}
else
{
// Let AutoCAD do the block edit.
ObjectId[] ids = ss.GetObjectIds();
// Set the implied selection to what it was
// before our command was called
ed.SetImpliedSelection(ids);
// call "bedit" command
activeDoc.SendStringToExecute
(
"_BEDIT ",
false,
false,
false
);
}
}
}
}

Leave a Reply to BenCancel reply