static void DoPossibleDowngradeOpen(DBObject dbObject, OpenMode openMode) { if ((openMode == OpenMode.ForRead) && dbObject.IsWriteEnabled) { dbObject.DowngradeOpen(); } }
/// <summary> /// 删除指定注册应用程序下的扩展数据 /// </summary> /// <param name="id">对象的Id</param> /// <param name="regAppName">注册应用程序名</param> public static void RemoveXData(this DBObject obj, string regAppName) { Database db = obj.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { try { obj.UpgradeOpen(); TypedValueList XData = obj.GetXDataForApplication(regAppName); if (XData != null)// 如果有扩展数据 { TypedValueList newXData = new TypedValueList(); newXData.Add(DxfCode.ExtendedDataRegAppName, regAppName); obj.XData = newXData; //为对象的XData属性重新赋值,从而删除扩展数据 } obj.DowngradeOpen(); } catch (Exception ex) { trans.Abort(); Logger.log("RemoveXData", ex.Message); } } }
/// <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> /// 删除实体的缩放比例 /// </summary> /// <param name="entId">实体的Id</param> /// <param name="scaleNames">缩放比例名列表</param> public static void RemoveScale(this ObjectId entId, params string[] scaleNames) { //获取entId指示的实体对象 DBObject obj = entId.GetObject(OpenMode.ForRead); //如果实体对象支持注释缩放 if (obj.Annotative != AnnotativeStates.NotApplicable) { //获得对象所有的注释比例 List <ObjectContext> scales = entId.GetAllScales(); obj.UpgradeOpen();//切换实体为写的状态 //获取当前图形的对象比例管理器 ObjectContextManager ocm = obj.Database.ObjectContextManager; //获取当前图形的注释比例列表,名为ACDB_ANNOTATIONSCALES ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); //遍历需要设置的注释比例 foreach (string scaleName in scaleNames) { //获取名为scaleName的注释比例 ObjectContext scale = occ.GetContext(scaleName); //若不存在scaleName的注释比例,则结束本次循环 if (scale == null) { continue; } //删除名为scaleName的注释比例 ObjectContexts.RemoveContext(obj, scale); } obj.DowngradeOpen();//为了安全将实体切换为读的状态 } }
/// <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 }
public static void RemoveXData(DBObject dbo) { if (dbo.GetXDataForApplication(RegAppNAME) != null) { ResultBuffer rb = rb = new ResultBuffer(new TypedValue(1001, RegAppNAME)); dbo.UpgradeOpen(); dbo.XData = rb; dbo.DowngradeOpen(); } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (_upgraded && _object != null) { _object.DowngradeOpen(); } _object = null; } }
public void Dispose() { if (_isNotifyEnabled) { _obj.DowngradeToNotify(_isWriteEnabled); } else if (!_isWriteEnabled) { _obj.DowngradeOpen(); } }
/// <summary> /// 删除指定注册应用程序下的扩展数据 /// </summary> /// <param name="id">对象的Id</param> /// <param name="regAppName">注册应用程序名</param> public static void RemoveXData(this ObjectId id, string regAppName) { // 以写的方式打开对象 DBObject obj=id.GetObject(OpenMode.ForWrite); // 获取regAppName注册应用程序下的扩展数据列表 TypedValueList xdata=obj.GetXDataForApplication(regAppName); if (xdata != null)// 如果有扩展数据 { // 新建一个TypedValue列表,并只添加注册应用程序名扩展数据项 TypedValueList newValues=new TypedValueList(); newValues.Add(DxfCode.ExtendedDataRegAppName, regAppName); obj.XData = newValues; //为对象的XData属性重新赋值,从而删除扩展数据 } obj.DowngradeOpen();// 为了安全,切换对象为读的状态 }
/// <summary> /// 用于替换扩展字典中的整个一条扩展记录 /// </summary> /// <param name="objId">对象id</param> /// <param name="xRecordSearchKey">扩展记录的名称</param> /// <param name="values">扩展记录的内容</param> public static void ModObjXrecord(ObjectId objId, string xRecordSearchKey, ResultBuffer values) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tr = db.TransactionManager.StartTransaction()) { try { DBObject obj = tr.GetObject(objId, OpenMode.ForRead); //以读的方式打开 if (obj.ExtensionDictionary.IsNull) //如果对象无扩展字典,那就给创建 { obj.UpgradeOpen(); //切换对象为写的状态 obj.CreateExtensionDictionary(); //为对象创建扩展字典,一个对象只能拥有一个扩展字典 obj.DowngradeOpen(); //将对象切换为读的状态 } DBDictionary dict = obj.ExtensionDictionary.GetObject(OpenMode.ForRead) as DBDictionary; if (!dict.Contains(xRecordSearchKey)) { Xrecord xrec = new Xrecord(); //为对象创建一个扩展记录 xrec.Data = values; //指定扩展记录的内容,这里用到了自定义类型转换,TypedValueList-->ResultBuffer dict.UpgradeOpen(); //将扩展字典切换为写的状态,以便添加一个扩展记录 ObjectId xrecId = dict.SetAt(xRecordSearchKey, xrec); //在扩展字典中加入新建的扩展记录,并指定它的搜索关键字 objId.Database.TransactionManager.AddNewlyCreatedDBObject(xrec, true); dict.DowngradeOpen(); //将扩展字典切换为读的状态 } else { ObjectId xrecordId = dict.GetAt(xRecordSearchKey); //获取扩展记录的id Xrecord xrecord = xrecordId.GetObject(OpenMode.ForWrite) as Xrecord; xrecord.Data = values; //覆盖原来的数据,因为values有了新的指向 xrecord.DowngradeOpen(); //将扩展记录切换为读的状态 } } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show("扩展属性存:" + ex.Message); } finally { tr.Commit(); m_DocumentLock.Dispose(); } } }
/// <summary> /// 为实体添加指定的注释比例 /// </summary> /// <param name="entId">实体的Id</param> /// <param name="scaleNames">注释比例名列表</param> public static void AttachScale(this ObjectId entId, params string[] scaleNames) { Database db = entId.Database; //获取entId指示的实体对象 DBObject obj = entId.GetObject(OpenMode.ForRead); //如果实体对象支持注释缩放 if (obj.Annotative != AnnotativeStates.NotApplicable) { //如果实体为块参照,则需要通过其所属的块表记录来设置可注释缩放 if (obj is BlockReference) { BlockReference br = obj as BlockReference; //打开对应的块表记录 BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForWrite); //设置块表记录为可注释缩放 btr.Annotative = AnnotativeStates.True; } //其它可注释缩放的实体,使其可注释缩放 else if (obj.Annotative == AnnotativeStates.False) { obj.Annotative = AnnotativeStates.True; } obj.UpgradeOpen();//切换实体为写的状态以添加注释比例 //获取当前图形的对象比例管理器 ObjectContextManager ocm = db.ObjectContextManager; //获取当前图形的注释比例列表,名为ACDB_ANNOTATIONSCALES ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); //遍历需要设置的注释比例 foreach (string scaleName in scaleNames) { //获取名为scaleName的注释比例 ObjectContext scale = occ.GetContext(scaleName); //若不存在scaleName的注释比例,则结束本次循环 if (scale == null) { continue; } //为实体添加名为scaleName的注释比例 ObjectContexts.AddContext(obj, scale); } obj.DowngradeOpen();//为了安全将实体切换为读的状态 } }
/// <summary> /// 添加扩展记录,如果没有扩展字典,那就创建扩展字典 /// </summary> /// <param name="objId">对象的objectid</param> /// <param name="xRecordSearchKey">扩展记录名称</param> /// <param name="values">扩展记录的内容</param> /// <returns></returns> public static void AddNumberToPolyline(ObjectId objId, string values) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tr = db.TransactionManager.StartTransaction()) { //添加扩展记录之前,先创建对象的扩展字典 DBObject obj = tr.GetObject(objId, OpenMode.ForRead); //以读的方式打开 if (obj.ExtensionDictionary.IsNull) //如果对象无扩展字典,那就给创建 { obj.UpgradeOpen(); //切换对象为写的状态 obj.CreateExtensionDictionary(); //为对象创建扩展字典,一个对象只能拥有一个扩展字典 obj.DowngradeOpen(); //将对象切换为读的状态 } //打开对象的扩展字典 DBDictionary dict = obj.ExtensionDictionary.GetObject(OpenMode.ForRead) as DBDictionary; //如果扩展字典中已包含指定的扩展记录对象 if (dict.Contains("polylineNumber")) { m_DocumentLock.Dispose(); } else //若没有包含扩展记录,则创建一个 { ResultBuffer valueBuffer = new ResultBuffer(); valueBuffer.Add(new TypedValue(5005, values)); Xrecord xrec = new Xrecord(); //为对象创建一个扩展记录 xrec.Data = valueBuffer; //指定扩展记录的内容,这里用到了自定义类型转换,TypedValueList-->ResultBuffer dict.UpgradeOpen(); //将扩展字典切换为写的状态,以便添加一个扩展记录 ObjectId xrecId = dict.SetAt("polylineNumber", xrec); //在扩展字典中加入新建的扩展记录,并指定它的搜索关键字 tr.AddNewlyCreatedDBObject(xrec, true); dict.DowngradeOpen(); //将扩展字典切换为读的状态 m_DocumentLock.Dispose(); } tr.Commit(); } }
/// <summary> /// 修改扩展数据 /// </summary> /// <param name="id">对象的Id</param> /// <param name="regAppName">注册应用程序名</param> /// <param name="code">扩展数据的类型</param> /// <param name="oldValue">原来的扩展数据值</param> /// <param name="newValue">新的扩展数据值</param> public static void ModXData(this ObjectId id, string regAppName, DxfCode code, object oldValue, object newValue) { // 以写的方式打开对象 DBObject obj=id.GetObject(OpenMode.ForWrite); // 获取regAppName注册应用程序下的扩展数据列表 TypedValueList xdata=obj.GetXDataForApplication(regAppName); for (int i = 0; i < xdata.Count; i++)// 遍历扩展数据列表 { TypedValue tv=xdata[i]; //扩展数据 //判断扩展数据的类型和值是否满足条件 if (tv.TypeCode == (short)code && tv.Value.Equals(oldValue)) { // 设置新的扩展数据值 xdata[i] = new TypedValue(tv.TypeCode, newValue); break; //要修改的数据已找到,跳出循环 } } obj.XData = xdata; // 覆盖原来的扩展数据,达到修改的目的 obj.DowngradeOpen();// 为了安全,切换对象为读的状态 }
/// <summary> /// disposes the object /// </summary> /// <param name="disposing">the disposing status input parameter</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (_db_object != null) { if (_is_upgraded) { _db_object.DowngradeOpen(); _is_upgraded = false; } if (_layer_id != ObjectId.Null) { // layer was locked, lock it again LockLayer(_layer_id); } //if } //if } //if }
/// <summary> /// 添加扩展记录,如果没有扩展字典,那就创建扩展字典 /// </summary> /// <param name="objId">对象的objectid</param> /// <param name="xRecordSearchKey">扩展记录名称</param> /// <param name="values">扩展记录的内容</param> /// <returns></returns> public static void AddXRecordToObj(ObjectId objId, string xRecordSearchKey, ResultBuffer values, Transaction mytransaction = null) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; if (mytransaction == null) { DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tr = db.TransactionManager.StartTransaction()) { //添加扩展记录之前,先创建对象的扩展字典 DBObject obj = objId.GetObject(OpenMode.ForRead); //以读的方式打开 if (obj.ExtensionDictionary.IsNull) //如果对象无扩展字典,那就给创建 { obj.UpgradeOpen(); //切换对象为写的状态 obj.CreateExtensionDictionary(); //为对象创建扩展字典,一个对象只能拥有一个扩展字典 obj.DowngradeOpen(); //将对象切换为读的状态 } //打开对象的扩展字典 DBDictionary dict = obj.ExtensionDictionary.GetObject(OpenMode.ForRead) as DBDictionary; //如果扩展字典中已包含指定的扩展记录对象 if (dict.Contains("Layer0")) { m_DocumentLock.Dispose(); } else //若没有包含扩展记录,则创建一个 { Xrecord xrec = new Xrecord();//为对象创建一个扩展记录 xrec.Data = values; //指定扩展记录的内容,这里用到了自定义类型转换,TypedValueList-->ResultBuffer dict.UpgradeOpen(); //将扩展字典切换为写的状态,以便添加一个扩展记录 ObjectId xrecId = dict.SetAt(xRecordSearchKey, xrec); //在扩展字典中加入新建的扩展记录,并指定它的搜索关键字 //objId.Database.TransactionManager.AddNewlyCreatedDBObject(xrec, true); tr.AddNewlyCreatedDBObject(xrec, true); dict.DowngradeOpen();//将扩展字典切换为读的状态 m_DocumentLock.Dispose(); } tr.Commit(); } } }
/// <summary> /// 新增或更新XData键值对 /// </summary> /// <param name="id"></param> /// <param name="regAppName"></param> /// <param name="key"></param> /// <param name="value"></param> public static void SetXData(this DBObject obj, string regAppName, string key, string value) { Database db = obj.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { try { obj.UpgradeOpen(); TypedValueList XData = obj.GetXDataForApplication(regAppName); if (XData == null) { XData = new TypedValueList(); XData.Add(DxfCode.ExtendedDataRegAppName, regAppName); } TypedValue tvKey = XData.FirstOrDefault(item => item.Value.Equals(key)); //查找key int indexKey = XData.IndexOf(tvKey); //Key索引 int indexValue = indexKey + 1; //Value索引 if (indexKey >= 0 && indexValue < XData.Count) { //键值对已存在,更新 TypedValue tvValue = XData[indexValue]; XData[indexValue] = new TypedValue(tvValue.TypeCode, value); } else { //键值对不存在,新增 XData.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, key)); XData.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, value)); } obj.XData = XData; // 覆盖原来的扩展数据,达到修改的目的 obj.DowngradeOpen(); trans.Commit(); } catch (Exception ex) { trans.Abort(); Logger.log("SetXData", ex.Message); } } }
public void autoBlockFit(Database db, BlockTableRecord blockTR, Transaction tr) { Point3d DownLeft_Point = new Point3d(); Point3d UpRight_Point = new Point3d(); string compareResult = string.Empty; bool started = false; try { blockTR.UpgradeOpen(); DBObjectCollection EntityInOldBlock = new DBObjectCollection(); string blockName = blockTR.Name; foreach (ObjectId entId in blockTR) { DBObject objSubBlock = (DBObject)tr.GetObject(entId, OpenMode.ForWrite); EntityInOldBlock.Add(objSubBlock); if (objSubBlock.Bounds == null) { continue; } else { if (started == false) { UpRight_Point = objSubBlock.Bounds.Value.MaxPoint; DownLeft_Point = objSubBlock.Bounds.Value.MinPoint; started = true; continue; } else { string compareMaxRes = string.Empty; string compareMinRes = string.Empty; if (objSubBlock.Bounds.Value.MaxPoint.X > UpRight_Point.X) { UpRight_Point = new Point3d(objSubBlock.Bounds.Value.MaxPoint.X, UpRight_Point.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MaxPoint.Y > UpRight_Point.Y) { UpRight_Point = new Point3d(UpRight_Point.X, objSubBlock.Bounds.Value.MaxPoint.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MinPoint.X < DownLeft_Point.X) { DownLeft_Point = new Point3d(objSubBlock.Bounds.Value.MinPoint.X, DownLeft_Point.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MinPoint.Y < DownLeft_Point.Y) { DownLeft_Point = new Point3d(DownLeft_Point.X, objSubBlock.Bounds.Value.MinPoint.Y, UpRight_Point.Z); } } } objSubBlock.DowngradeOpen(); } //CreateBlock(blockTR.Name, EntityInOldBlock, DownLeft_Point); objectBound ob = new objectBound(DownLeft_Point, UpRight_Point); sizeEntity.Add(blockName, ob); blockTR.Origin = DownLeft_Point; blockTR.DowngradeOpen(); //minPoint和maxPoint只差为块的大小. //通过缩放解决图形大小的问题. //minPoint的位置为块离原点的距离,默认为离基点的距离. //通过平移解决插入位置的问题 /* * Polyline c1 = new Polyline(); * c1.CreatePolyCircle(new Point2d(UpRight_Point.X, UpRight_Point.Y), 5); * * Polyline c2 = new Polyline(); * c2.CreatePolyCircle(new Point2d(DownLeft_Point.X, DownLeft_Point.Y), 5); ; * * c1.ColorIndex = 200; //upright * c2.ColorIndex = 100; * //db.AddToModelSpace(c1, c2); * * try * { * blockTR.UpgradeOpen(); * blockTR.AppendEntity(c1); * blockTR.AppendEntity(c2); * tr.AddNewlyCreatedDBObject(c1, true); * tr.AddNewlyCreatedDBObject(c2, true); * blockTR.DowngradeOpen(); * * * * } * catch (Exception ee) * { * MessageBox.Show(ee + ""); * } */ } catch (Autodesk.AutoCAD.Runtime.Exception ee) { } }