public static bool HasXrefsInPSpace(this Document doc) { bool value = false; var btrlist = ProjectSetup.GetAllBtrs(); var xreflist = ProjectSetup.Xref_List(doc); List <ObjectId> listoflayids = ProjectSetup.Project_ListOfLayoutIds(doc); var pspaces = listoflayids.GetRange(1, (listoflayids.Count() - 1)); if (xreflist.Count() > 0) { foreach (BlockTableRecord x in xreflist) { ObjectId[] xref_layids = ProjectSetup.Xref_LayoutFinder(x); if (xref_layids.Any().Equals(pspaces.Any())) { value = true; return(value); } } } return(value); }
public static bool HasXrefsInMSpace(this Document doc) { // retrieve a list of external references in entire document var xreflist = ProjectSetup.Xref_List(doc); // retrieve a list of the layouts in the document List <ObjectId> listoflayids = ProjectSetup.Project_ListOfLayoutIds(doc); // the first layout is apparently ALWAYS Model Space?? var mspace = listoflayids[0]; // if there are external references in the list if (xreflist.Count() > 0) { // cycle through each foreach (BlockTableRecord x in xreflist) { // find out which Layout they have Block References in ObjectId[] xref_layids = ProjectSetup.Xref_LayoutFinder(x); if (xref_layids.Any().Equals(mspace)) { return(true); } } } return(false); }
///Gets a List of BlockTableRecords that are External References ///</summary> ///<param name="database"> The database you want to query </param> ///<param name="doc"> The document you want to retrieve a list of Xrefs from </param> public static List <BlockTableRecord> Xref_List(Document doc) { //Create a list of all xrefs in the file List <BlockTableRecord> tempxrlist; List <BlockTableRecord> XList = new List <BlockTableRecord>(); //Check the drawing to see if there are any xrefs using (Transaction Tran = doc.TransactionManager.StartTransaction()) { //Get the Block table record for the entire file tempxrlist = ProjectSetup.GetAllBtrs(); //Make a list of all the Xrefs in the file foreach (BlockTableRecord a in tempxrlist) { if (a.IsFromExternalReference) { XList.Add(a); } } //Complete the command Tran.Commit(); } return(XList); }
static void Main() { //Access and lock the current document and database Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; DocumentLock lckdoc = doc.LockDocument(); Editor ed = doc.Editor; //Gather all the xrefs from the document, if there are any List <BlockTableRecord> xlist = ProjectSetup.Xref_List(doc); //Get a list of layouts/layoutids List <Layout> listoflayouts = new List <Layout>(); List <ObjectId> listoflayids = ProjectSetup.Project_ListOfLayoutIds(doc); ed.WriteMessage("\n There are {0} layouts in this model", listoflayids.Count); // The objectIDs for the BlockTableRecords of the Model and PaperSpace layouts var mspace = listoflayids[0]; var pspaces = listoflayids.GetRange(1, (listoflayids.Count() - 1)); using (Transaction tr = db.TransactionManager.StartTransaction()) { foreach (Layout l in listoflayouts) { var l_name = l.LayoutName; var layout_extents = l.Extents; var layout_limits = l.Limits; ObjectId lbtr_id = l.BlockTableRecordId; BlockTableRecord l_btr = tr.GetObject(lbtr_id, OpenMode.ForRead) as BlockTableRecord; List <ObjectId> objs_inLayout = new List <ObjectId>(); foreach (ObjectId x in l_btr) { objs_inLayout.Add(x); } int o = objs_inLayout.Count(); foreach (BlockTableRecord x in xlist) { ObjectIdCollection BlockRefIdsColl = x.GetBlockReferenceIds(true, true); List <ObjectId> BRI_list = new List <ObjectId>(); foreach (ObjectId objid in BlockRefIdsColl) { BRI_list.Add(objid); } ObjectId BlockId = BRI_list[0]; BlockReference blref = tr.GetObject(BlockId, OpenMode.ForRead) as BlockReference; var block_bounds = blref.Bounds; var block_geoextents = blref.GeometricExtents; //var block_xtra = blref.Wb } } } }
public static bool HasEmptyLayouts(this Document doc) { bool value = false; Database db = doc.Database; DocumentLock lckdoc = doc.LockDocument(); Editor ed = doc.Editor; //Gather all the xrefs from the document, if there are any List <BlockTableRecord> xlist = ProjectSetup.Xref_List(doc); //Get a list of layouts/layoutids List <Layout> listoflayouts = new List <Layout>(); List <ObjectId> listoflayids = ProjectSetup.Project_ListOfLayoutIds(doc); ed.WriteMessage("\n There are {0} layouts in this model", listoflayids.Count); // The objectIDs for the BlockTableRecords of the Model and PaperSpace layouts var mspace = listoflayids[0]; var pspaces = listoflayids.GetRange(1, (listoflayids.Count() - 1)); using (Transaction tr = db.TransactionManager.StartTransaction()) { foreach (Layout l in listoflayouts) { ObjectId lbtr_id = l.BlockTableRecordId; BlockTableRecord l_btr = tr.GetObject(lbtr_id, OpenMode.ForRead) as BlockTableRecord; List <ObjectId> objs_inLayout = new List <ObjectId>(); foreach (ObjectId x in l_btr) { objs_inLayout.Add(x); } int o = objs_inLayout.Count(); if (o <= 1) { value = true; return(value); } } } return(value); }
/// <summary> /// An extension method which adds an external reference to the current document /// </summary> /// <param name="doc">The document which should have the xref added to it</param> /// <param name="xref_fullpath">The path of the xref to be added</param> /// <param name="xref_name">How the name of the xref should appear in the new document</param> /// <param name="layoutname">Space where the xref should be added; can only be Model or PaperSpace</param> public static void Xref_Attach(this Document doc, string xref_fullpath, string xref_name, string layoutname) { Database db = doc.Database; using (DocumentLock doclock = doc.LockDocument()) { using (Transaction tr = db.TransactionManager.StartTransaction()) { if (File.Exists(xref_fullpath)) { ObjectId xrefid = new ObjectId(IntPtr.Zero); global::Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "x-xref"); doc.LayerManagement("Lock", "x-xref", false); try { xrefid = db.AttachXref(xref_fullpath, xref_name); } catch (global::Autodesk.AutoCAD.Runtime.Exception aex) { if (aex.ErrorStatus == ErrorStatus.FileAccessErr) { doc.Editor.WriteMessage("The following drawing is likely a new drawing and cannot be setup"); } } if (!xrefid.IsNull) { Point3d origin_default = new Point3d(0, 0, 0); BlockReference xref_block = new BlockReference(origin_default, xrefid); List <Layout> list_lays = ProjectSetup.Project_ListOfLayouts(doc); if (layoutname.ToUpper().Equals("MODEL")) { ObjectId modelspaceid = (from l in list_lays where l.LayoutName.ToUpper().Equals("MODEL") select l.OwnerId).First(); //BlockTableRecord mspacebtr = tr.GetObject(modelspaceid, OpenMode.ForWrite) as BlockTableRecord; BlockTable blt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord mspacebtr = tr.GetObject(blt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; mspacebtr.AppendEntity(xref_block); tr.AddNewlyCreatedDBObject(xref_block, true); } else { global::Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "x-tblk"); if (list_lays.Any(ln => ln.LayoutName.Equals(layoutname))) { //ObjectId otherspaceid = (from l in list_lays // where l.LayoutName.ToUpper().Equals(layoutname) // select l.OwnerId).First(); BlockTable blt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord otherspacebtr = tr.GetObject(blt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord; otherspacebtr.AppendEntity(xref_block); tr.AddNewlyCreatedDBObject(xref_block, true); } else { LayoutManager laymgr = LayoutManager.Current; ObjectId newlayoutid = laymgr.CreateLayout(layoutname); Layout newlayout = tr.GetObject(newlayoutid, OpenMode.ForRead) as Layout; BlockTableRecord newlayoutbtr = tr.GetObject(newlayout.OwnerId, OpenMode.ForRead) as BlockTableRecord; newlayoutbtr.AppendEntity(xref_block); tr.AddNewlyCreatedDBObject(xref_block, true); } } tr.Commit(); } } } } }