ScheduleAutomaticFormatter
这个例子介绍如何对明细表进行自动的格式化。
关于什么是明细表,可以参考:Revit API:明细表 ViewSchedule
这个例子的内容可以分为两个部分:
- 格式化明细表
- 添加 Updater 在明细表发生变化时自动进行格式化
注:为了能自动格式化,需要存储一些数据,这些数据存放在Revit Extensible Storage 里面。
执行结果:
public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
ViewSchedule viewSchedule = commandData.View as ViewSchedule;
// 设置用于扩展存储的 schema,保存数据用于判断是否需要进行格式化。
Schema schema = GetOrCreateSchema();
// 初始化 ScheduleFormatter
if (theFormatter == null)
{
theFormatter = new ScheduleFormatter();
theFormatter.Schema = schema;
theFormatter.AddInId = commandData.Application.ActiveAddInId;
}
using (Transaction t = new Transaction(viewSchedule.Document, "Format columns"))
{
t.Start();
// 进行格式化
theFormatter.FormatScheduleColumns(viewSchedule);
// 设置格式化的标记
AddMarkerEntity(viewSchedule, schema);
t.Commit();
}
// 添加一个 Updater 用于自动触发格式化
AddUpdater(theFormatter);
return Result.Succeeded;
}
扩展存储
创建 Schema:
private static Schema GetOrCreateSchema()
{
Guid schemaId = new Guid("98017A5F-F4A7-451C-8807-EF137B587C50");
Schema schema = Schema.Lookup(schemaId);
if (schema == null)
{
SchemaBuilder builder = new SchemaBuilder(schemaId);
builder.SetSchemaName("ScheduleFormatterFlag");
builder.AddSimpleField("Formatted", typeof(Boolean));
schema = builder.Finish();
}
return schema;
}
使用 Schema:
private void AddMarkerEntity(ViewSchedule viewSchedule, Schema schema)
{
// Is entity already present?
Entity entity = viewSchedule.GetEntity(schema);
// If not, add it.
if (!entity.IsValid())
{
entity = new Entity(schema);
entity.Set<bool>("Formatted", true);
viewSchedule.SetEntity(entity);
}
}
增加 Updater
private static void AddUpdater(ScheduleFormatter formatter)
{
// If not registered, register the updater
if (!UpdaterRegistry.IsUpdaterRegistered(formatter.GetUpdaterId()))
{
// Filter on: schedule type, and extensible storage entity of the target schema
ElementClassFilter classFilter = new ElementClassFilter(typeof(ViewSchedule));
ExtensibleStorageFilter esFilter = new ExtensibleStorageFilter(formatter.Schema.GUID);
LogicalAndFilter filter = new LogicalAndFilter(classFilter, esFilter);
// Register and add trigger for updater.
UpdaterRegistry.RegisterUpdater(formatter);
UpdaterRegistry.AddTrigger(formatter.GetUpdaterId(), filter, Element.GetChangeTypeAny());
}
}
格式化
间隔设定明细表背景色:
public void FormatScheduleColumns(ViewSchedule viewSchedule)
{
ScheduleDefinition definition = viewSchedule.Definition;
int index = 0;
Color white = new Color(0xFF, 0xFF, 0xFF);
Color highlight = new Color(0xd8, 0xd8, 0xd8);
bool applyHighlight = false;
// Loop over fields in order
foreach (ScheduleFieldId id in definition.GetFieldOrder())
{
// Index 0, 2, etc use highlight color
if (index % 2 == 0)
{
applyHighlight = true;
}
// Index 1, 3, etc use no background color
else
{
applyHighlight = false;
}
// Get the field style
ScheduleField field = definition.GetField(id);
TableCellStyle style = field.GetStyle();
TableCellStyleOverrideOptions options = style.GetCellStyleOverrideOptions();
// Set override options for background color per requirement
options.BackgroundColor = applyHighlight;
style.SetCellStyleOverrideOptions(options);
// Set background color per requirement
style.BackgroundColor = applyHighlight ? highlight : white;
field.SetStyle(style);
index++;
}
}
ScheduleToHTML
这个例子所作的事情,就是从明细表读取数据,转换成一个静态的HTML页面,初步判断,适用面比较窄。