/// <summary> /// Wrapper for the active Document's ModelSpace. /// Makes the ModelSpace writable and /// locks the document before starting a transaction! /// </summary> /// <param name="doc"></param> /// <param name="commandName">Name of action the TransactionManager is executing.</param> /// <param name="action"></param> /// <exception cref="AcHelper.Exceptions.TransactionException"/> public static void UsingModelSpace(Document doc, string commandName, Action <AcTransaction, BlockTableRecord> action) { commandName = commandName == "" ? "Acad_Transaction" : commandName; try { using (DocumentLock doclock = doc.LockDocument(DocumentLockMode.Write, commandName, commandName, true)) { doc.TransactionManager.EnableGraphicsFlush(true); using (AcTransaction tr = new AcTransaction(doc)) { var modelspace = tr.ModelSpace; // Modelspace using (WriteEnabler we = new WriteEnabler(doc, modelspace)) { if (modelspace.IsWriteEnabled) { action(tr, modelspace); } } } } } catch (Exception) { throw; } }
internal static void AddTextDataToEntity(string xKey, string text) { Document doc = Active.Document; Editor ed = Active.Editor; PromptEntityOptions opt = new PromptEntityOptions("\nSelect an entity to add xRecord"); doc.UsingModelSpace((tr, ms) => { Transaction t = tr.Transaction; ResultBuffer resbuf = new ResultBuffer(); resbuf.Add(new TypedValue((int)DxfCode.Text, text)); PromptEntityResult res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) { ObjectId oid = res.ObjectId; Entity ent = t.GetObject <Entity>(oid, OpenMode.ForRead); if (ent != null) { using (WriteEnabler we = new WriteEnabler(ent)) { if (ent.IsWriteEnabled) { if (ent.ExtensionDictionary == null) { ent.CreateExtensionDictionary(); } using (DBDictionary nod = t.GetObject <DBDictionary>(ent.ExtensionDictionary, OpenMode.ForRead)) { using (WriteEnabler nodWe = new WriteEnabler(nod)) { if (nod.IsWriteEnabled) { using (Xrecord xRec = new Xrecord()) { xRec.Data = resbuf; nod.SetAt(xKey, xRec); t.AddNewlyCreatedDBObject(xRec, true); } } } } } } } } }); }