- public static void tt1()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- PromptEntityOptions optsEnt = new PromptEntityOptions("\nSelect a Block:");
- optsEnt.SetRejectMessage("\nNot a Block!");
- optsEnt.AddAllowedClass(typeof(BlockReference), false);
- PromptEntityResult resEnt = ed.GetEntity(optsEnt);
- if (resEnt.Status == PromptStatus.OK)
- {
- PromptResult resStr = ed.GetString("\nInput BlockName:");
- if(resStr.Status == PromptStatus.OK)
- {
- Database db = doc.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- string blkname = resStr.StringResult;
- if(bt.Has(blkname))
- {
- BlockReference blkref = (BlockReference)tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite);
- BlockReference blkrefnew = new BlockReference(blkref.Position, bt[blkname]);
- blkrefnew.BlockTransform = blkref.BlockTransform;
- btr.AppendEntity(blkrefnew);
- tr.AddNewlyCreatedDBObject(blkrefnew, true);
- foreach(ObjectId attid in blkref.AttributeCollection)
- {
- AttributeReference att = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
- AttributeReference attnew = (AttributeReference)att.Clone();
- blkrefnew.AttributeCollection.AppendAttribute(attnew);
- tr.AddNewlyCreatedDBObject(attnew, true);
- }
- blkrefnew.XData = blkref.XData;
- blkref.Erase();
- tr.Commit();
- }
- }
- }
- }
- }
|