Open .rfa file to edit it

Open .rfa file to edit it

Yonas89
Collaborator Collaborator
3,789 Views
3 Replies
Message 1 of 4

Open .rfa file to edit it

Yonas89
Collaborator
Collaborator

Hello, I am stuck  a bit with this.

What would be the process to  open family file and make edit to it? By using the snippet below I get the following error: "starting a transaction from an external application running outside of API context is not allowed" And though I totally understand that I am trying to access it from outside, I have no clue what to do to access it right.  Thank you in advance!

 private void GetBfAndDfromRevit(Autodesk.Revit.ApplicationServices.Application app)
        {
            string path = ArticalNumberList.GetPath(articularTxtBox.Text);

            //Open Revit Family File in a separate document
            if(path!="")
            {            
                Document revDoc = app.OpenDocumentFile(path);
                //Get the familyManager instance from the open document
                FamilyManager mgr = revDoc.FamilyManager;
                using (Transaction tx = new Transaction(revDoc))
                {
                    tx.Start("addtype");
                    if (mgr.Types.Size == 0)
                    {
                        mgr.NewType("Type 1");
                    }
                    tx.Commit();
                }
            }
        }
0 Likes
Accepted solutions (2)
3,790 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

I answered an almost identical question this very morning:

 

https://stackoverflow.com/questions/52161965/revit-api-how-to-make-a-external-command-run-asynchrous...

 

The Revit API can only be used within a valid Revit API context, and such a context is provided exclusively by Revit events:

 

http://thebuildingcoder.typepad.com/blog/2015/08/revit-api-context-and-form-creation-errors.html#2

 

http://thebuildingcoder.typepad.com/blog/2015/12/external-event-and-10-year-forum-anniversary.html#2

  

You can however implement an external event and trigger that from outside to obtain access to a valid Revit API context. This is discussed in detail with many solutions provided by The Building Coder in the topic group on Idling and External Events for Modeless Access and Driving Revit from Outside:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

Another approach might be to make use of the DocumentOpened Event:

 

https://apidocs.co/apps/revit/2019/7e5bc7a1-0475-b2ec-0aec-c410015737fe.htm

 

You could use that to trigger the execution flow you desire.

 

I would start out reading the numerous solutions listed in the topic group, and probably end up making use of an external event.

 

Good luck and have fun!

 

Cheers, 

 

Jeremy

 



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

Message 3 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Just implement a normal external command and do the job inside its Execute method.

 



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

Message 4 of 4

Yonas89
Collaborator
Collaborator

@jeremytammik,

thanks a lot !

0 Likes