Are there any gotcha's I am missing,
I did not check rotation----need that added
There is no checking if block is inserted----needs to be added
It is assumed that the outermost entity in block is a rectangle
Basiclly it gets the block's Geometric extents and passes that into Editor.SelectWindow() then compares the number objects selected with the total objects in that space.
If it is in modelspace the counter starts at 0 if it is in paperspace it starts at -1 to count for extra viewport.
Code is for basic testing not production
[CommandMethod("CheckTB")]
public void CheckTB()
{
Document doc = Application.DocumentManager.MdiActiveDocument;//Assign the Current Document to doc
Database db = doc.Database;// Assign doc's(Current Active Document) database to db
Editor ed = doc.Editor;// Assign Editor to doc
Point3d minPoint = Point3d.Origin;
Point3d maxPoint = Point3d.Origin;
//number of total objects and set to negative 1 to count for extra viewport
int numberOfObjects = -1;
using (Transaction trx = db.TransactionManager.StartTransaction())// Start Transaction
{
BlockTable blocktable = trx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;//Assign BlockTable
BlockTableRecord currentSpace = trx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
// If modelSpace do not need to have extra number for viewport
if (currentSpace.ObjectId == blocktable[BlockTableRecord.ModelSpace])
{
numberOfObjects++;
}
BlockTableRecord titleBlock = blocktable["TB"].GetObject(OpenMode.ForRead) as BlockTableRecord;
// Iterate all the entites in the current space
foreach (ObjectId objId in currentSpace)
{
numberOfObjects++;// add 1 to running total
if (!(objId.ObjectClass.Name == "AcDbBlockReference"))// If not a BlockReference then skip it
{
continue;
}
BlockReference blockreference = trx.GetObject(objId, OpenMode.ForRead) as BlockReference;
if (blockreference.BlockTableRecord == titleBlock.ObjectId)
{
maxPoint = blockreference.GeometricExtents.MaxPoint;
minPoint = blockreference.GeometricExtents.MinPoint;
}
}
PromptSelectionResult psr = ed.SelectWindow(minPoint, maxPoint);
if (psr.Value.Count < numberOfObjects)
{
Application.ShowAlertDialog(@"Why do you keep drawing outside the Title Block area?
I hope you understand I am logging this and when you reach
a certain number, instead of this message box
you will see Outlook open with a e-mail sent to the boss
explaining to him that you are uncapable of following simple directions.");
}
else
{
Application.ShowAlertDialog(numberOfObjects.ToString());
}
trx.Commit();
}
}