CreateSpaceSolidCommand.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:CreateSpaceSolidCommand
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/12 17:12:59
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using Autodesk.Revit.Attributes;
  14. using Autodesk.Revit.DB;
  15. using Autodesk.Revit.DB.Mechanical;
  16. using Autodesk.Revit.UI;
  17. using FWindSoft.Revit;
  18. using FWindSoft.Revit.ExtensibleStorage;
  19. using FWindSoft.Revit.Utils;
  20. namespace LRH.Tool
  21. {
  22. public class User
  23. {
  24. public string Name { get; set; } = "JJPPS";
  25. public int Age { get; set; } = 29;
  26. }
  27. [Transaction(TransactionMode.Manual)]
  28. [Regeneration(RegenerationOption.Manual)]
  29. public class CreateSpaceSolidCommand : ExternalCommand
  30. {
  31. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  32. {
  33. var doc = RevitCore.Doc;
  34. using (Transaction tran = new Transaction(doc, "标记"))
  35. {
  36. try
  37. {
  38. tran.Start();
  39. var infoOld = RevitCore.Doc.ProjectInformation.GetSimpleClass<User>();
  40. if (infoOld == null)
  41. {
  42. infoOld = new User();
  43. }
  44. else
  45. {
  46. infoOld.Age = infoOld.Age + 1;
  47. }
  48. RevitCore.Doc.ProjectInformation.SetSimpleClass(infoOld);
  49. tran.Commit();
  50. MessageBox.Show("创建成功");
  51. }
  52. catch (Exception ex)
  53. {
  54. MessageBox.Show(ex.Message.ToString());
  55. tran.RollBack();
  56. }
  57. }
  58. //RevitCore.Doc.Create.NewFamilyInstance(,)
  59. #region 标记创建
  60. //using (Transaction tran = new Transaction(doc, "标记"))
  61. //{
  62. // try
  63. // {
  64. // tran.Start();
  65. // var elementIds = RevitCore.Selection.GetElementIds();
  66. // List<Element> newElements = new List<Element>();
  67. // foreach (var elementId in elementIds)
  68. // {
  69. // var element = RevitCore.Doc.GetElement(elementId);
  70. // if (element is Space space)
  71. // {
  72. // var segments = space.GetBoundarySegments(new SpatialElementBoundaryOptions());
  73. // List<CurveLoop> loops = new List<CurveLoop>();
  74. // foreach (var segment in segments)
  75. // {
  76. // CurveLoop loop = new CurveLoop();
  77. // foreach (var boundarySegment in segment)
  78. // {
  79. // loop.Append(boundarySegment.GetCurve());
  80. // }
  81. // if (loop.Any())
  82. // {
  83. // loops.Add(loop);
  84. // }
  85. // }
  86. // if (!loops.Any())
  87. // {
  88. // continue;
  89. // }
  90. // DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
  91. // var lowLevel =
  92. // (doc.GetElement(space.GetParameterElementId(BuiltInParameter.ROOM_LEVEL_ID)) as Level)
  93. // .Elevation;
  94. // var highLevel =
  95. // (doc.GetElement(space.GetParameterElementId(BuiltInParameter.ROOM_UPPER_LEVEL)) as Level)
  96. // .Elevation;
  97. // double height = (highLevel + space.GetParameterDouble(BuiltInParameter.ROOM_UPPER_OFFSET)) - (lowLevel + space.GetParameterDouble(BuiltInParameter.ROOM_LOWER_OFFSET));
  98. // var solid = SolidUtil.CreateSphere(loops, XYZ.BasisZ * height);
  99. // ds.AppendShape(new List<GeometryObject>() { solid });
  100. // newElements.Add(ds);
  101. // }
  102. // }
  103. // tran.Commit();
  104. // if (newElements.Any())
  105. // {
  106. // RevitCore.UIApp.SetShowElements(newElements);
  107. // MessageBox.Show("创建成功");
  108. // }
  109. // }
  110. // catch (Exception ex)
  111. // {
  112. // MessageBox.Show(ex.Message.ToString());
  113. // tran.RollBack();
  114. // }
  115. //}
  116. #endregion
  117. return Result.Succeeded;
  118. }
  119. }
  120. }