Programatically uncheck "V/G Overrides Model" parameter in a view template

Anonymous

Programatically uncheck "V/G Overrides Model" parameter in a view template

Anonymous
Not applicable

Hello, everyone.

 

I'm writing an addin that would allow users to hide model categories in a given view when a view template's included "V/G Overrides Model" parameter would otherwise restrict them from doing so.

 

I wrote this snippet of code to disable the template's V/G Model Overrides on the current view. (If I can get this to work, I'll apply it to all views in doc) 

 

 

Transaction tr = new Transaction(doc, "Template Override");
tr.Start()
View current = doc.ActiveView;       
current.get_Parameter(BuiltInParameter.VIS_GRAPHICS_MODEL).Set(0);
tr.Commit();

Running this gives me the "Object reference not set to an instance of an object" exception. If I'm not accessing this built-in parameter correctly, why does this method typically work for other built-in parameters?

 

I'm still somewhat new to this API, so any help would be much appreciated. 

 

Thank you.

 

EDIT: I now know that the current view, itself, is not a template, but it has a template. So, how do I go about accessing and modifying its template?

0 Likes
Reply
Accepted solutions (1)
2,821 Views
4 Replies
Replies (4)

RPTHOMAS108
Mentor
Mentor

The current view may not support model elements and therefore the parameter would not exist for that view type. You should therefore check for null separately rather that getting a parameter from an element and trying to set it on a single line. You can also inspect the View.ViewType property to see if the view likely contains the target parameter.

 

You can get the template of a view from the View.ViewTemplateID property of the view using Document.GetElement(View.ViewTemplateID). ViewTemplates share the same class as the view types they are applicable to (the View.IsTemplate property identifies if it is a template).

 

When you are collecting views from filtered element collectors etc. it is important to exclude view template objects from such a collection (unless you intended to include them).

0 Likes

Anonymous
Not applicable

Thanks for the advice, RPTHOMAS108.

 

I wrote the following code to check for null templates and parameters:

 

View cur = doc.ActiveView;
                if (!cur.IsTemplate)
                {
                    ElementId elid = cur.ViewTemplateId;
                    if (doc.GetElement(elid) != null)
                    {
                        TaskDialog.Show("Non-null template", cur.Name);
                        ParameterSet ps = doc.GetElement(elid).Parameters;
                        Parameter vgm = null;
                        foreach (Parameter p in ps)
                        {
                            if (p.Id == doc.GetElement(elid).get_Parameter(BuiltInParameter.VIS_GRAPHICS_MODEL).Id)
                            {
                                vgm = p;
                                break;
                            }
                        }
                        if (vgm != null)
                        {
                            TaskDialog.Show(vgm.ToString() + " - Non-null parameter", cur.Name);
                            vgm.Set(0);
                        }
                        else if (doc.GetElement(elid).get_Parameter(BuiltInParameter.VIS_GRAPHICS_MODEL) == null) TaskDialog.Show("V/G Overrides Model - Null parameter", cur.Name);
                    }
                    else if (doc.GetElement(elid) == null) TaskDialog.Show("Null template", cur.Name);
                }

I've found that this code

doc.GetElement(elid).get_Parameter(BuiltInParameter.VIS_GRAPHICS_MODEL).Id

causes the exception, so I used BIP Checker to look at the VIS_GRAPHICS_MODEL parameter in the current view's template. 

graphics BIP.PNG

I found that slightly more than half of these parameters in the "Graphics" parameter group are of type "None" and, thus, don't have a string value associated with them. I realized that this is due to the fact that these template parameters hold the visibility graphics settings for categories rather than a simple string value. All I want to do, however, is set the "Include" toggle for the parameter, rather than the non-existent string value:

viewTemplate.PNG

From this post, it seems that the API does not yet allow this. But, again, I'm still in the process of learning the API, so if anyone knows of a workaround for this, I would greatly appreciate it. 

 

Thank you.

 

FAIR59
Advisor
Advisor
Accepted solution

a View template has a List of Parameter Id's that are non-controlled by the template.

You have to add or remove the parameter Id for BuiltInParameter.VIS_GRAPHICS_MODEL from that List.

 

			View actView = doc.ActiveView;
			View template = null;
			if (actView.ViewTemplateId!= ElementId.InvalidElementId) template =doc.GetElement( actView.ViewTemplateId) as View;
			if (template==null) return;
			ElementId VGModel_ID = ElementId.InvalidElementId;
			List<ElementId> nonControledParams = template.GetNonControlledTemplateParameterIds().ToList();
			foreach ( var id in nonControledParams)
			{
				if (id.IntegerValue == (int) BuiltInParameter.VIS_GRAPHICS_MODEL)
				{
					VGModel_ID = id;
					break;
				}
			}
			using (Transaction t = new Transaction(doc,"change template"))
			{
					t.Start();
					// include VGModel
					if (VGModel_ID != ElementId.InvalidElementId)
					    {
						    nonControledParams.Remove(VGModel_ID);
							template.SetNonControlledTemplateParameterIds(nonControledParams);
					    }
					// exclude VGModel
					if (VGModel_ID == ElementId.InvalidElementId)
					    {
							nonControledParams.Add( new ElementId( BuiltInParameter.VIS_GRAPHICS_MODEL));
							template.SetNonControlledTemplateParameterIds(nonControledParams);
					    }
					//
					t.Commit();
			}

 

 

Anonymous
Not applicable

Thank you very much, FAIR59. The SetNonControlledTemplateParameterIds() method is exactly what I needed. 

 

I was able to apply this to all view templates in the current document with the following code:

 

UIApplication application = commandData.Application;
Document doc = application.ActiveUIDocument.Document;
            
FilteredElementCollector views = new FilteredElementCollector(doc).OfClass(typeof(View));
            
View template = null;
View cur = null;
try {
foreach(var current in views.Reverse()) { cur = current as View; if (!cur.IsTemplate) { if (cur.ViewTemplateId != ElementId.InvalidElementId) template = doc.GetElement(cur.ViewTemplateId) as View; else continue; ElementId VGModel_ID = ElementId.InvalidElementId; List<ElementId> nonControledParams = template.GetNonControlledTemplateParameterIds().ToList(); foreach (var id in nonControledParams) { if (id.IntegerValue == (int)BuiltInParameter.VIS_GRAPHICS_MODEL) { VGModel_ID = id; break; } } using (Transaction t = new Transaction(doc, "Change Template")) { t.Start(); if (VGModel_ID == ElementId.InvalidElementId) { nonControledParams.Add(new ElementId(BuiltInParameter.VIS_GRAPHICS_MODEL)); template.SetNonControlledTemplateParameterIds(nonControledParams); } t.Commit(); } } } } catch(Exception e) { TaskDialog.Show("Exception", e.Message); }

 

0 Likes