下载app免费领取会员
很多人出于某种用途为族设置特殊的参数之后,希望可以将其隐藏。在Revit官方暴露出来的API中,是没有方法来实现的。以下代码演示了如何将墙体所有参数隐藏!
using System;
using System.Linq;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using System.Reflection;
namespace RevitAPITest
{
[Transaction(TransactionMode.Manual)]
public class APITest : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiDoc = commandData.Application.ActiveUIDocument;
var doc = uiDoc.Document;
var elemIds = uiDoc.Selection.GetElementIds();
if (elemIds.Count == 0)
{
TaskDialog.Show("Error", "Please select an element....!");
return Result.Failed;
}
var elem = doc.GetElement(elemIds.First());
//var eum = elem.Parameters.GetEnumerator();
var elemType = doc.GetElement(elem.GetTypeId()) as ElementType;
if (elemType == null)
{
return Result.Failed;
}
var eum = elemType.Parameters.GetEnumerator();
while (eum.MoveNext())
{
var parameter = eum.Current as Parameter;
parameter.SetVisibility(false);
}
return Result.Succeeded;
}
}
public class NativeCore
{
public static bool InvokeMoudle(string mName, object[] paramters, bool apiMoudle = true)
{
try
{
MethodInfo m = null;
if (apiMoudle)
m = NativeMoudle.GetAPIMoudlerMethod(mName);
else
m = NativeMoudle.GetUIMoudlerMethod(mName);
m.Invoke(null, paramters);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public class NativeMoudle
{
/// 获取RevitAPI程序集Moudle内的方法名称.
/// <summary>
/// 获取RevitAPI程序集Moudle内的方法名称.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static MethodInfo GetAPIMoudlerMethod(string name)
{
var moudle = NativeMoudle.getAPIMoudle();
var rs = moudle.GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == name);
return rs.Count() == 0 ? null : rs.First();
}
/// 获取RevitAPIUI程序集Moudle内的方法名称.
/// <summary>
/// 获取RevitAPIUI程序集Moudle内的方法名称.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static MethodInfo GetUIMoudlerMethod(string name)
{
var moudle = NativeMoudle.getUIMoudle();
var rs = moudle.GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == name);
return rs.Count() == 0 ? null : rs.First();
}
/// 获取RevitAPI Modules.
/// <summary>
/// 获取RevitAPI Modules.
/// </summary>
/// <returns></returns>
internal static Module getAPIMoudle()
{
return typeof(Document).Assembly.Modules.First();
}
/// 获取RevitAPIUI Modules.
/// <summary>
/// 获取RevitAPIUI Modules.
/// </summary>
/// <returns></returns>
internal static Module getUIMoudle()
{
return typeof(UIDocument).Assembly.Modules.First();
}
}
public static class ParameterExtension
{
/// <summary>
/// 设置参数对象对用户是否可见.
/// </summary>
/// <param name="parameter"></param>
/// <param name="visible"></param>
/// <returns></returns>
public static bool SetVisibility(this Parameter parameter, bool visible)
{
var parameterIntPtr = parameter.ToParamDef();
if (parameterIntPtr == IntPtr.Zero)
{
return false;
}
var result = NativeCore.InvokeMoudle("ParamDef.setUserVisible", new object[]
{
parameterIntPtr, visible
});
return result;
}
/// <summary>
/// 将参数对象转为非托管指针.
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public static IntPtr ToParamDef(this Parameter parameter)
{
try
{
var m = typeof(Parameter).GetMethod("getParamDef", BindingFlags.NonPublic | BindingFlags.Instance);
return ((Pointer)m.Invoke(parameter, null)).ToIntPtr();
}
catch
{
return IntPtr.Zero;
}
}
}
public static class PointerExtension
{
/// 将指针转为句柄.
/// <summary>
/// 将指针转为句柄.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public unsafe static IntPtr ToIntPtr(this Pointer p)
{
return (IntPtr)p
.GetType()
.GetMethod("GetPointerValue", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(p, null);
}
}
}
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。