- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Background
In my previous post I was facing a type issue that someone helped me.
Now I tried to run my script in Revit and I get an error.
I dont know if its because its inside Revit environment, bur Im used to python and pyRevit where the error message is much more readable and clearer regarding what I need to fix.
This script is not exactly new, I wrote it in python (works and in use) and now I want to learn C#.
What Ive tried
After reading the error message my guess is that the error has something to do with .AsString() method
I tried both .AsString() and .AsStringValue() when I want to get the Parameter's value, and also made sure that the storage type is actually string.
My questions
1. Is this how you normally get error messages in C#?
2. What do you think about my error? please see code below.
Code
using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Security.Claims;
namespace UntaggedDevicesInSheet
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// variables
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
View activeView = doc.ActiveView;
var devicesDict = new Dictionary<string, Dictionary<string, List<ElementId>>>();
// creating a combined filter out of categories of devices we want to check if they're tagged or not
ICollection<BuiltInCategory> device_categories = [BuiltInCategory.OST_FireAlarmDevices, BuiltInCategory.OST_SecurityDevices];
List<ElementCategoryFilter> device_category_filters = [];
foreach (BuiltInCategory category in device_categories)
{
device_category_filters.Add(new ElementCategoryFilter(category));
}
LogicalOrFilter devices_combined_filter = new LogicalOrFilter((IList<ElementFilter>)device_category_filters);
// creating a combined filter out of categories of device tags we want
ICollection<BuiltInCategory> tag_categories = [BuiltInCategory.OST_FireAlarmDevices, BuiltInCategory.OST_SecurityDevices];
List<ElementCategoryFilter> tag_category_filters = [];
foreach (BuiltInCategory category in tag_categories)
{
device_category_filters.Add(new ElementCategoryFilter(category));
}
LogicalOrFilter tags_combined_filter = new LogicalOrFilter((IList<ElementFilter>)tag_category_filters);
// get all viewports in sheet
IEnumerable<Element> allViewports = new FilteredElementCollector(doc, activeView.Id).OfClass(typeof(Viewport)).WhereElementIsNotElementType().ToElements();
foreach (Element viewPort in allViewports)
{
string detailNum = viewPort.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsValueString();
// get & sort devices by category
ICollection<Element> allDevicesInViewport = new FilteredElementCollector(doc, viewPort.Id).WherePasses(devices_combined_filter).WhereElementIsNotElementType().ToElements();
// sort devices by category
List<Element> fireAlarmDevices = [];
List<Element> securityDevices = [];
foreach (Element device in allDevicesInViewport)
{
string categoryName = device.Category.Name;
if (categoryName == "Fire Alarm Devices")
{
fireAlarmDevices.Add(device);
}
else if (categoryName == "Security Devices")
{
securityDevices.Add(device);
}
}
// get & sort tags by category
IEnumerable<Element> allTagElements = new FilteredElementCollector(doc, viewPort.Id).WherePasses(tags_combined_filter).WhereElementIsNotElementType().ToElements();
IEnumerable<IndependentTag> allTags = allTagElements.Cast<IndependentTag>();
List<IndependentTag> allFireAlarmTags = [];
List<IndependentTag> allSecurityTags = [];
foreach (IndependentTag tag in allTags)
{
string catergoryName = tag.Category.Name;
if (catergoryName == "Fire Alarm Device Tags")
{
allFireAlarmTags.Add(tag);
}
else if (catergoryName == "Security Device Tags")
{
allSecurityTags.Add(tag);
}
}
// Get all TAGGED fire alarm + security devices tags
List<ElementId> taggedFireDevicesIds = allFireAlarmTags.Select(tag => tag.GetTaggedLocalElement().Id).ToList();
List<ElementId> taggedSecurityDevicesIds = allSecurityTags.Select(tag => tag.GetTaggedLocalElement().Id).ToList();
// Get all UNTAGGED fire alarm + security devices tags
List<ElementId> untaggedFireDevices = fireAlarmDevices.Where(fireDevice => !taggedFireDevicesIds.Contains(fireDevice.Id)).Select(fireDevice => fireDevice.Id).ToList();
List<ElementId> untaggedSecurityDevices = securityDevices.Where(securityDevice => !taggedFireDevicesIds.Contains(securityDevice.Id)).Select(securityDevice => securityDevice.Id).ToList();
devicesDict[detailNum] = new Dictionary<string, List<ElementId>>
{
{ "fire", untaggedFireDevices },
{ "security", untaggedSecurityDevices }
};
}
string finalReport = "";
foreach (var kvp in devicesDict)
{
int fireLen = kvp.Value["fire"].Count;
int securityLen = kvp.Value["security"].Count;
if (fireLen > 0 | securityLen > 0)
{
string viewportNum = kvp.Key;
finalReport += $"Viewport #{viewportNum}:\n" +
$"{fireLen} Untagged Fire devices\n" +
$"{securityLen} Untagged security devices\n" +
new string('-', 20) + "\n";
}
}
TaskDialog.Show("Viewport Information", finalReport);
return Result.Succeeded;
}
}
}
Solved! Go to Solution.