public void ReadMapLayer(Transaction t, ObjectId lyrDictionaryID) { DBDictionary dBDictionary = (DBDictionary)t.GetObject(lyrDictionaryID, 0); this.ID = (int)DocUtil.ReadXRecord(t, dBDictionary, (DxfCode)90, "ID"); this.ParentLayerID = (int)DocUtil.ReadXRecord(t, dBDictionary, (DxfCode)90, "ParentID"); this.Visible = false; if (dBDictionary.Contains("MapLayerName")) { Xrecord xrecord = (Xrecord)t.GetObject(dBDictionary.GetAt("MapLayerName"), 0); TypedValue[] array = xrecord.Data.AsArray(); for (int i = 0; i < array.Length; i++) { TypedValue typedValue = array[i]; if (typedValue.TypeCode == 1) { this.Name = typedValue.Value.ToString(); break; } } } if (dBDictionary.Contains("Visible")) { Xrecord xrecord2 = (Xrecord)t.GetObject(dBDictionary.GetAt("Visible"), 0); TypedValue[] array2 = xrecord2.Data.AsArray(); for (int j = 0; j < array2.Length; j++) { TypedValue typedValue2 = array2[j]; if (typedValue2.TypeCode == 290) { this.Visible = (0 != Convert.ToInt16(typedValue2.Value)); break; } } } this.Layers = new List <MSCMapLayer>(); if (dBDictionary.Contains("ChildLayers")) { DBDictionary dBDictionary2 = (DBDictionary)t.GetObject(dBDictionary.GetAt("ChildLayers"), 0); using (DbDictionaryEnumerator enumerator = dBDictionary2.GetEnumerator()) { while (enumerator.MoveNext()) { DBDictionaryEntry current = enumerator.Current; MSCMapLayer item = new MSCMapLayer(t, current.Key, current.Value); this.Layers.Add(item); } } } }
getXRec(ObjectId idDict, string nameApp) { ResultBuffer RB = null; try { using (Transaction tr = BaseObjs.startTransactionDb()) { DBDictionary dict = (DBDictionary)idDict.GetObject(OpenMode.ForRead); try { if (dict.Contains(nameApp)) { ObjectId idDictX = dict.GetAt(nameApp); DBObject dbObj = idDictX.GetObject(OpenMode.ForRead); Xrecord xrec = (Xrecord)dbObj; if (xrec != null) { RB = xrec.Data; } } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 534"); } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 541"); } return(RB); }
/// <summary> /// Removes Xrecord with given key. /// </summary> /// <param name="nod">Named Object Dictionary containing the Xrecord.</param> /// <param name="xKey">Key of the Xrecord.</param> /// <param name="dictionaryName">If not Xrecord of Entity, the Named Object Dictionary has a name.</param> /// <returns>True if succeed, False if Xrecord does not exist.</returns> private bool RemoveXrecord(DBDictionary nod, string xKey, string dictionaryName = "") { string err_message; dictionaryName = dictionaryName == "" ? "\b" : dictionaryName; // Entity Nod has no name. if (nod != null) { if (!nod.Contains(xKey)) { return(false); } using (new WriteEnabler(_document, nod)) { if (nod.IsWriteEnabled) { nod.Remove(xKey); return(true); } else { err_message = string.Format("Could not open Named Objects Dictionary '{0}' for write", dictionaryName); throw new XRecordHandlerException(dictionaryName, xKey, err_message); } } } else { err_message = string.Format("Could not get Named Objects Dictionary '{0}'", dictionaryName); throw new XRecordHandlerException(dictionaryName, xKey, err_message); } }
public void CleanAllPoly() { Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; using (Transaction transaction = acCurDb.TransactionManager.StartTransaction()) { PromptSelectionResult prompt = acDoc.Editor.SelectAll(); if (prompt.Status != PromptStatus.OK) { return; } SelectionSet selectionSet = prompt.Value; foreach (SelectedObject obj in selectionSet) { DBObject databaseObject = transaction.GetObject(obj.ObjectId, OpenMode.ForRead); if (!databaseObject.ExtensionDictionary.IsNull) { ObjectId extId = databaseObject.ExtensionDictionary; DBDictionary dbExt = (DBDictionary)transaction.GetObject(extId, OpenMode.ForRead); if (dbExt.Contains("CustomProp")) { dbExt.UpgradeOpen(); dbExt.Remove("CustomProp"); } } } transaction.Commit(); } }
/// <summary> /// 设置默认钢束总体参数,即创建新的DA_Tendons有名对象字典项 /// 如已有总体参数字典项则不做修改 /// </summary> /// <param name="db">图形数据库</param> public static void SetDefaultOverallParams(this Database db) { using (Transaction trans = db.TransactionManager.StartTransaction())//开始事务处理 { // 获取当前数据库的有名对象字典 DBDictionary dicts = db.NamedObjectsDictionaryId.GetObject(OpenMode.ForWrite) as DBDictionary; if (!dicts.Contains("DA_Tendons")) //如果字典中不含DA_Tendons的字典项 { ObjectId tdsDictNewId = db.AddNamedDictionary("DA_Tendons"); //则添加该字典项 //管道偏差系数 TypedValueList values = new TypedValueList(); values.Add(DxfCode.Real, 0.0015); tdsDictNewId.AddXrecord2DBDict("kii", values); //摩阻系数 values = new TypedValueList(); values.Add(DxfCode.Real, 0.16); tdsDictNewId.AddXrecord2DBDict("miu", values); //钢束弹性模量 values = new TypedValueList(); values.Add(DxfCode.Real, 1.95E5); tdsDictNewId.AddXrecord2DBDict("Ep", values); //张拉控制应力 values = new TypedValueList(); values.Add(DxfCode.Real, 1395); tdsDictNewId.AddXrecord2DBDict("ctrlStress", values); //张拉端工作长度 values = new TypedValueList(); values.Add(DxfCode.Real, 800); tdsDictNewId.AddXrecord2DBDict("workLen", values); } } }
public static string ReadWKT(Database db) { string text = ""; string result; using (Transaction transaction = db.TransactionManager.StartTransaction()) { DBDictionary dBDictionary = (DBDictionary)transaction.GetObject(db.NamedObjectsDictionaryId, 0, false); if (dBDictionary.Contains("ESRI_PRJ")) { dBDictionary.GetAt("ESRI_PRJ"); Xrecord xrecord = (Xrecord)transaction.GetObject(dBDictionary.GetAt("ESRI_PRJ"), 0); ResultBuffer data = xrecord.Data; if (data != null) { TypedValue[] array = data.AsArray(); text = array[0].Value.ToString(); } } else { text = ""; } transaction.Commit(); result = text; } return(result); }
/// <summary> /// 添加扩展记录 /// </summary> /// <param name="id">对象的Id</param> /// <param name="searchKey">扩展记录名称</param> /// <param name="values">扩展记录的内容</param> /// <returns>返回添加的扩展记录的Id</returns> public static ObjectId AddXrecord(this ObjectId id, string searchKey, TypedValueList values) { DBObject obj = id.GetObject(OpenMode.ForRead);//打开对象 // 判断对象是否已经拥有扩展字典,若无扩展字典,则 if (obj.ExtensionDictionary.IsNull) { obj.UpgradeOpen(); // 切换对象为写的状态 obj.CreateExtensionDictionary(); // 为对象创建扩展字典 obj.DowngradeOpen(); // 为了安全,将对象切换成读的状态 } // 打开对象的扩展字典 DBDictionary dict = (DBDictionary)obj.ExtensionDictionary.GetObject(OpenMode.ForRead); // 如果扩展字典中已包含指定的扩展记录对象,则返回 if (dict.Contains(searchKey)) { return(ObjectId.Null); } Xrecord xrec = new Xrecord(); // 为对象新建一个扩展记录 xrec.Data = values; // 指定扩展记录的内容 dict.UpgradeOpen(); // 将扩展字典切换成写的状态 //在扩展字典中加入新建的扩展记录,并指定它的搜索关键字 ObjectId idXrec = dict.SetAt(searchKey, xrec); id.Database.TransactionManager.AddNewlyCreatedDBObject(xrec, true); dict.DowngradeOpen(); // 为了安全,将扩展字典切换成读的状态 return(idXrec); // 返回添加的扩展记录的的Id }
/// <summary> /// <para>Add a group to the current autocad document</para> /// <para>Return: Group</para> /// </summary> /// <param name="name">Group name</param> /// <param name="DbObjs">Object Collection to associate with group</param> /// <param name="autorename"><para>if a group has already the specified name</para> /// <para>This let the function assign an unique generated name</para></param> /// <returns></returns> public ObjectId addGroup(string name, DBObjectCollection DbObjs, bool autorename) { Group grp = new Group(name, true); ObjectIdCollection ids = new ObjectIdCollection(); start_Transaction(); DBDictionary gd = openGroupDictionary(OpenMode.ForWrite); if (gd.Contains(name)) { if (autorename) { name = name + "_" + Guid.NewGuid().ToString(); } else { return(ObjectId.Null); } } gd.SetAt(name, grp); AC_Tr.AddNewlyCreatedDBObject(grp, true); openBlockTables(OpenMode.ForRead, OpenMode.ForWrite); foreach (Entity ent in DbObjs) { ObjectId id = Commit(ent); ids.Add(id); } grp.InsertAt(0, ids); gd.Dispose(); Dispose(); return(grp.ObjectId); }
public static bool DelObjXrecord(ObjectId objId, string xRecordSearchKey, Transaction transaction = null) { if (transaction == null) { DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject obj = objId.GetObject(OpenMode.ForRead); //以读的方式打开对象 ObjectId dictId = obj.ExtensionDictionary; //获取对象的扩展字典id if (dictId.IsNull) { return(false);//若对象没有扩展字典,则返回 } //如果对象有扩展字典,则以读的方式打开 DBDictionary dict = dictId.GetObject(OpenMode.ForRead) as DBDictionary; if (dict.Contains(xRecordSearchKey)) //如果扩展字典中包含指定关键字的扩展记录,则删除; { dict.UpgradeOpen(); //切换为写的状态 dict.Remove(xRecordSearchKey); //删除扩展记录 dict.DowngradeOpen(); //切换为读的状态 } tr.Commit(); } m_DocumentLock.Dispose(); } return(true); }
public void GetTotalNumbers(List <Polyline> list) { Document document = Application.DocumentManager.MdiActiveDocument; Database database = document.Database; for (int i = 0; i < list.Count; i++) { int numfloor = 0; int numbars = 0; using (Transaction transaction = database.TransactionManager.StartTransaction()) { DBObject databaseObject = transaction.GetObject(list[i].ObjectId, OpenMode.ForRead); ObjectId extId = databaseObject.ExtensionDictionary; DBDictionary dbExt = (DBDictionary)transaction.GetObject(extId, OpenMode.ForRead); if (dbExt.Contains("CustomProp")) { ObjectId recID = dbExt.GetAt("CustomProp"); Xrecord readBack = (Xrecord)transaction.GetObject(recID, OpenMode.ForRead); numfloor = int.Parse(readBack.Data.AsArray()[0].Value.ToString()); numbars = int.Parse(readBack.Data.AsArray()[1].Value.ToString()); Properties.Settings.Default.CounterLength += list[i].Length * numfloor; Properties.Settings.Default.CounterPils += numfloor * numbars; } transaction.Commit(); } } }
/// <summary> /// 从其它图形数据库中复制打印设置 /// </summary> /// <param name="destdb">目的图形数据库</param> /// <param name="sourceDb">源图形数据库</param> /// <param name="plotSettingName">打印设置名称</param> public static void CopyPlotSettings(this Database destdb, Database sourceDb, string plotSettingName) { using (Transaction trSource = sourceDb.TransactionManager.StartTransaction()) { using (Transaction trDest = destdb.TransactionManager.StartTransaction()) { //获取源图形数据库的打印设置字典 DBDictionary dict = trSource.GetObject(sourceDb.PlotSettingsDictionaryId, OpenMode.ForRead) as DBDictionary; if (dict != null && dict.Contains(plotSettingName)) { //获取指定名称的打印设置 ObjectId settingsId = dict.GetAt(plotSettingName); PlotSettings settings = trSource.GetObject(settingsId, OpenMode.ForRead) as PlotSettings; //新建一个打印设置对象 PlotSettings newSettings = new PlotSettings(settings.ModelType); newSettings.CopyFrom(settings);//复制打印设置 //将新建的打印设置对象添加到目的数据库的打印设置字典中 newSettings.AddToPlotSettingsDictionary(destdb); trDest.AddNewlyCreatedDBObject(newSettings, true); } trDest.Commit(); } trSource.Commit(); } }
public static void WriteState(Document doc, string key, string state) { // https://adndevblog.typepad.com/autocad/2012/05/how-can-i-store-my-custom-information-in-a-dwg-file.html var db = doc.Database; Xrecord xRecord; using (doc.LockDocument()) { using (Transaction trans = db.TransactionManager.StartTransaction()) { // Find the NOD in the database DBDictionary nod = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite); if (nod.Contains(key)) { ObjectId objectId = nod.GetAt(key); xRecord = (Xrecord)trans.GetObject(objectId, OpenMode.ForWrite); xRecord.Data = state.ToResultBuffer(); } else { xRecord = new Xrecord(); xRecord.Data = state.ToResultBuffer(); nod.SetAt(key, xRecord); trans.AddNewlyCreatedDBObject(xRecord, true); } trans.Commit(); } } }
/// <summary> /// 为图形添加一个新的表格样式 /// </summary> /// <param name="db">数据库对象</param> /// <param name="styleName">表格样式的名称</param> /// <returns>返回表格样式的Id</returns> public static ObjectId AddTableStyle(this Database db, string styleName) { ObjectId styleId; using (Transaction trans = db.TransactionManager.StartTransaction()) { //打开表格样式字典 DBDictionary dict = (DBDictionary)trans.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead); //判断是否存在指定的表格样式 if (dict.Contains(styleName)) { styleId = dict.GetAt(styleName);//如果存在则返回表格样式的Id } else { //新建一个表格样式 TableStyle style = new TableStyle(); dict.UpgradeOpen();//切换表格样式字典为写的状态 //将新的表格样式添加到样式字典并获取其 Id styleId = dict.SetAt(styleName, style); //将新建的表格样式添加到事务处理中 trans.AddNewlyCreatedDBObject(style, true); trans.Commit(); } } return(styleId);//返回表格样式的Id }
removeSubEntry(ObjectId idParent, string entrySub) { try { using (Transaction tr = BaseObjs.startTransactionDb()) { DBDictionary parent = (DBDictionary)idParent.GetObject(OpenMode.ForRead); if (parent.Contains(entrySub)) { parent.UpgradeOpen(); try { parent.Remove(entrySub); } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 688"); } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 696"); } }
//this could be altered to use the typedvalue and retrieve the type from reversing the dxfcode lookup, but we would still need a generic return for the calling function public static T Get <T>(string key) { try { if (World.Docu != null) { if (World.Docu.IsActive == true && World.Docu.IsDisposed == false) { using (World.Docu.LockDocument()) { using (Database db = World.Docu.Database) { using (Transaction tr = db.TransactionManager.StartTransaction()) { using (DBDictionary dict = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false)) { if (dict.Contains(key)) { Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead); ResultBuffer resBuf = xRec.Data; if (resBuf == null) { return(default(T)); } TypedValue[] typ = resBuf.AsArray(); return((T)Convert.ChangeType(typ[0].Value, typeof(T))); } } } } } } } } catch (System.Exception ex) { Err.Log(ex); } return(default(T)); }
public static ResultBuffer ReadX(ObjectId objId, string xRecordSearchKey) { ResultBuffer resBuf = new ResultBuffer(); Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject obj = objId.GetObject(OpenMode.ForRead); //以读的方式打开对象 ObjectId dictId = obj.ExtensionDictionary; //获取对象的扩展字典的id if (!dictId.IsNull) { DBDictionary dict = dictId.GetObject(OpenMode.ForRead) as DBDictionary;//获取对象的扩展字典 if (!dict.Contains(xRecordSearchKey)) { tr.Commit(); m_DocumentLock.Dispose(); return(null);//如果扩展字典中没有包含指定关键 字的扩展记录,则返回null; } //先要获取对象的扩展字典或图形中的有名对象字典,然后才能在字典中获取要查询的扩展记录 ObjectId xrecordId = dict.GetAt(xRecordSearchKey); //获取扩展记录对象的id Xrecord xrecord = xrecordId.GetObject(OpenMode.ForRead) as Xrecord; //根据id获取扩展记录对象 resBuf = xrecord.Data; } tr.Commit(); } m_DocumentLock.Dispose(); return(resBuf); }
public static void Delete(ObjectId id, string key) { try { if (id.IsErased == false && id.IsEffectivelyErased == false) { using (World.Docu.LockDocument()) { using (Database db = World.Docu.Database) { using (Transaction tr = db.TransactionManager.StartTransaction()) { using (DBDictionary dict = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite, false)) { if (dict.Contains(key)) { ObjectId deleteId = dict.GetAt(key); Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForWrite, false); xRec.Erase(); dict.Remove(deleteId); } } tr.Commit(); } } } } } catch (System.Exception ex) { Err.Log(ex); } }
private DBDictionary GetDict(Transaction tr) { DBDictionary dict = null; DBDictionary db_dict = tr.GetObject(AcadFuncs.GetActiveDb().NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary; if (null == db_dict) { tr.Dispose(); return(null); } if (!db_dict.Contains(DICT_NAME)) { dict = new DBDictionary(); dict.TreatElementsAsHard = true; db_dict.UpgradeOpen(); db_dict.SetAt(DICT_NAME, dict); tr.AddNewlyCreatedDBObject(dict, true); db_dict.DowngradeOpen(); } else { dict = tr.GetObject((ObjectId)db_dict[DICT_NAME], OpenMode.ForRead) as DBDictionary; } return(dict); }
public static void CreateGroup(string groupName, ObjectIdCollection ids) { //Document doc =Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; //Editor ed = doc.Editor; Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // Get the group dictionary from the drawing DBDictionary gd = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead); // Check the group name, to see whether it's // A variable for the group's name string grpName = ""; try { // Validate the provided symbol table name SymbolUtilityServices.ValidateSymbolName(groupName, false); // Only set the block name if it isn't in use if (gd.Contains(groupName)) { //ed.WriteMessage("\nA group with this name already exists."); //throw new System.Exception("A group with this name already exists."); AddIds2Group(groupName, ids, tr); return; } else { grpName = groupName; } } catch { // An exception has been thrown, indicating the // name is invalid //ed.WriteMessage("\nInvalid group name."); } // Create our new group... Group grp = new Group("Test group", true); // Add the new group to the dictionary gd.UpgradeOpen(); ObjectId grpId = gd.SetAt(grpName, grp); tr.AddNewlyCreatedDBObject(grp, true); //grp.InsertAt(0, ids); AddIds2Group(grp, ids); // Commit the transaction tr.Commit(); // Report what we've done //ed.WriteMessage("\nCreated group named \"{0}\" containing entities.",grpName); } }
/// <summary> /// Adds a new DBDictionary to the nod Collection of the drawing. /// </summary> /// <param name="transaction">Running transaction.</param> /// <param name="name">Name of the Dictionary to add to the collection.</param> public static DBDictionary AddNamedObjectsDictionary(Transaction transaction, string name) { Database db = Active.Database; DBDictionary newNod = null; DBDictionary nod_collection = GetNodCollection(transaction); if (!nod_collection.Contains(name)) { using (new WriteEnabler(nod_collection)) { if (nod_collection.IsWriteEnabled) { newNod = new DBDictionary(); nod_collection.SetAt(name, newNod); transaction.AddNewlyCreatedDBObject(newNod, true); } else { string err_message = string.Format("DBDictionary named: '{0}' could not be created.", name); throw new XRecordHandlerException(err_message); } } } return(newNod); }
/// <summary> /// Actualizamos la ruta de las imagenes del tablero /// </summary> /// <param name="tr">La transacción activa.</param> /// <param name="doc">El documento a cargar.</param> /// <param name="tablero">El nombre del bloque.</param> public static void UpdatePath(this Transaction tr, Document doc, string tablero) { BlockTable blk = doc.Database.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable; if (blk.Has(tablero)) { BlockTableRecord tabRec = blk[tablero].GetObject(OpenMode.ForRead) as BlockTableRecord; DBObject obj; foreach (ObjectId objId in tabRec) { obj = objId.GetObject(OpenMode.ForRead); if (obj is RasterImage) { obj.UpgradeOpen(); RasterImage img = obj as RasterImage; String appData = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); String imgFile = Path.Combine(appData, "Autodesk", "ApplicationPlugins", "Tabalim.bundle", "contents", "img", "tableros", String.Format("{0}.BMP", tablero)); DBDictionary imgDic = RasterImageDef.GetImageDictionary(doc.Database).GetObject(OpenMode.ForRead) as DBDictionary; if (File.Exists(imgFile) && imgDic.Contains(tablero)) { RasterImageDef imgDef = imgDic.GetAt(tablero).GetObject(OpenMode.ForWrite) as RasterImageDef; if (imgDef.SourceFileName != imgFile) { imgDef.SourceFileName = imgFile; imgDef.Load(); //img.UpgradeOpen(); //img.ImageDefId = imgDef.Id; //img.AssociateRasterDef(imgDef); } } } } } }
/// <summary> /// Gets a Named Objects Dictionary with the given key /// If the collection does not contain the key, it will create a new Named Objects Dictionary instance. /// </summary> /// <param name="name">Name of the Named Objects Dictionary.</param> /// <param name="create">True if the Dictionary must be created if not exist.</param> /// <returns>Named Objects Dictionary. New if <paramref name="create"/> is true and the Dictionary does not exist; Otherwise it will be null if the Dictionary</returns> public static DBDictionary GetNamedObjectsDictionary(string name, bool create) { DBDictionary nod = null; // Named Objects Dictionary Database db = Active.Database; try { Active.Document.StartTransaction(tr => { Transaction t = tr.Transaction; DBDictionary nod_collection = t.GetObject <DBDictionary>(db.NamedObjectsDictionaryId, OpenMode.ForRead); if (nod_collection.Contains(name)) { ObjectId oid = nod_collection.GetAt(name); nod = t.GetObject <DBDictionary>(oid, OpenMode.ForRead); } else if (create) { nod = AddNamedObjectsDictionary(t, name); } }); } catch (XRecordHandlerException) { throw; } catch (Exception ex) { string err_message = string.Format("DBDictionary named: '{0}' could not be found.", name); throw new XRecordHandlerException(err_message, ex); } return(nod); }
/// <summary> /// Удаляет Xrecord с заданным именем, если после удаления остается пустой словарь - он также удаляется /// </summary> /// <param name="o">Объект, у которого удаляется Xrecord</param> /// <param name="xrecordName">Имя записи</param> public static void DeleteXrecord(this DBObject o, string xrecordName) { bool delDict = false; if (xrecordName == null) { throw new ArgumentNullException("xrecordName is null"); } if (o.ExtensionDictionary != ObjectId.Null && !o.ExtensionDictionary.IsErased) { using (DBDictionary dict = (DBDictionary)o.ExtensionDictionary.GetObject(OpenMode.ForRead)) { if (dict.Contains(xrecordName)) { dict.UpgradeOpen(); dict.Remove(xrecordName); if (dict.Count == 0) { delDict = true; } } } if (delDict) { o.ReleaseExtensionDictionary(); } } }
private static ObjectId getDictionaryId(string dictionaryName) { using (DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument()) { using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) { DBDictionary nod = trans.GetObject(HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary; DBDictionary dictRoot; if (!nod.Contains(DictionaryRoot)) { dictRoot = new DBDictionary(); nod.UpgradeOpen(); nod.SetAt(DictionaryRoot, dictRoot); trans.AddNewlyCreatedDBObject(dictRoot, true); } else { dictRoot = trans.GetObject(nod.GetAt(DictionaryRoot), OpenMode.ForWrite) as DBDictionary; } if (!dictRoot.Contains(dictionaryName)) { DBDictionary dictEntry = new DBDictionary(); dictRoot.SetAt(dictionaryName, dictEntry); trans.AddNewlyCreatedDBObject(dictEntry, true); } trans.Commit(); return(dictRoot.GetAt(dictionaryName)); } } }
internal bool CheckDrawingForCivil3D(Document doc) { using (Transaction trans = doc.TransactionManager.StartTransaction()) { // Find the NOD in the database DBDictionary nod = (DBDictionary)trans.GetObject(doc.Database.NamedObjectsDictionaryId, OpenMode.ForRead); string id = this.GetType().FullName + "Civil3DRequired"; if (nod.Contains(id)) { ObjectId objId = nod.GetAt(id); Xrecord XRecord = (Xrecord)trans.GetObject(objId, OpenMode.ForRead); foreach (TypedValue value in XRecord.Data) { if (value.TypeCode == (short)DxfCode.Bool) { bool castValue = Convert.ToInt32(value.Value) != 0; if (castValue) { return(true); } } } } } return(false); }
private static ObjectId getDictionaryId(ObjectId id, string dictionaryName) { using (DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument()) { using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) { DBObject dbo = trans.GetObject(id, OpenMode.ForRead); if (dbo.ExtensionDictionary == ObjectId.Null) { dbo.UpgradeOpen(); dbo.CreateExtensionDictionary(); } DBDictionary dictRoot = trans.GetObject(dbo.ExtensionDictionary, OpenMode.ForRead) as DBDictionary; if (!dictRoot.Contains(dictionaryName)) { dictRoot.UpgradeOpen(); DBDictionary dictEntry = new DBDictionary(); dictRoot.SetAt(dictionaryName, dictEntry); trans.AddNewlyCreatedDBObject(dictEntry, true); } trans.Commit(); return(dictRoot.GetAt(dictionaryName)); } } }
/// <summary> /// 创建一个新的多线样式 /// </summary> /// <param name="db">数据库对象</param> /// <param name="styleName">多线样式名</param> /// <param name="elements">加入到多线样式的元素集合</param> /// <returns>返回加入的多线样式的Id</returns> public static ObjectId CreateMLineStyle(this Database db, string styleName, List <MlineStyleElement> elements) { //打开当前数据库的多线样式字典对象 DBDictionary dict = (DBDictionary)db.MLStyleDictionaryId.GetObject(OpenMode.ForRead); if (dict.Contains(styleName)) // 如果已经存在指定名称的多线样式 { return((ObjectId)dict[styleName]); // 返回该多线样式的Id } MlineStyle mStyle = new MlineStyle(); // 创建一个多线样式对象 mStyle.Name = styleName; //设置多线样式的名称 //为多线样式添加新的元素 foreach (var element in elements) { mStyle.Elements.Add(element, true); } dict.UpgradeOpen(); //切换多线字典为写 //在多线样式字典中加入新创建的多线样式对象,并指定搜索关键字为styleName dict.SetAt(styleName, mStyle); //通知事务处理完成多线样式对象的加入 db.TransactionManager.AddNewlyCreatedDBObject(mStyle, true); dict.DowngradeOpen(); //为了安全,将多线样式字典切换成读 return(mStyle.ObjectId); // 返回该多线样式的Id }
/// <summary> /// Retrieve or create an Entry with the given name into the Extension Dictionary of the passed-in object. /// </summary> /// <param name="id">The object hosting the Extension Dictionary</param> /// <param name="entryName">The name of the dictionary entry to get or set</param> /// <returns>The ObjectId of the diction entry, old or new</returns> public static ObjectId GetSetExtensionDictionaryEntry(ObjectId id, string entryName) { ObjectId ret = ObjectId.Null; using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) { DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead); if (obj.ExtensionDictionary == ObjectId.Null) { obj.UpgradeOpen(); obj.CreateExtensionDictionary(); obj.DowngradeOpen(); } DBDictionary dict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForRead); if (!dict.Contains(entryName)) { Xrecord xRecord = new Xrecord(); dict.UpgradeOpen(); dict.SetAt(entryName, xRecord); tr.AddNewlyCreatedDBObject(xRecord, true); ret = xRecord.ObjectId; } else { ret = dict.GetAt(entryName); } tr.Commit(); } return(ret); }
/// <summary> /// Gets the data from an Xrecord. /// </summary> /// <param name="dictionaryName">Name of Named Objects Dictionary</param> /// <param name="xKey">Key of Xrecord</param> /// <returns>ResultBuffer with data or Null if nothing found.</returns> /// <exception cref="Wrappers.XRecordException"/> public ResultBuffer GetXrecord(string dictionaryName, string xKey) { ResultBuffer result = null; try { if (_document != null) { _document.StartTransaction(tr => { Transaction t = tr.Transaction; using (DBDictionary nod = Dictionaries.GetNamedObjectsDictionary(dictionaryName)) { if (nod != null && nod.Contains(xKey)) { ObjectId oid = nod.GetAt(xKey); using (Xrecord xrec = t.GetObject <Xrecord>(oid, OpenMode.ForRead)) { result = xrec.Data; } } } }); } } catch (XRecordHandlerException) { throw; } catch (Exception ex) { string err_message = string.Format("Unexpected error occured while retrieving xRecord '{0}' from Named Objects Dictionary '{1}'.", xKey, dictionaryName); throw new XRecordHandlerException(dictionaryName, xKey, err_message, ex, ErrorCode.XrecordNotFound); } return(result); }
getSubEntry(ObjectId idParent, string entrySub) { ObjectId idDict = ObjectId.Null; try { using (Transaction tr = BaseObjs.startTransactionDb()) { DBDictionary parent = (DBDictionary)idParent.GetObject(OpenMode.ForRead); if (parent.Contains(entrySub)) { try { idDict = parent.GetAt(entrySub); } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 491"); } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Dict.cs: line: 499"); } return(idDict); }