Hi @Jeff_M and @hosneyalaa,
Good day.
I attached the DWG File as reference.

User Steps:
Select BlockReference as BlockSheet
Select Target Entities (Circle, Line, Polyline, SPLine)
Desired Output:
Circle (Cyan): 053, 054, 060 & 061
Line(Green): 067
SPLine(Yellow): 056, 057, 061, 062 & 063
Polyline(White): 069, 070, 072, 073 & 074
Block <BlockSheet>: Contains Polyline and Text
Polyline ("A-TITL-TXT2" & "Z_DWG_SHT")
TEST ("Z_DWG_SHT_NO")
My idea on the process:
Explode the BlockSheet to SheetCollection
Remove any objects except objects having layer ("Z_DWG_SHT" & "Z_DWG_SHT_NO")
Loop through to Selected Entities
(SelectCrossing or Alternative function) using boundingbox of the entity as Crv1 to identify the objects in SheetCollection.
(SelectCrossing or Alternative function) Using the boundary of the entity as Sheet having layer "Z_DWG_SHT" from the sheetCollection to check if Crv1 exist in the selection
Once Crv1 exist grab the Text with layer "Z_DWG_SHT_NO"
Currently I don't have an alternative function that can SelectCrossing in the Collection.
Below is the sample code that I did
private void _SelectObjectsFromExploded(object sender, EventArgs e)
{
Autodesk.Civil.ApplicationServices.CivilDocument civilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
listBox1.Items.Clear();
string mSheetBlockName = "SheetBlock";
string mSheetLayerName = "Z_DWG_SHT";
string mLabelLayerLName = "Z_DWG_SHT_NO";
cSelection mSel = new cSelection();
ObjectId mSheetBlockId = mSel.Select_SingeEntity("Select Sheet Block");
// Set up our selection to only select 2D solids
var pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nSelect Curve";
//var sf = new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "3DSOLID") });
//var psr = ed.GetSelection(pso, sf);
var psr = ed.GetSelection(pso);
if (psr.Status != PromptStatus.OK) return;
using (doc.LockDocument())
{
DBObjectCollection mSheets = new DBObjectCollection();
DBObjectCollection mValidSheets = new DBObjectCollection();
BlockReference oSheet = null;
doc.TransactionManager.EnableGraphicsFlush(true);
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
var oBlk = tr.GetObject(mSheetBlockId, OpenMode.ForWrite);
if (oBlk is BlockReference)
{
oSheet = oBlk as BlockReference;
if (oSheet.Name.ToUpper() != mSheetBlockName.ToUpper())
{
ed.WriteMessage("\nSelected block is not recognize as Block Sheet.");
return;
}
// Create Temp Sheets
oSheet.Explode(mSheets);
mValidSheets = GetValidSheets(tr, mSheets);
}
tr.Commit();
}
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
if(oSheet != null)
{
foreach (ObjectId oID in psr.Value.GetObjectIds())
{
var o = tr.GetObject(oID, OpenMode.ForRead);
if (o is Curve)
{
Curve mCurve = o as Curve;
CreateLine(tr, mCurve.GeometricExtents.MinPoint, mCurve.GeometricExtents.MaxPoint);
string mSheetCnt = GetSheetNumber(mValidSheets, mCurve);
listBox1.Items.Add(mSheetCnt);
}
}
// Clear Temporary Sheets
foreach (Entity ent in mValidSheets)
{
Entity ForDel = tr.GetObject(ent.ObjectId, OpenMode.ForWrite) as Entity;
ForDel.Erase();
}
}
else
{
ed.WriteMessage("\nInvalid Block Selected.");
}
tr.Commit();
}
}
}
DBObjectCollection GetValidSheets(Transaction tr, DBObjectCollection mSheets, string LayerSheet="Z_DWG_SHT", string LayerSheetNo= "Z_DWG_SHT_NO")
{
BlockTable acBlkTbl = tr.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
DBObjectCollection mValidSheets = new DBObjectCollection();
foreach (Entity oEnt in mSheets)
{
if (oEnt.Layer == LayerSheet || oEnt.Layer == LayerSheetNo)
{
mValidSheets.Add(oEnt);
acBlkTblRec.AppendEntity(oEnt);
tr.AddNewlyCreatedDBObject(oEnt, true);
}
}
return mValidSheets;
}
string GetSheetNumber(DBObjectCollection mSheets, Curve mCurve, string LayerSheet = "Z_DWG_SHT", string LayerSheetNo = "Z_DWG_SHT_NO")
{
dynamic acadApp = Application.AcadApplication;
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
string mSheetCnt = "";
PromptSelectionResult mRes = ed.SelectCrossingWindow(mCurve.GeometricExtents.MaxPoint, mCurve.GeometricExtents.MinPoint);
using (SelectionSet ss = mRes.Value)
{
if (ss is null) return "No Selection";
foreach (ObjectId oId in ss.GetObjectIds())
{
Entity obj = oId.GetObject(OpenMode.ForRead) as Entity;
//if (mSheets.Contains(obj))
//System.Diagnostics.Debug.Print(obj.GetType().Name + " - " + obj.Layer);
listBox1.Items.Add(" - " + obj.GetType().Name + " - " + obj.Layer);
if(CollectionHas(mSheets, obj))
{
if (obj.Layer == LayerSheet) // Sheet Box
{
// Select using box
PromptSelectionResult mBoxSelect = ed.SelectCrossingWindow(obj.GeometricExtents.MaxPoint, obj.GeometricExtents.MinPoint);
bool HasCurveId = false;
bool HasSheetCnt = false;
Entity oSheetCnt = null;
foreach (ObjectId oInBoxId in mBoxSelect.Value.GetObjectIds())
{
if (mCurve.ObjectId == oInBoxId) HasCurveId = true;
Entity oBoxObj = oInBoxId.GetObject(OpenMode.ForRead) as Entity;
if(oBoxObj.Layer == LayerSheetNo)
{
HasSheetCnt = true;
oSheetCnt = oBoxObj;
}
}
// If curve and sheetcnt exist add the sheet number
if (HasSheetCnt && HasCurveId)
{
if (oSheetCnt is DBText) mSheetCnt = mSheetCnt +", " + ((DBText)oSheetCnt).TextString;
if (oSheetCnt is MText) mSheetCnt = mSheetCnt + ", " + ((MText)oSheetCnt).Text;
}
}
}
}
}
return mSheetCnt;
}