Add a row to a schedule

Add a row to a schedule

Anonymous
Not applicable
2,601 Views
7 Replies
Message 1 of 8

Add a row to a schedule

Anonymous
Not applicable

I want to add a row into a revit (key) schedule 

My code:

TableData tableData = viewSchedule.GetTableData();
TableSectionData tsd = tableData.GetSectionData(SectionType.Body);
tsd.InsertRow(tsd.FirstRowNumber );
tsd.SetCellText(tsd.FirstRowNumber + 1, tsd.FirstColumnNumber, "Schedule of column top and base levels with offsets");

This is the same code as given in: https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/CloudHelp/cloudhelp/2015/EN...

but somehow I get a ModificationOutsideTransactionException

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

BenoitE&A
Collaborator
Collaborator

Hey,

Modifications of the document need to be inserted in a Transaction.

See here for a description of how it works.

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-...

 

And be careful to the parameter of your MainClass (in this line the TransactionMode needs to be in Manual)

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

 

Cheers

Benoit


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
Message 3 of 8

Anonymous
Not applicable

Thank you, but what does "An exception will be thrown if a transaction is started from an outside thread or outside modeless dialog." mean?

0 Likes
Message 4 of 8

Anonymous
Not applicable

This is my whole code:

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    class Elementabgleich : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            View activeView = commandData.View;
            if (activeView is ViewSchedule)
            {
                ViewSchedule CellsToAdd = activeView as ViewSchedule;
                TableData tableData = CellsToAdd.GetTableData();
                TableSectionData tsd = tableData.GetSectionData(SectionType.Body);
                tsd.InsertRow(1);
                tsd.SetCellText(tsd.FirstRowNumber + 1, tsd.FirstColumnNumber, "Schedule of column top and base levels with offsets");
} else { TaskDialog.Show("Wrong View", "Change View"); } return Result.Succeeded; } 
}
Message 5 of 8

BenoitE&A
Collaborator
Collaborator

So :

- the error you mention means you can't have Transactions in 2 threads simultaneously. You can search in the web what threads are if interested. But it is easy to understand when you understand what transactions are for : encapsulate the modifications you make to the model so that you can cancel them (the use of the Cancel button of Revit is possible). So Revit denies you the possibility to make modifications in 2 threads at the same time, since they can have effects one to the other and you could not undo them.

- in your code the 2 lines starting with tsd are modifications of the model and need to be encapsulated in a 

 

using(Transaction t = new Transaction(myDoc, "myTrans")

{

 

}

 

block

Benoit

 


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 6 of 8

Anonymous
Not applicable

 

after implementing you code, I still get the same error 

 

                using (Transaction t = new Transaction(doc, "myTrans"))
                {
                    tsd.InsertRow(1);
                    tsd.SetCellText(tsd.FirstRowNumber + 1, tsd.FirstColumnNumber, "Schedule of column top and base levels with offsets");

                }

 

0 Likes
Message 7 of 8

BenoitE&A
Collaborator
Collaborator
Accepted solution

t.Start();

blabla

t.Commit();

 

Please, read the guides...


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 8 of 8

Anonymous
Not applicable

thank you so much, this worked 🙂

0 Likes