SolidUtil.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:SolidUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/12 10:13:46
  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 Autodesk.Revit.DB;
  13. namespace FWindSoft.Revit.Utils
  14. {
  15. public static class SolidUtil
  16. {
  17. #region 静态方法
  18. /// <summary>
  19. /// 替换视图中的图形,来修改颜色
  20. /// </summary>
  21. public static void SetColor(Element elem, Color color, View view = null,
  22. int transparency = 0)
  23. {
  24. var ogs = new OverrideGraphicSettings();
  25. #region 表面填充图案
  26. //可见
  27. ogs.SetProjectionFillPatternVisible(true);
  28. //颜色
  29. ogs.SetProjectionFillColor(color);
  30. //透明度
  31. ogs.SetSurfaceTransparency(transparency);
  32. #endregion
  33. #region 截面填充图案
  34. //可见
  35. ogs.SetCutFillPatternVisible(true);
  36. //颜色
  37. ogs.SetCutFillColor(color);
  38. //填充图案
  39. #endregion
  40. if (view != null)
  41. {
  42. view.SetElementOverrides(elem.Id, ogs);
  43. }
  44. else
  45. {
  46. elem.Document.ActiveView.SetElementOverrides(elem.Id, ogs);
  47. }
  48. }
  49. /// <summary>
  50. /// 使用指定的球心和半径创建球体
  51. /// </summary>
  52. public static Solid CreateSphereAtPoint(
  53. XYZ center,
  54. double radius)
  55. {
  56. // 使用标准的全局坐标系创建 Frame
  57. Frame frame = new Frame(center,
  58. XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ);
  59. // 创建一个Z轴方向的半圆闭合曲线(注意所有的坐标都是相对全局坐标系的)
  60. Arc arc = Arc.Create(
  61. center - radius * XYZ.BasisZ,
  62. center + radius * XYZ.BasisZ,
  63. center + radius * XYZ.BasisX);
  64. Line line = Line.CreateBound(
  65. arc.EndPoint(),
  66. arc.StartPoint());
  67. CurveLoop halfCircle = new CurveLoop();
  68. halfCircle.Append(arc);
  69. halfCircle.Append(line);
  70. List<CurveLoop> loops = new List<CurveLoop>(1);
  71. loops.Add(halfCircle);
  72. return GeometryCreationUtilities
  73. .CreateRevolvedGeometry(
  74. frame, loops, 0, 2 * Math.PI);
  75. }
  76. public static Solid CreateSphere(List<CurveLoop> loops,XYZ vector)
  77. {
  78. return GeometryCreationUtilities.CreateExtrusionGeometry(loops, vector.Normalize(), vector.GetLength());
  79. }
  80. #endregion
  81. }
  82. }