We talked about setting Parameter values before. Now let’s see how Parameter Writer of RevitAddinWizard can assist us to create the help methods. The Parameter Writer can be found from either the Revit Addin Coder sub-menu under the Tools or from Revit Addin Coder toolbar.
When the menu item or button is clicked, the Parameter Writer window will show up:
If we’d like to add the Parameter Writer as both a help method to a class and an extension method to the ExtensionCenter, we can check off both options and choose the class. After the OK button is clicked, the following Parameter Writer help method will be created in the chosen class:
public static void RawSetParameterValue(Parameter p, object value)
{
try
{
if (value.GetType().Equals(typeof(string)))
{
if (p.SetValueString(value as string))
return;
}
switch (p.StorageType)
{
case StorageType.None:
break;
case StorageType.Double:
if (value.GetType().Equals(typeof(string)))
{
p.Set(double.Parse(value as string));
}
else
{
p.Set(Convert.ToDouble(value));
}
break;
case StorageType.Integer:
if (value.GetType().Equals(typeof(string)))
{
p.Set(int.Parse(value as string));
}
else
{
p.Set(Convert.ToInt32(value));
}
break;
case StorageType.ElementId:
if (value.GetType().Equals(typeof(ElementId)))
{
p.Set(value as ElementId);
}
else if (value.GetType().Equals(typeof(string)))
{
p.Set(new ElementId(int.Parse(value as string)));
}
else
{
p.Set(new ElementId(Convert.ToInt32(value)));
}
break;
case StorageType.String:
p.Set(value.ToString());
break;
}
}
catch
{
throw new Exception("Invalid Value Input!");
}
}
And the following extension class and method will be created as well:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Analysis;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCS
{
public static class ExtensionCenter
{
public static void SetValue(this Parameter p, object value)
{
try
{
if (value.GetType().Equals(typeof(string)))
{
if (p.SetValueString(value as string))
return;
}
switch (p.StorageType)
{
case StorageType.None:
break;
case StorageType.Double:
if (value.GetType().Equals(typeof(string)))
{
p.Set(double.Parse(value as string));
}
else
{
p.Set(Convert.ToDouble(value));
}
break;
case StorageType.Integer:
if (value.GetType().Equals(typeof(string)))
{
p.Set(int.Parse(value as string));
}
else
{
p.Set(Convert.ToInt32(value));
}
break;
case StorageType.ElementId:
if (value.GetType().Equals(typeof(ElementId)))
{
p.Set(value as ElementId);
}
else if (value.GetType().Equals(typeof(string)))
{
p.Set(new ElementId(int.Parse(value as string)));
}
else
{
p.Set(new ElementId(Convert.ToInt32(value)));
}
break;
case StorageType.String:
p.Set(value.ToString());
break;
}
}
catch
{
throw new Exception("Invalid Value Input!");
}
}
}
}
In terms of how to use these methods, please refer to previous posts for ideas and code examples.
Parameter Writer of RevitAddinWizard can do all these in a second.
Links to some related articles:
Parameter of Revit API - BuiltInParameter
Parameter Retriever of RevitAddCoder
Parameter of Revit API - Parameter Information
Parameter Infoer of RevitAddCoder
Parameter of Revit API - ParameterType And StorageType
Parameter Typer of RevitAddCoder
Parameter of Revit API - BuiltInParameterGroup
Parameter Grouper of RevitAddCoder
Parameter Filter of RevitAddCoder
Recent Comments