简体   繁体   English

使用Revit API并行处理以提取数据

[英]Parallel Processing to Extract Data Using Revit API

As I understand, you are not supposed to use parallel processing to do stuff in Revit through its API. 据我了解,您不应使用并行处理通过Revit的API在Revit中进行操作。 More information is available at http://thebuildingcoder.typepad.com/blog/2014/11/the-revit-api-is-never-ever-thread-safe.html 有关更多信息,请访问http://thebuildingcoder.typepad.com/blog/2014/11/the-revit-api-is-never-ever-thread-safe.html

However, I am currently dealing with a problem of getting (and only getting) and then examining a lot of data to make some decisions. 但是,我目前正在处理以下问题:获取(并且仅获取)然后检查大量数据以做出一些决策。 A sample code using TPL is given below, that seems to be working: 下面给出了使用TPL的示例代码,该代码似乎可以正常工作:

                Parallel.ForEach<Element>(allDocumentElementsNotVisibleInCurrentView,
                parallelOptions,
                element =>
                {
                    MyDerivedElement iaElement = new MyDerivedElement(uiDocument.Document, element, ElementStatusInView.Found); 

                    // The following condition line does the real heavy lifting and can be a lengthy process, involving data extraction from Revit, such as bounding boxes, geometry and curves
                    if (iaElement.IsContained(iaView))
                        lock (result)
                            result.Add(iaElement);
                });

So my question is, given that the above seems to work in tests, is it a good idea to let this one pass on parallel processing? 因此,我的问题是,鉴于以上内容似乎可以在测试中工作,让它通过并行处理是否是一个好主意?

Thank you for your help! 谢谢您的帮助!

The Revit API cannot be used outside a valid Revit API context, and such a context is only provided by a Revit event notification to be processed within a Revit event handler. Revit API不能在有效的Revit API上下文之外使用,并且此类上下文仅由Revit事件通知提供,并在Revit事件处理程序中进行处理。 The most common event is the external command Execute method. 最常见的事件是外部命令Execute方法。 In the past, this requirement was not strictly enforced. 过去,没有严格执行此要求。 However, use of the Revit API outside a valid Revit API context can lead to a crash and data corruption. 但是,在有效的Revit API上下文之外使用Revit API可能会导致崩溃和数据损坏。 Read-only access may still work, but is risky. 只读访问可能仍然有效,但存在风险。 I certainly would not store the Element instance itself. 我当然不会存储Element实例本身。 Storing the ElementId seems like a safer bet. 存储ElementId似乎是一个更安全的选择。 Please expect to crash at any time. 请期待随时崩溃。

My recommendation would be to reduce the Revit API interaction and processing to an absolute minimimum, collect all the data required for processing, terminate the Revit API interaction after collecting the data, and then run the pure calculations with no further Revit API interaction in a separate thread or several threads at a later point after leaving the Revit API context. 我的建议是将Revit API的交互和处理减少到最低限度,收集处理所需的所有数据,在收集数据后终止Revit API的交互,然后在没有其他Revit API交互的情况下运行纯计算离开Revit API上下文之后的一个或多个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM