Transaction does NOT Start even when document is modifiable

Transaction does NOT Start even when document is modifiable

fawzimasri7137
Advocate Advocate
2,499 Views
8 Replies
Message 1 of 9

Transaction does NOT Start even when document is modifiable

fawzimasri7137
Advocate
Advocate

I am having an issue with starting a transaction.

In the code Below:
1- m_document IS modifiable.
2- New transaction code completes, and checking its status returns "Uninitialized"... not sure what to expect here.
3- I never get through the "loadFamilies.Start". my macro quits silently. I was expecting to at least execute one of its if-else dialogs.


So, I am trying to understand why i cannot execute ".Start" if the document is modifiable, and whether "uninitialized" means anything here.... how do i debug this?

 

if (m_document.IsModifiable) 
{
 TaskDialog.Show("RtPrfBldr", "Document is modifiable"); 
}

Transaction loadFamilies = new Transaction(m_document, "Load Families");
      
 TaskDialog.Show("RtPrfBldr", "loadFamilies GetStatus:  " + loadFamilies.GetStatus().ToString());

if (loadFamilies.Start() != TransactionStatus.Started)
    {
     TaskDialog.Show("RtPrfBldr", "Started trx = " + loadFamilies.GetStatus().ToString());
     }
else
    {
    TaskDialog.Show("RtPrfBldr", "Could not Start trx:" + loadFamilies.GetStatus().ToString());
     }

 

0 Likes
Accepted solutions (1)
2,500 Views
8 Replies
Replies (8)
Message 2 of 9

franciscopossetto
Advocate
Advocate

Hey,

 

This is what documentation says about the property IsModifiable:

"This is not a permanent state such as, for example IsReadOnlyFile . Value of this property changes dynamically multiple times within the life-time of an open document. Regardless of the mode a document is opened with, the model can only be modified inside an open transaction. Furthermore, even with a transaction open, the model is not always modifiable..."

 

So, as the model can only be modified inside an open transaction I think the first statement you have will always be evaluated as false. If you want to check if the document is not read-only maybe you can use IsReadOnlyFile property.

 

I run this code and it worked:

 

 

Document m_document = commandData.Application.ActiveUIDocument.Document;
UIDocument uidoc = commandData.Application.ActiveUIDocument;

Transaction loadFamilies = new Transaction(m_document, "Load Families");
if (loadFamilies.Start().Equals(TransactionStatus.Started))
{
    TaskDialog.Show("RtPrfBldr", "Started trx = " + loadFamilies.GetStatus().ToString());
    bool docIsModifiable = m_document.IsModifiable;
}
else
{
    TaskDialog.Show("RtPrfBldr", "Could not Start trx:" + loadFamilies.GetStatus().ToString());
}
loadFamilies.Commit();

 

 

 

Transaction stated and the document is modifiable.

 

I hope it helps,

Kind regards.

Github:
https://github.com/franpossetto
0 Likes
Message 3 of 9

fawzimasri7137
Advocate
Advocate

hi @franciscopossetto 

maybe i need to rephrase my question.

My code was exactly like yours, except that "loadFamilies.Start" would not execute and macro stops there.

So i thought maybe another Transaction is still open, and that's why i checked to see if the document is modifiable.

 

I just added a test for IsReadOnly, and it is NOT a ReadOnly document.

 

But what is bothering me is that why "loadFamilies.Start" does not execute.  i should see a dialog box telling me that the transaction started or that it did not start.  Instead the macro silently stops, and i am not sure how to debug that.

any ideas?

0 Likes
Message 4 of 9

franciscopossetto
Advocate
Advocate

Hey,

 

When I executed the code you shared, I got an error that said there was an open transaction. Have you added a loadFamilies.Commit() on some place of your code? You could add a try/catch to get the error and know what is happening.

Github:
https://github.com/franpossetto
0 Likes
Message 5 of 9

fawzimasri7137
Advocate
Advocate

great idea, why did i not think of that..

 

OK, so i put the whole thing in try-catch and i got the following:

"Autodesk. Revit.Exceptions.InvalidOperationException:

Starting a new transaction is not permitted. It could be because another transaction already started and has
not been completed yet, or the document is in a state in which it cannot start a new transaction (e.g. during
failure handling or a read-only mode, which could be either permanent or temporary), at Autodesk.Revit.DB.Transaction.Start() at Revit.SDK.Samples.RoutingPreferenceTools.CS.Routing...xDoc)"

 

But this is not helping me much, since all the issues they mention, are already addressed, except for the failure handling.

1- i Checked if document is modifiable... which means that there is no other open transaction ( or am i still wrong on that?)

2- the Document is NOT in readonly nor in readonly file state.

 

 

one more question, how could you execute my code and get an open transaction error?  i mean there is nothing there in  the code  that i posted that would have opened a transaction before me creating the loadFamilies transaction?

0 Likes
Message 6 of 9

franciscopossetto
Advocate
Advocate

Hey,

 

I created a new empty solution and paste your script there.

I think as you started the transaction but you never closed it, you cannot start a new one.

 

1. Not sure about that. As Remarks say "Value of this IsModifiable changes dynamically". 

 

I would do the following:

1. Comment the first if statement where you ask if the document is modified or not.

2. Add loadFamilies.Commit() after the second if/else statement you have.

3. Restart Revit and try again.

 

I think it should work.

Github:
https://github.com/franpossetto
0 Likes
Message 7 of 9

fawzimasri7137
Advocate
Advocate
Accepted solution

OK, i think i figured it out... it is a misunderstanding on my part.

if the Document is Modifiable, then some transaction is going on and i cannot create another.

My mind was stuck on that if a document is modifiable, then I can go ahead and start a transaction to modify things....wrong!!!

 

So now i have to figure out what transaction has not closed yet .

 

@franciscopossetto Thanks for your help...  if you could answer my question on how testing my code snippet showed an open transaction, it would be most helpful.

Message 8 of 9

franciscopossetto
Advocate
Advocate

I am glad you could solve it !

Yes, for sure.

 

1. I created a new external command and paste your code there.

2. I built the solution.

3. I attached the process to Revit and start the debug mode

4. I run the command with Add-in Manager.

 

You can see this article, it has a detailed explanation:

https://archi-lab.net/debugging-revit-add-ins/

 

If you haven´t created Revit plug-ins before, you can follow this tutorial:

https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/simplecontent/content/my-fi...

 

 

Kind regards!

Github:
https://github.com/franpossetto
0 Likes
Message 9 of 9

fawzimasri7137
Advocate
Advocate

@franciscopossetto Thanks a lot for the help and the links...

0 Likes