Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

Revit .NET Creations API: Create 3D View and Make It Active

$
0
0

It is good that the View3D class has a CreateIsometric method now which looks reasonable enough to create a View3D accordingly.

View3D.CreateIsometric Method 
public static View3D CreateIsometric(
    Document document,
    ElementId viewFamilyTypeId
)
 
Its home looks much nicer than the one (ItemFactoryBase) of the obsolete NewView3D method in the not so natural ItemFactoryBase.

ItemFactoryBase.NewView3D Method 
[ObsoleteAttribute("This method is obsolete in Revit 2013. Call View3D.CreateIsometric() instead.")]
public View3D NewView3D(
    XYZ viewDirection
)

However, as can be seen, they are different animals now, pretty much as dogs and wolfs. Though it is possible to train wolfs nowadays to make them docsile enough, it still takes efforts. We’ve worked out some such for references. 

public static View3D NewView3D(RvtDocument doc)
{
    ElementId viewTypeId = FindViewTypes(doc, ViewType.ThreeD).First().Id;

    View3D view = View3D.CreateIsometric(doc, viewTypeId);

    return view;
}


public static View3D NewView3D(RvtDocument doc, string viewName)
{
    View3D view = NewView3D(doc);
    view.ViewName = viewName;

    return view;
}

public static View3D NewView3D(RvtDocument doc, XYZ viewDir)
{
    ElementId viewTypeId = FindViewTypes(doc, ViewType.ThreeD).First().Id;

    View3D view = View3D.CreateIsometric(doc, viewTypeId);
    //view.ViewDirection = viewDir;
    //view.UpDirection = viewDir;
           
    return view;
}

public static View3D NewView3D(RvtDocument doc, string viewName, XYZ viewDir)
{
    View3D view = NewView3D(doc, viewName);
    //view.ViewDirection = viewDir;
    //view.UpDirection = viewDir;

    return view;
}
 
The View Direction setting hasn’t been found a good way at this moment. In case anybody has good ideas, please feel free to share. Otherwise, we may revisit the topic in the future when things change.

To set the newly created 3D View as active is pretty straightforward, through setting the UIDocument.ActiveView property. One important point to note however is that the call should not made inside any transactions, otherwise, exceptions would just occur.

CachedUiApp.ActiveUIDocument.ActiveView = view3d;

Here is some sample caller code for the 3D view generation, active view switch, graphics turnning on as shown previously, and the arch gate creation as demonstrated before.

...
View3D view3d = null;
using (Transaction tran = new Transaction(CachedDoc, "NewView3D"))
{
    tran.Start();

    view3d = ViewCreation.NewView3D(CachedDoc, new XYZ(1, 2, 3));

    tran.Commit();
}

CachedUiApp.ActiveUIDocument.ActiveView = view3d;

using (Transaction tran = new Transaction(CachedDoc, "InsertArchGate"))
{
    tran.Start();

    SettingAdjuster.TurnOnAllGraphicsInActiveView(CachedDoc);

    FamilySymbol symbol = ColumnCreation.GetFirstSymbol(ColumnCreation.FindColumnFamilies(CachedDoc).FirstOrDefault());
    IOrderedEnumerable<Level> levels = FindAndSortLevels(CachedDoc);
    Level baseLevel = levels.First();
    Level topLevel = levels.ElementAt(1);
    MassInsertion.InsertArchGate(CachedDoc, baseLevel, topLevel, new XYZ(10, 0, 0), Math.PI / 4, 14);

    tran.Commit();
}
 …

After the code is executed, the 3D View will be created and set as active properly, the arch gate will be generated and made visible.
 3DViewActiveArchGate

Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.


Viewing all articles
Browse latest Browse all 872

Trending Articles