Distinguished Contributor spu
Distinguished Contributor
Posts: 141
Registered: ‎02-21-2007
Message 1 of 2 (293 Views)

How verify presence of a block in paper space

293 Views, 1 Replies
01-30-2012 09:47 PM



How to verify a block is already inserted in a layout [paper space] using autocad .net. There will be only one instance per layout.






Posts: 150
Registered: ‎07-20-2011
Message 2 of 2 (280 Views)

Re: How verify presence of a block in paper space

01-31-2012 02:11 AM in reply to: spu

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;







You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community