样式demo

This commit is contained in:
GG Z
2025-12-28 11:47:54 +08:00
parent ceccab9abb
commit 1fd8d2ced7
65 changed files with 2369 additions and 799 deletions

View File

@@ -1,29 +0,0 @@
using Autodesk.Revit.DB;
namespace ShrlAlgoToolkit.Revit.Assists;
public static class CategoryAssist
{
public static Element ToElement(this ElementId elementId, Document doc)
{
return doc.GetElement(elementId);
}
public static Category ToCategory(this BuiltInCategory builtInCategory, Document doc)
{
return Category.GetCategory(doc, builtInCategory);
}
public static BuiltInCategory ToBuiltInCategory(this Category category)
{
#if REVIT2018 || REVIT2020
var builtInCategory = (BuiltInCategory)category.Id.IntegerValue;
#elif REVIT2025
var builtInCategory = (BuiltInCategory)category.Id.Value;
#endif
return Enum.IsDefined(typeof(BuiltInCategory), builtInCategory)
? builtInCategory
: throw new ArgumentNullException(nameof(category), "不存在该内建类别");
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
namespace ShrlAlgoToolkit.Core.Assists
{
public class ColorAssist
{
public static Color GetDistinctColorById(ElementId id)
{
int hash = id.GetHashCode();
// 修正点:强制转换为 (int),利用 unchecked 溢出机制
// 2654435761 在 int 中就是 -1640531527
// unchecked 关键字确保在某些开启了溢出检查的项目配置中不会报错
unchecked
{
hash = hash * (int)2654435761;
}
// 2. 将哈希值映射到 0.0 - 1.0 的色相 (Hue)
// 使用黄金分割比 0.6180339887... 累加可以让颜色在色环上分布最均匀
double goldenRatioConjugate = 0.618033988749895;
double h = (hash * goldenRatioConjugate) % 1.0;
// 保证 h 为正数
if (h < 0) h += 1.0;
// 3. 固定饱和度 (S) 和 亮度 (V) 为较高值,保证颜色鲜艳
double s = 0.7; // 饱和度 0.0-1.0
double v = 0.9; // 亮度 0.0-1.0
// 4. HSV 转 Revit Color (RGB)
return HsvToRgb(h, s, v);
}
// 辅助函数HSV 转 Revit Color
private static Color HsvToRgb(double h, double s, double v)
{
int hi = (int)(h * 6);
double f = h * 6 - hi;
byte p = (byte)(v * (1 - s) * 255);
byte q = (byte)(v * (1 - f * s) * 255);
byte t = (byte)(v * (1 - (1 - f) * s) * 255);
byte vByte = (byte)(v * 255);
switch (hi % 6)
{
case 0: return new Color(vByte, t, p);
case 1: return new Color(q, vByte, p);
case 2: return new Color(p, vByte, t);
case 3: return new Color(p, q, vByte);
case 4: return new Color(t, p, vByte);
default: return new Color(vByte, p, q);
}
}
}
}

View File

@@ -6,7 +6,7 @@ namespace ShrlAlgoToolkit.Revit.Assists;
/// <summary>
/// 三维视图可用
/// </summary>
internal class EnableInView3D : IExternalCommandAvailability
public class EnableInView3D : IExternalCommandAvailability
{
public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories) =>
applicationData.ActiveUIDocument?.Document.ActiveView is View3D && applicationData.ActiveUIDocument?.Document.IsFamilyDocument == false;

View File

@@ -9,16 +9,13 @@ namespace ShrlAlgoToolkit.Revit.Assists;
public class FuncFilter(Func<Element, bool> allowElement, Func<Reference, XYZ, bool> allowReference = null)
: ISelectionFilter
{
private readonly Func<Element, bool> elementFunc = allowElement;
private readonly Func<Reference, XYZ, bool> referenceFunc = allowReference;
public bool AllowElement(Element elem)
{
return elementFunc(elem);
return allowElement(elem);
}
public bool AllowReference(Reference reference, XYZ position)
{
return referenceFunc == null || referenceFunc(reference, position);
return allowReference == null || allowReference(reference, position);
}
}

View File

@@ -6,6 +6,45 @@ namespace ShrlAlgoToolkit.Revit.Assists
{
internal class GeometryAssist
{
/// <summary>
///获取元素的包围框
/// </summary>
/// <param name="elements"></param>
/// <returns></returns>
public static BoundingBoxXYZ GetBoundingBox(List<Element> elements)
{
if (elements == null || elements.Count == 0)
{
return new BoundingBoxXYZ();
}
var gmax = XYZ.Zero;
var gmin = XYZ.Zero;
var elementIds = elements.Select(e => e.Id).ToList();
var doc = elements.FirstOrDefault()?.Document;
using (SubTransaction sub = new SubTransaction(doc))
{
sub.Start();
Group g;
if (doc.IsFamilyDocument)
{
g = doc.FamilyCreate.NewGroup(elementIds);
}
else
{
g = doc.Create.NewGroup(elementIds);
}
gmax = g.get_BoundingBox(null).Max;
gmin = g.get_BoundingBox(null).Min;
sub.RollBack();
}
return new BoundingBoxXYZ
{
Max = gmax,
Min = gmin
};
}
/// <summary>
/// 对曲线列表进行排序,使其正确排序和定向,形成线串。
/// </summary>

View File

@@ -3,8 +3,6 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using Autodesk.Revit.UI;
using Autodesk.Windows;
namespace ShrlAlgoToolkit.Revit.Assists;
@@ -17,10 +15,10 @@ public class KeyIntPtrHelper
private static void CompleteMultiSelection()
{
var rvtwindow = ComponentManager.ApplicationWindow;
var window = ComponentManager.ApplicationWindow;
var list = new List<IntPtr>();
var flag = EnumChildWindows(
rvtwindow,
window,
(hwnd, l) =>
{
var windowText = new StringBuilder(200);
@@ -79,15 +77,13 @@ public class KeyIntPtrHelper
foreach (var intPtr2 in handles)
{
var windowTextLength = GetWindowTextLength(intPtr2);
if (windowTextLength != 0)
if (windowTextLength == 0) continue;
var stringBuilder = new StringBuilder(windowTextLength);
_ = GetWindowText(intPtr2, stringBuilder, windowTextLength + 1);
if (stringBuilder.ToString().ToLower().Contains("autodesk revit"))
{
var stringBuilder = new StringBuilder(windowTextLength);
_ = GetWindowText(intPtr2, stringBuilder, windowTextLength + 1);
if (stringBuilder.ToString().ToLower().Contains("autodesk revit"))
{
intPtr = intPtr2;
break;
}
intPtr = intPtr2;
break;
}
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
namespace ShrlAlgoToolkit.Revit.Assists
@@ -18,32 +12,32 @@ namespace ShrlAlgoToolkit.Revit.Assists
/// var command = RevitCommandId.LookupPostableCommandId(PostableCommand.TypeProperties);
/// UiApplication.PostCommand(command);</c>
/// </example>
internal class RevitCommandEndedMonitor
public class RevitCommandEndedMonitor
{
private readonly UIApplication _revitUiApplication;
private readonly UIApplication revitUiApplication;
private bool _initializingCommandMonitor;
private bool initializingCommandMonitor;
public event EventHandler CommandEnded;
public RevitCommandEndedMonitor(UIApplication revituiApplication)
public RevitCommandEndedMonitor(UIApplication uIApplication)
{
_revitUiApplication = revituiApplication;
revitUiApplication = uIApplication;
_initializingCommandMonitor = true;
initializingCommandMonitor = true;
_revitUiApplication.Idling += OnRevitUiApplicationIdling;
revitUiApplication.Idling += OnRevitUiApplicationIdling;
}
private void OnRevitUiApplicationIdling(object sender, IdlingEventArgs idlingEventArgs)
{
if (_initializingCommandMonitor)
if (initializingCommandMonitor)
{
_initializingCommandMonitor = false;
initializingCommandMonitor = false;
return;
}
_revitUiApplication.Idling -= OnRevitUiApplicationIdling;
revitUiApplication.Idling -= OnRevitUiApplicationIdling;
OnCommandEnded();
}

View File

@@ -2,7 +2,7 @@
namespace ShrlAlgoToolkit.Revit.Assists;
internal static class SpatialAssist
public static class SpatialAssist
{
//private static List<Curve> _curvesSorted;
/// <summary>
@@ -432,7 +432,8 @@ internal static class SpatialAssist
// return list;
//}
}
internal interface ICurveContainer
public interface ICurveContainer
{
}