Select Object While Executing Command Programmatically
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello All,
I'm trying to execute a built-in command "DIMBREAK" that requires to selecting dimensions then selecting the intersected object. The problem occurs when I tried to pass the intersected objected Id (BlockReference Id), AutoCAD writes <Bad Entity name: 114C0550>. Although when I select the block reference manually, it works properly.
Could you help me to know what's missing in the following code?
[CommandMethod("BreakDimensionsWithBlock", CommandFlags.UsePickSet)]
public static void BreakDimWithBlock()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Ask the user to select a block
try
{
SelectionSet brokenDimSelectionSet = null;
SelectionSet blocksSelectionSet = null;
using (var tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord spaceBtr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
ObjectIdCollection dimensionsIds = new ObjectIdCollection();
foreach (var id in spaceBtr)
{
if (id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Dimension))))
dimensionsIds.Add(id);
}
if (dimensionsIds.Count < 1)
{
ed.WriteMessage("The current space doesn't contain any dimensions");
return;
}
ObjectId[] idArray = new ObjectId[dimensionsIds.Count];
dimensionsIds.CopyTo(idArray, 0);
brokenDimSelectionSet = SelectionSet.FromObjectIds(idArray);
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a block that will break dimensions:") { AllowNone = false };
peo.SetRejectMessage("\nMust select a block.");
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
var blockRef = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
BlockTableRecord btr = blockRef.IsDynamicBlock ?
(BlockTableRecord)tr.GetObject(blockRef.DynamicBlockTableRecord, OpenMode.ForRead) :
(BlockTableRecord)tr.GetObject(blockRef.BlockTableRecord, OpenMode.ForWrite);
var allBlocksIds = btr.GetBlockReferenceIds(true, true);
foreach (ObjectId blockRefId in btr.GetAnonymousBlockIds())//for dynamic blocks
allBlocksIds.Add(blockRefId);
ObjectId[] blocksIdArray = new ObjectId[allBlocksIds.Count];
allBlocksIds.CopyTo(blocksIdArray, 0);
blocksSelectionSet = SelectionSet.FromObjectIds(blocksIdArray);
tr.Commit();
}
//Application.DocumentManager.MdiActiveDocument.SendStringToExecute($"DIMBREAK Multiple {blocksSelectionSet} {blockRef.Id}", false, false, false);
//ed.Command("_.DIMBREAK", "_Multiple", brokenDimSelectionSet, "", blocksSelectionSet);//AutoCad throws InvalidInput Exception
foreach (ObjectId blockRefId in blocksSelectionSet.GetObjectIds())
{
ed.Command("_.DIMBREAK", "_Multiple", brokenDimSelectionSet, "", blockRefId, "");//AutoCad says <Bad Entity Name> ! Is blockRefId invalid?
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
// Something went wrong
ed.WriteMessage(e.ToString());
}
}
Link copied