.NET

.NET

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

How verify presence of a block in paper space

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

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

 

 

Mentor
SENL1362
Posts: 229
Registered: ‎07-20-2011
Message 2 of 2 (293 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;

             ....

 

 

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.