应用程序:ProgressNotifier
Revit平台:全部
Revit版本:2013.0
首次发布版本:2013.0
编程语言:C#
技能水平:中级
类别:基础知识
类型:ExternalCommand
主题:在堆数据结构中显示操作的进度信息以便更容易分析。
摘要:
本示例演示了如何订阅ProgressNotify相关事件,访问事件处理程序参数中的属性,并将子事务进度信息组织成堆栈。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Events.ProgressChangedEventArgs
项目文件:
Application.cs
该文件包含一个实现IExternalCommand接口的Command类。
ProgressItem.cs
一个包含进度事件名称、位置和状态的对象。
ProgressStack.cs
一个ProgressItem对象的集合,排列在一个堆栈中。
MainWindow.xaml
命令对话框的布局
MainWindow.xaml.cs
提供一个UI以打开文档并显示ProgressStack的对话框类。
描述:
该示例实现了IExternalCommand接口,当命令启动时订阅ProgressChanged事件并启动对话框。
说明:
1.复制提供的.addin文件到Revit安装文件夹下的样本文件夹中,并指定dll的完整路径。
2.启动Revit。注意,一旦命令开始,ProgressNotify事件将被注册。选择“打开文档”按钮,并选择一个文件。注意在窗口中显示的进度信息。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
ProgressItem.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 System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Diagnostics;
using Autodesk.Revit.DB.Events;
namespace Revit.SDK.Samples.ProgressNotifier.CS
{
/// <summary>
/// An object containing a progress event name, position, and status.
/// </summary>
public class ProgressItem
{
/// <summary>
/// Flag of progress
/// </summary>
private bool m_done;
/// <summary>
/// Name
/// </summary>
private string m_name;
/// <summary>
/// Lower
/// </summary>
private int m_lower;
/// <summary>
/// Upper
/// </summary>
private int m_upper;
/// <summary>
/// Position
/// </summary>
private int m_position;
/// <summary>
/// Progress stage
/// </summary>
ProgressStage m_stage;
/// <summary>
/// Constructor
/// </summary>
/// <param name="name"></param>
/// <param name="lower"></param>
/// <param name="upper"></param>
/// <param name="position"></param>
/// <param name="stage"></param>
public ProgressItem(string name, int lower, int upper, int position, ProgressStage stage)
{
m_name = name;
m_lower = lower;
m_upper = upper;
m_position = position;
m_done = false;
m_stage = stage;
}
/// <summary>
/// Name property
/// </summary>
public string Name
{
get
{
if (string.IsNullOrEmpty(m_name) || (m_name == " "))
return "<None>";
else
return m_name;
}
set
{
m_name = value;
}
}
/// <summary>
/// IsDone property
/// </summary>
public bool IsDone
{
get
{
return m_done;
}
set
{
m_done = value;
}
}
/// <summary>
/// Lower property
/// </summary>
public int Lower
{
get
{
return m_lower;
}
set
{
m_lower = value;
}
}
/// <summary>
/// Upper property
/// </summary>
public int Upper
{
get
{
return m_upper;
}
set
{
m_upper = value;
}
}
/// <summary>
/// Position property
/// </summary>
public int Position
{
get
{
return m_position;
}
set
{
m_position = value;
}
}
/// <summary>
/// progress stage property
/// </summary>
public ProgressStage Stage
{
get
{
return m_stage;
}
set
{
m_stage = value;
}
}
/// <summary>
/// percent of progress
/// </summary>
/// <returns></returns>
public double PercentDone()
{
return ((double)m_position / (double)(m_upper - m_lower)) * 100;
}
/// <summary>
/// ToString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "Name: " + Name + ", Stage: " + m_stage.ToString() + ", Percent Done: " + this.PercentDone().ToString("F") + ", Upper: " + m_upper + ", Position: " + m_position;
}
}
}
ProgressStack.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 System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Diagnostics;
using Autodesk.Revit.DB.Events;
namespace Revit.SDK.Samples.ProgressNotifier.CS
{
/// <summary>
/// A collection of ProgressItem objects arranged in a stack
/// </summary>
public class ProgressStack
{
/// <summary>
/// ProgressItem stack
/// </summary>
public System.Collections.Generic.Stack<ProgressItem> m_itemStack;
/// <summary>
/// Constructor
/// </summary>
public ProgressStack()
{
this.m_itemStack = new Stack<ProgressItem>();
}
/// <summary>
/// Add event data
/// </summary>
/// <param name="progressEvent"></param>
/// <returns></returns>
public ProgressItem AddEventData(Autodesk.Revit.DB.Events.ProgressChangedEventArgs progressEvent)
{
ProgressItem currentProgressItem = null;
switch (progressEvent.Stage)
{
case ProgressStage.Started:
{
ProgressItem pi = new ProgressItem(progressEvent.Caption, progressEvent.LowerRange, progressEvent.UpperRange, progressEvent.Position, progressEvent.Stage);
m_itemStack.Push(pi);
currentProgressItem = pi;
break;
}
case ProgressStage.PositionChanged:
{
ProgressItem pi = m_itemStack.Peek();
if (pi.Name != progressEvent.Caption)
{
Debug.WriteLine("Name not matching?");
}
pi.Position = progressEvent.Position;
pi.Stage = progressEvent.Stage;
currentProgressItem = pi;
break;
}
case ProgressStage.RangeChanged:
{
ProgressItem pi = m_itemStack.Peek();
pi.Upper = progressEvent.UpperRange;
pi.Stage = progressEvent.Stage;
currentProgressItem = pi;
break;
}
case ProgressStage.Finished:
{
ProgressItem pi = m_itemStack.Pop();
pi.IsDone = true;
pi.Stage = progressEvent.Stage;
currentProgressItem = pi;
break;
}
case ProgressStage.CaptionChanged:
{
ProgressItem pi = m_itemStack.Peek();
pi.Name = progressEvent.Caption;
pi.Stage = progressEvent.Stage;
Debug.WriteLine("Caption Change at top.");
currentProgressItem = pi;
break;
}
case ProgressStage.Unchanged:
{
Debug.WriteLine("Idle.");
currentProgressItem = new ProgressItem(progressEvent.Caption, progressEvent.LowerRange, progressEvent.UpperRange, progressEvent.Position, progressEvent.Stage);
break;
}
default:
throw new Exception("Unknown stage.");
}
if (m_itemStack.Count == 0)
{
Debug.WriteLine("Stack empty");
}
else
Debug.WriteLine(this.ToString());
return currentProgressItem;
}
/// <summary>
/// ToString
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("-ProgressBar Stack-");
foreach (ProgressItem pi in m_itemStack)
{
sb.AppendLine(pi.ToString());
}
return sb.ToString();
}
/// <summary>
/// ToStringList
/// </summary>
/// <param name="padDepth"></param>
/// <returns></returns>
public List<String> ToStringList(int padDepth = 0)
{
List<string> itemList = new List<string>();
if (padDepth != 0)
{
int padding = padDepth - m_itemStack.Count;
for (int index = 0; index != padding; ++index)
{
itemList.Add("");
}
}
foreach (ProgressItem pi in m_itemStack)
{
itemList.Add(pi.ToString());
}
return itemList;
}
}
}