- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- using System;
- using System.Diagnostics;
- using System.Collections;
- using System.Windows.Forms;
- using System.Reflection;
- using System.IO;
- using System.Runtime.InteropServices;
- using Yekai.CsArx.Geometry;
- //using Yekai.CsArx.Enum;
- using Yekai.CsArx.Function;
- using Yekai.CsArx.AcadCom;
-
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Colors;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Windows;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.Interop.Common;
- using app = Autodesk.AutoCAD.ApplicationServices.Application;
- using AColor = Autodesk.AutoCAD.Colors.Color;
-
- namespace Yekai.CsArx
- {
- /// <summary>
- /// 读写扩展记录Xrecord 扩展数据XData 链表ResultBuffer TypedValue 等数据的类
- /// Version : 2009.06.11
- /// 备注:上传[url]www.pudn.com[/url]
- /// </summary>
- public class sXr
- {
- /// <summary>
- /// 将字符串转换为对应类型值
- /// 以能处理的数据类型bool Boolean int Int32 Radian360 Double double
- /// String string Point Point3d Vector3d sPosition sPositionCollection
- /// Version : 2009.02.14
- /// 1,数组字符串要使用":"好分隔
- /// </summary>
- /// <param name="valueString">字符串</param>
- /// <param name="valueType">将要转换的数据类型</param>
- /// <returns>成功返回对应值,否则返回null</returns>
- public static object GetValue(string valueString, System.Type valueType)
- {
- try
- {
- if (valueType.Equals(typeof(System.Type)))
- { return System.Type.GetType(valueString, true, true); }
- else if (valueType.IsEnum)
- {
- //smc.ed.WriteMessage(" 41 ");
- return System.Enum.Parse(valueType, valueString, true);
- }
- else if (valueType.IsArray)
- {
- //smc.ed.WriteMessage(" 42 ");
- char[] chars2 = { ':' };
- return fun.ArrayFromString(valueString, valueType.GetElementType(), chars2);
- }
- else
- {
- switch (valueType.Name)
- {
- case "bool":
- case "Boolean":
- //smc.ed.WriteMessage(" 43 ");
- return System.Convert.ToBoolean(valueString);
- case "int":
- case "Int32":
- //smc.ed.WriteMessage(" 44 ");
- return System.Convert.ToInt32(valueString);
- case "Radian360":
- //smc.ed.WriteMessage(" 45 ");
- return (Radian360)valueString;
- case "sPosition":
- //smc.ed.WriteMessage(" 45 ");
- return (sPosition)valueString;
- case "Double":
- case "double":
- //smc.ed.WriteMessage(" 45 ");
- return System.Convert.ToDouble(valueString);
- case "String":
- case "string":
- //smc.ed.WriteMessage(" 46 ");
- return valueString;
- case "Point":
- case "Point3d":
- Point3d p1;
- if (fun.GetPoint3d(valueString, out p1))
- {
- //smc.ed.WriteMessage(" 47 ");
- return p1;
- }
- break;
- case "Vector3d":
- Point3d p2;
- if (fun.GetPoint3d(valueString, out p2))
- {
- return p2.GetAsVector();
- }
- break;
- case "ObjectId":
- try
- {
- return smc.db.GetObjectId(false, new Handle(Convert.ToInt64(valueString, 16)), 0);
- }
- catch { return ObjectId.Null; }
- case "Point3dCollection":
- case "Vector3dCollection":
- case "ObjectIdCollection":
- case "sPositionCollection":
- return fun.CollectionFromString(valueString, valueType, ":".ToCharArray());
- case "Hanhle":
- return new Handle(Convert.ToInt64(valueString, 16));
- }
- }
- return null;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 获取链表里某个关键字对应的值
- /// Note:
- /// 1,没有分割符号,即前名后值时separator应为""空字符串,
- /// 此时valueType被忽略,返回值类型就是链表里该值的类型
- /// 2,有分隔符号而valueType==null时返回值部分的字符串(即分隔符号右边字符串)
- /// </summary>
- /// <param name="resBuf">链表</param>
- /// <param name="keyName">关键字</param>
- /// <param name="valueType">返回值类型</param>
- /// <param name="separator">分割符号</param>
- /// <returns>成功找到关键字并成功转换成返回值类型则返回该值,否则返回null</returns>
- public static object GetKeyValue(ResultBuffer resBuf, string keyName, System.Type valueType, string separator)
- {
- try
- {
- //smc.ed.WriteMessage("\nGetPartFromResultBuffer step 1");
- if (resBuf == null) return null;
- TypedValue[] tValues = resBuf.AsArray();
- string propertyName;
- string propertyValue;
- if (separator == "")
- {
- int startIndex = 0;
- if (tValues[0].TypeCode == 1001)
- startIndex = 1;
- for (int i = startIndex; i < tValues.Length - 1; i += 2)
- {
- if (string.Equals(tValues[i].Value.ToString(), keyName, StringComparison.OrdinalIgnoreCase))
- {
- return tValues[i + 1].Value;
- }
- }
- }
- else
- {
- for (int i = 0; i < tValues.Length; i++)
- {
- char[] arrChar = separator.ToCharArray();
- string[] nameValue = tValues[i].Value.ToString().Split(arrChar);
- if (nameValue.Length == 2)
- {
- propertyName = nameValue[0];
- propertyValue = nameValue[1];
- if (string.Equals(propertyName, keyName, StringComparison.OrdinalIgnoreCase))
- {
- if (valueType == null)
- return propertyValue;
- else
- return sXr.GetValue(propertyValue, valueType);
- }
- }
- }
- }
- return null;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 设置链表里某个关键字对应的值,若输入键不存在则在表里添加
- /// Version : 2008.11.10
- /// </summary>
- /// <param name="resBuf">链表,可以为空null</param>
- /// <param name="keyName">键名</param>
- /// <param name="keyValue">键值</param>
- /// <param name="separator">键名与键值的分隔符号,没有则为""</param>
- /// <param name="forXData">是否为用于扩展数据的链表</param>
- /// <returns>成功返回设置后的链表,否则返回null</returns>
- public static ResultBuffer SetKeyValue(ResultBuffer resBuf, string keyName, object keyValue, string separator,bool forXData)
- {
- try
- {
- if (resBuf == null)
- {
- resBuf = new ResultBuffer();
- if (separator == "")
- {
- resBuf.Add(fun.GetTypedValue(keyName, forXData));
- resBuf.Add(fun.GetTypedValue(keyValue, forXData));
- }
- else
- {
- resBuf.Add(fun.GetTypedValue(keyName + separator + keyValue.ToString(), forXData));
- }
- return resBuf;
- }
- TypedValue[] tValues = resBuf.AsArray();
- if (separator == "")
- {
- //当TypedValue[]来自扩展数据时第一个是扩展数据名
- for (int i = forXData ? 1 : 0; i < tValues.Length - 1; i += 2)
- {
- if (tValues[i].Value.ToString().ToUpper() == keyName.ToUpper())
- {
- tValues[i + 1] = fun.GetTypedValue(keyValue, forXData);
- resBuf = new ResultBuffer(tValues);
- return resBuf;
- }
- }
- }
- else
- {
- for (int i = 0; i < tValues.Length; i++)
- {
- char[] arrChar = separator.ToCharArray();
- string[] nameValue = tValues[i].Value.ToString().Split(arrChar);
- if (nameValue.Length == 2)
- {
- if (string.Equals(nameValue[0], keyName, StringComparison.OrdinalIgnoreCase))
- {
- tValues[i] = fun.GetTypedValue(keyName + separator + keyValue.ToString(), forXData);
- resBuf = new ResultBuffer(tValues);
- return resBuf;
- }
- }
- }
- }
- if (separator == "")
- {
- resBuf.Add(fun.GetTypedValue(keyName, forXData));
- resBuf.Add(fun.GetTypedValue(keyValue, forXData));
- }
- else
- {
- resBuf.Add(fun.GetTypedValue(keyName + separator + keyValue.ToString(), forXData));
- }
- return resBuf;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- #region Xrecord read and write
-
- /// <summary>
- /// 从专用的属性链表生成一个对象实例
- /// 专用的属性链表格式
- /// 以字符串|ObjectType|标识开始,后面紧跟着对象类型名全名
- /// 以字符串PropertyStart标识可写属性开始,PropertyEnd标识可写属性结束
- /// TypedValue数据格式 Name=Value,以=分割属性名和属性值 如: Name=Sieben
- /// PropertyStart和PropertyEnd可以嵌套
- /// Version : 2008.02.27 改用sXr.GetValue()
- /// 特殊处理数据类型 : bool Boolean int Int32 Radian360 Double double
- /// String string Point3d Pointt Vector3d
- /// </summary>
- /// <param name="resBuf">专用的属性链表</param>
- /// <returns>成功返回一个对象实例,否则返回null</returns>
- public static object GetObjectFromResultBuffer(Assembly tAss, ResultBuffer resBuf)
- {
- try
- {
- //smc.ed.WriteMessage("\nGetPartFromResultBuffer step 1");
- if (resBuf == null) return null;
- TypedValue[] tValues = resBuf.AsArray();
- string typeStr = "";
- int startPlace = 0;
- for (int i = 0; i < tValues.Length; i++)
- {
- if (string.Equals(tValues[i].Value.ToString(), "|ObjectType|", StringComparison.OrdinalIgnoreCase))
- {
- typeStr = tValues[i + 1].Value.ToString();
- startPlace = i + 1;
- break;
- }
- }
- //smc.ed.WriteMessage("\nGetPartFromResultBuffer step 2");
- if (typeStr == "") return null;
- //System.Type partType = System.Type.GetType(typeStr, true);
- System.Type partType = tAss.GetType(typeStr);
- if (partType == null) return null;
- //smc.ed.WriteMessage("\nGetPartFromResultBuffer step 3");
- return sXr.GetObjectFromResultBuffer(partType, tValues, ref startPlace);
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 从专用的属性链表生成一个对象实例
- /// 专用的属性链表格式
- /// 以字符串|ObjectType|标识开始,后面紧跟着对象类型名全名
- /// 以字符串PropertyStart标识可写属性开始,PropertyEnd标识可写属性结束
- /// TypedValue数据格式 Name=Value,以=分割属性名和属性值 如: Name=Sieben
- /// PropertyStart和PropertyEnd可以嵌套
- /// Version : 2009.02.14
- /// </summary>
- /// <param name="objType">对象实例类型</param>
- /// <param name="tValues">专用的属性链表</param>
- /// <param name="proIndex">属性链表的开始位置</param>
- /// <returns>成功返回一个对象实例,否则返回null</returns>
- public static object GetObjectFromResultBuffer(System.Type objType, TypedValue[] tValues, ref int proIndex)
- {
- try
- {
- //smc.ed.WriteMessage("\nGetObjectFromResultBuffer 1 Index = " + proIndex.ToString());
- System.Type[] partParams = { };
- ConstructorInfo objConstructor = objType.GetConstructor(partParams);
- if (objConstructor == null) return null;
- object[] constructorParams = { };
- object curObj = objConstructor.Invoke(constructorParams);
- if (curObj == null) return null;
- bool propertyStart = false;
- bool propertyEnd = false;
- string propertyName = "";
- string propertyValue = "";
- char[] chars = { '=' };
- object[] pars = { };
- //smc.ed.WriteMessage("\nGetObjectFromResultBuffer 2 curObj = " + curObj.GetType().Name);
- for (; proIndex < tValues.Length; proIndex++)
- {
- //smc.ed.WriteMessage("\nIndex = " + proIndex.ToString() + " " + tValues[proIndex].Value.ToString() + "::");
- //smc.ed.WriteMessage(curObj.GetType().Name);
- if (string.Equals(tValues[proIndex].Value.ToString(), "PropertyStart", StringComparison.OrdinalIgnoreCase))
- {
- //smc.ed.WriteMessage(" 11 ");
- propertyStart = true;
- }
- else if (string.Equals(tValues[proIndex].Value.ToString(), "PropertyEnd", StringComparison.OrdinalIgnoreCase))
- {
- //smc.ed.WriteMessage(" 12 ");
- propertyEnd = true;
- if (propertyStart && propertyEnd)
- {
- //smc.ed.WriteMessage(" 13 ");
- return curObj;
- }
- }
- else
- {
- //smc.ed.WriteMessage(" 14 ");
- if (propertyStart && !propertyEnd)
- {
- //smc.ed.WriteMessage(" 2 ");
- string[] nameValue = tValues[proIndex].Value.ToString().Split(chars);
- if (nameValue.Length == 2)
- {
- propertyName = nameValue[0];
- propertyValue = nameValue[1];
- System.Reflection.PropertyInfo pInfo = objType.GetProperty(propertyName);
- //smc.ed.WriteMessage(" 31 ");
- if (pInfo != null)
- {
- //smc.ed.WriteMessage(" 32 ");
- if (pInfo.PropertyType.IsEnum)
- {
- //smc.ed.WriteMessage(" 41 ");
- pInfo.SetValue(curObj, System.Enum.Parse(pInfo.PropertyType, propertyValue, true), pars);
- }
- else if (pInfo.PropertyType.IsArray)
- {
- //smc.ed.WriteMessage(" 42 ");
- char[] chars2 = { ':' };
- pInfo.SetValue(curObj, fun.ArrayFromString(propertyValue, pInfo.PropertyType.GetElementType(), chars2), pars);
- }
- else
- {
- //smc.ed.WriteMessage("\nsXr.GetObjectFromResultBuffer pInfo.PropertyType = {0}",pInfo.PropertyType.Name);
- switch (pInfo.PropertyType.Name)
- {
- case "bool":
- case "Boolean":
- //smc.ed.WriteMessage(" 43 ");
- pInfo.SetValue(curObj, System.Convert.ToBoolean(propertyValue), pars);
- break;
- case "int":
- case "Int32":
- //smc.ed.WriteMessage(" 44 ");
- pInfo.SetValue(curObj, System.Convert.ToInt32(propertyValue), pars);
- break;
- case "Radian360":
- //smc.ed.WriteMessage(" 45 ");
- pInfo.SetValue(curObj, (Radian360)propertyValue, pars);
- break;
- case "sPosition":
- //smc.ed.WriteMessage(" 45 ");
- pInfo.SetValue(curObj, (sPosition)propertyValue, pars);
- break;
- case "Double":
- case "double":
- //smc.ed.WriteMessage(" 45 ");
- pInfo.SetValue(curObj, System.Convert.ToDouble(propertyValue), pars);
- break;
- case "String":
- case "string":
- //smc.ed.WriteMessage(" 46 ");
- pInfo.SetValue(curObj, propertyValue, pars);
- break;
- case "Point":
- case "Point3d":
- smc.ed.WriteMessage(" 471 ");
- Point3d p1;
- if (fun.GetPoint3d(propertyValue, out p1))
- {
- smc.ed.WriteMessage(" 472 ");
- pInfo.SetValue(curObj, p1, pars);
- }
- smc.ed.WriteMessage(" 473 ");
- break;
- case "Vector3d":
- Point3d p2;
- if (fun.GetPoint3d(propertyValue, out p2))
- {
- pInfo.SetValue(curObj, p2.GetAsVector(), pars);
- }
- break;
- case "Point3dCollection":
- case "Vector3dCollection":
- case "ObjectIdCollection":
- case "sPositionCollection":
- {
- Object tObj = fun.CollectionFromString(propertyValue, pInfo.PropertyType, ":".ToCharArray());
- pInfo.SetValue(curObj, tObj, pars);
- }
- break;
- default:
- //smc.ed.WriteMessage(" 48 ");
- if (pInfo.PropertyType.IsClass)
- {
- //smc.ed.WriteMessage(" 51 ");
- if (propertyValue != "")
- {
- //smc.ed.WriteMessage(" 52 ");
- object tempObj = null;
- System.Type valueType = System.Type.GetType(propertyValue);
- if (valueType != null)
- tempObj = sXr.GetObjectFromResultBuffer(valueType, tValues, ref proIndex);
- else
- tempObj = sXr.GetObjectFromResultBuffer(pInfo.PropertyType, tValues, ref proIndex);
- pInfo.SetValue(curObj, tempObj, pars);
-
- }
- }
- else
- {
- pInfo.SetValue(curObj, sXr.GetValue(propertyValue, pInfo.PropertyType), pars);
- }
- break;
- }
- }
- }
- }
- }
- }
- }
- return curObj;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- private static int GetResultBufferFromObject_time = 0;
- /// <summary>
- /// 根据一个对象实例的可写属性生成一个专用的属性链表,
- /// 专用的属性链表格式
- /// 以字符串|ObjectType|标识开始,后面紧跟着对象类型名全名
- /// 以字符串PropertyStart标识可写属性开始,PropertyEnd标识可写属性结束
- /// TypedValue数据格式 Name=Value,以=分割属性名和属性值 如: Name=Sieben
- /// PropertyStart和PropertyEnd可以嵌套
- /// Version : 2007.10.11
- /// 1,增加对死循环的监控 2008.02.18
- /// 2,当引用类型属性无法获得非null实例时使用0代替
- /// </summary>
- /// <param name="obj">对象实例</param>
- /// <param name="resBuf">专用的属性链表,可以为null,若非null则新链表追加到里面</param>
- /// <param name="forXData">是否用于扩展数据</param>
- /// <returns>成功返回专用的属性链表,否则返回null</returns>
- public static ResultBuffer GetResultBufferFromObject(object obj, ResultBuffer resBuf,bool forXData)
- {
- try
- {
- //smc.ed.WriteMessage("\n sXr.GetResultBufferFromObject 1");
- if (obj == null) return null;
- //smc.ed.WriteMessage("\nsXr.GetResultBufferFromObject {0}", obj.GetType());
- if (GetResultBufferFromObject_time > 200)
- {
- //smc.ed.WriteMessage("\nsXr.GetResultBufferFromObject 可能陷入死循环 {0}", obj.GetType());
- return null;
- }
- else
- {
- GetResultBufferFromObject_time++;
- }
- object[] pars = { };
- PropertyInfo[] pInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- if (pInfos.Length == 0) return resBuf;
- if (resBuf == null)
- {
- GetResultBufferFromObject_time = 0;
- resBuf = new ResultBuffer();
- resBuf.Add(fun.GetTypedValue("|ObjectType|", forXData));
- resBuf.Add(fun.GetTypedValue(obj.GetType().FullName, forXData));
- }
- resBuf.Add(fun.GetTypedValue("PropertyStart", forXData));//引用数据开始
- for (int i = 0; i < pInfos.Length; i++)
- {
- if (pInfos[i].CanWrite)
- {
- //smc.ed.WriteMessage("\n PropertyType = {0}", pInfos[i].PropertyType.Name);
- if (pInfos[i].PropertyType.IsValueType)//属性为值类型
- {
- //smc.ed.WriteMessage(" 21 ");
- object objValue = null;
- try
- {
- objValue = pInfos[i].GetValue(obj, pars);
- }
- catch (System.Exception ex1)
- {
-
- smc.WriteLine(ex1);
- }
- finally
- {
- if (objValue == null)
- objValue = "";
- }
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + objValue.ToString(), forXData));
- }
- else if (pInfos[i].PropertyType.IsClass)//属性为引用类型
- {
- //smc.ed.WriteMessage(" 22 ");
- object objValue = pInfos[i].GetValue(obj, pars);
- if (objValue == null)
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=", forXData));//对象为空是用空字符串表示
- else if (pInfos[i].PropertyType.Equals(typeof(string)))//string类
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + objValue.ToString(), forXData));
- else if (pInfos[i].PropertyType.IsArray)//属性为数组
- {
- //smc.ed.WriteMessage(" 23 ");
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + fun.ArrayToString(objValue, ':'), forXData));
- }
- else if (pInfos[i].PropertyType.Equals(typeof(ObjectIdCollection))
- || pInfos[i].PropertyType.Equals(typeof(Point3dCollection))
- || pInfos[i].PropertyType.Equals(typeof(Vector3dCollection))
- || pInfos[i].PropertyType.Equals(typeof(sPositionCollection)))
- {
- //smc.ed.WriteMessage(" 24 ");
- string strClt = fun.CollectionToString(objValue, ':');
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + strClt, forXData));
- }
- else
- {
- //smc.ed.WriteMessage(" 25 ");
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + objValue.GetType().FullName, forXData));
- if (GetResultBufferFromObject_time > 40)
- {
- //smc.ed.WriteMessage("\nsXr.GetResultBufferFromObject 可能陷入死循环 {0}",objValue.GetType().FullName );
- return null;
- }
- sXr.GetResultBufferFromObject(objValue, resBuf, forXData);//记录属性对象的属性
- }
- }
- else//属性为其他类型,应该没有了
- {
- //smc.ed.WriteMessage(" 25 ");
- resBuf.Add(fun.GetTypedValue(pInfos[i].Name + "=" + pInfos[i].PropertyType.Name, forXData));
- }
- }
- GetResultBufferFromObject_time = 0;
- }
- resBuf.Add(fun.GetTypedValue("PropertyEnd", forXData));//引用数据结束
- //smc.ed.WriteMessage("\nGetPartResultBuffer 1 step 2");
- return resBuf;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 获取数据词典某个记录的值
- /// Version : 2007.10.30 旧的专用函数,可以考虑取消
- /// </summary>
- /// <param name="rootDictionary">根数据词典</param>
- /// <param name="dictionary">数字词典</param>
- /// <param name="name">记录名</param>
- /// <returns></returns>
- public static sRecords GetSRecords(string rootDictionary, string dictionary, string name)
- {
- sRecords recs;
- Database cdb = HostApplicationServices.WorkingDatabase;
- using (Transaction ctrans = cdb.TransactionManager.StartTransaction())
- {
- try
- {
- DBDictionary NOD = (DBDictionary)ctrans.GetObject(cdb.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- DBDictionary sDict = (DBDictionary)ctrans.GetObject(NOD.GetAt(rootDictionary), OpenMode.ForRead);
- DBDictionary divDict = (DBDictionary)ctrans.GetObject(sDict.GetAt(dictionary), OpenMode.ForRead);
- Xrecord xrec = (Xrecord)ctrans.GetObject(divDict.GetAt(name), OpenMode.ForRead);
- recs = sRecords.FromTypedVales(xrec.Data.AsArray());
- ctrans.Commit();
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- recs = null;
- }
- }
- return recs;
- }
- /// <summary>
- /// Version : 2007.10.30 旧的专用函数,可以考虑取消
- /// </summary>
- /// <param name="rootDictionary"></param>
- /// <param name="dictionary"></param>
- /// <param name="groupName"></param>
- /// <param name="recordName"></param>
- /// <returns></returns>
- public static object GetSRecords(string rootDictionary, string dictionary, string groupName, string recordName)
- {
- sRecords recs = sXr.GetSRecords(rootDictionary, dictionary, groupName);
- return recs[recordName];
- }
- /// <summary>
- /// 获取数据词典
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="dictNames">数据词典全名数组</param>
- /// <param name="create">数据词典不存在时是否创建</param>
- /// <returns>成功返回数据词典ObjectId,否则返回ObjectId.Null</returns>
- public static ObjectId GetDictionary(string[] dictNames, bool create)
- {
- try
- {
- //smc.ed.WriteMessage("\nadb.GetDictionary : " );
- ObjectId reId = ObjectId.Null;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- ObjectId dictId;
- DBDictionary curDict = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- for (int i = 0; i < dictNames.Length; i++)
- {
- if (curDict.Contains(dictNames[i]))
- {
- dictId = curDict.GetAt(dictNames[i]);
- curDict = (DBDictionary)ctrans.GetObject(dictId, OpenMode.ForRead);
- }
- else
- {
- if (create)
- {
- curDict.UpgradeOpen();
- DBDictionary tDict = new DBDictionary();
- curDict.SetAt(dictNames[i], tDict);
- ctrans.AddNewlyCreatedDBObject(tDict, true);
- curDict = tDict;
- }
- else
- {
- ctrans.Dispose();
- return ObjectId.Null;
- }
- }
- }
- reId = curDict.ObjectId;
- ctrans.Commit();
- }
- return reId;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return ObjectId.Null;
- }
- }
- /// <summary>
- /// 获取或创建一个数据词典
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="dictNames">数据词典路径全名</param>
- /// <param name="create">当数据词典不存在时是否创建</param>
- /// <returns>成功返回数据词典的ObjectId,否则返回ObjectId.Null</returns>
- public static ObjectId GetDictionary(string dictNames, bool create)
- {
- try
- {
- //smc.ed.WriteMessage("\nadb.GetDictionary : " + dictNames);
- char[] chars = { '.' };
- string[] nameArr = dictNames.Split(chars);
- return GetDictionary(nameArr, create);
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return ObjectId.Null;
- }
- }
- public static DBDictionary GetDictionary(Transaction ctrans, string[] dictNames, bool create)
- {
- try
- {
- if (ctrans == null || dictNames == null) return null;
- DBDictionary curDict = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- for (int i = 0; i < dictNames.Length; i++)
- {
- if (curDict.Contains(dictNames[i]))
- {
- curDict = (DBDictionary)ctrans.GetObject(curDict.GetAt(dictNames[i]), OpenMode.ForRead);
- }
- else
- {
- if (create)
- {
- curDict.UpgradeOpen();
- DBDictionary tDict = new DBDictionary();
- curDict.SetAt(dictNames[i], tDict);
- ctrans.AddNewlyCreatedDBObject(tDict, true);
- curDict = tDict;
- }
- else
- {
- return null;
- }
- }
- }
- return curDict;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 获取或创建一个数据词典
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="dictNames">数据词典路径全名</param>
- /// <param name="create">当数据词典不存在时是否创建</param>
- /// <returns>成功返回数据词典的ObjectId,否则返回ObjectId.Null</returns>
- public static DBDictionary GetDictionary(Transaction ctrans, string dictNames, bool create)
- {
- char[] chars = { '.' };
- string[] nameArr = dictNames.Split(chars);
- return GetDictionary(ctrans, nameArr, create);
- }
- /// <summary>
- /// 获取扩展记录
- /// </summary>
- /// <param name="dictNames">扩展记录所在数据词典全名数组</param>
- /// <param name="xrecordName">扩展记录名</param>
- /// <returns>成功返回扩展记录的ObjectId,否则返回ObjectId.Null</returns>
- public static ObjectId GetXrecord(string[] dictNames, string xrecordName)
- {
- try
- {
- ObjectId reId = ObjectId.Null;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction()) //Start the transaction.
- {
- ObjectId dictId;
- DBDictionary curDict = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- for (int i = 0; i < dictNames.Length; i++)
- {
- if (curDict.Contains(dictNames[i]))
- {
- dictId = curDict.GetAt(dictNames[i]);
- curDict = (DBDictionary)ctrans.GetObject(dictId, OpenMode.ForRead);
- }
- else
- {
- ctrans.Dispose();
- return ObjectId.Null;
- }
- }
- if (curDict.Contains(xrecordName))
- {
- Xrecord xRec = (Xrecord)ctrans.GetObject(curDict.GetAt(xrecordName), OpenMode.ForRead);
- reId = xRec.ObjectId;
- }
- ctrans.Commit();
- }
- return reId;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return ObjectId.Null;
- }
- }
- /// <summary>
- /// 获取扩展记录
- /// </summary>
- /// <param name="dictNames">扩展记录所在数据词典全名</param>
- /// <param name="xrecordName">扩展记录名</param>
- /// <returns>成功返回扩展记录的ObjectId,否则返回ObjectId.Null</returns>
- public static ObjectId GetXrecord(string dictNames, string xrecordName)
- {
- try
- {
- char[] chars = { '.' };
- string[] nameArr = dictNames.Split(chars);
- return GetXrecord(nameArr, xrecordName);
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return ObjectId.Null;
- }
- }
- /// <summary>
- /// 设置数据词典记录,若数据词典和词典记录还没有存在就创建
- /// Version : 2007.10.30
- /// Note :
- /// 1,resBuf = ModuleDict.PartDict
- /// 2,resBuf = ModuleDict
- /// </summary>
- /// <param name="dictNames">数据词典全名</param>
- /// <param name="xrecordName">词典记录名</param>
- /// <param name="resBuf">将要写入词典记录的链表</param>
- /// <param name="replace">false当已有记录时在后面追加链表内容,true替换原有内容</param>
- /// <returns></returns>
- public static bool SetXrecord(string dictNames, string xrecordName, ResultBuffer resBuf, bool replace)
- {
- try
- {
- //smc.ed.WriteMessage("\nadb.SetXrecord :" + dictNames+xrecordName);
- if (resBuf == null) return false;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- ObjectId dictId = sXr.GetDictionary(dictNames, true);
- if (dictId == ObjectId.Null)
- {
- ctrans.Dispose(); return false;
- }
- DBDictionary pDict = (DBDictionary)ctrans.GetObject(dictId, OpenMode.ForRead);
- if (pDict.Contains(xrecordName))
- {
- DBObject pXrec = (DBObject)ctrans.GetObject(pDict.GetAt(xrecordName), OpenMode.ForWrite);
- if (pXrec.GetType() == typeof(Xrecord))
- {
- if (replace)
- {
- ((Xrecord)pXrec).Data = resBuf;
- }
- else
- {
- ResultBuffer oldResBuf = ((Xrecord)pXrec).Data;
- foreach (TypedValue typeValue in resBuf)
- {
- oldResBuf.Add(typeValue);
- }
- ((Xrecord)pXrec).Data = oldResBuf;
-
- }
- ctrans.Commit();
- return true;
- }
- else
- {
- ctrans.Commit();
- return false;
- }
- }
- else
- {
- pDict.UpgradeOpen();
- Xrecord pXrec = new Xrecord();
- pXrec.Data = resBuf;
- pDict.SetAt(xrecordName, pXrec);
- ctrans.AddNewlyCreatedDBObject(pXrec, true);
- ctrans.Commit();
- return true;
- }
- }
- }
- catch (System.Exception ex)
- {
- smc.ed.WriteMessage("\nadb.SetXrecord dictNames={0},xrecordName={1}:", dictNames, xrecordName);
- smc.WriteLine(ex);
- return false;
- }
- }
-
- /// <summary>
- /// 设置系统变量
- /// 系统变量在扩展记录的数据结构KeyName=KeyValue
- /// Version : 2008.11.05
- /// </summary>
- /// <param name="dictNames">数据词典路径</param>
- /// <param name="xrName">扩展记录名</param>
- /// <param name="varName">自定义系统变量名</param>
- /// <param name="varValue">自定义系统变量值</param>
- /// <returns></returns>
- public static bool SetSysVar(string dictNames, string xrName, string varName, object varValue)
- {
- try
- {
- //smc.ed.WriteMessage("\nsXr.SetSysVar start varName={0},varValue={1}",varName,varValue);
- ResultBuffer rBuf = sXr.GetResultBufferFromXrecord(dictNames, xrName);
- rBuf = sXr.SetKeyValue(rBuf, varName, varValue, "", false);
- if (rBuf != null)
- return sXr.SetXrecord(dictNames, xrName, rBuf, true);
- else
- return false;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- /// <summary>
- /// 获取自定义系统变量
- /// 系统变量在扩展记录的数据结构KeyName=KeyValue
- /// Version : 2008.11.05
- /// </summary>
- /// <param name="dictNames">数据词典路径</param>
- /// <param name="xrName">扩展记录名</param>
- /// <param name="varName">自定义系统变量名</param>
- /// <param name="valueType">返回值数据类型</param>
- /// <returns>成功返回自定义系统变量值,否则返回null</returns>
- public static object GetSysVar(string dictNames, string xrName,string varName, System.Type valueType)
- {
- try
- {
- ResultBuffer resBuf = sXr.GetResultBufferFromXrecord(dictNames, xrName);
- if (resBuf == null) return null;
- return sXr.GetKeyValue(resBuf, varName, valueType, "");
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 设置数据词典记录,若数据词典和词典记录还没有存在就创建,若已经存在则更改为输入值
- /// Version : 2007.10.30 没有使用过
- /// </summary>
- /// <param name="rootDictionary">根数据词典</param>
- /// <param name="dictionary">数据词典</param>
- /// <param name="name">记录名</param>
- /// <param name="Value_">值</param>
- /// <returns></returns>
- public static bool SetXrecord(string rootDictionary, string dictionary, string name, string[] Value_)
- {
- //smc.WriteLine("AddXRecord start " + dictionary+ "," + name + "," + Value_.ToString() , 0);
- bool success = true;
- try
- {
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- DBDictionary sDict;
- using (DBDictionary NOD = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForWrite, false))
- {
- try
- {
- sDict = (DBDictionary)ctrans.GetObject(NOD.GetAt(rootDictionary), OpenMode.ForWrite);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- //NOD.UpgradeOpen();
- sDict = new DBDictionary();
- NOD.SetAt(rootDictionary, sDict);
- ctrans.AddNewlyCreatedDBObject(sDict, true);
- }
- }
- DBDictionary divDict;
- try
- {
- divDict = (DBDictionary)ctrans.GetObject(sDict.GetAt(dictionary), OpenMode.ForWrite);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- sDict.UpgradeOpen();
- divDict = new DBDictionary(); //Division doesn//t exist, create one
- sDict.SetAt(dictionary, divDict);
- ctrans.AddNewlyCreatedDBObject(divDict, true);
- }
- Xrecord mgrXRec;
- try
- {
- mgrXRec = (Xrecord)ctrans.GetObject(divDict.GetAt(name), OpenMode.ForWrite);
- //注意 : 在这里不能使用
- //mgrXRec.Data.Add(new TypedValue((int)DxfCode.Text, Value_.ToString()));
- //这里应该是一个Bug
- TypedValue[] tvs = new TypedValue[Value_.Length];
- for (int i = 0; i < Value_.Length; i++)
- {
- tvs[i] = new TypedValue((int)DxfCode.Text, Value_[i]);
- }
- mgrXRec.Data = new ResultBuffer(tvs);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- mgrXRec = new Xrecord();
- TypedValue[] tvs = new TypedValue[Value_.Length];
- for (int i = 0; i < Value_.Length; i++)
- {
- tvs[i] = new TypedValue((int)DxfCode.Text, Value_[i]);
- }
- mgrXRec.Data = new ResultBuffer(tvs);
- divDict.SetAt(name, mgrXRec);
- ctrans.AddNewlyCreatedDBObject(mgrXRec, true);
- }
- ctrans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- success = false;
- }
- return success;
- }
- /// <summary>
- /// 设置数据词典记录,若数据词典和词典记录还没有存在就创建,若已经存在则更改为输入值
- /// Version : 2007.10.30 没有使用过
- /// </summary>
- /// <param name="rootDictionary">根数据词典</param>
- /// <param name="dictionary">数据词典</param>
- /// <param name="name">记录名</param>
- /// <param name="recs">记录集合</param>
- /// <returns></returns>
- public static bool SetXrecord(string rootDictionary, string dictionary, string name, sRecords recs)
- {
- //smc.WriteLine("SetXrecord sRecords start " + dictionary + "," + name, 0);
- bool success = true;
- try
- {
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- DBDictionary sDict;
- using (DBDictionary NOD = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForWrite, false))
- {
- try
- {
- sDict = (DBDictionary)ctrans.GetObject(NOD.GetAt(rootDictionary), OpenMode.ForWrite);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- //NOD.UpgradeOpen();
- sDict = new DBDictionary();
- NOD.SetAt(rootDictionary, sDict);
- ctrans.AddNewlyCreatedDBObject(sDict, true);
- }
- }
- DBDictionary divDict;
- try
- {
- divDict = (DBDictionary)ctrans.GetObject(sDict.GetAt(dictionary), OpenMode.ForWrite);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- //sDict.UpgradeOpen();
- divDict = new DBDictionary(); //Division doesn//t exist, create one
- sDict.SetAt(dictionary, divDict);
- ctrans.AddNewlyCreatedDBObject(divDict, true);
- }
- Xrecord mgrXRec;
- try
- {
- mgrXRec = (Xrecord)ctrans.GetObject(divDict.GetAt(name), OpenMode.ForWrite);
- mgrXRec.Data = recs.ToResultBuffer();
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- //smc.WriteLine(ex);
- mgrXRec = new Xrecord();
- mgrXRec.Data = recs.ToResultBuffer();
- divDict.SetAt(name, mgrXRec);
- ctrans.AddNewlyCreatedDBObject(mgrXRec, true);
- }
- ctrans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- //smc.WriteLine(ex);
- success = false;
- }
- return success;
-
- }
- /// <summary>
- /// 获取扩展记录链表
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="dictNames">数据词典全名数组</param>
- /// <param name="xrecordName">扩展记录名</param>
- /// <returns>成功返回扩展记录的数据链表,否则返回null</returns>
- public static ResultBuffer GetResultBufferFromXrecord(string[] dictNames, string xrecordName)
- {
- try
- {
- ResultBuffer resBuf = null;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction()) //Start the transaction.
- {
- ObjectId dictId;
- DBDictionary curDict = (DBDictionary)ctrans.GetObject(smc.db.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- for (int i = 0; i < dictNames.Length; i++)
- {
- if (curDict.Contains(dictNames[i]))
- {
- dictId = curDict.GetAt(dictNames[i]);
- curDict = (DBDictionary)ctrans.GetObject(dictId, OpenMode.ForRead);
- }
- else
- {
- ctrans.Dispose();
- return null;
- }
- }
- if (curDict.Contains(xrecordName))
- {
- Xrecord xRec = (Xrecord)ctrans.GetObject(curDict.GetAt(xrecordName), OpenMode.ForRead);
- resBuf = xRec.Data;
- }
- ctrans.Commit();
- }
- return resBuf;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 获取扩展记录链表
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="dictNames">数据词典全名</param>
- /// <param name="xrecordName">扩展记录名</param>
- /// <returns>成功返回扩展记录的数据链表,否则返回null</returns>
- public static ResultBuffer GetResultBufferFromXrecord(string dictNames, string xrecordName)
- {
- try
- {
- char[] chars = { '.' };
- string[] nameArr = dictNames.Split(chars);
- return GetResultBufferFromXrecord(nameArr, xrecordName);
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 获取扩展记录链表
- /// Version : 2007.10.30
- /// </summary>
- /// <param name="xrecordId">扩展记录ObjectId</param>
- /// <returns>成功返回扩展记录的数据链表,否则返回null</returns>
- public static ResultBuffer GetResultBufferFromXrecord(ObjectId xrecordId)
- {
- try
- {
- if (xrecordId == ObjectId.Null) return null;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction()) //Start the transaction.
- {
- Xrecord xRec = (Xrecord)ctrans.GetObject(xrecordId, OpenMode.ForRead);
- ctrans.Commit();
- return xRec.Data;
- }
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 列出数据词典某个记录的值
- /// Version : 2007.10.30 没有使用
- /// </summary>
- /// <param name="rootDictionary"></param>
- /// <param name="dictionary">数字词典</param>
- /// <param name="name">记录名</param>
- /// <returns></returns>
- public static bool ListXrecord(string rootDictionary, string dictionary, string name)
- {
- //smc.WriteLine("ListXrecord start: " + dictionary + " " + name, 0);
- bool success = true;
- Database cdb = HostApplicationServices.WorkingDatabase;
- using (Transaction ctrans = cdb.TransactionManager.StartTransaction()) //Start the transaction.
- {
- try
- {
- DBDictionary NOD = (DBDictionary)ctrans.GetObject(cdb.NamedObjectsDictionaryId, OpenMode.ForRead, false);
- DBDictionary sDict = (DBDictionary)ctrans.GetObject(NOD.GetAt(rootDictionary), OpenMode.ForRead);
- DBDictionary divDict = (DBDictionary)ctrans.GetObject(sDict.GetAt(dictionary), OpenMode.ForRead);
- Xrecord xrec = (Xrecord)ctrans.GetObject(divDict.GetAt(name), OpenMode.ForRead);
- ResultBuffer rb = xrec.Data;
- TypedValue[] tvs = rb.AsArray();
- for (int i = 0; i < tvs.Length; i++)
- {
- smc.WriteLine("Value [" + i.ToString() + "] = " + tvs[i].Value.ToString(), 4);
- }
- ctrans.Commit();
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- // smc.WriteLine(ex);
- success = false;
- }
- }
- //smc.WriteLine("ListXrecord end " , 0);
- return success;
- }
- /// <summary>
- /// 列出数据词典的内容
- /// Version : 2007.10.30 较旧函数,应该可以替换或改进
- /// </summary>
- /// <param name="dbDictionary"></param>
- /// <param name="baseName"></param>
- public static void ListNameDictionary(DBDictionary dbDictionary, string baseName)
- {
- try
- {
- if (dbDictionary == null) return;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- DBObject dbObj;
- foreach (System.Collections.DictionaryEntry dEntry in dbDictionary)
- {
- dbObj = (DBObject)ctrans.GetObject((ObjectId)dEntry.Value, OpenMode.ForRead, false);
- //smc.ed.WriteMessage("\ndbObj.Type = " + dbObj.GetType().FullName);
- //smc.ed.WriteMessage("\nKey = " + dEntry.Key.ToString());
- if (dbObj.GetType() == typeof(DBDictionary))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(DBDictionary)");
- ListNameDictionary((DBDictionary)dbObj, baseName + dEntry.Key.ToString() + ".");
- }
- else if (dbObj.GetType() == typeof(Xrecord))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(Xrecord)");
- }
- else if (dbObj.GetType() == typeof(Layout))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(Layout)");
- }
- else if (dbObj.GetType() == typeof(TableStyle))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(TableStyle)");
- }
- else if (dbObj.GetType() == typeof(MlineStyle))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(MlineStyle)");
- }
- else if (dbObj.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.ImpDBObject")
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(ImpDBObject)");
- }
- else if (dbObj.GetType() == typeof(DictionaryWithDefaultDictionary))
- {
- smc.ed.WriteMessage("\n" + baseName + dEntry.Key.ToString() + "(DictionaryWithDefaultDictionary)");
- }
- }
- ctrans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- smc.ed.WriteMessage("\nadb.ListNameDictionary Error : " + ex.Message);
- }
- }
- #endregion Xrecord read and write
- #region XData read and write
- /// <summary>
- /// 向DBObject 对象附加扩展数据XData
- /// </summary>
- /// <param name="id">DBObject ObjectId</param>
- /// <param name="appName">注册应用程序名</param>
- /// <param name="xData">扩展数据值</param>
- /// <param name="replace">是否替换原有扩展数据</param>
- /// <returns>成功返回true</returns>
- public static bool SetXData(ObjectId id, string appName, TypedValue xData, bool replace)
- {
- try
- {
- if (id == ObjectId.Null || xData == null) return false;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- Entity ent = (Entity)ctrans.GetObject(id, OpenMode.ForWrite);
- RegAppTable rat = (RegAppTable)ctrans.GetObject(smc.db.RegAppTableId, OpenMode.ForWrite);
- if (!rat.Has(appName))
- {
- RegAppTableRecord ratr = new RegAppTableRecord();
- ratr.Name = appName;
- rat.Add(ratr);
- ctrans.AddNewlyCreatedDBObject(ratr, true);
- }
- ResultBuffer resBuf = ent.GetXDataForApplication(appName);
- if (resBuf == null || replace)
- {
- resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
- }
- resBuf.Add(xData);
- ent.XData = resBuf;
- ctrans.Commit();
- }
- return true;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- /// <summary>
- /// 向DBObject 对象附加扩展数据XData
- /// Version : 2007.10.31
- /// </summary>
- /// <param name="objId">DBObject对象 ObjectId</param>
- /// <param name="appName">注册应用程序名</param>
- /// <param name="xData">扩展数据集合</param>
- /// <param name="replace">是否替换原有数据</param>
- /// <returns>成功返回true,否则返回false</returns>
- public static bool SetXData(ObjectId objId, string appName, ResultBuffer xData, bool replace)
- {
- if (objId == ObjectId.Null || xData == null || appName == "") return false;
- try
- {
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- Entity ent = (Entity)ctrans.GetObject(objId, OpenMode.ForWrite);
- RegAppTable raTable = (RegAppTable)ctrans.GetObject(smc.db.RegAppTableId, OpenMode.ForWrite);
- if (!raTable.Has(appName))
- {
- RegAppTableRecord ratRec = new RegAppTableRecord();
- ratRec.Name = appName;
- raTable.Add(ratRec);
- ctrans.AddNewlyCreatedDBObject(ratRec, true);
- }
- ResultBuffer resBuf = ent.GetXDataForApplication(appName);
- if (resBuf == null || replace)
- {
- resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
- }
- TypedValue[] tValues = xData.AsArray();
- for (int i = 0; i < tValues.Length; i++)
- {
- resBuf.Add(tValues[i]);
- }
- ent.XData = resBuf;
- ctrans.Commit();
- }
- return true;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- public static bool SetXData(ObjectIdCollection idClt, string appName, ResultBuffer xData, bool replace)
- {
- if (idClt == null || idClt.Count == 0 || xData == null || appName == "") return false;
- try
- {
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- foreach (ObjectId objId in idClt)
- {
- if (objId != ObjectId.Null)
- {
- Entity ent = (Entity)ctrans.GetObject(objId, OpenMode.ForWrite);
- RegAppTable raTable = (RegAppTable)ctrans.GetObject(smc.db.RegAppTableId, OpenMode.ForWrite);
- if (!raTable.Has(appName))
- {
- RegAppTableRecord ratRec = new RegAppTableRecord();
- ratRec.Name = appName;
- raTable.Add(ratRec);
- ctrans.AddNewlyCreatedDBObject(ratRec, true);
- }
- ResultBuffer resBuf = ent.GetXDataForApplication(appName);
- if (resBuf == null || replace)
- {
- resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
- }
- TypedValue[] tValues = xData.AsArray();
- for (int i = 0; i < tValues.Length; i++)
- {
- resBuf.Add(tValues[i]);
- }
- ent.XData = resBuf;
- }
- }
- ctrans.Commit();
- }
- return true;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- /// <summary>
- /// 对实体添加XData扩展数据
- /// </summary>
- /// <param name="ctrans">事务</param>
- /// <param name="ent">实体</param>
- /// <param name="appName">注册程序名</param>
- /// <param name="xData">扩展数据链表</param>
- /// <param name="replace">是否替换已有扩展数据</param>
- /// <returns>成功返回true,失败返回false</returns>
- public static bool SetXData(Transaction ctrans, Entity ent, string appName, ResultBuffer xData, bool replace)
- {
- if (ctrans == null || ent == null || xData == null) return false;
- try
- {
- if (!ent.IsWriteEnabled) return false;
- RegAppTable raTable = (RegAppTable)ctrans.GetObject(smc.db.RegAppTableId, OpenMode.ForWrite);
- if (!raTable.Has(appName))
- {
- RegAppTableRecord ratRec = new RegAppTableRecord();
- ratRec.Name = appName;
- raTable.Add(ratRec);
- ctrans.AddNewlyCreatedDBObject(ratRec, true);
- }
- ResultBuffer resBuf = ent.GetXDataForApplication(appName);
- if (resBuf == null || replace)
- {
- resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
- }
- TypedValue[] tValues = xData.AsArray();
- for (int i = 0; i < tValues.Length; i++)
- {
- resBuf.Add(tValues[i]);
- }
- ent.XData = resBuf;
- return true;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dbClt"></param>
- /// <param name="appName"></param>
- /// <param name="xData"></param>
- /// <param name="replace"></param>
- /// <returns></returns>
- public static bool SetXData(Transaction ctrans, DBObjectCollection dbClt, string appName, ResultBuffer xData, bool replace)
- {
- if (dbClt == null || xData == null || appName == "") return false;
- try
- {
- RegAppTable raTable = (RegAppTable)ctrans.GetObject(smc.db.RegAppTableId, OpenMode.ForRead);
- if (!raTable.Has(appName))
- {
- raTable.UpgradeOpen();
- RegAppTableRecord ratRec = new RegAppTableRecord();
- ratRec.Name = appName;
- raTable.Add(ratRec);
- ctrans.AddNewlyCreatedDBObject(ratRec, true);
- }
- foreach (DBObject dbObj in dbClt)
- {
- if (dbObj != null && dbObj is Entity && dbObj.IsWriteEnabled)
- {
- Entity ent = (Entity)dbObj;
- ResultBuffer resBuf = ent.GetXDataForApplication(appName);
- if (resBuf == null || replace)
- {
- resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
- }
- TypedValue[] tValues = xData.AsArray();
- for (int i = 0; i < tValues.Length; i++)
- {
- resBuf.Add(tValues[i]);
- }
- ent.XData = resBuf;
- }
- }
- return true;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return false;
- }
- }
- /// <summary>
- /// 获取ObjectId的XData,
- /// Version : 2007.10.31
- /// </summary>
- /// <param name="objId">ObjectId</param>
- /// <param name="appName">注册程序名</param>
- /// <returns>成功返回XData的ResultBuffer链表,否则返回null</returns>
- public static ResultBuffer GetXData(ObjectId objId, string appName)
- {
- try
- {
- if (objId == ObjectId.Null) return null;
- ResultBuffer resBuf = null;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- Entity ent = (Entity)ctrans.GetObject(objId, OpenMode.ForRead);
- resBuf = ent.GetXDataForApplication(appName);
- ctrans.Commit();
- }
- return resBuf;
- }
- catch (System.Exception ex)
- {
- smc.WriteLine(ex);
- return null;
- }
- }
- /// <summary>
- /// 列出id实体所附加的全部扩展数据
- /// </summary>
- /// <param name="id">实体的ObjectId</param>
- public static void ShowXData(ObjectId id)
- {
- if (id == ObjectId.Null) return;
- using (Transaction ctrans = smc.db.TransactionManager.StartTransaction())
- {
- using (Entity ent = (Entity)ctrans.GetObject(id, OpenMode.ForRead))
- {
- ResultBuffer rb = ent.XData;
- if (rb != null)
- {
- TypedValue[] tvs = rb.AsArray();
- smc.ed.WriteMessage("\nObject XData :");
- for (int i = 0; i < tvs.Length; i++)
- {
- smc.ed.WriteMessage("\n " + tvs[i].TypeCode.ToString() + " : " + tvs[i].Value.ToString());
- }
- }
- else
- smc.ed.WriteMessage("\nSelect Object hasn't XData !");
- }
- }
- }
- #endregion XData read and write
-
- }
- }
|
|