Element ID in schedules c# revit api

Element ID in schedules c# revit api

alena.karle
Explorer Explorer
333 Views
1 Reply
Message 1 of 2

Element ID in schedules c# revit api

alena.karle
Explorer
Explorer

Hello Forum,

 

How to get Element ID in schedules with c# and revit API? similar to this video with Dynamo  - https://youtu.be/U-tVoCYilxo - but with c# and revit api.

 

here is my try:

 

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nice3point.Revit.Toolkit.External;
using Ecoworx.Core.Elements;

namespace Ecoworx
{
[Transaction(TransactionMode.Manual)]
public class CreateScheduleCommandHandler : ExternalEventHandler
{
public override void Execute(UIApplication uiapp)
{
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;

CreateSchedule(uiapp);
}

public static void CreateSchedule(UIApplication uiapp)
{
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;

using (Transaction t = new Transaction(doc, "Create single-category"))
{
t.Start();

// Create schedule
ViewSchedule vs = ViewSchedule.CreateSchedule(doc, new ElementId(BuiltInCategory.OST_Windows));

ElementId someId = new ElementId(BuiltInCategory.OST_Windows);

BuiltInParameter bip = (BuiltInParameter)(someId.IntegerValue);

doc.Regenerate();

// Add fields to the schedule
AddRegularFieldToSchedule(vs, new ElementId(BuiltInParameter.CASEWORK_WIDTH));
AddRegularFieldToSchedule(vs, new ElementId(BuiltInParameter.CASEWORK_HEIGHT));
AddRegularFieldToSchedule(vs, new ElementId(bip));

t.Commit();
}
}

public static void AddRegularFieldToSchedule(ViewSchedule schedule, ElementId paramId)
{
ScheduleDefinition definition = schedule.Definition;

// Find a matching SchedulableField
SchedulableField schedulableField =
definition.GetSchedulableFields().FirstOrDefault(sf => sf.ParameterId == paramId);

if (schedulableField != null)
{
// Add the found field
definition.AddField(schedulableField);
}
}

}
}

0 Likes
Accepted solutions (1)
334 Views
1 Reply
Reply (1)
Message 2 of 2

architect.bim
Collaborator
Collaborator
Accepted solution

Hi!

Similar to this video, you need to create a FilteredElementCollector for the required category, select all its instances and for each element get its Id and assign a value to some of its instance parameters (for example, to "Comments" parameter). Then you can add this parameter as a schedule field and retrieve the ElementId through the schedule. Here is an example in python, I'm sure you can adapt it to C#:

 

elements = FilteredElementCollector(doc) \
    .OfCategory(DB.BuiltInCategory.OST_Windows) \
    .WhereElementIsNotElementType()

with DB.Transaction(doc, 'Set "Comments" Parameter Value for windows') as t:
    t.Start()
    for element in elements:
        element.Parameter[DB.BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS].Set(str(element.Id))
    t.Commit()

 


Maxim Stepannikov | Architect, BIM Manager, Instructor