How to Apply a View Template

public void ApplyViewTemplateToActiveView()
{
    Document doc = this.ActiveUIDocument.Document;

    // see http://wp.me/p2X0gy-2p for more info on how this works
    View viewTemplate = (from v in new FilteredElementCollector(doc)
        .OfClass(typeof(View))
        .Cast<View>()
        where v.IsTemplate == true && v.Name == "MyViewTemplate"
        select v)
        .First();

    using (Transaction t = new Transaction(doc,"Set View Template"))
    {
        t.Start();           
        doc.ActiveView.ViewTemplateId = viewTemplate.Id;
        t.Commit();
    }
}
Advertisements

2 thoughts on “How to Apply a View Template

  1. I get some errors when i apply the code

    The type or namespace name ‘Room’ could not be found (are you missing a using directive or an assembly reference?) (CS0246) –
    ‘ObjectType’ is inaccessible due to its protection level (CS0122) –
    The type or namespace name ‘Room’ could not be found (are you missing a using directive or an assembly reference?) (CS0246) –

    all on this line
    Room room = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Room;

    it doesn’t make the macro

    What am i doing wrong??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s