【问题标题】:Checking that an Extensible Storage Schema exists before retrieving it's fields在检索字段之前检查可扩展存储模式是否存在
【发布时间】:2014-05-27 01:14:40
【问题描述】:

我使用了一个 if 语句来检查检索模式的方法是否为空,这是一个单独的表单,其中包含要填充的 checkedListBox。代码在下面,我已经标记了检查这个的条件。我的问题是; 确保每次在新的 .rvt 文件中运行 revit 加载项时,在尝试检索架构之前架构记录已经存在的最有效方法是什么?当事情出错时,尝试访问空模式时会发生空引用错误。

           //CheckedListBox for filter01 this exists in the form and calls the main 
           class function to retrieve the record.
                checkedListBox1.DataSource = WS.categoryList(rvtDoc, intSwitch = 1);
                Filter01_CategoryList = new List<BuiltInCategory>();


                **if (WS.retSchemaBICMethod(rvtDoc) != null)**
                {
                    TaskDialog.Show("Schema 1 ", " exists");
                    Filter01_CategoryList = WS.retSchemaBICMethod(rvtDoc);
                }
                else
                {
                    TaskDialog.Show("Schema 1 "," has not been created");

                    //Update stored schema field values 
                    inputBIC = checkedListBox1.CheckedItems.Cast<BuiltInCategory>
                    ().ToList<BuiltInCategory>();
                    WS.getSetBIC(rvtDoc, inputBIC);
                    WS.storeSchema(rvtDoc, WS.projectInfoElement, inputBIC,
                    out WS.retrieveBIC);

                    //set checkedlistbox 1
                    Filter01_CategoryList = WS.retSchemaBICMethod(rvtDoc);

                }


       //this code returns the retrieved schema from the main class
    public List<BuiltInCategory>retSchemaBICMethod(Document doc)
    {

        Element piElement = projectInfoFilter(doc);

            // Read back the data from ProjInfo
            Entity retrievedEntity = piElement.GetEntity(Schema.Lookup(schemaGuid));
            IList<int> retrievedData = retrievedEntity.Get<IList<int>>
            (Schema.Lookup(schemaGuid).GetField("BuiltInCatIds"));


            //cast int list back to built-in category list
            retSchemaBIC = retrievedData.Cast<BuiltInCategory>
            ().ToList<BuiltInCategory>();

        return retSchemaBIC;
    }

【问题讨论】:

  • 在 Revit 会话中打开另一个连续文件后,运行插件后会出现架构空引用。莫名其妙地,在启动新的 Revit 会话并打开新文件时不会发生这种情况。

标签: api null storage revit extensible


【解决方案1】:

每个 Revit 项目的 GUID 很可能不同。

目前尚不清楚您如何为 retSchemaBICMethod 提供 guid,因此这似乎很可能是您的问题的原因。

我会从 GetEntity 调用中取出 Schema.Lookup 并首先检查您的值是否有效,因为我怀疑您的空引用发生在哪里。

我还认为 Revit 处理多个文档和可扩展存储的方式存在问题。在打开第二个文档并就该问题向 Revit 提交错误报告时,我遇到了许多问题。

【讨论】:

  • Thanx sweetfa,我会试试这个。
【解决方案2】:

要检查架构是否存在,我使用以下name 是您的架构名称。如果你使用!= null,它会在运行时产生错误。

Schema s = Schema.ListSchemas().FirstOrDefault(q => q.SchemaName == name);
if (s == null)
{
    // no schema found, create one
}
else
{
    // schema found, use it
}

【讨论】:

    【解决方案3】:

    要检索字段数据,您必须检查是否存在可扩展存储架构。为此,您必须先了解整个结构。

    数据存储 -> 架构 GUID -> 架构 -> 实体 -> 字段

    首先您必须检查数据存储是否存在。您可以通过FilteredElementCollector查看。

    FilteredElementCollector collector = new FilteredElementCollector(doc);
            var dataStorage = collector.OfClass(typeof(DataStorage)).FirstElement();
    
            if (dataStorage == null)
            {
                TaskDialog.Show("No data storage found");
                return null;
            }
    

    每个架构都有唯一的 GUID。针对存储在项目中的每个架构检索 GUID。

    IList<Guid> SchemaGuids = dataStorage.GetEntitySchemaGuids();
    

    现在您可以通过 Schema.Lookup(guid) 方法获取架构。获取架构后,您可以轻松获取架构中存储的实体及其值。

     foreach (Guid guid in SchemaGuids)
            {
                Schema schema = Schema.Lookup(guid);
                Entity entity = dataStorage.GetEntity(schema);
                tokenValue = entity.Get<string>("Token");
            }
    

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2013-05-12
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      相关资源
      最近更新 更多