revit二次开发 材质相关_liunianwangshui的博客-程序员宅基地 - 程序员宅基地 (cxyzjd.com)
有一个需求,需要修改构件颜色,本来以为挺简单的,只要新建一个Material,改一下Material属性下面的颜色就行了,结果发现毫无效果,经过多方探查之后才发现只有修改Material=>AppearanceAssetElement=>GetRenderingAsset=>AssetProperty=>AssetPropertyDoubleArray4d的generic_diffuse属性(也可能是别的属性)值才行
官网已经提供了修改颜色的方法,但
AppearanceAssetElement还是需要自己创建,(地址我忘了)
public static void UpdateAssetColor(this Document doc, AppearanceAssetElement assetElem, Color color)
{
using (Transaction t = new Transaction(doc, "Change material color"))
{
t.Start();
using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc))
{
Asset editableAsset = editScope.Start(assetElem.Id);
// returns an editable copy of the appearance asset
// unlike AppearanceAssetElement.GetRenderingAsset(), the edit scope
// sets up the asset for editing using the classes and utilities from
// MaterialsManager. The scope can keep a mapping from asset to Protein
// class members that are obtained from the asset, to permit use of
// Protein validation. If the Asset was not obtained from an edit scope,
// the setter operations for asset properties must throw.
// Try to change values
AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset.FindByName("generic_diffuse") as AssetPropertyDoubleArray4d;
// 4 length double asset properties often represent colors.
genericDiffuseProperty.SetValueAsColor(color);
// Shortcut methods should be added to work directly with Color objects
// for these types.
// Commit the entire edit scope. If the appearance asset element is
// used in one or more materials, they will be updated to match any changes made.
editScope.Commit(true);
}
t.Commit();
}
}
添加材质步骤:
- 新建Material
- 新建AppearanceAssetElement
- 设置颜色
源码
方法
/// <summary>
/// 添加新材料(无事务)
/// </summary>
/// <param name="doc"></param>
public static ElementId AddNewMaterial(this Document doc, string name)
{
IList<Element> elements = doc.GetElementByType(typeof(Material));
Material a = (from i in elements
where i.Name == name
select i as Material).FirstOrDefault();
if (a != null) { return a.Id; }
else
{
ElementId id = Material.Create(doc, name);
return id;
}
}
/// <summary>
/// 添加AppearanceAssetElement (带条件)
/// </summary>
/// <param name="doc"></param>
/// <param name="name"></param>
/// <param name="generic_diffuse">Asset属性</param>
/// <returns></returns>
public static ElementId AddNewAppearanceAsset(this Document doc, string name, string generic_diffuse)
{
AppearanceAssetElement a = (from i in doc.GetAppearanceAsset()
where i.Name == name
select i as AppearanceAssetElement).FirstOrDefault();
if (a != null)
{
return a.Id;
}
else
{
List<Asset> assets = doc.GetAsset();
foreach (Asset asset in assets)
{
// 确保Asset有对应属性
AssetProperty assetProperty = asset.FindByName(generic_diffuse);
if (assetProperty != null)
{
AppearanceAssetElement assetElement = AppearanceAssetElement.Create(doc, name, asset);
return assetElement.Id;
}
}
return new ElementId(-1);
}
}
/// <summary>
/// 修改单个颜色
/// </summary>
/// <param name="doc"></param>
/// <param name="assetElemId">AppearanceAssetElement的Id</param>
/// <param name="color"></param>
public static void UpdateAssetColor(this Document doc, ElementId assetElemId, Color color)
{
using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc))
{
Asset editableAsset = editScope.Start(assetElemId);
AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset.FindByName("generic_diffuse") as AssetPropertyDoubleArray4d;
genericDiffuseProperty.SetValueAsColor(color);
editScope.Commit(true);
}
}
启动
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class ShowRemarkCommandTest : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
using (Transaction tran = new Transaction(doc, "Start"))
{
tran.Start();
Reference refer = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "选择构件");
// 获取实例
FamilyInstance element = doc.GetElement(refer) as FamilyInstance;
// 添加材质(自定义方法)
ElementId materialId = doc.AddNewMaterial("测试材质");
// 添加外观材质(自定义方法)
ElementId appearanceId = doc.AddNewAppearanceAsset("测试外观", "generic_diffuse");
// 修改颜色(自定义方法)
doc.UpdateAssetColor(appearanceId, new Color(121, 121, 211));
Material material = doc.GetElement(materialId) as Material;
material.AppearanceAssetId = appearanceId;
element.StructuralMaterialId = materialId;
tran.Commit();
}
return Result.Succeeded;
}
}
注意事项
- 修改颜色时一次只能添加一种颜色,批量添加时,
editScope.Commit(true)执行完毕后才能继续修改颜色 - 该方法在2021Revit(.net framework4.8)版本上可以使用时,在2018版本无法使用