public static void openxref_readonly() { Document acDoc = acApp.DocumentManager.MdiActiveDocument; Database acDb = acDoc.Database; Editor acEd = acDoc.Editor; PromptSelectionResult psr = acEd.GetSelection(); if (psr.Status != PromptStatus.OK) { return; } using (Transaction acTr = acDb.TransactionManager.StartTransaction()) { foreach (ObjectId id in psr.Value.GetObjectIds()) { BlockReference acBr = (BlockReference)acTr.GetObject(id, OpenMode.ForRead); BlockTableRecord acBtr = (BlockTableRecord)acTr.GetObject(acBr.BlockTableRecord, OpenMode.ForRead); if (acBtr.IsFromExternalReference) { Document doc = acApp.DocumentManager.Open(acBtr.GetXrefDatabase(true).Filename, true); if (doc != null) { acApp.DocumentManager.MdiActiveDocument = doc; } } else { acEd.WriteMessage("Selection must be external reference"); } } } }
Stream(ArrayList data, BlockTableRecord rec) { data.Add(new Snoop.Data.ClassSeparator(typeof(BlockTableRecord))); data.Add(new Snoop.Data.String("Path name", rec.PathName)); data.Add(new Snoop.Data.String("Comments", rec.Comments)); data.Add(new Snoop.Data.Point3d("Origin", rec.Origin)); data.Add(new Snoop.Data.ObjectId("Layout ID", rec.LayoutId)); data.Add(new Snoop.Data.Bool("Has attribute definitions", rec.HasAttributeDefinitions)); data.Add(new Snoop.Data.Bool("Has preview icon", rec.HasPreviewIcon)); //if (rec.HasPreviewIcon) // data.Add(new Snoop.Data.Bitmap("Preview icon", rec.PreviewIcon)); // TBD: throws exception, Not Implemented! data.Add(new Snoop.Data.String("Block scaling", rec.BlockScaling.ToString())); data.Add(new Snoop.Data.Bool("Explodable", rec.Explodable)); data.Add(new Snoop.Data.String("Units", rec.Units.ToString())); data.Add(new Snoop.Data.Bool("Is anonymous", rec.IsAnonymous)); data.Add(new Snoop.Data.Bool("Is dynamic block", rec.IsDynamicBlock)); data.Add(new Snoop.Data.Bool("Is from external reference", rec.IsFromExternalReference)); data.Add(new Snoop.Data.Bool("Is from overlay reference", rec.IsFromOverlayReference)); data.Add(new Snoop.Data.Bool("Is layout", rec.IsLayout)); data.Add(new Snoop.Data.Bool("Is unloaded", rec.IsUnloaded)); data.Add(new Snoop.Data.ObjectId("Draw order table ID", rec.DrawOrderTableId)); data.Add(new Snoop.Data.ObjectId("BlockBegin ID", rec.BlockBeginId)); data.Add(new Snoop.Data.ObjectId("BlockEnd ID", rec.BlockEndId)); data.Add(new Snoop.Data.ObjectIdCollection("Entities within block", MgdDbg.Utils.SymTbl.CollectBlockEnts(rec))); data.Add(new Snoop.Data.ObjectIdCollection("Block reference IDs (directOnly = true)", rec.GetBlockReferenceIds(true, false))); data.Add(new Snoop.Data.ObjectIdCollection("Block reference IDs (directOnly = false)", rec.GetBlockReferenceIds(false, false))); data.Add(new Snoop.Data.ObjectIdCollection("Block reference IDs (Erased)", rec.GetErasedBlockReferenceIds())); data.Add(new Snoop.Data.String("Xref status", rec.XrefStatus.ToString())); data.Add(new Snoop.Data.Database("Xref database", rec.GetXrefDatabase(true))); }
AttachXref() { m_db = Utils.Db.GetCurDwg(); if (m_db == null) { return; } string fileName = Utils.Dialog.OpenFileDialog(".dwg", "Choose dwg to attach as Xref", "Dwg files (*.dwg)|*.dwg"); if (fileName.Length == 0) { return; } string blockName = System.IO.Path.GetFileNameWithoutExtension(fileName); if (blockName.Length == 0) { return; } if (blockName.ToLower() == System.IO.Path.GetFileNameWithoutExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name).ToLower()) { MessageBox.Show("External file name used could lead to a circular reference.", "MgdDbg", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ObjectId objId = m_db.AttachXref(fileName, blockName); using (TransactionHelper trHlp = new TransactionHelper(m_db)) { trHlp.Start(); BlockTableRecord btr = (BlockTableRecord)trHlp.Transaction.GetObject(objId, OpenMode.ForRead); Database xrefDb = btr.GetXrefDatabase(true); BlockTable blkTbl = (BlockTable)trHlp.Transaction.GetObject(m_db.BlockTableId, OpenMode.ForRead); ObjectIdCollection blkRecIds = new ObjectIdCollection(); blkRecIds.Add(btr.ObjectId); foreach (ObjectId tblRecId in blkTbl) { BlockTableRecord blkRec = (BlockTableRecord)trHlp.Transaction.GetObject(tblRecId, OpenMode.ForRead); if (blkRec.Database == xrefDb) { blkRecIds.Add(tblRecId); } } Snoop.Forms.DBObjects form = new Snoop.Forms.DBObjects(blkRecIds, trHlp); form.Text = "Xref Block Table Records"; form.ShowDialog(); trHlp.Commit(); } }
getXRefDatabase(string name) { try { using (Transaction tr = BaseObjs.startTransactionDb()) { SelectionSet SS = Base_Tools45.Select.buildSSet(typeof(BlockReference), true); if (SS == null) { return(null); } foreach (SelectedObject ssObj in SS) { BlockReference BR = (BlockReference)ssObj.ObjectId.GetObject(OpenMode.ForRead); if (BR != null) { ObjectId idBtr = BR.BlockTableRecord; BlockTableRecord Btr = (BlockTableRecord)idBtr.GetObject(OpenMode.ForRead); if (Btr != null) { if (Btr.IsFromExternalReference || Btr.IsFromOverlayReference) { var db = Btr.GetXrefDatabase(false); if (db.Filename == name) { return(db); } //if (Btr.PathName == name || Btr.Name.Contains(name)) // return Btr; } } } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRef.cs: line: 1013"); } return(null); }
public static void ExportExternalReference(BlockTableRecord xref, string pathToExportTo) { try { var xrefResolved = CheckExternalReferenceResolution(xref); if (!xrefResolved) { throw new Autodesk.AutoCAD.Runtime.Exception(); } using (var tr = CurrentDatabase.TransactionManager.StartTransaction()) { using (var xrefDb = xref.GetXrefDatabase(true)) { CurrentDatabase.WblockCloneObjects( new ObjectIdCollection(), xref.Id, new IdMapping(), DuplicateRecordCloning.Ignore, false); tr.Commit(); SaveExternalReferenceToFileSystem(xrefDb, pathToExportTo); } } } catch (Autodesk.AutoCAD.Runtime.Exception e) { if (e.ErrorStatus == ErrorStatus.InvalidKey) { CurrentDocument.Editor.WriteMessage("\n" + $"{xref.Name} was not found, please ask the client to send it"); } } catch (Exception e) { } }
/// <summary> /// Exports a BlockTableRecord out to another location using a WBlock methodology /// </summary> /// <param name="blockTableRecord"> The External Reference to be exported; must be a Block Table Record </param> public static void ExportExternalReferenceToFile(BlockTableRecord blockTableRecord) { // if the file has already been exported // OR the ExternalReference can't be resolved - fail early string newXrefPath = ExternalReferenceFolder + blockTableRecord.Name.ToUpper() + ".dwg"; if (File.Exists(newXrefPath) || !CanExternalReferenceBeResolved(blockTableRecord)) { return; } using (Transaction transaction = Database.TransactionManager.StartTransaction()) { using (Database xrefDatabase = blockTableRecord.GetXrefDatabase(true)) { Database.WblockCloneObjects(new ObjectIdCollection(), blockTableRecord.ObjectId, new IdMapping(), DuplicateRecordCloning.Ignore, false); transaction.Commit(); // save the Xref to the new directory xrefDatabase.SaveAs(newXrefPath, DwgVersion.AC1021); } } }
/// <summary> /// Exports a BlockTableRecord out to another location using a WBlock methodology /// </summary> /// <param name="btr"> The External Reference to be exported; must be a Block Table Record </param> public static void Xref_Exporter(BlockTableRecord btr) { //Access the current document Document Cdoc = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database Adbase = Cdoc.Database; //***Gets the current directory by finding the file path string FilePath = Path.GetDirectoryName(Adbase.Filename); //Creates a new directory for all files to be saved to //called "_Setup Files" and "Xrefs" string NewFol = "_Setup Files"; string NewSetup = "SETUP"; string NewXrPath = null; string XrPath = btr.PathName; string XrOrigin = btr.Origin.ToString(); string XrName = btr.Name.ToString().ToUpper(); Setup_Methods.Directory_Creator(FilePath, NewFol, NewSetup); //if the Xref doesn't already exist in a xref/setup folder export it there if (!File.Exists(Path.Combine(FilePath, NewFol, NewSetup, XrName))) { NewXrPath = Path.Combine(FilePath, NewFol, NewSetup); NewXrPath = Path.Combine(NewXrPath, XrName); try { // if the xref in question can't be found in CAD // i.e. because the xref is set to Absolute instead of Relative // or because the xref is in another place for some reason // search in all directories and try to resolve the xref if (!btr.IsResolved) { Adbase.ResolveXrefs(true, false); } //if the xref has been found if (btr.IsResolved) { //writes the xref out to a base location with its same name using (Transaction tr = Adbase.TransactionManager.StartTransaction()) { using (Database newDb = btr.GetXrefDatabase(true)) { ObjectIdCollection idCol = new ObjectIdCollection(); IdMapping idMap = new IdMapping(); Adbase.WblockCloneObjects(idCol, btr.ObjectId, idMap, DuplicateRecordCloning.Ignore, false); tr.Commit(); if (!Directory.Exists(NewXrPath)) { Directory.CreateDirectory(Path.GetDirectoryName(NewXrPath)); } //adds an .dwg to the end of the path if there isn't any if (!Path.HasExtension(NewXrPath)) { NewXrPath += ".dwg"; } newDb.SaveAs(NewXrPath, DwgVersion.AC1021); } } } else { // Write code to export the name of the file that is necessary out to the Xrefs area // in a txt file preferably Editor ed = Cdoc.Editor; ed.WriteMessage("\n {0} was not found, ask the client to send it.", btr.Name); } } catch (global::Autodesk.AutoCAD.Runtime.Exception aex) { if (aex.ErrorStatus == ErrorStatus.InvalidKey) { // Write code to export the name of the file that is necessary out to the Xrefs area // in a txt file preferably Editor ed = Cdoc.Editor; ed.WriteMessage("\n {0} was not found, ask the client to send it.", btr.Name); } } } }