C# 中CreateEmployee()函数如何使用

发布时间:2021-07-07 17:27:23 作者:Leah
来源:亿速云 阅读:127

今天就跟大家聊聊有关C# 中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());  

//替换   xRec.Data = new ResultBuffer(  new TypedValue((int)DxfCode.Text, "Earnest Shackleton"),  new TypedValue((int)DxfCode.Real, 72000),  new TypedValue((int)DxfCode.Text, "Sales"));   //为   xRec.Data = new ResultBuffer(  new TypedValue((int)DxfCode.Text, name),  new TypedValue((int)DxfCode.Real, salary),  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,但要在提交事务处理之后才能返回:

trans.Commit();  return br.ObjectId;

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

[CommandMethod("Test")]  public void Test()  {  CreateEmployee("Earnest Shackleton", "Sales", 10000, new Point3d(10, 10, 0));  }

看完上述内容,你们对C# 中CreateEmployee()函数如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. File类中构造方法及功能是什么
  2. php跳转语句是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:C#中怎么实现事件注册和注销

下一篇:​dng格式文件怎么打开

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》