- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I have been working on a Revit Addin and, so far, the end result is in the image bellow:
However, I want the Level Heads visible in the viewports to the left, the elevation markers and the axis arrows in the right-most viewport to not be visible. Bellow is parts of the code I have so far:
CreateView.cs (Creates views visible in the left-most viewports):
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#endregion
namespace RevitAddin
{
[Transaction(TransactionMode.Manual)]
class CreateViewCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData1,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData1.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Get available Views - Filtered Element Collector
FilteredElementCollector colViewFamilies = new FilteredElementCollector(doc);
colViewFamilies.OfClass(typeof(ViewFamilyType));
// Find 3D View type views
var view3DFamilyTypes = from elem in colViewFamilies
let type = elem as ViewFamilyType
where type.ViewFamily == ViewFamily.ThreeDimensional
select type;
// Modify document within a transaction
using (Transaction tx1 = new Transaction(doc))
{
tx1.Start("CreateView");
// Arrays with the XYZ coordinates of the up and forward vectors for the needed views
XYZ[] positionsUp = new XYZ[3];
positionsUp[0] = new XYZ(0, 0, 1); // Left view
positionsUp[1] = new XYZ(0, 0, 1); // Front view
positionsUp[2] = new XYZ(1, 0, 0); // Top view
XYZ[] positionsForward = new XYZ[3];
positionsForward[0] = new XYZ(1, 0, 0); // Left view
positionsForward[1] = new XYZ(0, 1, 0); // Front view
positionsForward[2] = new XYZ(0, 0, -1); // Top view
// Create new 2D Views of the model with different orientations
for (int i = 0; i < 3; i++)
{
View3D view = View3D.CreateIsometric(doc, view3DFamilyTypes.Last().Id);
XYZ eye = new XYZ(0, 0, 0);
XYZ up = positionsUp[i];
XYZ forward = positionsForward[i];
view.SetOrientation(new ViewOrientation3D(eye, up, forward));
view.DetailLevel = ViewDetailLevel.Fine;
view.Scale = 300;
// Disable Analytical Model category Graphic Override
foreach(Category cat in doc.Settings.Categories)
{
if(cat.CategoryType == CategoryType.AnalyticalModel)
{
if (view.CanCategoryBeHidden(cat.Id))
{
view.SetCategoryHidden(cat.Id, true);
}
}
}
}
tx1.Commit();
}
return Result.Succeeded;
}
}
}
AddViewToSheetCommand.cs (Gets created views and Level 1 (default level), creates viewports and adds them to a sheet):
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#endregion
namespace RevitAddin
{
[Transaction(TransactionMode.Manual)]
class AddViewToSheetCommand : 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;
// Filtered Element Collector list for the 3D Views
FilteredElementCollector collector3DViews = new FilteredElementCollector(doc);
collector3DViews.OfClass(typeof(View3D));
List<View3D> all3DViews = collector3DViews.Cast<View3D>().ToList();
// Filtered Element Collector for the Floor Plans
FilteredElementCollector collectorFloorPlans = new FilteredElementCollector(doc);
collectorFloorPlans.OfClass(typeof(ViewPlan));
List<ViewPlan> allViewPlans = collectorFloorPlans.Cast<ViewPlan>().ToList();
// Filtered Element Collector list for Sheet Views
FilteredElementCollector collectorSheetViews = new FilteredElementCollector(doc);
collectorSheetViews.OfClass(typeof(ViewSheet));
List<ViewSheet> allSheetViews = collectorSheetViews.Cast<ViewSheet>().ToList();
// Grab created Sheet View
ViewSheet sheet = allSheetViews.LastOrDefault();
// Grab default level 1 floor plan
ViewPlan level1 = allViewPlans.First(n => n.Name == "Level 1");
// Grab all created Views
View3D v1 = all3DViews[all3DViews.Count - 3];
View3D v2 = all3DViews[all3DViews.Count - 2];
View3D v3 = all3DViews[all3DViews.Count - 1];
// Get ids of Project Points
List<ElementId> basePointIds = new FilteredElementCollector(uidoc.Document, level1.Id)
.WhereElementIsNotElementType()
.OfClass(typeof(BasePoint))
.ToElementIds()
.ToList();
// Get ids for the view's Elevation Markers points
List<ElementId> viewElevationMarkerIds = new FilteredElementCollector(uidoc.Document, level1.Id)
.WhereElementIsNotElementType()
.OfClass(typeof(ElevationMarker))
.ToElementIds()
.ToList();
// Modify document within a transaction
using (Transaction tx3 = new Transaction(doc))
{
tx3.Start("AddViewToSheet");
// Edit level1 to fit criteria
level1.DetailLevel = ViewDetailLevel.Fine;
level1.Scale = 300;
if (basePointIds?.Count > 0)
level1.HideElements(basePointIds);
if (viewElevationMarkerIds?.Count > 0)
level1.HideElements(viewElevationMarkerIds);
// Set default viewport to "No Title" type
ElementType elementType = new FilteredElementCollector(doc)
.OfClass(typeof(ElementType)).Cast<ElementType>()
.Where(x => x.Name == "No Title").First();
doc.SetDefaultElementTypeId(ElementTypeGroup.ViewportType, elementType.Id);
// Add 4 viewports, 1 for each view
Viewport viewport1 = Viewport.Create(doc, sheet.Id, v1.Id, new XYZ(0.45, 0.30, 0));
Viewport viewport2 = Viewport.Create(doc, sheet.Id, v2.Id, new XYZ(0.35, 0.85, 0));
Viewport viewport3 = Viewport.Create(doc, sheet.Id, v3.Id, new XYZ(0.45, 0.50, 0));
Viewport viewport4 = Viewport.Create(doc, sheet.Id, level1.Id, new XYZ(1, 0.40, 0));
tx3.Commit();
}
return Result.Succeeded;
}
}
}
What is missing here? I've been having this issue for a while so any feedback is appreciated.
Also, bellow is the test .rvt document and the .sln document for the addin.
Solved! Go to Solution.
Link copied