BimDocument.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:BimDocument
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/13 11:01:06
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace JBIM
  14. {
  15. /// <summary>
  16. /// Bim文件类信息
  17. /// </summary>
  18. public class BimDocument
  19. {
  20. public BimDocument()
  21. {
  22. BimObjects = new ReadOnlyCollection<BimObject>(m_InnerObjects = new List<BimObject>());
  23. }
  24. private List<BimObject> m_InnerObjects;
  25. private Dictionary<BimId, BimObject> m_IndexObjects = new Dictionary<BimId, BimObject>();
  26. /// <summary>
  27. /// 关联所有对象
  28. /// </summary>
  29. public ReadOnlyCollection<BimObject> BimObjects { get; private set; }
  30. /// <summary>
  31. /// 当前索引信息
  32. /// </summary>
  33. private long CurrentIndex { get; set; } = 10000;
  34. internal BimId GenerateId()
  35. {
  36. return new BimId((++CurrentIndex).ToString());
  37. }
  38. public BimObject NewObject(BimObject originObject)
  39. {
  40. if (originObject.Id != null)
  41. {
  42. return originObject;
  43. }
  44. originObject.Id = GenerateId();
  45. AddObject(originObject);
  46. return originObject;
  47. }
  48. /// <summary>
  49. /// 增加预定义对象
  50. /// </summary>
  51. /// <param name="originObject"></param>
  52. /// <returns></returns>
  53. public bool AddPredefinedObject(BimObject originObject)
  54. {
  55. if (originObject.Id == null)
  56. {
  57. return false;
  58. }
  59. if (m_IndexObjects.TryGetValue(originObject.Id, out BimObject oldObject))
  60. {
  61. //移除信息
  62. if (oldObject != null)
  63. {
  64. m_InnerObjects.Remove(oldObject);
  65. }
  66. m_IndexObjects[originObject.Id] = originObject;
  67. }
  68. else
  69. {
  70. AddObject(originObject);
  71. }
  72. return true;
  73. }
  74. private void AddObject(BimObject originObject)
  75. {
  76. this.m_InnerObjects.Add(originObject);
  77. m_IndexObjects[originObject.Id] = originObject;
  78. }
  79. public BimObject GetBimObject(BimId id)
  80. {
  81. //查询可优化,简单的二分查找
  82. if (m_IndexObjects.TryGetValue(id, out BimObject result))
  83. {
  84. return result;
  85. }
  86. return null;
  87. }
  88. }
  89. }