C# CreateEmployee()函数

开发 后端
这里我们来修改C# CreateEmployee()函数,以让它可以接收名字、薪水、部门和职位并返回创建的雇员块索引的ObjectId。

C#语言有很多值得学习的地方,这里我们主要介绍C# CreateEmployee()函数,包括介绍 测试C# CreateEmployee()函数。加入一个Test命令来测试CreateEmployee等方面。

修改C# CreateEmployee()函数以重用

1)让我们来修改C# CreateEmployee()函数,以让它可以接收名字、薪水、部门和职位并返回创建的雇员块索引的ObjectId。函数的形式如下(你可以改变参数顺序)

  1. public ObjectId CreateEmployee
    (string name, string division, double salary, Point3d pos) 

2) 移除上面函数中的CommandMethod属性”CREATE”,这样它就不再是用来创建雇员的命令。

3) 修改函数的代码,这样就可以正确地设置块索引的名字、职位、部门和薪水和它的扩展字典。

  1. //替换  
  2. BlockReference br = new BlockReference
    (new Point3d(10, 10, 0), CreateEmployeeDefinition());  
  3. //为  
  4. BlockReference br = new BlockReference
    (pos, CreateEmployeeDefinition());  
  1. //替换  
  2.  
  3. xRec.Data = new ResultBuffer(  
  4. new TypedValue((int)DxfCode.Text, "Earnest Shackleton"),  
  5. new TypedValue((int)DxfCode.Real, 72000),  
  6. new TypedValue((int)DxfCode.Text, "Sales"));  
  7.  
  8. //为  
  9.  
  10. xRec.Data = new ResultBuffer(  
  11. new TypedValue((int)DxfCode.Text, name),  
  12. new TypedValue((int)DxfCode.Real, salary),  
  13. new TypedValue((int)DxfCode.Text, division));  

4) 因为我们把雇员的名字从MText替换成块的属性定义,因此我们要创建一个相应的属性索引来显示雇员的名字。属性索引将使用属性定义的属性。

  1. //替换:  
  2.  
  3. btr.AppendEntity(br);//加入索引到模型空间  
  4. trans.AddNewlyCreatedDBObject(br,true);//让事务处理知道  
  5.  
  6. //为  
  7.  
  8. AttributeReferenceattRef=newAttributeReference();  
  9. //遍历雇员块来查找属性定义  
  10. BlockTableRecordempBtr=(BlockTableRecord)trans.
    GetObject(bt["EmployeeBlock"],OpenMode.ForRead);  
  11. foreach(ObjectIdidinempBtr)  
  12. {  
  13. Entityent=(Entity)trans.GetObject(id,OpenMode.ForRead,false);  
  14. //打开当前的对象!  
  15. if(entisAttributeDefinition)  
  16. {  
  17. //设置属性为属性索引中的属性定义  
  18. AttributeDefinitionattDef=((AttributeDefinition)(ent));  
  19. attRef.SetPropertiesFrom(attDef);  
  20. attRef.Position=newPoint3d(attDef.Position.X+br.Position.X,
    attDef.Position.Y+br.Position.Y,attDef.Position.Z+br.Position.Z);  
  21. attRef.Height=attDef.Height;  
  22. attRef.Rotation=attDef.Rotation;  
  23. attRef.Tag=attDef.Tag;  
  24. attRef.TextString=name;  
  25. }  
  26. }  
  27. //把索引加入模型空间  
  28. btr.AppendEntity(br);  
  29. //把属性索引加入到块索引  
  30. br.AttributeCollection.AppendAttribute(attRef);  
  31. //让事务处理知道  
  32. trans.AddNewlyCreatedDBObject(attRef,true);  
  33. trans.AddNewlyCreatedDBObject(br,true); 

5)不要忘记返回雇员块索引的ObjectId,但要在提交事务处理之后才能返回:

  1. trans.Commit();  
  2. return br.ObjectId;  

6) 测试C# CreateEmployee()函数。加入一个Test命令来测试CreateEmployee:

  1. [CommandMethod("Test")]  
  2. public void Test()  
  3. {  
  4. CreateEmployee("Earnest Shackleton", "Sales", 10000, new Point3d(10, 10, 0));  
  5. }  

【编辑推荐】

  1. C#创建快捷方式简单描述
  2. C#压缩Access数据库详细介绍
  3. C#实现加载动态库概述
  4. C#日期型数据简单剖析
  5. C#装箱和拆箱简单描述
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-31 09:59:13

C# CreateEm

2009-08-31 10:14:49

C# CreateEm

2009-07-31 14:26:38

JavaScript函C#函数

2009-07-30 15:24:13

C#析构函数C#构造函数

2009-08-19 14:26:58

C# JavaScri

2009-08-10 14:43:03

C#函数Convert

2009-07-31 14:54:48

dll函数C#导出

2009-07-31 14:08:54

C# 匿名函数

2009-07-31 16:00:30

C#函数重载

2009-08-24 18:09:13

C#构造函数

2009-07-31 14:15:38

C# 构造函数

2009-07-31 14:03:21

C# Format函数

2009-08-14 17:24:28

C#构造函数和析构函数

2009-07-31 15:22:56

C#判等函数

2009-07-31 15:37:45

C#静态构造函数

2009-08-20 14:28:00

C#静态构造函数

2009-09-04 11:15:07

选择C#构造函数

2009-07-31 16:12:10

Windows APIC#

2009-08-07 17:12:07

C# DLL函数

2009-08-25 10:59:00

C#调用函数显示值
点赞
收藏

51CTO技术栈公众号