Template - Check if Template controls some field

Template - Check if Template controls some field

Julian.Wandzilak
Advocate Advocate
365 Views
1 Reply
Message 1 of 2

Template - Check if Template controls some field

Julian.Wandzilak
Advocate
Advocate

Hello,

I am trying to check if view template controls formatting of the columns in the selected schedule. And I have a problem, I am sure one of you will be able to help;)

 

ViewSchedule scheduleToBeChanged = doc.GetElement(scheduleSheetInstance.ScheduleId) as ViewSchedule;

//TODO : older versions 
if (scheduleToBeChanged.ViewTemplateId.Value != -1)
{
    //Autodesk.Revit.DB.Element template = doc.GetElement(scheduleToBeChanged.ViewTemplateId);

    IEnumerable<Autodesk.Revit.DB.View> views = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).Cast<Autodesk.Revit.DB.View>().Where(v => v.Id.Equals(scheduleToBeChanged.ViewTemplateId));

    Autodesk.Revit.DB.View template = views.FirstOrDefault();

    try
    {
        Autodesk.Revit.DB.Parameter Formatting = (template as ViewSchedule).get_Parameter(BuiltInParameter.SCHEDULE_FORMAT_PARAM);

 

 My idea was to save a formatting parameter, Get its ID and check against :

 

ICollection<ElementId> paramsId = (template as Autodesk.Revit.DB.View).GetTemplateParameterIds();
ICollection<ElementId> paramsId2 = (template as Autodesk.Revit.DB.View).GetNonControlledTemplateParameterIds();

if (paramsId.Contains(Formatting.Id))
{
    TaskDialog.Show("TEST", "Param in GetTemplateParameterIds");
}

if (paramsId2.Contains(Formatting.Id))
{
    TaskDialog.Show("TEST", "Param in GetNonControlledTemplateParameterIds");
}

 

 

I don't know why, but when I try to get the parameter, I end up with  "Object reference not set to an instance of an object." Any idea what I do wrong / or what should I study 😉 

blog: w7k.pl more about me: Linkedin Profile
My add-ins for Revit: Drafter(180+ scripts) & Leveler
0 Likes
366 Views
1 Reply
Reply (1)
Message 2 of 2

longt61
Advocate
Advocate

It is quite obvious that the schedule view does not have a template. Therefore, you can not get the controlled parameter Ids from it. You should always check if the ViewTemplateId is different from null and ElementId.Invalid to see whether the selected schedule view has a template or not.

 

P/s: In line 6 from the first snippet of code, you can cast it to Vew instead of trying to compare the view Id to all the existing view in current document.

0 Likes