.NET

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

How verify presence of a block in paper space

305 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: 214
Registered: ‎07-20-2011
Message 2 of 2 (292 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
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!