public static MyXrefInformation[] FindXrefs(Database db) { MyXrefInformation[] XrefArray; string[] tempStrArray; int tempCnt; using (Transaction Trans = db.TransactionManager.StartTransaction()) { BlockTable BlkTbl = (BlockTable)Trans.GetObject(db.BlockTableId, OpenMode.ForRead); //db.ResolveXrefs(false, true); XrefGraph XrGph = db.GetHostDwgXrefGraph(false); XrefArray = new MyXrefInformation[XrGph.NumNodes - 1]; for (int i = 1; i < XrGph.NumNodes; ++i) { XrefGraphNode XrNode = XrGph.GetXrefNode(i); BlockTableRecord btr = (BlockTableRecord)Trans.GetObject(XrNode.BlockTableRecordId, OpenMode.ForRead); MyXrefInformation XrInfo = new MyXrefInformation(); XrInfo.Name = XrNode.Name; XrInfo.NewName = XrNode.Name; XrInfo.Path = btr.PathName; XrInfo.NewPath = btr.PathName; XrInfo.DrawingPath = db.Filename; XrInfo.XId = XrNode.BlockTableRecordId; string FoundAt = WillLoad(btr.PathName, db); if (XrNode.XrefStatus == XrefStatus.Unresolved) { if (string.IsNullOrEmpty(FoundAt)) { XrInfo.Status = XrefStatus.Unresolved; } else { XrInfo.Status = XrefStatus.Resolved; } } else { XrInfo.Status = XrNode.XrefStatus; } if (XrInfo.Status == XrefStatus.Resolved) { XrInfo.FoundAtPath = FoundAt; } XrInfo.IsNested = XrNode.IsNested; XrInfo.IsOverlay = btr.IsFromOverlayReference; ObjectIdCollection ObjIdCol = (ObjectIdCollection)btr.GetBlockReferenceIds(true, true); string[] InsertedAtArray = new string[ObjIdCol.Count]; for (int j = 0; j < ObjIdCol.Count; ++j) { ObjectId ObjId = ObjIdCol[j]; BlockReference BlkRef = (BlockReference)Trans.GetObject(ObjId, OpenMode.ForRead); BlockTableRecord tempbtr = (BlockTableRecord)Trans.GetObject(BlkRef.OwnerId, OpenMode.ForRead); if (tempbtr.IsLayout) { Layout templo = (Layout)Trans.GetObject(tempbtr.LayoutId, OpenMode.ForRead); InsertedAtArray[j] = "Layout: " + templo.LayoutName; } else { InsertedAtArray[j] = "Block: " + tempbtr.Name; } } XrInfo.InsertedWhere = InsertedAtArray; if (!XrNode.NumIn.Equals(0)) { tempStrArray = new string[XrNode.NumIn]; tempCnt = 0; for (int j = 0; j < XrNode.NumIn; j++) { int tempInt = FindGraphLocation(XrNode.In(j)); if (tempInt.Equals(-1)) { continue; } tempStrArray[tempCnt] = XrGph.GetXrefNode(tempInt).Name; tempCnt++; } XrInfo.OwnerNames = tempStrArray; } if (!XrNode.NumOut.Equals(0)) { tempStrArray = new string[XrNode.NumOut]; tempCnt = 0; for (int j = 0; j < XrNode.NumOut; j++) { int tempInt = FindGraphLocation(XrNode.Out(j)); if (tempInt.Equals(-1)) { continue; } tempStrArray[tempCnt] = XrGph.GetXrefNode(tempInt).Name; tempCnt++; } XrInfo.ChildrenNames = tempStrArray; } XrefArray[i - 1] = XrInfo; } } return(XrefArray); }
void ListFiles(string[] Files) { RoList = new List <string>(); foreach (string DwgPath in Files) { if ((File.GetAttributes(DwgPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { RoList.Add(DwgPath); continue; } string[] tempStrArray = DwgPath.Split('\\'); tempStrArray = tempStrArray[tempStrArray.Length - 1].Split('.'); ListViewItem lvi = new ListViewItem(tempStrArray[0]); Database Db; DocumentLock tempLock = null; Document OpenDoc = null; OpenDoc = GetDocumentFrom(DocCol, DwgPath); bool DocInEditor = (OpenDoc != null); if (DocInEditor) { Db = OpenDoc.Database; tempLock = OpenDoc.LockDocument(); Db.ResolveXrefs(false, true); } else { Db = new Database(true, false); } try { if (!DocInEditor) { Db.ReadDwgFile(DwgPath, System.IO.FileShare.ReadWrite, true, null); } XrInfo = MyXrefInformation.FindXrefs(Db); lvi.Tag = XrInfo; if (!XrInfo.Equals(0)) { DwgListview.Items.Add(lvi); } } catch (Autodesk.AutoCAD.Runtime.Exception AcadEx) { MessageBox.Show(AcadEx.Message + "\n\n" + DwgPath + "\n\n" + AcadEx.StackTrace, "AutoCAD error."); } catch (System.Exception SysEx) { MessageBox.Show(SysEx.Message + System.Environment.NewLine + System.Environment.NewLine + SysEx.StackTrace, "System error."); } if (DocInEditor) { tempLock.Dispose(); } else { Db.Dispose(); } } StringBuilder Sb = new StringBuilder(); foreach (string str in RoList) { Sb.AppendLine(str); } if (!string.IsNullOrEmpty(Sb.ToString())) { MessageBox.Show(Sb.ToString(), "Read-only files not able to update."); } }