Document modification, transactions, parameters etc

Document modification, transactions, parameters etc

ollikat
Collaborator Collaborator
1,579 Views
4 Replies
Message 1 of 5

Document modification, transactions, parameters etc

ollikat
Collaborator
Collaborator

Hello all.

I had a conversation with my colleague about the Revit parameters, transactions and modifications in the project document in general. As we realized that we didn't have absolute certainty about few subjects I decided to write something here.

I'm pretty sure I have read possible answers for all these questions but I can't recall anymore where I have seen those articles / discussions. So maybe some API specialist could summarize the most important things related.

 

Actually two main questions.

1. How/when exactly updated values of parameter can be read?

We have observed that after setting the parameter, it requires at least document regeneration in order to read updated data from the parameter again. But is the regeneration always needed or sufficient? Or are there cases where a transaction commit is needed?

2. Is it 100% safe to use existing Element object after regenerate / transaction commit (let's forget the deletion)?

 

So if I for example have fetched some Element objects and after that I commit a transaction. After that usually using same elements is not a problem but is it always like that? Or can it be that the existint element might become somehow deprecated? Or is it so that regenerate / transaction ensures the validity of already existing Element objects?

 

 

So...I was hoping to get some brief clarifications for these. Propably it needs to be someone from ADSK who can give accurate answer or someone who has a very solid understanding what he/she has been doing in the past :-). Guessing is not necessary here...we can do that by our self also :-).

0 Likes
Accepted solutions (1)
1,580 Views
4 Replies
Replies (4)
Message 2 of 5

arnostlobel
Alumni
Alumni
Accepted solution

I cannot promise I'll be brief, but I will try. Problem is that the answers are even more complicated than the questions. I will first try to simplify the questions. Let's take a look at them in the order they were asked.

 

When can values of parameters be read? I assume there are two concerns implied in this question.  First, is it "safe" to read the value of a parameter? (Here "safe" means that such an attempt could be finished without an exception.) Second, can the value be trusted? (Meaning - is the value really what one should work with?)

 

The answer is: Reading parameters' values should be considered both unsafe and potentially inaccurate after changes were made to the model and the model have not been regenerated yet. After the model is regenerated, reading is safe assuming the parameter still exists. In most cases the acquired values can be trusted, even though it may not be the final values. Values are guaranteed to be final and up-o-date only between transactions. (Here up-to-date relates too local changes only; not chances happening in a central model.)

 

Is an Element object safe to be accessed after regeneration? Here the question seems clear and I assume the "safety" relates to being able to invoke methods and evaluate properties without causing exceptions. 

 

The answer is: Yes, assuming the element has indeed not been deleted, directly or indirectly, it can be accessed. The problem here is, however, how can one really know whether an element has been deleted or not? Luckily for the Revit API developer, elements that have been deleted (here meaning: removed from the model, not necessarily physically deleted) will have their IsValidObject property returning False. That one property is always safe to access even on deleted elements, for it is implemented at the API layer only and is detached from the underlying element object (native) itself. Again, if there have been changes to the model, even still valid elements should not be read from at least until regeneration is performed. Furthermore, like with the reading parameters addressed earlier, if one needs an absolute guarantee that an element may be not only safely accessed but its properties are also accurate and up-to-date, one must finish the active transaction.

 

To put a little perspective on the subject, MOST values can be safely read and trusted just after a successful regeneration. However, for the 100% guarantee only, it must to be stated that no change is really done unless the transaction is done. As one example, dynamic updaters are called at the very end of every transaction. If an updater has a certain policy about what is considered valid, the updater can still make changes to satisfy its policy. So, from the perspective of that one particular updater, the model is not really valid until the updater is executed (i.e., practically, after the transaction committed.)

 

As a side note, perhaps, I can share a bit from our internal guidelines.  We Revit developers try not to hold onto element objects in situations involving changes of the model and eventual regeneration. Instead, we prefer using element Ids and fetching the elements when we need them. The main reason is, again, that it is virtually impossible to predict what a regeneration would do, thus we cannot be sure whether or not we can safely access elements we got. (And, unfortunately, we do not have the convenience of the IsValidObject method the API developers enjoy.)

Arnošt Löbel
Message 3 of 5

ollikat
Collaborator
Collaborator

Thank's you very much for your profound answer . Superb!

 

I think we got an answer(s) that we were looking for the questions above. Now we can be more convinced what we are doing. Really appreciate having solid information about these things.

 

Although I have couple of more questions that are a bit related the ones mentioned earlier.

 

1. From a purely theoretical point of view,  it might also be interesting to know what happens when a transaction is rolled back; is it possible that certain element objects can get invalid? I think we have observed this kind of behaviour...I mean also in cases where the element hasn't been deleted.


2. Also, how are element ids (which are integers) reused after an element has been deleted? Is it possible that at some point in time another element would get the same id, or not?

0 Likes
Message 4 of 5

arnostlobel
Alumni
Alumni

More answers to more questions:

 

What happens when a transaction is rolled back? Indeed, as with committing a transaction, some elements may be invalidated when a transaction is rolled back instead. Consequently, some elements may be re-invalidated in the same process. Basically, any element that was created during a transaction would cease to exist (thus become invalid for the API user) when the transaction is rolled back. On the other hand, any element that was deleted in the transaction is resurrected (thus becomes valid again for the API user) when the transaction is rolled back. This is practically the reverse behavior to a transaction being committed.

 

We could go even beyond this, up one level to transaction groups. The same concept is applied there, meaning whatever was created (in already committed transactions) ceases to exist if the encapsulating transaction group is rolled back, which effectively undoes all transactions committed within the group.

 

How are element Ids reused? They aren’t. Revit always increment Ids for new elements no matter what have happened to already existing elements. Say you create 5 elements with Ids 1 through 5. If you then delete the last one and create a new one, the new element’s Id will be 6. Naturally, this is all in the context of a single local file. In a work-sharing environment it is a  bit more complicated as one ought to expect. If you delete an element of which Id is already used for another element in the central file, then upon syncing with the central file the Id might be seen as “kinda” reused in your local file. That is why we generally do not recommend holding on to element Ids in a work-sharing environment. It is what Unique Ids are for.

Arnošt Löbel
Message 5 of 5

ollikat
Collaborator
Collaborator
Thanks again for the reply.

Everything pretty much something we expected.

I think this is all for now... 🙂
0 Likes