Remove Parameters from view template

Remove Parameters from view template

will.wydock
Advocate Advocate
398 Views
4 Replies
Message 1 of 5

Remove Parameters from view template

will.wydock
Advocate
Advocate

Hello,

 

I am working on a tool that requires that certain text parameters removed from a view template. I have written a macro to test how to do this but I am getting a null error that i think is related to the parameters in the view template. Can anyone tell me if i am heading in the right direction for this?

 

public void getvpid()
		{
			Document doc = this.ActiveUIDocument.Document;
			var view = doc.ActiveView;
			var elementid = view.ViewTemplateId;
			
			View template = doc.GetElement(elementid) as View;
			var parameterids = template.GetTemplateParameterIds();
			IList<ElementId> parameterlist = myList(parameterids);
			using(var transaction = new Transaction(doc,"test"))
			{
				transaction.Start();
				var noncontrolledparameterid = template.GetNonControlledTemplateParameterIds();
				foreach(var p in noncontrolledparameterid)
				{
					parameterlist.Add(p);
				}
				template.SetNonControlledTemplateParameterIds(parameterlist);
				transaction.Commit();
			}
				
		}
		public IList<ElementId> myList(IList<ElementId> parameterids)
		{
			IList<ElementId> parameterlist = new List<ElementId>();
			foreach(ElementId paramid in parameterids)
			{
				var parameter = doc.GetElement(paramid);
				if(parameter.Name=="SCALE1"||parameter.Name=="SCALE2"||parameter.Name=="SCALE3"||parameter.Name=="SCALE4")
					parameterlist.Add(paramid);
			}
				return parameterlist;
		
		}

 Thanks in Advanced

0 Likes
399 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

One: As far as I can tell from your code, you are not removing any parameters at all, but adding four named SCALE1, SCALE2, SCALE3 and SCALE4. Can you clarify that?

  

Two: Can you run the code in the debugger to look and see exactly which statement is producing the null error?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

will.wydock
Advocate
Advocate

1. From my understanding is from the view template i need to GetNonControlledTemplateParameterIds as a list. Add the additional parameters that I don't want to be controlled by the view template and then use that list of ids to SetNonControlledTemplateParameterIds.

 

2. It appears to be at the foreach loop at line 27.

0 Likes
Message 4 of 5

will.wydock
Advocate
Advocate

If I commented line 28 and 29 the routine would remove all parameters from the view template. What I believe is this issue is my attempt to get the parameter from the Id and then retrieve the name. I have not been able to find a way to convert an elementid to a parameter instead of an element. Any ideas?

0 Likes
Message 5 of 5

RPTHOMAS108
Mentor
Mentor

Negative parameter ids including those that control a view template are built-in, so the element id integer values relate to ids from the BuiltInParameter enum. So you don't need to get the parameter object for that.

 

Once you convert the negative id to a BuiltInParameter member you can use: LabelUtils.GetLabelFor(BuiltInParameter)

 

Positive parameter ids including those that control a view template refer to ParameterElement (or SharedParameterElement) objects. So in that case you can obtain the name for those parameters simply by using Document.GetElement and referring to the name of the returned object (which will match the name of the parameter it is for).

 

The null is as you suspect related to the fact you can't get an object of Parameter from Document.GetElement because Parameter isn't derived from Element.