public static void TecRnXref() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction Tx = db.TransactionManager.StartTransaction()) { //ObjectIdCollection btrCol = new ObjectIdCollection(); // look at all Nodes in the XrefGraph. Skip 0 node since it is the drawing itself. XrefGraph xrgraph = db.GetHostDwgXrefGraph(false); for (int i = 1; i < (xrgraph.NumNodes); i++) { XrefGraphNode xrNode = xrgraph.GetXrefNode(i); if (!xrNode.IsNested) { BlockTableRecord btr = (BlockTableRecord)Tx.GetObject(xrNode.BlockTableRecordId, OpenMode.ForWrite); string fileName = System.IO.Path.GetFileNameWithoutExtension(btr.PathName); //ed.WriteMessage(fileName); db.XrefEditEnabled = true; btr.Name = fileName; } } Tx.Commit(); } }
public void checkFile() { string fileName = @"G:\65038\SITE PLOT\520_N_Formosa-PLAN_20-Spanish\5038_PLAN 20 - SP_520_N_Formosa.dwg"; Directory.SetCurrentDirectory(Path.GetDirectoryName(fileName)); Database db = new Database(); db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, ""); XrefGraph xg = db.GetHostDwgXrefGraph(true); GraphNode root = xg.RootNode; List <XrefDetails> xrefList = new List <XrefDetails>(); using (Transaction tr = db.TransactionManager.StartTransaction()) { for (int i = 0; i < root.NumOut; i++) { XrefDetails xrefDetails = new XrefDetails(); XrefGraphNode child = (XrefGraphNode)root.Out(i); xrefDetails.name = child.Name; xrefDetails.isNested = child.IsNested; xrefDetails.status = child.XrefStatus; if (child.XrefStatus != XrefStatus.NotAnXref) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite); xrefDetails.relativePathName = btr.PathName; xrefDetails.fullPathName = Path.GetFullPath(btr.PathName); } xrefList.Add(xrefDetails); } } }
public static void BindXrefs(Database db) { ObjectIdCollection xrefCollection = new ObjectIdCollection(); using (XrefGraph xg = db.GetHostDwgXrefGraph(false)) { int numOfNodes = xg.NumNodes; for (int cnt = 0; cnt < xg.NumNodes; cnt++) { XrefGraphNode xNode = xg.GetXrefNode(cnt) as XrefGraphNode; if (!xNode.Database.Filename.Equals(db.Filename)) { if (xNode.XrefStatus == XrefStatus.Resolved) { xrefCollection.Add(xNode.BlockTableRecordId); } } } } if (xrefCollection.Count != 0) { db.BindXrefs(xrefCollection, true); } }
public override void Run(string filename, Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { try { // List xrefs ObjectIdCollection xrefs = new ObjectIdCollection(); XrefGraph graph = db.GetHostDwgXrefGraph(false); if (!graph.IsEmpty && graph.NumNodes > 1) { for (int i = 0; i < graph.NumNodes; i++) { XrefGraphNode node = graph.GetXrefNode(i); // Skip if cannot read if (node == null) { OnError(new Exception("XREF okunamadı.")); continue; } // Skip if it is the root node (current drawing) if (node == graph.HostDrawing) { continue; } // Skip if nested if (node.IsNested) { continue; } xrefs.Add(node.BlockTableRecordId); } // Strip paths foreach (ObjectId id in xrefs) { BlockTableRecord block = (BlockTableRecord)tr.GetObject(id, OpenMode.ForWrite); if (block.IsFromExternalReference) { block.PathName = StripPath(block.PathName); } } } } catch (System.Exception ex) { OnError(ex); } tr.Commit(); } }
Stream(ArrayList data, XrefGraphNode xGraphNode) { data.Add(new Snoop.Data.ClassSeparator(typeof(XrefGraphNode))); data.Add(new Snoop.Data.String("Name", xGraphNode.Name)); data.Add(new Snoop.Data.ObjectId("Block table record ID", xGraphNode.BlockTableRecordId)); data.Add(new Snoop.Data.Database("Database", xGraphNode.Database)); data.Add(new Snoop.Data.Bool("Is nested", xGraphNode.IsNested)); data.Add(new Snoop.Data.String("Xref status", xGraphNode.XrefStatus.ToString())); data.Add(new Snoop.Data.String("Xref notification status", xGraphNode.XrefNotificationStatus.ToString())); }
public void detach_xref() { Document Doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Doc.Editor; string mainDrawingFile = @"H:\Google Drive\Test\E20 Vårgårda\100T0201.dwg"; Database db = new Database(false, false); using (db) { try { db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, false, ""); } catch (System.Exception) { ed.WriteMessage("\nUnable to read the drawingfile."); return; } bool saveRequired = false; db.ResolveXrefs(true, false); using (Transaction tr = db.TransactionManager.StartTransaction()) { XrefGraph xg = db.GetHostDwgXrefGraph(true); int xrefcount = xg.NumNodes; for (int j = 0; j < xrefcount; j++) { XrefGraphNode xrNode = xg.GetXrefNode(j); String nodeName = xrNode.Name; if (xrNode.XrefStatus == XrefStatus.FileNotFound) { ObjectId detachid = xrNode.BlockTableRecordId; db.DetachXref(detachid); saveRequired = true; ed.WriteMessage("\nDetached successfully"); break; } } tr.Commit(); } if (saveRequired) { db.SaveAs(mainDrawingFile, DwgVersion.Current); } } }
public static List <string> GetXrefsPath(GraphNode node, List <string> files) { for (int i = 0; i < node.NumOut; i++) { XrefGraphNode child = node.Out(i) as XrefGraphNode; if (child != null && child.XrefStatus == XrefStatus.Resolved) { files.Add(GetRealPath(child.Database)); GetXrefsPath(child, files); } } return(files); }
Stream(ArrayList data, GraphNode node) { data.Add(new Snoop.Data.ClassSeparator(typeof(GraphNode))); data.Add(new Snoop.Data.Bool("Is cycle node", node.IsCycleNode)); data.Add(new Snoop.Data.Int("Num cycle in", node.NumCycleIn)); data.Add(new Snoop.Data.Int("Num cycle out", node.NumCycleOut)); data.Add(new Snoop.Data.Int("Num in", node.NumIn)); data.Add(new Snoop.Data.Int("Num out", node.NumOut)); XrefGraphNode xGraphNode = node as XrefGraphNode; if (xGraphNode != null) { Stream(data, xGraphNode); } }
public static XrefNode[] GetNodes(Document doc, QuickTransaction tr = null) { string strnullfy(string s) => string.IsNullOrEmpty(s.Trim()) ? null : s; Database db = doc.Database; Editor ed = doc.Editor; bool tr_new = tr == null; var ret = new List <XrefNode>(); try { tr = tr_new ? new QuickTransaction() : tr; db.ResolveXrefs(true, false); XrefGraph xg = db.GetHostDwgXrefGraph(true); GraphNode root = xg.RootNode; for (int o = 0; o < root.NumOut; o++) { XrefGraphNode child = root.Out(o) as XrefGraphNode; if (child == null) { ed.WriteMessage($"\nUnable to load xref of type {root.Out(o)?.GetType().FullName}"); continue; } //if (child.XrefStatus == XrefStatus.Resolved) { //BlockTableRecord bl = tr.GetObject(child.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord; var t = child.GetType(); try { Quick.Editor.WriteMessage("\n" + child.Database.Filename); } catch { } var status = child.XrefStatus; ret.Add(new XrefNode( strnullfy(child.Name) ?? strnullfy(child.Database?.ProjectName) ?? strnullfy(Path.GetFileNameWithoutExtension(child.Database?.Filename)) ?? "", child.Database.Filename, status == XrefStatus.Resolved || status == XrefStatus.FileNotFound || status == XrefStatus.Unresolved)); } } finally { if (tr_new) { tr?.Dispose(); } } return(ret.ToArray()); }
public string GetXrefPath(Database db) { string result = ""; bool saveRequired = false; Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { XrefGraph xg = db.GetHostDwgXrefGraph(true); int xrefcount = xg.NumNodes; for (int j = 0; j < xrefcount; j++) { XrefGraphNode xrNode = xg.GetXrefNode(j); string nodeName = xrNode.Name; if (xrNode.XrefStatus == XrefStatus.Resolved) { try { BlockTableRecord bl = tr.GetObject(xrNode.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord; if (bl.IsFromExternalReference == true) { ed.WriteMessage("\nXref path name: " + bl.PathName); } saveRequired = true; } catch (System.Exception ex) { } return(nodeName); break; } } tr.Commit(); } //if (saveRequired) // db.SaveAs(db.Filename, DwgVersion.Current); return(result); }
private static void GetAllXrefNames(GraphNode i_root, List <string> list, Transaction i_Tx) { for (int o = 0; o < i_root.NumOut; o++) { XrefGraphNode child = i_root.Out(o) as XrefGraphNode; if (child.XrefStatus == XrefStatus.Resolved) { BlockTableRecord bl = i_Tx.GetObject(child.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord; list.Add(child.Database.Filename); // Name of the Xref (found name) // You can find the original path too: //if (bl.IsFromExternalReference == true) // i_ed.WriteMessage("\n" + i_indent + "Xref path name: " // + bl.PathName); GetAllXrefNames(child, list, i_Tx); } } }
private List <CrawlDocument> GetXrefs(Document aDoc) { //http://adndevblog.typepad.com/autocad/2012/06/finding-all-xrefs-in-the-current-database-using-cnet.html XrefGraph xGraph = aDoc.Database.GetHostDwgXrefGraph(false); int numXrefs = xGraph.NumNodes; List <CrawlDocument> result = new List <CrawlDocument>(); for (int i = 0; i < numXrefs; i++) { XrefGraphNode xrefNode = xGraph.GetXrefNode(i); if (xrefNode.XrefStatus == XrefStatus.Resolved) { //Document theDoc = TeighaApp.DocumentManager.GetDocument(xrefNode.Database); CrawlDocument acDoc = new CrawlDocument(xrefNode.Database.Filename); result.Add(acDoc); } } return(result); }
public void RelXref() { Document doc = acad.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Transaction tr = doc.TransactionManager.StartTransaction(); try { using (tr) { ObjectIdCollection btrCol = new ObjectIdCollection(); XrefGraph xrgraph = doc.Database.GetHostDwgXrefGraph(false); // look at all Nodes in the XrefGraph. Skip 0 node since it is the drawing itself. for (int i = 1; i < (xrgraph.NumNodes - 1); i++) { XrefGraphNode xrNode = xrgraph.GetXrefNode(i); if (!xrNode.IsNested) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject (xrNode.BlockTableRecordId, OpenMode.ForWrite); string origPath = btr.PathName; string relativePath = AbstoRel(origPath); ed.WriteMessage("\n Xref: " + btr.Name + " has path " + origPath + " or relative path " + relativePath); db.XrefEditEnabled = true; btr.PathName = relativePath; } } tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(ex.ToString()); } }
public override void Run(string filename, Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { try { // List xrefs ObjectIdCollection xrefs = new ObjectIdCollection(); XrefGraph graph = db.GetHostDwgXrefGraph(false); if (!graph.IsEmpty && graph.NumNodes > 1) { for (int i = 0; i < graph.NumNodes; i++) { XrefGraphNode node = graph.GetXrefNode(i); // Skip if cannot read if (node == null) { OnError(new Exception("XREF okunamadı.")); continue; } // Skip if it is the root node (current drawing) if (node == graph.HostDrawing) { continue; } // Skip if nested if (node.IsNested) { continue; } xrefs.Add(node.BlockTableRecordId); } // Rename foreach (ObjectId id in xrefs) { BlockTableRecord block = (BlockTableRecord)tr.GetObject(id, OpenMode.ForWrite); if (block.IsFromExternalReference) { foreach (FindReplaceOptions opt in options) { if (string.Compare(block.Name, opt.Find, StringComparison.CurrentCultureIgnoreCase) == 0) { block.Name = opt.Replace; } if (renamePaths && Regex.IsMatch(block.PathName, Regex.Escape(opt.Find + ".dwg"), RegexOptions.IgnoreCase)) { block.PathName = Regex.Replace(block.PathName, Regex.Escape(opt.Find + ".dwg"), opt.Replace + ".dwg", RegexOptions.IgnoreCase); } } } } } } catch (Exception ex) { OnError(ex); } tr.Commit(); } }
public void detachXref() { var DrawingList = new List <string>(); using (var _form = new AddXrefForm()) { _form.SetTab(2); var result = _form.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { DrawingList = _form.DetachXrefDrawingList; } if (result == System.Windows.Forms.DialogResult.None) { return; } } //Get the document Document Doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Doc.Editor; foreach (var drawing in DrawingList) { //create a database and try to load the file Database db = new Database(false, true); using (db) { try { db.ReadDwgFile(drawing, System.IO.FileShare.ReadWrite, false, ""); } catch (System.Exception) { ed.WriteMessage("\nUnable to read the drawingfile."); return; } db.ResolveXrefs(true, false); using (Transaction tr = db.TransactionManager.StartTransaction()) { XrefGraph xg = db.GetHostDwgXrefGraph(true); int xrefcount = xg.NumNodes; for (int j = 0; j < xrefcount; j++) { XrefGraphNode xrNode = xg.GetXrefNode(j); String nodeName = xrNode.Name; if (xrNode.XrefStatus == XrefStatus.FileNotFound) { ObjectId detachid = xrNode.BlockTableRecordId; db.DetachXref(detachid); ed.WriteMessage("\nDetached successfully\n"); break; } } tr.Commit(); } // Overwrite the current drawing file with new updated XRef paths try { db.SaveAs(drawing, false, DwgVersion.Current, null); } catch (System.Exception) { continue; } } } Doc.Editor.WriteMessage("Xref is detached"); }
public void unloadXref() { var DrawingList = new List <string>(); using (var _form = new AddXrefForm()) { _form.SetTab(1); var result = _form.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { DrawingList = _form.UnloadXrefDrawingList; } if (result == System.Windows.Forms.DialogResult.None) { return; } } //Get the document Document Doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Doc.Editor; foreach (var drawing in DrawingList) { //create a database and try to load the file Database db = new Database(false, true); using (db) { try { db.ReadDwgFile(drawing, FileShare.ReadWrite, false, ""); } catch (System.Exception) { ed.WriteMessage("\nUnable to read the drawingfile."); continue; } using (Transaction tr = db.TransactionManager.StartTransaction()) { XrefGraph xg = db.GetHostDwgXrefGraph(true); int xrefcount = xg.NumNodes - 1; if (xrefcount != 0) { ObjectIdCollection XrefColl = new ObjectIdCollection(); for (int r = 1; r < (xrefcount + 1); r++) { XrefGraphNode xrefNode = xg.GetXrefNode(r); ObjectId xrefId = xrefNode.BlockTableRecordId; XrefColl.Add(xrefId); } db.UnloadXrefs(XrefColl); tr.Commit(); } } // Overwrite the current drawing file with new updated XRef paths db.SaveAs(drawing, false, DwgVersion.Current, null); } } Doc.Editor.WriteMessage("Xrefs unloaded\n"); }
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); }
private void button2_Detach_Click(object sender, EventArgs e) { DocCol = AcadApp.DocumentManager; //XrInfoList = new List<MyXrefInformation>(); int index = DwgListview.SelectedIndices[0]; //List<ListViewItem> LocList; foreach (ListViewItem lvi in DwgListview.SelectedItems) { using (DocumentLock DocLock = DocCol.MdiActiveDocument.LockDocument()) { foreach (MyXrefInformation xrInfoAr in lvi.Tag as MyXrefInformation[]) { string DwgName = xrInfoAr.DrawingPath; Database Db; Document OpenDoc = null; DocumentLock tempLock = null; //bool ShouldSave = false; OpenDoc = GetDocumentFrom(DocCol, DwgName); bool DocInEditor = (OpenDoc != null); if (DocInEditor) { Db = OpenDoc.Database; tempLock = OpenDoc.LockDocument(); } else { Db = new Database(true, false); } using (Transaction Trans = Db.TransactionManager.StartTransaction()) { try { foreach (ListViewItem xr in XrefListview.SelectedItems) { if (xrInfoAr.Name == xr.Text) // !!! error compare filename from XrefListview_Listbox wrong !!! { if (!DocInEditor) { Db.ReadDwgFile(DwgName, System.IO.FileShare.ReadWrite, true, null); } //BlockTable BlkTbl = Trans.GetObject(Db.BlockTableId, OpenMode.ForRead) as BlockTable; XrefGraph XrGraph = Db.GetHostDwgXrefGraph(true); XrefGraphNode XrNode = XrGraph.GetXrefNode(xr.Text); for (int i = 1; i < XrGraph.NumNodes; i++) { //XrefGraphNode tempNode = XrGraph.GetXrefNode(i); //XrNode = tempNode; ObjectId detachid = XrNode.BlockTableRecordId; Db.DetachXref(detachid); Db.SaveAs(DwgName, DwgVersion.Current); xr.Remove(); ListFile(DwgName); break; } } } } catch (Autodesk.AutoCAD.Runtime.Exception AcadEx) { MessageBox.Show(AcadEx.Message + "\n\n" + AcadEx.StackTrace + "\n\n" + DwgName, "AutoCAD error."); } catch (System.Exception SysEx) { MessageBox.Show(SysEx.Message, "System error."); } Trans.Commit(); } if (DocInEditor) { tempLock.Dispose(); } else { Db.Dispose(); } } } } }
public override void Run(string filename, Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { try { // Resolve XREFs if (resolveXREFs) { db.ResolveXrefs(true, false); } // List xrefs ObjectIdCollection xrefs = new ObjectIdCollection(); XrefGraph graph = db.GetHostDwgXrefGraph(false); if (!graph.IsEmpty && graph.NumNodes > 1) { for (int i = 0; i < graph.NumNodes; i++) { XrefGraphNode node = graph.GetXrefNode(i); // Skip if cannot read if (node == null) { OnError(new Exception("XREF okunamadı.")); continue; } // Skip if it is the root node (current drawing) if (node == graph.HostDrawing) { continue; } // Skip if nested if (node.IsNested) { continue; } // Skip if XREF is not resolved if (node.XrefStatus != XrefStatus.Resolved) { OnError(new Exception("XREF bulunamadı: " + node.Name)); continue; } xrefs.Add(node.BlockTableRecordId); } // Bind if (xrefs.Count != 0) { db.BindXrefs(xrefs, insertMode); } } } catch (Exception ex) { OnError(ex); } tr.Commit(); } }