溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 中CreateEmployee()函數(shù)如何使用

發(fā)布時(shí)間:2021-07-07 17:27:23 來(lái)源:億速云 閱讀:141 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)C# 中CreateEmployee()函數(shù)如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

修改C# CreateEmployee()函數(shù)以重用

1)讓我們來(lái)修改C# CreateEmployee()函數(shù),以讓它可以接收名字、薪水、部門和職位并返回創(chuàng)建的雇員塊索引的ObjectId。函數(shù)的形式如下(你可以改變參數(shù)順序)

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

2) 移除上面函數(shù)中的CommandMethod屬性”CREATE”,這樣它就不再是用來(lái)創(chuàng)建雇員的命令。

3) 修改函數(shù)的代碼,這樣就可以正確地設(shè)置塊索引的名字、職位、部門和薪水和它的擴(kuò)展字典。

  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) 因?yàn)槲覀儼压蛦T的名字從MText替換成塊的屬性定義,因此我們要?jiǎng)?chuàng)建一個(gè)相應(yīng)的屬性索引來(lái)顯示雇員的名字。屬性索引將使用屬性定義的屬性。

  1. //替換:  

  2.  

  3. btr.AppendEntity(br);//加入索引到模型空間  

  4. trans.AddNewlyCreatedDBObject(br,true);//讓事務(wù)處理知道  

  5.  

  6. //為  

  7.  

  8. AttributeReferenceattRef=newAttributeReference();  

  9. //遍歷雇員塊來(lái)查找屬性定義  

  10. BlockTableRecordempBtr=(BlockTableRecord)trans.
    GetObject(bt["EmployeeBlock"],OpenMode.ForRead);  

  11. foreach(ObjectIdidinempBtr)  

  12. {  

  13. Entityent=(Entity)trans.GetObject(id,OpenMode.ForRead,false);  

  14. //打開當(dāng)前的對(duì)象!  

  15. if(entisAttributeDefinition)  

  16. {  

  17. //設(shè)置屬性為屬性索引中的屬性定義  

  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. //讓事務(wù)處理知道  

  32. trans.AddNewlyCreatedDBObject(attRef,true);  

  33. trans.AddNewlyCreatedDBObject(br,true); 

5)不要忘記返回雇員塊索引的ObjectId,但要在提交事務(wù)處理之后才能返回:

trans.Commit();  return br.ObjectId;

6) 測(cè)試C# CreateEmployee()函數(shù)。加入一個(gè)Test命令來(lái)測(cè)試CreateEmployee:

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

看完上述內(nèi)容,你們對(duì)C# 中CreateEmployee()函數(shù)如何使用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI