Dear Santa,
I know I'm a bit late but hopefully I can get this for my 2022 Christmas.
I'm trying to copy elements from one document into another, which I'm managing fine. The issue I'm facing is that, despite having the origin document phases present in the destination document (same phase names at least), the copied elements' Phase Created and Phase Demolished are not matching the original elements'.
I've checked if the ElementIds returned by CopyElements() come up in the same order as the given ElementIds but unfortunately that was not the case and once the elements are copied I'm struggling to make a connection between origin and destination so I can match the phases.
I'll try and behave this year!
Fabio
Solved! Go to Solution.
Solved by sragan. Go to Solution.
I believe that when you collect a bunch of elements and copy them all together in one single operation, Revit will try to maintain and restore all their mutual relationships in the target database. Therefore, it might help if you add all possible references these elements have to other source database elements to the set of elements to copy. These references will include the element instances themselves, their types, phases, levels, views, materials, and whatever other objects you are interested in. Then you will have to test and see what Revit can do to try to avoid creating duplicates of them in the target database and map them to existing target objects instead.
This behaviour is hinted at in the list of extensible storage features:
https://thebuildingcoder.typepad.com/blog/2011/06/extensible-storage-features.html#7
That’s standard behavior for the Revit User Interface. Elements that are pasted into a view do not get their phases from the copied elements. Instead, they get their phases from the phase of the view they are pasted into. For example, if the view pasted into has a new construction phase, that’s the phase that the elements will inherit for their created in phase.
It’s possible to check the phases of every element copied, store those in an array, and then apply the phases to the new elements. I’ve created an addin that does that, and I’ve posted the code online. You will have to do some extra work to deal with the fact that you are pasting into a different document, but that should be fairly easy to do.
https://sites.google.com/site/revitapi123/copy-similar-code
Thanks for the help, @jeremy_tammik and @sragan .
@sragan I was doing exactly what you suggested but I was getting inconsistent results when comparing the elements from the source model against the destination model. I then tried a few different ways and ended up realising that the CopyElements() does return the same order of the given ElementId list, which then solved my problem.
When I collected the elements in my source document I also collected their Phase Created and Phase Demolished and stored all 3 in a list as shown below. This allowed me to compared the output of CopyElements() with that list and apply the settings of each element individually.
List<KeyValuePair<ElementId, KeyValuePair<string, string>>>()
Thank you both for the interesting discussion and useful sample code! I added it to the blog for posterity: