AutoCAD .net开发之三-图形绘制

  1 None.gif using  System ;
  2 None.gif using  Autodesk.AutoCAD.Runtime ;
  3 None.gif using  Autodesk.AutoCAD.Geometry;
  4 None.gif using  Autodesk.AutoCAD.DatabaseServices;
  5 None.gif using  Autodesk.AutoCAD.Colors;
  6 None.gif
  7 None.gif[assembly: CommandClass( typeof (ClassLibrary.Class))]
  8 None.gif
  9 None.gif namespace  ClassLibrary
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 12InBlock.gif    /// Summary description for Class.
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    public class Class
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        public Class()
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif    
 19ExpandedSubBlockEnd.gif        }

 20InBlock.gif
 21InBlock.gif
 22InBlock.gif
 23InBlock.gif        [CommandMethod("test1")]
 24InBlock.gif        public void createCircle()
 25InBlock.gif
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            //首先声明我们要使用的对象
 28InBlock.gif
 29InBlock.gif            Circle circle; //这个是我们要加入到模型空间的圆
 30InBlock.gif
 31InBlock.gif            BlockTableRecord btr;//要加入圆,我们必须打开模型空间
 32InBlock.gif            BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它
 33InBlock.gif            //我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
 34InBlock.gif            Transaction trans;
 35InBlock.gif
 36InBlock.gif            //使用TransactionManager的StartTransaction()成员来开始事务处理
 37InBlock.gif            
 38InBlock.gif            trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
 39InBlock.gif            //现在创建圆……请仔细看这些参数——注意创建Point3d对象的‘New’和Vector3d的静态成员ZAxis
 40InBlock.gif            
 41InBlock.gif            circle = new Circle(new Point3d(10100), Vector3d.ZAxis, 2);
 42InBlock.gif            bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
 43InBlock.gif            //使用当前的空间Id来获取块表记录——注意我们是打开它用来写入
 44InBlock.gif            btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite );
 45InBlock.gif            //现在使用btr对象来加入圆
 46InBlock.gif            btr.AppendEntity(circle);
 47InBlock.gif            trans.AddNewlyCreatedDBObject(circle, true); //并确定事务处理知道要加入圆!
 48InBlock.gif            //一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
 49InBlock.gif            trans.Commit();
 50InBlock.gif            //…然后销毁事务处理,因为我们已经完成了相关的操作(事务处理不是数据库驻留对象,可以销毁)
 51InBlock.gif
 52InBlock.gif            
 53InBlock.gif
 54InBlock.gif
 55InBlock.gif
 56InBlock.gif            trans.Dispose();
 57InBlock.gif
 58InBlock.gif            
 59InBlock.gif
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif        [CommandMethod("test2")]
 62InBlock.gif        public void f()
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            Transaction trans1 = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
 65InBlock.gif            Ellipse e = new Ellipse(new Point3d(10,10,0),Vector3d.ZAxis,new Vector3d(3,0,0),0.5,0,0);
 66InBlock.gif            BlockTable bt1 = (BlockTable)trans1.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId,OpenMode.ForRead);
 67InBlock.gif            BlockTableRecord btr1 = (BlockTableRecord)trans1.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite);
 68InBlock.gif            btr1.AppendEntity(e);
 69InBlock.gif            trans1.AddNewlyCreatedDBObject(e,true);
 70InBlock.gif            trans1.Commit();
 71InBlock.gif
 72InBlock.gif            
 73ExpandedSubBlockEnd.gif        }

 74InBlock.gif
 75InBlock.gif
 76InBlock.gif        public ObjectId CreateLayer()
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78InBlock.gif            ObjectId layerId; //它返回函数的值
 79InBlock.gif        
 80InBlock.gif            Database db = HostApplicationServices.WorkingDatabase;
 81InBlock.gif            Transaction trans = db.TransactionManager.StartTransaction();
 82InBlock.gif            //首先取得层表……
 83InBlock.gif            LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
 84InBlock.gif            //检查EmployeeLayer层是否存在……
 85InBlock.gif            if (lt.Has("EmployeeLayer"))
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                layerId = lt["EmployeeLayer"];
 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif            else
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 91InBlock.gif                //如果EmployeeLayer层不存在,就创建它
 92InBlock.gif
 93InBlock.gif                LayerTableRecord ltr = new LayerTableRecord();
 94InBlock.gif                ltr.Name = "EmployeeLayer"//设置层的名字
 95InBlock.gif                ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
 96InBlock.gif                layerId = lt.Add(ltr);
 97InBlock.gif                trans.AddNewlyCreatedDBObject(ltr, true);
 98InBlock.gif
 99ExpandedSubBlockEnd.gif            }

100InBlock.gif
101InBlock.gif            trans.Commit();
102InBlock.gif
103InBlock.gif            trans.Dispose();
104InBlock.gif
105InBlock.gif            return layerId;
106InBlock.gif
107ExpandedSubBlockEnd.gif        }

108InBlock.gif
109InBlock.gif
110InBlock.gif
111ExpandedSubBlockEnd.gif    }

112ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值