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.
Solved by joaofmoco. Go to Solution.
Sorry for repeating myself hundreds of times over, but somehow I have become an anti-ToList fanatic.
You can save yourself some code and your computer some performance time and memory space by avoiding the calls to ToList for the base point and other ids. You may need to convert them from List to ICollection or something, but it will save (minimal) code, space and execution time.
I'm sorry this does not answer your question, though...
Hello Jeremy and thank you for your reply,
Although this does not answer my question, I appreciate the suggestion and I will figure out a way to not use .ToList so to save performance.
Thank you
I have figured the solutions for my issue and I'll leave the corrected code bellow:
CreateViewCommand.cs:
#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;
// Get id list of the elements regarding the NSWE building sections on the level 1 view
var levelHeadsCollector = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Levels)
.ToElementIds();
// 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;
view.HideElements(levelHeadsCollector);
// 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:
#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 id list of Project Points
List<ElementId> basePointIds = new FilteredElementCollector(uidoc.Document, level1.Id)
.WhereElementIsNotElementType()
.OfClass(typeof(BasePoint))
.ToElementIds()
.ToList();
// Get id list for the view's Elevation Markers points
List<ElementId> viewElevationMarkerIds = new FilteredElementCollector(uidoc.Document, level1.Id)
.WhereElementIsNotElementType()
.OfClass(typeof(ElevationMarker))
.ToElementIds()
.ToList();
// Get id list of the elements regarding the NSWE building sections on the level 1 view
FilteredElementCollector viewerCollector = new FilteredElementCollector(doc, level1.Id)
.OfCategory(BuiltInCategory.OST_Viewers);
// Get building elevation Ids
IEnumerable<ElementId> elemIds = from Element d in viewerCollector
where doc.GetElement(d.GetTypeId()).Name.Equals("Building Elevation")
select d.Id;
// Create a list with the building elevation Ids
ICollection<ElementId> hideMarksIds = elemIds.Cast<ElementId>().ToList();
// Get id list for axis arrow y on level1 view
var internalOriginCollector = new FilteredElementCollector(doc, level1.Id)
.OfClass(typeof(InternalOrigin))
.ToElementIds();
// 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);
if (hideMarksIds?.Count > 0)
level1.HideElements(hideMarksIds);
if (internalOriginCollector?.Count > 0)
level1.HideElements(internalOriginCollector);
// 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.25, 0));
Viewport viewport2 = Viewport.Create(doc, sheet.Id, v2.Id, new XYZ(0.45, 0.85, 0));
Viewport viewport3 = Viewport.Create(doc, sheet.Id, v3.Id, new XYZ(0.45, 0.55, 0));
Viewport viewport4 = Viewport.Create(doc, sheet.Id, level1.Id, new XYZ(1.10, 0.55, 0));
tx3.Commit();
}
return Result.Succeeded;
}
}
}
Thank you all!
Can't find what you're looking for? Ask the community or share your knowledge.