【共享】acad的net例子,初学者可以看一看
<p>using System;<br/>using System.Collections.Generic;<br/>using System.Text;<br/>using Autodesk.AutoCAD.EditorInput ;<br/>using Autodesk.AutoCAD.Runtime ;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.DatabaseServices ;</p><p>namespace CH02<br/>{<br/> public class Class1<br/> {</p><p> //--------------------------------------------------------------<br/> // 功能:获取用户输入<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> // <br/> //----------------------------------------------------------------<br/> <br/> public void GetData()<br/> {<br/> //获取Editor对象<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> //获取整型数据<br/> PromptIntegerOptions intOp = new PromptIntegerOptions("请输入多边形的边数:");<br/> PromptIntegerResult intRes;<br/> intRes = ed.GetInteger(intOp);<br/> //判断用户输入<br/> if (intRes.Status == PromptStatus.OK)<br/> {<br/> int nSides = intRes.Value;<br/> ed.WriteMessage("多边形的边数为:" + nSides);<br/> } if (intRes.Status == PromptStatus.Cancel)<br/> {<br/> ed.WriteMessage("用户按了取消ESC键/n" );<br/> }</p><p> }</p><p> //--------------------------------------------------------------<br/> // 功能:要求用户输入点<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> // <br/> //----------------------------------------------------------------<br/> <br/> static public void PickPoint() <br/> {<br/> //获取Editor对象<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> PromptPointOptions promptPtOp = new PromptPointOptions("选择一个点:");<br/> //指定的基点,如果指定了该点,则在选择的时候绘制一条橡皮线。<br/> promptPtOp.BasePoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);<br/> PromptPointResult resPt; <br/> resPt = ed.GetPoint(promptPtOp); <br/> if (resPt.Status == PromptStatus.OK) <br/> {</p><p> ed.WriteMessage("选择的点为:" + resPt.Value.ToString());<br/> } <br/> }</p><p><br/> //--------------------------------------------------------------<br/> // 功能:获取选择集<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> // <br/> //----------------------------------------------------------------<br/> <br/> static public void SelectEnt() <br/> {<br/> //获取Editor对象<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> PromptSelectionOptions selectionOp = new PromptSelectionOptions();<br/> PromptSelectionResult ssRes = ed.GetSelection(selectionOp);<br/> if (ssRes.Status == PromptStatus.OK)<br/> {<br/> SelectionSet SS = ssRes.Value;<br/> int nCount = SS.Count;<br/> ed.WriteMessage("选择了{0}个实体" , nCount);<br/> } <br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:获取选择集(带过滤)<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> // <br/> //----------------------------------------------------------------<br/> <br/> static public void SelectEnt2() <br/> {<br/> //获取Editor对象<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;</p><p> // 定义选择集选项<br/> PromptSelectionOptions selectionOp = new PromptSelectionOptions();<br/> //创建选择集过滤器,只选择块对象<br/> TypedValue[] filList = new TypedValue;<br/> filList = new TypedValue((int)DxfCode.Start, "INSERT");<br/> SelectionFilter filter = new SelectionFilter(filList);</p><p> PromptSelectionResult ssRes = ed.GetSelection(selectionOp, filter);<br/> if (ssRes.Status == PromptStatus.OK)<br/> {<br/> SelectionSet SS = ssRes.Value;<br/> int nCount = SS.Count;<br/> ed.WriteMessage("选择了{0}个块" , nCount);<br/> } </p><p> }</p><p> }<br/> <br/> }<br/> </p> <p>using System;<br/>using System.Collections.Generic;<br/>using System.Text;<br/>using Autodesk.AutoCAD.EditorInput;<br/>using Autodesk.AutoCAD.Runtime;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.DatabaseServices;<br/>using Autodesk.AutoCAD.Geometry;</p><p>namespace CH03<br/>{<br/> public class Class1<br/> {</p><p> //--------------------------------------------------------------<br/> // 功能:创建一个新层<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------</p><p> <br/> public void CreateLayer()<br/> {<br/> ObjectId layerId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> //开始一个事务<br/> Transaction trans = db.TransactionManager.StartTransaction();<br/> try<br/> {<br/> //首先取得层表<br/> LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);<br/> //检查MyLayer层是否存在<br/> if (lt.Has("MyLayer"))<br/> {<br/> layerId = lt["MyLayer"];<br/> }<br/> else<br/> {<br/> //如果MyLayer层不存在,就创建它<br/> LayerTableRecord ltr = new LayerTableRecord();<br/> ltr.Name = "MyLayer"; //设置层的名字<br/> layerId = lt.Add(ltr);<br/> trans.AddNewlyCreatedDBObject(ltr, true);<br/> }<br/> //提交事务<br/> trans.Commit();<br/> }<br/> catch (Autodesk.AutoCAD.Runtime.Exception e)<br/> {<br/> //放弃事务<br/> trans.Abort();<br/> }<br/> finally<br/> {<br/> // 显式地释放<br/> trans.Dispose();<br/> }<br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:创建一个圆<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void CreateCircle()<br/> {<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> // 使用 "using" ,结束是自动调用事务的 "Dispose" <br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> //获取块表和模型空间<br/> BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));<br/> BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);</p><p> //创建一个圆并添加到块表记录(模型空间)<br/> Point3d center = new Point3d(10, 10, 0);<br/> Circle circle = new Circle(center, Vector3d.ZAxis, 10.0);<br/> circle.ColorIndex = 1;</p><p> btr.AppendEntity(circle);<br/> trans.AddNewlyCreatedDBObject(circle, true);<br/> trans.Commit();<br/> }</p><p> }</p><p> //--------------------------------------------------------------<br/> // 功能:创建一个块定义(块表记录)<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> // <br/> //----------------------------------------------------------------<br/> public ObjectId CreateBlkDef()<br/> {<br/> //定义函数的返回值ObjectId<br/> ObjectId blkObjId = new ObjectId(); <br/> Database db = HostApplicationServices.WorkingDatabase; </p><p> // 使用 "using"关键字指定事务的边界<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> //获取块表<br/> BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);<br/> //通过块名myBlkName判断块表中是否包含块表记录<br/> if ((bt.Has("myBlkName")))<br/> {<br/> blkObjId = bt["myBlkName"];//如果已经存在,通过块名获取块对应的ObjectId<br/> }<br/> else<br/> {<br/> //创建一个圆<br/> Point3d center = new Point3d(10, 10, 0); <br/> Circle circle = new Circle(center, Vector3d.ZAxis, 2);<br/> circle.ColorIndex = 1; <br/> //创建文本Text:<br/> MText text = new MText();<br/> text.Contents = " ";<br/> text.Location = center;<br/> text.ColorIndex = 2;</p><p> //创建新的块表记录 myBlkName<br/> BlockTableRecord newBtr = new BlockTableRecord();<br/> newBtr.Name = "myBlkName";<br/> newBtr.Origin = center;<br/> //保存块表记录到块表<br/> blkObjId = bt.Add(newBtr); // 返回块对应的ObjectId<br/> trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database!<br/> <br/> //保存新创建的实体到块表记录<br/> newBtr.AppendEntity(circle); <br/> newBtr.AppendEntity(text);<br/> // 通知事务新创建了对象<br/> trans.AddNewlyCreatedDBObject(circle, true);<br/> trans.AddNewlyCreatedDBObject(text, true);<br/> }<br/> trans.Commit(); //提交事务<br/> }<br/> return blkObjId;<br/> }</p><p><br/> //--------------------------------------------------------------<br/> // 功能:创建一个块引用<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void CreateBlkRef()<br/> {<br/> <br/> <br/> //获取块的插入点<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> PromptPointOptions ptOps = new PromptPointOptions("选择块的插入点");<br/> PromptPointResult ptRes;<br/> ptRes = ed.GetPoint(ptOps);<br/> Point3d ptInsert;<br/> if (ptRes.Status == PromptStatus.OK)<br/> {<br/> ptInsert = ptRes.Value ;<br/> }<br/> else<br/> {<br/> ptInsert = new Point3d(0, 0, 0);<br/> }</p><p> Database db = HostApplicationServices.WorkingDatabase;<br/> // 使用 "using"关键字指定事务的边界<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> //获取块表和模型空间<br/> BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));<br/> BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);<br/> <br/> //创建块引用<br/> BlockReference blkRef = new BlockReference(ptInsert,CreateBlkDef());// 指定插入点和所引用的块表记录<br/> blkRef.Rotation = 1.57;//指定旋转角,按弧度</p><p> //保存新创建的块引用到模型空间 <br/> btr.AppendEntity(blkRef); <br/> trans.AddNewlyCreatedDBObject(blkRef, true); // 通知事务新创建了对象</p><p> trans.Commit(); //提交事务<br/> }<br/> <br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:读取对象的属性<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void OpenEnt()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象");<br/> PromptEntityResult entRes;<br/> entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> ObjectId objId = entRes.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Entity ent = (Entity)trans.GetObject(objId, OpenMode.ForWrite);<br/> ent.ColorIndex = 1;<br/> trans.Commit();<br/> }</p><p> }<br/> }<br/>}<br/></p> <p>using System;<br/>using System.Collections.Generic;<br/>using System.Text;</p><p>using Autodesk.AutoCAD.EditorInput;<br/>using Autodesk.AutoCAD.Runtime;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.DatabaseServices;<br/>using Autodesk.AutoCAD.Geometry;<br/>namespace CH04<br/>{<br/> public class Class1<br/> {</p><p><br/> //--------------------------------------------------------------<br/> // 功能:通过ObjectId打开对象<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void OpenEnt()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("通过ObjectId打开对象\n");<br/> PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\n");<br/> PromptEntityResult entRes;<br/> entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> ObjectId objId = entRes.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity ;<br/> ent.ColorIndex = 1;<br/> trans.Commit();<br/> }<br/> <br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:类型识别和转换<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void GetType()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("数据库对象的类型识别和转换\n");</p><p> PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象");<br/> PromptEntityResult entRes;<br/> entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> ObjectId objId = entRes.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity;<br/> ed.WriteMessage("ent.GetRXClass().Name :" + ent.GetRXClass().Name + "\n");</p><p> if (ent is Line)<br/> {<br/> Line aLine = ent as Line;<br/> aLine.ColorIndex = 1;<br/> }<br/> else if (ent.GetType() == typeof(Circle))<br/> {<br/> Circle cir = (Circle)ent;<br/> cir.ColorIndex = 2;<br/> }</p><p> trans.Commit();<br/> }<br/> }<br/> //--------------------------------------------------------------<br/> // 功能:实体对象的属性<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void EntPro()<br/> {<br/> <br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("实体对象的属性\n");</p><p> PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\n");<br/> PromptEntityResult entRes;<br/> entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出\n");<br/> return;<br/> }<br/> ObjectId objId = entRes.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity;<br/> ed.WriteMessage("获取或设置实体的线型\n");<br/> ed.WriteMessage("实体的原先的线型为 :" + ent.Linetype + "\n");<br/> // 获取线型表记录<br/> LinetypeTable lineTypeTbl = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;<br/> // 确保DOT线型名已经加载到当前数据库<br/> LinetypeTableRecord lineTypeTblRec = trans.GetObject(lineTypeTbl["DOT"], OpenMode.ForRead) as LinetypeTableRecord;<br/> // 设置实体的线型<br/> ent.LinetypeId = lineTypeTblRec.ObjectId;</p><p> // 设置实体的线型比例<br/> ed.WriteMessage("设置实体的线型比例为2.0\n");<br/> ent.LinetypeScale = 2.0;</p><p> //设置实体的可见性<br/> ent.Visible = true;</p><p> //设置实体所在的层<br/> ed.WriteMessage("实体的原先所在的层为 :" + ent.Layer + "\n");<br/> ent.Layer = "layer0";<br/> trans.Commit();<br/> }<br/> }<br/> }<br/>}<br/></p> <p>using System;<br/>using System.Collections.Generic;<br/>using System.Text;<br/>using System.Collections;</p><p>using Autodesk.AutoCAD.EditorInput;<br/>using Autodesk.AutoCAD.Runtime;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.DatabaseServices;<br/>using Autodesk.AutoCAD.Geometry;</p><p>namespace CH05<br/>{<br/> public class Class1<br/> {</p><p> //--------------------------------------------------------------<br/> // 功能:添加扩展数据XDATA<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void AddXData()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("添加扩充数据XDATA\n");<br/> PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\n");<br/> PromptEntityResult entRes;<br/> entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> ObjectId objId = entRes.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity ;<br/> ent.ColorIndex = 1;</p><p> RegAppTable appTbl = trans.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable ;<br/> if (!appTbl.Has("MyAppName"))<br/> {<br/> RegAppTableRecord appTblRcd = new RegAppTableRecord();<br/> appTblRcd.Name = "MyAppName";<br/> appTbl.Add(appTblRcd);<br/> trans.AddNewlyCreatedDBObject(appTblRcd, true);<br/> }<br/> ResultBuffer resBuf = new ResultBuffer();//new TypedValue(1001, "MyAppName"), new TypedValue(1000, "开发部门"));</p><p> resBuf.Add(new TypedValue(1001, "MyAppName"));//注册程序名称<br/> resBuf.Add(new TypedValue(1000 , " 张三"));//姓名<br/> resBuf.Add(new TypedValue(1000 , " 工程部"));//部门<br/> resBuf.Add(new TypedValue(1040, 2000.0));//薪水<br/> ent.XData = resBuf;<br/> trans.Commit();<br/> }<br/> <br/> }</p><p><br/> //--------------------------------------------------------------<br/> // 功能:获取扩展数据XDATA<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //------------------------------------------------------------<br/> <br/> public void GETXDATA()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("获取扩充数据XDATA\n");</p><p> PromptEntityOptions entOps = new PromptEntityOptions("选择带扩展数据的对象");<br/> PromptEntityResult entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {</p><p> Entity ent = (Entity)trans.GetObject(entRes.ObjectId, OpenMode.ForRead);<br/> ResultBuffer resBuf = ent.XData;<br/> if (resBuf != null)<br/> {<br/> //<br/> IEnumerator iter = resBuf.GetEnumerator();<br/> while (iter.MoveNext())<br/> {<br/> TypedValue tmpVal = (TypedValue)iter.Current;<br/> ed.WriteMessage(tmpVal.TypeCode.ToString() + ":");<br/> ed.WriteMessage(tmpVal.Value.ToString() + "\n");<br/> }<br/> }<br/> }<br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:在命名对象词典中添加数据<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //------------------------------------------------------------<br/> <br/> public void AddInNOD()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("在命名对象词典中添加数据\n");<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> //获取命名对象词典(NOD)<br/> DBDictionary NOD =trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite) as DBDictionary ;<br/> // 声明一个新的词典<br/> DBDictionary copyrightDict;<br/> // 判断是否存在COPYRIGHT词典,没有则创建<br/> try<br/> {<br/> // 获取COPYRIGHT词典<br/> copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("COPYRIGHT"), OpenMode.ForRead);<br/> }<br/> catch<br/> {<br/> //在NOD下创建COPYRIGHT词典<br/> copyrightDict = new DBDictionary();<br/> NOD.SetAt("COPYRIGHT", copyrightDict);<br/> trans.AddNewlyCreatedDBObject(copyrightDict, true);<br/> }</p><p> // 在copyrightDict中,获取或创建 "author" 词典<br/> DBDictionary authorDict;<br/> try<br/> {<br/> authorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForWrite);<br/> }<br/> catch<br/> {<br/> authorDict = new DBDictionary();<br/> //"author" doesn't exist, create one<br/> copyrightDict.UpgradeOpen();<br/> copyrightDict.SetAt("Author", authorDict);<br/> trans.AddNewlyCreatedDBObject(authorDict, true);<br/> }</p><p> // 通过Xrecord和ResultBuffer添加扩展数据<br/> Xrecord authorRec;<br/> try<br/> {<br/> authorRec = (Xrecord)trans.GetObject(authorDict.GetAt("AuthorInfo"), OpenMode.ForWrite);<br/> }<br/> catch<br/> {<br/> authorRec = new Xrecord();<br/> authorRec.Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "张三"));<br/> authorDict.SetAt("AuthorInfo", authorRec);<br/> trans.AddNewlyCreatedDBObject(authorRec, true);<br/> }<br/> trans.Commit();<br/> }<br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:获取命名对象词典中的数据<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //------------------------------------------------------------<br/> <br/> public void GetInNod()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("获取命名对象词典中数据\n");</p><p> Database db = HostApplicationServices.WorkingDatabase;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> // 获取NOD <br/> DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false);<br/> // 获取COPYRIGHT词典<br/> DBDictionary copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("COPYRIGHT"), OpenMode.ForRead);<br/> // 获取Author词典<br/> DBDictionary AuthorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForRead);<br/> // 获取AuthorInfo扩展记录Xrecord<br/> Xrecord authorXRec = (Xrecord)trans.GetObject(AuthorDict.GetAt("AuthorInfo"), OpenMode.ForRead);<br/> ResultBuffer resBuf = authorXRec.Data;<br/> TypedValue val = resBuf.AsArray();<br/> ed.WriteMessage("该图纸由{0}设计\n", val.Value);<br/> }<br/> }</p><p> //--------------------------------------------------------------<br/> // 功能:添加数据到数据库对象的扩展词典中<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //------------------------------------------------------------<br/> <br/> public void AddExtDict()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("创建对象扩展词典\n");</p><p> PromptEntityOptions entOps = new PromptEntityOptions("选择要添加扩展数据的块\n");<br/> PromptEntityResult entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject;<br/> BlockReference blkRef;<br/> if (obj is BlockReference)<br/> {<br/> blkRef = obj as BlockReference;<br/> }<br/> else<br/> {<br/> return;<br/> }</p><p> // 创建对象的扩展词典<br/> blkRef.CreateExtensionDictionary();<br/> DBDictionary extensionDict = (DBDictionary)trans.GetObject(blkRef.ExtensionDictionary, OpenMode.ForWrite, false);<br/> <br/> // 通过Xrecord准备附加属性数据<br/> Xrecord xRec = new Xrecord();<br/> xRec.Data = new ResultBuffer(<br/> new TypedValue((int)DxfCode.Text, "张三"),// 姓名<br/> new TypedValue((int)DxfCode.Real, 1200.0),//薪水<br/> new TypedValue((int)DxfCode.Text, "技术部"));// 部门 <br/> // 在扩展词典中添加扩展记录<br/> extensionDict.SetAt("EmployeeInfomation", xRec); <br/> trans.AddNewlyCreatedDBObject(xRec, true);</p><p> trans.Commit();<br/> }</p><p> }</p><p><br/> //--------------------------------------------------------------<br/> // 功能:获取数据库对象的扩展词典中的数据<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //------------------------------------------------------------<br/> <br/> public void GetExtDict()<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("获取对象扩展词典信息\n");</p><p> PromptEntityOptions entOps = new PromptEntityOptions("选择添加了扩展数据的块\n");<br/> PromptEntityResult entRes = ed.GetEntity(entOps);<br/> if (entRes.Status != PromptStatus.OK)<br/> {<br/> ed.WriteMessage("选择对象失败,退出");<br/> return;<br/> }<br/> Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;<br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject;<br/> BlockReference blkRef;<br/> if (obj is BlockReference)<br/> {<br/> blkRef = obj as BlockReference;<br/> }<br/> else<br/> {<br/> ed.WriteMessage("选择对象不是块,退出\n");<br/> return;<br/> }</p><p> // 创建对象的扩展词典<br/> DBDictionary extensionDict = (DBDictionary)trans.GetObject(blkRef.ExtensionDictionary, OpenMode.ForWrite, false);<br/> // 获取AuthorInfo扩展记录Xrecord<br/> Xrecord EmpXRec = (Xrecord)trans.GetObject(extensionDict.GetAt("EmployeeInfomation"), OpenMode.ForRead);<br/> ResultBuffer resBuf = EmpXRec.Data;<br/> TypedValue val = resBuf.AsArray();<br/> ed.WriteMessage("是员工姓名:{0}\n", val.Value);<br/> val = resBuf.AsArray();<br/> ed.WriteMessage("该员工的薪水:{0}\n", val.Value);<br/> val = resBuf.AsArray();<br/> ed.WriteMessage("该员工属于:{0}\n", val.Value);<br/> <br/> trans.Commit();<br/> }</p><p> }<br/> }<br/>}</p> <p>唉,。。。。怎么这样多英文啊,,比LSP难多啦</p> //--------------------------------------------------------------<br/> // 功能:创建一个圆<br/> // 作者: <br/> // 日期:2007-7-20<br/> // 说明:<br/> //<br/> //----------------------------------------------------------------<br/> <br/> public void CreateCircle()<br/> {<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> // 使用 "using" ,结束是自动调用事务的 "Dispose" <br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> //获取块表和模型空间<br/> BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));<br/> BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);<p> //创建一个圆并添加到块表记录(模型空间)<br/> Point3d center = new Point3d(10, 10, 0);<br/> Circle circle = new Circle(center, Vector3d.ZAxis, 10.0);<br/> circle.ColorIndex = 1;</p><p> btr.AppendEntity(circle);<br/> trans.AddNewlyCreatedDBObject(circle, true);<br/> trans.Commit();<br/> }</p><p> }</p><p></p><p>新建一个层都这么多代码啊,还全是英文来的,唉还是VLISP办法快些,少些</p> 谢谢,正想学习一下 不错,对入门及了解cad与.net的相互关系很有用。 <p>很好 论坛需要你这样的同志 再来点吧</p> liminnet发表于2009-6-3 22:02:00static/image/common/back.gif //-------------------------------------------------------------- // 功能:创建一个圆  <p>唉,现在要把话返过来啦,还是C#好的,代码符合语言书写规律,一目了然,不难学</p>