Hi,
How to verify a block is already inserted in a layout [paper space] using autocad .net. There will be only one instance per layout.
Regards,
Shijith
Several solutions possible:
1. general solution to be used for every BlockTable, i.e. Modelspace, paperspace etc
// based on samples from http://drive-cad-with-code.blogspot.com/2011/04/tips-of-using-linq-in-autocad-net.html
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockTableRecord ptr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead);
var psIDs = ptr.Cast<ObjectId>().ToList();
ed.WriteMessage("\n{0} paperspace entities", psIDs.Count);
//All blocks Named=xxx
var blks = psIDs.Where(id => id.ObjectClass.DxfName.ToUpper() == "INSERT").Select(id => GetBlockRef(id, trans)).ToList<BlockReference>();
var myNamedblks = blks.Where(c => c.Name.ToUpper() == "MyXXX".ToUpper()).ToList<BlockReference>();
foreach (BlockReference myBlk in myNamedblks)
ed.WriteMessage("\n" + myBlk.Name + ", " + myBlk.Handle);
.....................................
2. Selection sample for active Layout
TypedValue[] selectBlkName = new TypedValue[]
{
new TypedValue((int)DxfCode.Start, "INSERT"),
new TypedValue((int)DxfCode.Name, blkName),
};
SelectionFilter selFilter = new SelectionFilter(selectBlkName );
db.TileMode=false; //activate paperspace
PromptSelectionResult prSelRes = ed.SelectAll(selFilter);
//return new ObjectIdCollection(prSelRes.Value.GetObjectIds());
if (prSelRes.Status == PromptStatus.OK)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId selID in prSelRes.Value.GetObjectIds())
{
Entity theEnt = (Entity)trans.GetObject(selID, OpenMode.ForRead);
ed.WriteMessage("\n" + selID.ObjectClass.DxfName + ", " + theEnt.GetType().ToString() + ", " + theEnt.Handle.ToString() + ", " + theEnt.Layer.ToString());
.........................................................
3. select from specific layout only
....
db.TileMode=false; //activate paperspace
LayoutManager.Current.CurrentLayout = layoutname;
....
Can't find what you're looking for? Ask the community or share your knowledge.