Revit SDK 介绍:ScheduleAutomaticFormatter & ScheduleToHTML

ScheduleAutomaticFormatter

这个例子介绍如何对明细表进行自动的格式化。
关于什么是明细表,可以参考:Revit API:明细表 ViewSchedule
这个例子的内容可以分为两个部分:

  1. 格式化明细表
  2. 添加 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页面,初步判断,适用面比较窄。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值