Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Transaction problem - opening a family inside a document in code

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
emil
898 Views, 7 Replies

Transaction problem - opening a family inside a document in code

I am trying to do this : views.ElementAt(0).AreAnnotationCategoriesHidden = true;

to a view of a family.

 

with the following code

 

 

familyDoc = this.revitApplication.Application.OpenDocumentFile(filename);
using (TransactionGroup t = new TransactionGroup(familyDoc))
// using (SubTransaction t = new SubTransaction(familyDoc))
{
t.Start();
if (familyDoc != null)
{
IEnumerable<Autodesk.Revit.DB.View> views
= new FilteredElementCollector(familyDoc)
.OfClass(typeof(Autodesk.Revit.DB.View))
.Cast<Autodesk.Revit.DB.View>()
.Where<Autodesk.Revit.DB.View>(v => v.CanBePrinted);
Autodesk.Revit.UI.PreviewControl vc = PreviewElementHost.Child as Autodesk.Revit.UI.PreviewControl;
if (vc != null) { vc.Dispose(); }
views.First().AreAnnotationCategoriesHidden = true;
PreviewElementHost.Child = new Autodesk.Revit.UI.PreviewControl(familyDoc, views.Last().Id);
t.RollBack();
}
}
}
catch (System.Exception ex)
{
throw ex;
}

 

I allways get that Im either not in a transaction or the transaction group is null.

Any suggestions ?

yes I have automatic transactions annotated on the class.

 

Regards,

Emil

 

7 REPLIES 7
Message 2 of 8
kwhite
in reply to: emil

You may have your try statement called out before the snippet of text that you posted, but I am not seeing it.  I do, however, see the catch statement.

 

I am also not seeing t.Commit(); in your code.  I do see the t.Start() and t.Rollback().

 

I am still learning a lot about writing code myself, but maybe those things I see above will be a help to you.

 

Keith

Message 3 of 8
emil
in reply to: kwhite

here is the correct code with the try block , but that isnt the problem, the problem is that the transactions are connected to the documents and Im opening a new family in code there

            try
            {
                familyDoc = this.revitApplication.Application.OpenDocumentFile(filename);
                using (TransactionGroup t = new TransactionGroup(familyDoc))
//                using (SubTransaction t = new SubTransaction(familyDoc))
                {
                    t.Start();

                    if (familyDoc != null)
                    {
                        IEnumerable<Autodesk.Revit.DB.View> views
                            = new FilteredElementCollector(familyDoc)
                              .OfClass(typeof(Autodesk.Revit.DB.View))
                              .Cast<Autodesk.Revit.DB.View>()
                              .Where<Autodesk.Revit.DB.View>(v => v.CanBePrinted);

                            //views.ElementAt(0).AreAnnotationCategoriesHidden = true;
                            Autodesk.Revit.UI.PreviewControl vc = PreviewElementHost.Child as Autodesk.Revit.UI.PreviewControl;
                            if (vc != null) { vc.Dispose(); }
                            //views.First().DisplayStyle = DisplayStyle.Rendering;
                            //views.First().AreAnnotationCategoriesHidden = true;

                            if (st == SwingType.DOOR)
                                PreviewElementHost.Child = new Autodesk.Revit.UI.PreviewControl(familyDoc, views.First().Id);
                            else if (st == SwingType.WINDOW)
                                PreviewElementHost.Child = new Autodesk.Revit.UI.PreviewControl(familyDoc, views.Last().Id);
                            else if(st == SwingType.FURNITURE)
                                PreviewElementHost.Child = new Autodesk.Revit.UI.PreviewControl(familyDoc, views.Last().Id);
                            t.RollBack();
                    }
                }
            }
            catch (System.Exception ex)
            {
	        t.RollBack();
                throw ex;
            }

 

Message 4 of 8
emil
in reply to: emil

The reason you dont see a commit is simple, Im temporarily changing properties before displaying the view in a preview control.

and then rollbacking that because I dont want to change the family object itself.

 

And I get an error when I dont have a transaction associated with this.

 

that is if I try to do anything to the view before I display it (this is the view associated with the family object) I get an error.

 

views.First().AreAnnotationCategoriesHidden = true;
Message 5 of 8
emil
in reply to: emil

 

using the right type of transaction is important, this works

 

using(Transaction t = new Transaction(familyDoc))

 

 

also apparently I have to commit the transaction to be able to see the changes in the preview control.

 

so that is working 🙂

 

 

Message 6 of 8
arnostlobel
in reply to: emil

 Yes, you have figured out, emil. Rather then having the right transaction it is about having a transaction in the right document. A transaction must always be open in the document that is being modified.

Arnošt Löbel
Message 7 of 8
arnostlobel
in reply to: arnostlobel

Emil,

 

A minute ago I posted a comment based on my wrong looking at your code. I then deleted a part of the comment , looked at your code again, and now I am posting a new comment.

 

Yes again, you need a transaction in order to modify a document. You mentioned the "right transaction", and that is a little misleading, for there is only one kind of transactions in Revit and that is the one represented by the Transaction class. Neither TransactionGroup nor SubTransaction is a transaction in spite of their similar names. Both those latter classes are related to transactions and are parts of the overall transaction framework. However, neither is enough to allow modifications of a document. (There is a lot more on the subject in the SDK documentation as well as in the RevitAPI.chm help.)

 

By the way, you probably also have figured out by now that you need to name your transaction in order to be able to start it.

Arnošt Löbel
Message 8 of 8
ssw9UZNL
in reply to: emil

Hello,

 

In your first line, family doc, what can I pass in, instead of 'this'.

 

Essentially I want to be able to open a family file from within revit, using a path that I have.

 

Thanks

 

familyDoc = this.revitApplication.Application.OpenDocumentFile(filename);
using (TransactionGroup t = new TransactionGroup(familyDoc))

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community