Modify Extensible Storage Document DataStorage during Save/Save As Event

Modify Extensible Storage Document DataStorage during Save/Save As Event

kraftwerk15
Advocate Advocate
1,847 Views
7 Replies
Message 1 of 8

Modify Extensible Storage Document DataStorage during Save/Save As Event

kraftwerk15
Advocate
Advocate

Hello all-

 

I'm writing some code that will write to a DataStorage object that is connected to a Family Document that is attaching a GUID. When I attempt to write to the DataStorage object connected to the Document during a Saved/Saved As event, Revit API returns a transaction completed outside of a Transaction. k. cool. I attempt to create a transaction with a using, and the API tells me I cannot start a transaction during the Saved/Saved As events.

 

So, this leads me to think that I need to raise this method after the Saved/Saved As events are completed. Is all of that true? I would think that I would be able to write to the document in the Saved/Saved As events, because this is post the Save, so the document should be ready to receive some action.

 

Exception is being thrown at that last try catch. fyi

public void SetFieldAsGUID(Document doc, string fieldName, Guid value)
        {
            if (Entity == null)
                return;
            DataStorage dataStorage = GetDataStorage(doc);
            Entity ent;
            if (dataStorage == null)
            {
            //ent = SetDataStorage(doc);
                if (doc.IsModifiable)
                    System.Diagnostics.Debug.WriteLine("document is modifiable");
                if (doc.IsReadOnly)
                    System.Diagnostics.Debug.WriteLine("document is readonly");
                ent = new Entity(IDSchema);
            }
            else
                ent = new Entity(IDSchema);
            Field field = IDSchema.GetField(fieldName);
            ent.Set(field, value);
            try
            {
                dataStorage.SetEntity(ent);
            }
            catch (Autodesk.Revit.Exceptions.ModificationOutsideTransactionException ex) { TaskDialog.Show("Modification Outside Transaction", ex.Message); }
        }

 

0 Likes
Accepted solutions (1)
1,848 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

It makes no sense to me to open a transaction after the Saved event.

  

Why would you want to modify anything after it is saved? You would need to resave again afterwards for that to make sense.

  

What do you mean by 'DataStorage object that is connected to a Family Document'? Is it in the family document or not?

 

Are you mixing up two different documents, project and family? 

 

Maybe opening a transaction on one of them and not the other?

 

Questions over questions.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 8

kraftwerk15
Advocate
Advocate

It did need to be wrapped in a using transaction.

 

 

try
            {
                using (Transaction t = new Transaction(doc, "Create DataStorage_Not Rollback"))
                {
                    t.Start(); 
                    dataStorage.SetEntity(ent);
                    t.Commit();
                }
            }
            catch (Autodesk.Revit.Exceptions.ModificationOutsideTransactionException ex) { TaskDialog.Show("Modification Outside Transaction", ex.Message); }
            catch(Autodesk.Revit.Exceptions.InvalidOperationException ex) { TaskDialog.Show("Invalid Operation", ex.Message); }

 

@jeremytammik That is a good point. Now that I'm thinking of it, I'm trying to grab where the doc.Path is saving to. So, when someone saves the family an operation completes on that family depending on if it is saved locally, saved to a network in a project folder, or saved on a network in a library folder.

 

0 Likes
Message 4 of 8

jeremytammik
Autodesk
Autodesk

So now it works?

 

Congratulations!

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 8

Revitalizer
Advisor
Advisor
Accepted solution

Hi,

 

you can use Saving/SavingAs.

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 6 of 8

kraftwerk15
Advocate
Advocate

So the SavingAs/Saving EventArgs only give me a "IsCancelled" method. I guess where I'm lost is if I am writing this GUID to JSON somewhere else, my method runs and sucessfully writes that to the JSON file, then for some reason the document was not able to be saved successfully for some reason, would I then have to run a different method in the SavedAs/Saved EventArgs to go back to that JSON file and delete the contents for that family if it is unsuccessful?

 

if(DocumentSavedAs.Status == RevitAPIEventStatus.Failed)

     then Open JSON and Delete what was just saved?

0 Likes
Message 7 of 8

Revitalizer
Advisor
Advisor

Hi,

 

sorry, I don't understand what you want to do.

 

Your JSON - is it just a JSON string you want to append to a Revit element, or is it a JSON file?

Writing external files does not need a Revit transaction at all, so this can be done anytime, even in the SAVED event.

 

You can write Revit data (and yes, you will need a transaction), but only if Revit is *about* to save - for this purpose, you must use the SAVING event.

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 8 of 8

kraftwerk15
Advocate
Advocate

Good deal. That got me where I needed to go. @jeremytammik was right to rethink that process of events. I've moved items over to the Saving/Saving As events, then I'm catching in the Saved/Saved As events if the RevitAPIEventStatus is Cancelled or Failed to then run a Delete method on the object that was just saved in the Saving/SavingAs event in the JSON file.

 

Thanks @Revitalizer and @jeremytammik 

0 Likes