应用程序:外观资产编辑
Revit平台:所有
Revit版本:2018.1
首次发布用于:2018.1
编程语言:C#
技能等级:中等
类别:材料
类型:外部命令和外部应用
主题:通过小控件对话框编辑外观资源属性
摘要:此示例演示了使用 AppearanceAssetEditScope 和 AssetProperty 类的基本用法,以更改特定材质中资产属性的值。
相关类:
Autodesk.Revit.DB.Material
Autodesk.Revit.DB.AppearanceAssetElement
Autodesk.Revit.DB.Visual.AppearanceAssetEditScope
Autodesk.Revit.DB.Visual.AssetPropertyBoolean
Autodesk.Revit.DB.Visual.AssetPropertyDoubleArray4d
项目文件:
Command.cs它包含从接口IExternalCommand继承并实现Execute方法的类Command。
Application.cs它包含从接口IExternalApplication继承并实现其方法的类Application。
Request.cs包含当前请求的变量周围的类。
RequestHandler.cs具有执行对话框用户发出的请求的方法的类。
AssetEditingForm.cs是我们的无模式对话框的类
功能:
此示例演示了AppearanceAssetEditScope和AssetProperty类的基本用法,以更改给定材质中资产属性的值。
实施:
打开Revit应用程序并执行命令“显示材质色调控制”。
1.单击无模式对话框中的箭头按钮,在项目中选择一个绘制的面。
预期结果:如果选择了一个面,并且我们可以识别其材质,并且该材质的架构支持“色调”,则对话框按钮将启用。如果我们无法识别材质或材质不支持“色调”,则按钮将被禁用。
2.使用无模式对话框中的命令按钮使色调变浅或变暗。
预期结果:元素色调颜色将发生变化。如果色调被最小化(255255255),则按钮Lighter被禁用。如果色调最大化(0 0 0),则禁用“深色”。
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
Application.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Visual;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using System.Diagnostics;
namespace Revit.SDK.Samples.AppearanceAssetEditing.CS
{ /// <summary>
/// Implements the Revit add-in interface IExternalApplication
/// </summary>
public class Application : IExternalApplication
{
// class instance
internal static Application thisApp = null;
#region Class member variables
// ModelessForm instance
private AppearanceAssetEditingForm m_MyForm;
Autodesk.Revit.UI.UIApplication m_revit;
Document m_document;
Material m_currentMaterial;
ElementId m_currentAppearanceAssetElementId;
#endregion
#region IExternalApplication Members
/// <summary>
/// Implements the OnShutdown event
/// </summary>
/// <param name="application"></param>
/// <returns></returns>
public Result OnShutdown(UIControlledApplication application)
{
if (m_MyForm != null && !m_MyForm.IsDisposed)
{
m_MyForm.Dispose();
m_MyForm = null;
// if we've had a dialog, we had subscribed
application.Idling -= IdlingHandler;
}
return Result.Succeeded;
}
/// <summary>
/// Implements the OnStartup event
/// </summary>
/// <param name="application"></param>
/// <returns></returns>
public Result OnStartup(UIControlledApplication application)
{
m_MyForm = null; // no dialog needed yet; the command will bring it
thisApp = this; // static access to this application instance
return Result.Succeeded;
}
/// <summary>
/// This method creates and shows a modeless dialog, unless it already exists.
/// </summary>
/// <remarks>
/// The external command invokes this on the end-user's request
/// </remarks>
///
public void ShowForm(UIApplication uiapp)
{
m_revit = uiapp;
m_document = uiapp.ActiveUIDocument.Document;
// If we do not have a dialog yet, create and show it
if (m_MyForm == null || m_MyForm.IsDisposed)
{
m_MyForm = new AppearanceAssetEditingForm();
m_MyForm.Show();
// if we have a dialog, we need Idling too
uiapp.Idling += IdlingHandler;
}
}
/// <summary>
/// Compares two colors.
/// </summary>
/// <param name="color1">The first color.</param>
/// <param name="color2">The second color.</param>
/// <returns>True if the colors are equal, false otherwise.</returns>
private bool ColorsEqual(Color color1, Color color2)
{
return color1.Red == color2.Red && color1.Green == color2.Green && color1.Blue == color2.Blue;
}
void Log(string msg)
{
string dt = DateTime.Now.ToString("u");
Trace.WriteLine(dt + " " + msg);
}
/// <summary>
/// Custom filter for selection.
/// </summary>
private class IsPaintedFaceSelectionFilter : ISelectionFilter
{
Document selectedDocument = null;
public bool AllowElement(Element element)
{
selectedDocument = element.Document;
return true;
}
public bool AllowReference(Reference refer, XYZ point)
{
if (selectedDocument == null)
{
throw new Exception("AllowElement was never called for this reference...");
}
Element element = selectedDocument.GetElement(refer);
Face face = element.GetGeometryObjectFromReference(refer) as Face;
return selectedDocument.IsPainted(element.Id, face);
}
}
/// <summary>
/// Get the painted material from selection.
/// </summary>
internal void GetPaintedMaterial()
{
Reference refer;
try
{
refer = m_revit.ActiveUIDocument.Selection.PickObject(ObjectType.Face, new IsPaintedFaceSelectionFilter(), "Select a painted face for editing.");
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
// Selection Cancelled.
return;
}
catch (Exception ex)
{
// If any error, give error information and return failed
Log(ex.Message);
return;
}
if (refer == null)
{
return;
}
else
{
Element element = m_document.GetElement(refer);
Face face = element.GetGeometryObjectFromReference(refer) as Face;
ElementId matId = m_document.GetPaintedMaterial(element.Id, face);
m_currentMaterial = m_document.GetElement(matId) as Material;
if (m_currentMaterial != null)
{
m_currentAppearanceAssetElementId = m_currentMaterial.AppearanceAssetId;
// Clear selection
m_revit.ActiveUIDocument.Selection.GetElementIds().Clear();
}
}
}
/// <summary>
/// Check if the selected material supports "tint".
/// </summary>
/// <returns>True if the selected material supports "tint" or not.</returns>
private bool SupportTintColor()
{
if (this.m_currentAppearanceAssetElementId == ElementId.InvalidElementId)
return false;
AppearanceAssetElement assetElem = m_document.GetElement(this.m_currentAppearanceAssetElementId) as AppearanceAssetElement;
if (assetElem == null)
return false;
Asset asset = assetElem.GetRenderingAsset();
AssetPropertyDoubleArray4d tintColorProp = asset.FindByName("common_Tint_color") as AssetPropertyDoubleArray4d;
if (tintColorProp == null)
return false;
// If the material supports tint but it is not enabled, it will be enabled first with a value (255 255 255)
AssetPropertyBoolean tintToggleProp = asset.FindByName("common_Tint_color") as AssetPropertyBoolean;
if ((tintToggleProp != null) && !(tintToggleProp.Value))
{
EnableTintColor();
}
return true;
}
/// <summary>
/// Enable tint color.
/// </summary>
private void EnableTintColor()
{
using (Transaction transaction = new Transaction(m_document, "Enable tint color"))
{
transaction.Start();
using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(m_document))
{
Asset editableAsset = editScope.Start(m_currentAppearanceAssetElementId);
// If the material supports tint but it is not enabled, it will be enabled first with a value (255 255 255)
AssetPropertyBoolean tintToggleProp = editableAsset.FindByName("common_Tint_color") as AssetPropertyBoolean;
AssetPropertyDoubleArray4d tintColorProp = editableAsset.FindByName("common_Tint_color") as AssetPropertyDoubleArray4d;
tintToggleProp.Value = true;
tintColorProp.SetValueAsColor(new Color(255, 255, 255));
editScope.Commit(true);
}
transaction.Commit();
}
}
/// <summary>
/// Check if the button Lighter is enabled.
/// </summary>
/// <returns>True if the material can be made lighter or not.</returns>
internal bool IsLighterEnabled()
{
if (!SupportTintColor())
return false;
AppearanceAssetElement assetElem = m_document.GetElement(m_currentAppearanceAssetElementId) as AppearanceAssetElement;
if (assetElem == null)
return false;
Asset asset = assetElem.GetRenderingAsset();
AssetPropertyDoubleArray4d tintColorProp = asset.FindByName("common_Tint_color") as AssetPropertyDoubleArray4d;
Color tintColor = tintColorProp.GetValueAsColor();
Color white = new Color(255, 255, 255);
return !ColorsEqual(tintColor, white);
}
/// <summary>
/// Check if the button Darker is enabled.
/// </summary>
/// <returns>True if the material can be made darker or not.</returns>
internal bool IsDarkerEnabled()
{
if (!SupportTintColor())
return false;
AppearanceAssetElement assetElem = m_document.GetElement(this.m_currentAppearanceAssetElementId) as AppearanceAssetElement;
if (assetElem == null)
return false;
Asset asset = assetElem.GetRenderingAsset();
AssetPropertyDoubleArray4d tintColorProp = asset.FindByName("common_Tint_color") as AssetPropertyDoubleArray4d;
Color tintColor = tintColorProp.GetValueAsColor();
Color black = new Color(0, 0, 0);
return !ColorsEqual(tintColor, black);
}
/// <summary>
/// A handler for the Idling event.
/// </summary>
/// <remarks>
/// We keep the handler very simple. First we check
/// if we still have the dialog. If not, we unsubscribe from Idling,
/// for we no longer need it and it makes Revit speedier.
/// If we do have the dialog around, we check if it has a request ready
/// and process it accordingly.
/// </remarks>
///
public void IdlingHandler(object sender, IdlingEventArgs args)
{
UIApplication uiapp = sender as UIApplication;
UIDocument uidoc = uiapp.ActiveUIDocument;
if (m_MyForm.IsDisposed)
{
uiapp.Idling -= IdlingHandler;
return;
}
else // dialog still exists
{
// fetch the request from the dialog
RequestId requestId = m_MyForm.Request.Take();
if (requestId != RequestId.None)
{
try
{
// we take the request, if any was made,
// and pass it on to the request executor
RequestHandler.Execute(this, requestId);
}
finally
{
// The dialog may be in its waiting state;
// make sure we wake it up even if we get an exception.
m_MyForm.EnableButtons(IsLighterEnabled(), IsDarkerEnabled());
}
}
}
return;
}
/// <summary>
/// Limit value to 0-255
/// </summary>
/// <returns>True if the material can be made darker or not.</returns>
private int LimitValue(int value)
{
return (value < 0) ? 0 : (value > 255) ? 255 : value;
}
/// <summary>
/// The main material-modification subroutine - called from every request
/// </summary>
/// <remarks>
/// It searches the current selection for all doors
/// and if it finds any it applies the requested operation to them
/// </remarks>
/// <param name="text">Caption of the transaction for the operation.</param>
/// <param name="lighter">Increase the tint color property or not.</param>
///
internal void ModifySelectedMaterial(String text, bool lighter)
{
// Since we'll modify the document, we need a transaction
// It's best if a transaction is scoped by a 'using' block
using (Transaction trans = new Transaction(m_document))
{
// The name of the transaction was given as an argument
if (trans.Start(text) == TransactionStatus.Started)
{
// apply the requested operation to every door
EditMaterialTintColorProperty(lighter);
trans.Commit();
}
}
}
/// <summary>
/// Edit tint color property.
/// </summary>
/// <param name="lighter">Increase the tint color property or not.</param>
internal void EditMaterialTintColorProperty(bool lighter)
{
using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(m_document))
{
Asset editableAsset = editScope.Start(m_currentAppearanceAssetElementId);
AssetPropertyDoubleArray4d metalColorProp = editableAsset.FindByName("common_Tint_color") as AssetPropertyDoubleArray4d;
Color color = metalColorProp.GetValueAsColor();
byte red = color.Red;
byte green = color.Green;
byte blue = color.Blue;
// Increment factor (value related to 255)
int factor = 25;
if (lighter)
{
red = (byte)LimitValue(red + factor);
green = (byte)LimitValue(green + factor);
blue = (byte)LimitValue(blue + factor);
}
else
{
red = (byte)LimitValue(red - factor);
green = (byte)LimitValue(green - factor);
blue = (byte)LimitValue(blue - factor);
}
if (metalColorProp.IsValidValue(color))
metalColorProp.SetValueAsColor(new Color(red, green, blue));
editScope.Commit(true);
}
}
#endregion
}
}
Request.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System.Threading;
namespace Revit.SDK.Samples.AppearanceAssetEditing.CS
{ /// <summary>
/// A list of requests the dialog has available
/// </summary>
///
public enum RequestId : int
{
/// <summary>
/// None
/// </summary>
None = 0,
/// <summary>
/// "Select" request
/// </summary>
Select = 1,
/// <summary>
/// "Lighter" request
/// </summary>
Lighter = 2,
/// <summary>
/// "Darker" request
/// </summary>
Darker = 3
}
/// <summary>
/// A class around a variable holding the current request.
/// </summary>
/// <remarks>
/// Access to it is made thread-safe, even though we don't necessarily
/// need it if we always disable the dialog between individual requests.
/// </remarks>
///
public class Request
{
// Storing the value as a plain Int makes using the interlocking mechanism simpler
private int m_request = (int)RequestId.None;
/// <summary>
/// Take - The Idling handler calls this to obtain the latest request.
/// </summary>
/// <remarks>
/// This is not a getter! It takes the request and replaces it
/// with 'None' to indicate that the request has been "passed on".
/// </remarks>
///
public RequestId Take()
{
return (RequestId)Interlocked.Exchange(ref m_request, (int)RequestId.None);
}
/// <summary>
/// Make - The Dialog calls this when the user presses a command button there.
/// </summary>
/// <remarks>
/// It replaces any older request previously made.
/// </remarks>
///
public void Make(RequestId request)
{
Interlocked.Exchange(ref m_request, (int)request);
}
}
}
RequestHandler.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Visual;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI;
using System.Diagnostics;
namespace Revit.SDK.Samples.AppearanceAssetEditing.CS
{
/// <summary>
/// A class with methods to execute requests made by the dialog user.
/// </summary>
///
public static class RequestHandler
{
/// <summary>
/// The top function that distributes requests to individual methods.
/// </summary>
///
public static void Execute(Application app, RequestId request)
{
switch (request)
{
case RequestId.None:
{
return; // no request at this time -> we can leave immediately
}
case RequestId.Select:
{
app.GetPaintedMaterial();
break;
}
case RequestId.Lighter:
{
app.ModifySelectedMaterial("Lighter", true);
break;
}
case RequestId.Darker:
{
app.ModifySelectedMaterial("Darker", false);
break;
}
default:
{
// some kind of a warning here should
// notify us about an unexpected request
break;
}
}
return;
}
} // class
}
AppearanceAssetEditingForm.cs
namespace Revit.SDK.Samples.AppearanceAssetEditing.CS
{
partial class AppearanceAssetEditingForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.lighterToolTip = new System.Windows.Forms.ToolTip(this.components);
this.darkerToolTip = new System.Windows.Forms.ToolTip(this.components);
this.buttonSelect = new System.Windows.Forms.Button();
this.buttonDarker = new System.Windows.Forms.Button();
this.buttonLighter = new System.Windows.Forms.Button();
this.selectTooltip = new System.Windows.Forms.ToolTip(this.components);
this.labelSelect = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonSelect
//
this.buttonSelect.Image = global::Revit.SDK.Samples.AppearanceAssetEditing.CS.Properties.Resources.element_select;
this.buttonSelect.Location = new System.Drawing.Point(191, 17);
this.buttonSelect.Name = "buttonSelect";
this.buttonSelect.Size = new System.Drawing.Size(50, 30);
this.buttonSelect.TabIndex = 2;
this.selectTooltip.SetToolTip(this.buttonSelect, "Select the painted object to modify.");
this.buttonSelect.UseVisualStyleBackColor = true;
this.buttonSelect.Click += new System.EventHandler(this.buttonSelect_Click);
//
// buttonDarker
//
this.buttonDarker.Enabled = false;
this.buttonDarker.Image = global::Revit.SDK.Samples.AppearanceAssetEditing.CS.Properties.Resources.shortcut_remove;
this.buttonDarker.Location = new System.Drawing.Point(126, 65);
this.buttonDarker.Name = "buttonDarker";
this.buttonDarker.Size = new System.Drawing.Size(25, 25);
this.buttonDarker.TabIndex = 1;
this.darkerToolTip.SetToolTip(this.buttonDarker, "Change Tint Color to Darker.");
this.buttonDarker.UseVisualStyleBackColor = true;
this.buttonDarker.Click += new System.EventHandler(this.buttonDarker_Click);
//
// buttonLighter
//
this.buttonLighter.Enabled = false;
this.buttonLighter.Image = global::Revit.SDK.Samples.AppearanceAssetEditing.CS.Properties.Resources.shortcut_assign;
this.buttonLighter.Location = new System.Drawing.Point(78, 65);
this.buttonLighter.Name = "buttonLighter";
this.buttonLighter.Size = new System.Drawing.Size(25, 25);
this.buttonLighter.TabIndex = 0;
this.lighterToolTip.SetToolTip(this.buttonLighter, "Change tint color to lighter.");
this.buttonLighter.UseVisualStyleBackColor = true;
this.buttonLighter.Click += new System.EventHandler(this.buttonLighter_Click);
//
// labelSelect
//
this.labelSelect.AutoSize = true;
this.labelSelect.Location = new System.Drawing.Point(25, 26);
this.labelSelect.Name = "labelSelect";
this.labelSelect.Size = new System.Drawing.Size(160, 13);
this.labelSelect.TabIndex = 3;
this.labelSelect.Text = "Select a painted face for editing.";
//
// AppearanceAssetEditingForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(259, 110);
this.Controls.Add(this.labelSelect);
this.Controls.Add(this.buttonSelect);
this.Controls.Add(this.buttonDarker);
this.Controls.Add(this.buttonLighter);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "AppearanceAssetEditingForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Appearance Asset Editing";
this.TopMost = true;
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button buttonLighter;
private System.Windows.Forms.Button buttonDarker;
private System.Windows.Forms.ToolTip lighterToolTip;
private System.Windows.Forms.ToolTip darkerToolTip;
private System.Windows.Forms.Button buttonSelect;
private System.Windows.Forms.ToolTip selectTooltip;
private System.Windows.Forms.Label labelSelect;
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598