EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-9 12:40:39

访问删除的元素Access Deleted Element



Here is a question posted by Kristian (Krispy5) Parsons, Systems Analyst - Design at Westfield Design & Construction Pty. Ltd. on accessing deleted element data in the DocumentChanged event handler:Question: How do I find the category of the deleted elements? When I try to get the Element from the ElementId it fails since the element has already been deleted.In my case I would like to warn users when they have deleted a Floor.Answer: I was planning to test your assertion by looking at the Revit SDK ChangesMonitor sample which demonstrates the use of the DocumentChanged method.I assume that your question is based on using that event to be notified of the deleted elements. Looking at the ChangesMonitor DocumentChanged event handler, I note that it calls a helper method AddChangeInfoRow to list the added, deleted and modified element information in its modeless form. That method includes the following statements and comments which clearly tell you that the deleted element information is no longer accessible:private void AddChangeInfoRow(ElementId id,Document doc,string changeType ){// retrieve the changed elementElement elem = doc.get_Element( id );   DataRow newRow = m_ChangesInfoTable.NewRow();   // set the relative information of   // this event into the table.   if( elem == null ){    // this branch is for deleted element due   // to the deleted element cannot be retrieved   // from the document.   newRow["ChangeType"] = changeType;    newRow["Id"] = id.IntegerValue.ToString();    newRow["Name"] = "";    newRow["Category"] = "";    newRow["Document"] = "";}else{    newRow["ChangeType"] = changeType;    newRow["Id"] = id.IntegerValue.ToString();    newRow["Name"] = elem.Name;   newRow["Category"] = (null == elem.Category)      ? "<null>"       : elem.Category.Name; // added by jeremy   newRow["Document"] = doc.Title;}m_ChangesInfoTable.Rows.Add( newRow );}So the answer to your question is presumably 'no way', and that is as designed.There is an obvious workaround, though:The DocumentChanged event is a very simple notification after the fact. The transaction in which the document was modified has already been closed, you cannot do anything more about it yourself, and as we have seen, you cannot even query the deleted elements for their data.If you wish to access the document before the transaction is closed, there is a very powerful alternative possibility, the dynamic model update framework or DMU. It enables you to register an updater to be triggered by certain events, and also to react on these events within the same transaction that triggered them. It is demonstrated by theDynamicModelUpdate and DistanceToSurfaces SDK samplesand Saikat's Structural DMU sample.In your case, you can easily implement an updater that simply brings up a message box as described above, and register a trigger for it which reacts to the deletion of floors and nothing else.Your updater Execute method will be called while the transaction is still open, and you can analyse and even modify the elements being deleted before the transaction is terminated.Response: Yes that all looks good. By the way, your assumption is correct that I was using the DocumentChanged event.I looked into the 'dynamic model update framework'. This sounds like what I want.Later: Thanks again, this solved my problem and was easy to implement.I filter for floors and use the 'GetChangeTypeElementDeletion' in the 'AddTrigger' call, then in my 'IUpdater' class in the 'Execute' method I simply display a task dialog showing the number of floors being deleted. There is no need for any more filtering, I can just use 'data.GetDeletedElementIds().Count'.

冰山 发表于 2014-2-20 14:43:37

顶起来…………

页: [1]
查看完整版本: 访问删除的元素Access Deleted Element