Creating a Drafting View

Creating a Drafting View

wils02
Enthusiast Enthusiast
1,101 Views
2 Replies
Message 1 of 3

Creating a Drafting View

wils02
Enthusiast
Enthusiast

I am trying to just create a new drafting view....I have found the following series of back and forth, and it definitely helped me get somewhere.

 

https://forums.autodesk.com/t5/revit-api-forum/viewdrafting-create-viewfamilytypeid/td-p/5679197

 

HOWEVER, when I click the command in a Revit project, it just crashes and closes. From what little I understand at the moment, I think this has something to do with the "Transaction".

 

Is there anyone that wouldnt mind explaining, on a basic level since I am new at this, how to create a new drafting view, and where I am going wrong. I want to understand this on a fundamental level and not simply cookbook code.

 

The following is the code I managed to pull together

 

 

[Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            ViewFamilyType vd = new FilteredElementCollector(doc)
                .OfClass(typeof(ViewFamilyType))
                .Cast<ViewFamilyType>()
                .FirstOrDefault(q => q.ViewFamily == ViewFamily.Drafting);

            
            ViewDrafting view = ViewDrafting.Create(doc, vd.Id);
            
            return Result.Succeeded;
        }

    }

 

Please and thank you.

JW

0 Likes
1,102 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

Dear JW,

 

When you create a drafting view, or any other view, for that matter, the Revit database is modified.

 

Any modification to the database must be performed within a transaction.

 

This is the very basis or Revit API programming.

 

If you are new to this, please begin by working through the getting started material:

 

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

 

That will answer this question of yours and many more besides, and save you and others lots of hassle and complications.

 

Here is lots more on the topic of transactions:

 

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

 

Good luck and have fun with the Revit API!

 

Cheers,

 

Jeremy

 

 



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

0 Likes
Message 3 of 3

wils02
Enthusiast
Enthusiast

Jeremy,

 

Thanks for the links, especially the "transactions"; its like drinking out of a fire hose, but that is how these things typically are. I will respond again if I am still confused!

 

JW

0 Likes