ViewDrafting.Create | viewFamilyTypeId

ViewDrafting.Create | viewFamilyTypeId

Anonymous
Not applicable
4,554 Views
4 Replies
Message 1 of 5

ViewDrafting.Create | viewFamilyTypeId

Anonymous
Not applicable

In the process of developing a Revit 2015 add-in I have encountered a road block with the ViewDrafting.Create() method. More specifically, I am not sure how to generate the ElementId viewFamilyTypeId parameter. I have experimented with ViewType.DraftingView as well as the view family type class without any success. As I understand the API, ElementId objects can be retrieved using the .Id function for element items, and the element view family is where I need to be operating. Does anyone have ideas about how to receive a reference to this viewFamilyTypeId? For context, my current code:

 

    [Transaction(TransactionMode.Automatic)]
    public class Command : IExternalCommand
    {
        // Add-in requried Execute method called on button press.
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // generate form, organize returned doors list, and draw details
            ArrayList list = UserInterface.generate();
            if (list != null)
            {
                // create new draft view for detailing
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document;
                ElementId id = ViewFamily.Drafting.;
                ViewDrafting draftView = ViewDrafting.Create(doc, id);
                uidoc.ActiveView = draftView;
                // create organizer to manage construction of all details
                Organizer organizer = new Organizer(list, commandData);
            }
            // return to revit process
            return Result.Succeeded;
        }
    }
0 Likes
Accepted solutions (2)
4,555 Views
4 Replies
Replies (4)
Message 2 of 5

Troy_Gates
Advocate
Advocate
Accepted solution

To get the ViewFamilyType for a Drafting view use the following code:

 

ViewFamilyType vd = new FilteredElementCollector(doc)
			.OfClass(typeof(ViewFamilyType))
		        .Cast<ViewFamilyType>()
			.FirstOrDefault(q => q.ViewFamily == ViewFamily.Drafting);
		       
ViewDrafting draftView = ViewDrafting.Create(doc, vd.Id);
Message 3 of 5

Anonymous
Not applicable

Thanks for your help Troy! This is definitely a step in the right direction for me but I'm still encountering a problem. I am no longer getting any warnings, errors, or even exceptions, but Revit crashes at the ViewDrafting.Create line. I've broken the process down into some small subsets and have found that my document reference seems to be spot on, my ViewFamilyType view object appears to be valid, and I'm receiving an actual ElementId from this view. I'll attach a picture of my local variable conditions just before the crash as well as the portion of my code producing the crash. It might be useful to know that I moved to manual transaction mode because I encountered an issue where the active view could not be changed while in an open transaction. Any help would be greatly appreciated. Thank you for your time.

 

    /// <summary>
    /// Command class, contains initilization code for button press event. Sets
    /// transaction mode to automatic and prepares user interface form.
    /// </summary>
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        // Add-in requried Execute method called on button press.
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // generate form, organize returned doors list, and draw details
            ArrayList list = UserInterface.generate();
            // generate document references
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            // if doors in list
            if (list != null)
            {
                // create new draft view for detailing
                ViewFamilyType view = new FilteredElementCollector(doc)
                                    .OfClass(typeof(ViewFamilyType))
                                    .Cast<ViewFamilyType>()
                                    .FirstOrDefault(q => q.ViewFamily == ViewFamily.Drafting);
                ElementId id = view.Id;
                ViewDrafting draftView = ViewDrafting.Create(doc, id); // *PROGRAM CRASHES HERE*
                uidoc.ActiveView = draftView;
                // attempt to begin new transaction
                Transaction transaction = new Transaction(doc);
                if (transaction.Start("Generate Details") == TransactionStatus.Started)
                {
                    // create organizer to manage construction of all details
                    Organizer organizer = new Organizer(list, commandData);
                    // attempt to commit transaction and check status
                    if (transaction.Commit() != TransactionStatus.Committed)
                    {
                        TaskDialog.Show("Failure", "Transaction could not be committed. "
                                        + "Revit detected an error while attempting to commit "
                                        + "chagnes made by Door Hardware Automator");
                    }
                    // if commit fails then rollback to previous transaction
                    else
                    {
                        transaction.RollBack();
                    }
                }
            }
            // return to revit process
            return Result.Succeeded;
        }
    }   

*Additonal: After some expirementaiton I have discovered that if I move back to Automatic mode and remove the active view change statement the drafting view is succesfully created but the automatic transaction encounteres an error in the process. So, the ViewDrafting.Create statement seems to work but only in Automatic mode and produces some form of a transaction error. 

Message 4 of 5

Troy_Gates
Advocate
Advocate
Accepted solution

The ViewDrafting.Create method would need to be inside your transaction since it is modifying the model. 

Message 5 of 5

Anonymous
Not applicable

Troy,

 

That was absolutely correct! I can't beleive I missed such a simple error. Forest for the trees I suppose. I moved my draft view creation to its own transaction and switched current views before entering my second transaction. Works great! Thank you very much!

0 Likes