CurveElementExtension.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Autodesk.Revit.DB;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace FWindSoft.Revit
  8. {
  9. public static class CurveElementExtension
  10. {
  11. public static GraphicsStyle GetGraphicStyle(this CurveElement curveElement,string styleName)
  12. {
  13. var ids = curveElement.GetLineStyleIds();
  14. var document = curveElement.Document;
  15. foreach (var id in ids)
  16. {
  17. var style=document.GetElement(id) as GraphicsStyle;
  18. if(style!=null&&styleName== style.Name)
  19. {
  20. return style;
  21. }
  22. }
  23. return null;
  24. }
  25. public static GraphicsStyle SetLineStyle(this CurveElement curveElement, Color color, int lineWeight,
  26. string styleName)
  27. {
  28. if (string.IsNullOrWhiteSpace(styleName))
  29. {
  30. styleName =string.Format(color.ToString()+"-"+ lineWeight);
  31. }
  32. var useStyle = curveElement.GetGraphicStyle(styleName);
  33. if (useStyle == null)
  34. {
  35. var subCategory = curveElement.Document.Settings.Categories.NewSubcategory(curveElement.Category, styleName);
  36. useStyle = subCategory.GetGraphicsStyle(GraphicsStyleType.Projection);
  37. }
  38. useStyle.GraphicsStyleCategory.LineColor = color;
  39. if (lineWeight > 0)
  40. {
  41. useStyle.GraphicsStyleCategory.SetLineWeight(lineWeight, GraphicsStyleType.Projection);
  42. }
  43. curveElement.LineStyle = useStyle;
  44. return useStyle;
  45. }
  46. }
  47. }