转载自 Cad人生 的博客,链接:http://www.cnblogs.com/cadlife/articles/2668158.html
内容粘贴如下,小伙伴们可以看看。
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using Autodesk.AutoCAD.EditorInput ; 5 using Autodesk.AutoCAD.Runtime ; 6 using Autodesk.AutoCAD.ApplicationServices; 7 using Autodesk.AutoCAD.DatabaseServices ; 8 namespace CH02 9 { 10 public class Class1 11 { 12 //-------------------------------------------------------------- 13 // 功能:获取用户输入 14 // 作者: 15 // 日期:2007-7-20 16 // 说明: 17 // 18 //---------------------------------------------------------------- 19 [CommandMethod("GetData")] 20 public void GetData() 21 { 22 //获取Editor对象 23 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 24 //获取整型数据 25 PromptIntegerOptions intOp = new PromptIntegerOptions("请输入多边形的边数:"); 26 PromptIntegerResult intRes; 27 intRes = ed.GetInteger(intOp); 28 //判断用户输入 29 if (intRes.Status == PromptStatus.OK) 30 { 31 int nSides = intRes.Value; 32 ed.WriteMessage("多边形的边数为:" + nSides); 33 } if (intRes.Status == PromptStatus.Cancel) 34 { 35 ed.WriteMessage("用户按了取消ESC键/n" ); 36 } 37 } 38 //-------------------------------------------------------------- 39 // 功能:要求用户输入点 40 // 作者: 41 // 日期:2007-7-20 42 // 说明: 43 // 44 //---------------------------------------------------------------- 45 [CommandMethod("PickPoint")] 46 static public void PickPoint() 47 { 48 //获取Editor对象 49 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 50 PromptPointOptions promptPtOp = new PromptPointOptions("选择一个点:"); 51 //指定的基点,如果指定了该点,则在选择的时候绘制一条橡皮线。 52 promptPtOp.BasePoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0); 53 PromptPointResult resPt; 54 resPt = ed.GetPoint(promptPtOp); 55 if (resPt.Status == PromptStatus.OK) 56 { 57 ed.WriteMessage("选择的点为:" + resPt.Value.ToString()); 58 } 59 } 60 61 //-------------------------------------------------------------- 62 // 功能:获取选择集 63 // 作者: 64 // 日期:2007-7-20 65 // 说明: 66 // 67 //---------------------------------------------------------------- 68 [CommandMethod("SelectEnt")] 69 static public void SelectEnt() 70 { 71 //获取Editor对象 72 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 73 PromptSelectionOptions selectionOp = new PromptSelectionOptions(); 74 PromptSelectionResult ssRes = ed.GetSelection(selectionOp); 75 if (ssRes.Status == PromptStatus.OK) 76 { 77 SelectionSet SS = ssRes.Value; 78 int nCount = SS.Count; 79 ed.WriteMessage("选择了{0}个实体" , nCount); 80 } 81 } 82 //-------------------------------------------------------------- 83 // 功能:获取选择集(带过滤) 84 // 作者: 85 // 日期:2007-7-20 86 // 说明: 87 // 88 //---------------------------------------------------------------- 89 [CommandMethod("SelectEnt2")] 90 static public void SelectEnt2() 91 { 92 //获取Editor对象 93 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 94 // 定义选择集选项 95 PromptSelectionOptions selectionOp = new PromptSelectionOptions(); 96 //创建选择集过滤器,只选择块对象 97 TypedValue[] filList = new TypedValue[1]; 98 filList[0] = new TypedValue((int)DxfCode.Start, "INSERT"); 99 SelectionFilter filter = new SelectionFilter(filList); 100 PromptSelectionResult ssRes = ed.GetSelection(selectionOp, filter); 101 if (ssRes.Status == PromptStatus.OK) 102 { 103 SelectionSet SS = ssRes.Value; 104 int nCount = SS.Count; 105 ed.WriteMessage("选择了{0}个块" , nCount); 106 } 107 } 108 } 109 110 }
using System; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; namespace CH03 { public class Class1 { //-------------------------------------------------------------- // 功能:创建一个新层 // 作者: // 日期:2007-7-20 // 说明: // //---------------------------------------------------------------- [CommandMethod("CreateLayer")] public void CreateLayer() { ObjectId layerId; Database db = HostApplicationServices.WorkingDatabase; //开始一个事务 Transaction trans = db.TransactionManager.StartTransaction(); try { //首先取得层表 LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite); //检查MyLayer层是否存在 if (lt.Has("MyLayer")) { layerId = lt["MyLayer"]; } else { //如果MyLayer层不存在,就创建它 LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "MyLayer"; //设置层的名字 layerId = lt.Add(ltr); trans.AddNewlyCreatedDBObject(ltr, true); } //提交事务 trans.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception e) { //放弃事务 trans.Abort(); } finally { // 显式地释放 trans.Dispose(); } } //-------------------------------------------------------------- // 功能:创建一个圆 // 作者: // 日期:2007-7-20 // 说明: // //---------------------------------------------------------------- [CommandMethod("CreateCircle")] public void CreateCircle() { Database db = HostApplicationServices.WorkingDatabase; // 使用 "using" ,结束是自动调用事务的 "Dispose" using (Transaction trans = db.TransactionManager.StartTransaction()) { //获取块表和模型空间 BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead)); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); //创建一个圆并添加到块表记录(模型空间) Point3d center = new Point3d(10, 10, 0); Circle circle = new Circle(center, Vector3d.ZAxis, 10.0); circle.ColorIndex = 1; btr.AppendEntity(circle); trans.AddNewlyCreatedDBObject(circle, true); trans.Commit(); } } //-------------------------------------------------------------- // 功能:创建一个块定义(块表记录) // 作者: // 日期:2007-7-20 // 说明: // //---------------------------------------------------------------- public ObjectId CreateBlkDef() { //定义函数的返回值ObjectId ObjectId blkObjId = new ObjectId(); Database db = HostApplicationServices.WorkingDatabase; // 使用 "using"关键字指定事务的边界 using (Transaction trans = db.TransactionManager.StartTransaction()) { //获取块表 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite); //通过块名myBlkName判断块表中是否包含块表记录 if ((bt.Has("myBlkName"))) { blkObjId = bt["myBlkName"];//如果已经存在,通过块名获取块对应的ObjectId } else { //创建一个圆 Point3d center = new Point3d(10, 10, 0); Circle circle = new Circle(center, Vector3d.ZAxis, 2); circle.ColorIndex = 1; //创建文本Text: MText text = new MText(); text.Contents = " "; text.Location = center; text.ColorIndex = 2; //创建新的块表记录 myBlkName BlockTableRecord newBtr = new BlockTableRecord(); newBtr.Name = "myBlkName"; newBtr.Origin = center; //保存块表记录到块表 blkObjId = bt.Add(newBtr); // 返回块对应的ObjectId trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database! //保存新创建的实体到块表记录 newBtr.AppendEntity(circle); newBtr.AppendEntity(text); // 通知事务新创建了对象 trans.AddNewlyCreatedDBObject(circle, true); trans.AddNewlyCreatedDBObject(text, true); } trans.Commit(); //提交事务 } return blkObjId; } //-------------------------------------------------------------- // 功能:创建一个块引用 // 作者: // 日期:2007-7-20 // 说明: // //---------------------------------------------------------------- [CommandMethod("CreateBlk")] public void CreateBlkRef() { //获取块的插入点 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptPointOptions ptOps = new PromptPointOptions("选择块的插入点"); PromptPointResult ptRes; ptRes = ed.GetPoint(ptOps); Point3d ptInsert; if (ptRes.Status == PromptStatus.OK) { ptInsert = ptRes.Value ; } else { ptInsert = new Point3d(0, 0, 0); } Database db = HostApplicationServices.WorkingDatabase; // 使用 "using"关键字指定事务的边界 using (Transaction trans = db.TransactionManager.StartTransaction()) { //获取块表和模型空间 BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite)); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); //创建块引用 BlockReference blkRef = new BlockReference(ptInsert,CreateBlkDef());// 指定插入点和所引用的块表记录 blkRef.Rotation = 1.57;//指定旋转角,按弧度 //保存新创建的块引用到模型空间 btr.AppendEntity(blkRef); trans.AddNewlyCreatedDBObject(blkRef, true); // 通知事务新创建了对象 trans.Commit(); //提交事务 } } //-------------------------------------------------------------- // 功能:读取对象的属性 // 作者: // 日期:2007-7-20 // 说明: // //---------------------------------------------------------------- [CommandMethod("OpenEnt")] public void OpenEnt() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象"); PromptEntityResult entRes; entRes = ed.GetEntity(entOps); if (entRes.Status != PromptStatus.OK) { ed.WriteMessage("选择对象失败,退出"); return; } ObjectId objId = entRes.ObjectId; Database db = HostApplicationServices.WorkingDatabase; using (Transaction trans = db.TransactionManager.StartTransaction()) { Entity ent = (Entity)trans.GetObject(objId, OpenMode.ForWrite); ent.ColorIndex = 1; trans.Commit(); } } } }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using Autodesk.AutoCAD.EditorInput; 5 using Autodesk.AutoCAD.Runtime; 6 using Autodesk.AutoCAD.ApplicationServices; 7 using Autodesk.AutoCAD.DatabaseServices; 8 using Autodesk.AutoCAD.Geometry; 9 namespace CH04 10 { 11 public class Class1 12 { 13 14 //-------------------------------------------------------------- 15 // 功能:通过ObjectId打开对象 16 // 作者: 17 // 日期:2007-7-20 18 // 说明: 19 // 20 //---------------------------------------------------------------- 21 [CommandMethod("OpenEnt")] 22 public void OpenEnt() 23 { 24 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 25 ed.WriteMessage("通过ObjectId打开对象\\n"); 26 PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\\n"); 27 PromptEntityResult entRes; 28 entRes = ed.GetEntity(entOps); 29 if (entRes.Status != PromptStatus.OK) 30 { 31 ed.WriteMessage("选择对象失败,退出"); 32 return; 33 } 34 ObjectId objId = entRes.ObjectId; 35 Database db = HostApplicationServices.WorkingDatabase; 36 using (Transaction trans = db.TransactionManager.StartTransaction()) 37 { 38 Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity ; 39 ent.ColorIndex = 1; 40 trans.Commit(); 41 } 42 43 } 44 //-------------------------------------------------------------- 45 // 功能:类型识别和转换 46 // 作者: 47 // 日期:2007-7-20 48 // 说明: 49 // 50 //---------------------------------------------------------------- 51 [CommandMethod("GetType")] 52 public void GetType() 53 { 54 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 55 ed.WriteMessage("数据库对象的类型识别和转换\\n"); 56 PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象"); 57 PromptEntityResult entRes; 58 entRes = ed.GetEntity(entOps); 59 if (entRes.Status != PromptStatus.OK) 60 { 61 ed.WriteMessage("选择对象失败,退出"); 62 return; 63 } 64 ObjectId objId = entRes.ObjectId; 65 Database db = HostApplicationServices.WorkingDatabase; 66 using (Transaction trans = db.TransactionManager.StartTransaction()) 67 { 68 Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity; 69 ed.WriteMessage("ent.GetRXClass().Name :" + ent.GetRXClass().Name + "\\n"); 70 if (ent is Line) 71 { 72 Line aLine = ent as Line; 73 aLine.ColorIndex = 1; 74 } 75 else if (ent.GetType() == typeof(Circle)) 76 { 77 Circle cir = (Circle)ent; 78 cir.ColorIndex = 2; 79 } 80 trans.Commit(); 81 } 82 } 83 //-------------------------------------------------------------- 84 // 功能:实体对象的属性 85 // 作者: 86 // 日期:2007-7-20 87 // 说明: 88 // 89 //---------------------------------------------------------------- 90 [CommandMethod("EntPro")] 91 public void EntPro() 92 { 93 94 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 95 ed.WriteMessage("实体对象的属性\\n"); 96 PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\\n"); 97 PromptEntityResult entRes; 98 entRes = ed.GetEntity(entOps); 99 if (entRes.Status != PromptStatus.OK) 100 { 101 ed.WriteMessage("选择对象失败,退出\\n"); 102 return; 103 } 104 ObjectId objId = entRes.ObjectId; 105 Database db = HostApplicationServices.WorkingDatabase; 106 using (Transaction trans = db.TransactionManager.StartTransaction()) 107 { 108 Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity; 109 ed.WriteMessage("获取或设置实体的线型\\n"); 110 ed.WriteMessage("实体的原先的线型为 :" + ent.Linetype + "\\n"); 111 // 获取线型表记录 112 LinetypeTable lineTypeTbl = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable; 113 // 确保DOT线型名已经加载到当前数据库 114 LinetypeTableRecord lineTypeTblRec = trans.GetObject(lineTypeTbl["DOT"], OpenMode.ForRead) as LinetypeTableRecord; 115 // 设置实体的线型 116 ent.LinetypeId = lineTypeTblRec.ObjectId; 117 // 设置实体的线型比例 118 ed.WriteMessage("设置实体的线型比例为2.0\\n"); 119 ent.LinetypeScale = 2.0; 120 //设置实体的可见性 121 ent.Visible = true; 122 //设置实体所在的层 123 ed.WriteMessage("实体的原先所在的层为 :" + ent.Layer + "\\n"); 124 ent.Layer = "layer0"; 125 trans.Commit(); 126 } 127 } 128 } 129 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Collections; 5 using Autodesk.AutoCAD.EditorInput; 6 using Autodesk.AutoCAD.Runtime; 7 using Autodesk.AutoCAD.ApplicationServices; 8 using Autodesk.AutoCAD.DatabaseServices; 9 using Autodesk.AutoCAD.Geometry; 10 namespace CH05 11 { 12 public class Class1 13 { 14 //-------------------------------------------------------------- 15 // 功能:添加扩展数据XDATA 16 // 作者: 17 // 日期:2007-7-20 18 // 说明: 19 // 20 //---------------------------------------------------------------- 21 [CommandMethod("AddXData")] 22 public void AddXData() 23 { 24 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 25 ed.WriteMessage("添加扩充数据XDATA\\n"); 26 PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\\n"); 27 PromptEntityResult entRes; 28 entRes = ed.GetEntity(entOps); 29 if (entRes.Status != PromptStatus.OK) 30 { 31 ed.WriteMessage("选择对象失败,退出"); 32 return; 33 } 34 ObjectId objId = entRes.ObjectId; 35 Database db = HostApplicationServices.WorkingDatabase; 36 using (Transaction trans = db.TransactionManager.StartTransaction()) 37 { 38 Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity ; 39 ent.ColorIndex = 1; 40 RegAppTable appTbl = trans.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable ; 41 if (!appTbl.Has("MyAppName")) 42 { 43 RegAppTableRecord appTblRcd = new RegAppTableRecord(); 44 appTblRcd.Name = "MyAppName"; 45 appTbl.Add(appTblRcd); 46 trans.AddNewlyCreatedDBObject(appTblRcd, true); 47 } 48 ResultBuffer resBuf = new ResultBuffer();//new TypedValue(1001, "MyAppName"), new TypedValue(1000, "开发部门")); 49 resBuf.Add(new TypedValue(1001, "MyAppName"));//注册程序名称 50 resBuf.Add(new TypedValue(1000 , " 张三"));//姓名 51 resBuf.Add(new TypedValue(1000 , " 工程部"));//部门 52 resBuf.Add(new TypedValue(1040, 2000.0));//薪水 53 ent.XData = resBuf; 54 trans.Commit(); 55 } 56 57 } 58 59 //-------------------------------------------------------------- 60 // 功能:获取扩展数据XDATA 61 // 作者: 62 // 日期:2007-7-20 63 // 说明: 64 // 65 //------------------------------------------------------------ 66 [CommandMethod("GETXDATA")] 67 public void GETXDATA() 68 { 69 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 70 ed.WriteMessage("获取扩充数据XDATA\\n"); 71 PromptEntityOptions entOps = new PromptEntityOptions("选择带扩展数据的对象"); 72 PromptEntityResult entRes = ed.GetEntity(entOps); 73 if (entRes.Status != PromptStatus.OK) 74 { 75 ed.WriteMessage("选择对象失败,退出"); 76 return; 77 } 78 Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; 79 using (Transaction trans = db.TransactionManager.StartTransaction()) 80 { 81 Entity ent = (Entity)trans.GetObject(entRes.ObjectId, OpenMode.ForRead); 82 ResultBuffer resBuf = ent.XData; 83 if (resBuf != null) 84 { 85 // 86 IEnumerator iter = resBuf.GetEnumerator(); 87 while (iter.MoveNext()) 88 { 89 TypedValue tmpVal = (TypedValue)iter.Current; 90 ed.WriteMessage(tmpVal.TypeCode.ToString() + ":"); 91 ed.WriteMessage(tmpVal.Value.ToString() + "\\n"); 92 } 93 } 94 } 95 } 96 //-------------------------------------------------------------- 97 // 功能:在命名对象词典中添加数据 98 // 作者: 99 // 日期:2007-7-20 100 // 说明: 101 // 102 //------------------------------------------------------------ 103 [CommandMethod("AddInNOD")] 104 public void AddInNOD() 105 { 106 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 107 ed.WriteMessage("在命名对象词典中添加数据\\n"); 108 Database db = HostApplicationServices.WorkingDatabase; 109 using (Transaction trans = db.TransactionManager.StartTransaction()) 110 { 111 //获取命名对象词典(NOD) 112 DBDictionary NOD =trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite) as DBDictionary ; 113 // 声明一个新的词典 114 DBDictionary copyrightDict; 115 // 判断是否存在COPYRIGHT词典,没有则创建 116 try 117 { 118 // 获取COPYRIGHT词典 119 copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("COPYRIGHT"), OpenMode.ForRead); 120 } 121 catch 122 { 123 //在NOD下创建COPYRIGHT词典 124 copyrightDict = new DBDictionary(); 125 NOD.SetAt("COPYRIGHT", copyrightDict); 126 trans.AddNewlyCreatedDBObject(copyrightDict, true); 127 } 128 // 在copyrightDict中,获取或创建 "author" 词典 129 DBDictionary authorDict; 130 try 131 { 132 authorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForWrite); 133 } 134 catch 135 { 136 authorDict = new DBDictionary(); 137 //"author" doesn‘t exist, create one 138 copyrightDict.UpgradeOpen(); 139 copyrightDict.SetAt("Author", authorDict); 140 trans.AddNewlyCreatedDBObject(authorDict, true); 141 } 142 // 通过Xrecord和ResultBuffer添加扩展数据 143 Xrecord authorRec; 144 try 145 { 146 authorRec = (Xrecord)trans.GetObject(authorDict.GetAt("AuthorInfo"), OpenMode.ForWrite); 147 } 148 catch 149 { 150 authorRec = new Xrecord(); 151 authorRec.Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "张三")); 152 authorDict.SetAt("AuthorInfo", authorRec); 153 trans.AddNewlyCreatedDBObject(authorRec, true); 154 } 155 trans.Commit(); 156 } 157 } 158 //-------------------------------------------------------------- 159 // 功能:获取命名对象词典中的数据 160 // 作者: 161 // 日期:2007-7-20 162 // 说明: 163 // 164 //------------------------------------------------------------ 165 [CommandMethod("GetInNOD")] 166 public void GetInNod() 167 { 168 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 169 ed.WriteMessage("获取命名对象词典中数据\\n"); 170 Database db = HostApplicationServices.WorkingDatabase; 171 using (Transaction trans = db.TransactionManager.StartTransaction()) 172 { 173 // 获取NOD 174 DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false); 175 // 获取COPYRIGHT词典 176 DBDictionary copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("COPYRIGHT"), OpenMode.ForRead); 177 // 获取Author词典 178 DBDictionary AuthorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForRead); 179 // 获取AuthorInfo扩展记录Xrecord 180 Xrecord authorXRec = (Xrecord)trans.GetObject(AuthorDict.GetAt("AuthorInfo"), OpenMode.ForRead); 181 ResultBuffer resBuf = authorXRec.Data; 182 TypedValue val = resBuf.AsArray()[0]; 183 ed.WriteMessage("该图纸由{0}设计\\n", val.Value); 184 } 185 } 186 //-------------------------------------------------------------- 187 // 功能:添加数据到数据库对象的扩展词典中 188 // 作者: 189 // 日期:2007-7-20 190 // 说明: 191 // 192 //------------------------------------------------------------ 193 [CommandMethod("AddExtDict")] 194 public void AddExtDict() 195 { 196 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 197 ed.WriteMessage("创建对象扩展词典\\n"); 198 PromptEntityOptions entOps = new PromptEntityOptions("选择要添加扩展数据的块\\n"); 199 PromptEntityResult entRes = ed.GetEntity(entOps); 200 if (entRes.Status != PromptStatus.OK) 201 { 202 ed.WriteMessage("选择对象失败,退出"); 203 return; 204 } 205 Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; 206 using (Transaction trans = db.TransactionManager.StartTransaction()) 207 { 208 DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject; 209 BlockReference blkRef; 210 if (obj is BlockReference) 211 { 212 blkRef = obj as BlockReference; 213 } 214 else 215 { 216 return; 217 } 218 // 创建对象的扩展词典 219 blkRef.CreateExtensionDictionary(); 220 DBDictionary extensionDict = (DBDictionary)trans.GetObject(blkRef.ExtensionDictionary, OpenMode.ForWrite, false); 221 222 // 通过Xrecord准备附加属性数据 223 Xrecord xRec = new Xrecord(); 224 xRec.Data = new ResultBuffer( 225 new TypedValue((int)DxfCode.Text, "张三"),// 姓名 226 new TypedValue((int)DxfCode.Real, 1200.0),//薪水 227 new TypedValue((int)DxfCode.Text, "技术部"));// 部门 228 // 在扩展词典中添加扩展记录 229 extensionDict.SetAt("EmployeeInfomation", xRec); 230 trans.AddNewlyCreatedDBObject(xRec, true); 231 trans.Commit(); 232 } 233 } 234 235 //-------------------------------------------------------------- 236 // 功能:获取数据库对象的扩展词典中的数据 237 // 作者: 238 // 日期:2007-7-20 239 // 说明: 240 // 241 //------------------------------------------------------------ 242 [CommandMethod("GetExtDict")] 243 public void GetExtDict() 244 { 245 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 246 ed.WriteMessage("获取对象扩展词典信息\\n"); 247 PromptEntityOptions entOps = new PromptEntityOptions("选择添加了扩展数据的块\\n"); 248 PromptEntityResult entRes = ed.GetEntity(entOps); 249 if (entRes.Status != PromptStatus.OK) 250 { 251 ed.WriteMessage("选择对象失败,退出"); 252 return; 253 } 254 Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; 255 using (Transaction trans = db.TransactionManager.StartTransaction()) 256 { 257 DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject; 258 BlockReference blkRef; 259 if (obj is BlockReference) 260 { 261 blkRef = obj as BlockReference; 262 } 263 else 264 { 265 ed.WriteMessage("选择对象不是块,退出\\n"); 266 return; 267 } 268 // 创建对象的扩展词典 269 DBDictionary extensionDict = (DBDictionary)trans.GetObject(blkRef.ExtensionDictionary, OpenMode.ForWrite, false); 270 // 获取AuthorInfo扩展记录Xrecord 271 Xrecord EmpXRec = (Xrecord)trans.GetObject(extensionDict.GetAt("EmployeeInfomation"), OpenMode.ForRead); 272 ResultBuffer resBuf = EmpXRec.Data; 273 TypedValue val = resBuf.AsArray()[0]; 274 ed.WriteMessage("是员工姓名:{0}\\n", val.Value); 275 val = resBuf.AsArray()[1]; 276 ed.WriteMessage("该员工的薪水:{0}\\n", val.Value); 277 val = resBuf.AsArray()[2]; 278 ed.WriteMessage("该员工属于:{0}\\n", val.Value); 279 280 trans.Commit(); 281 } 282 } 283 } 284 }