Parameter Converter – SharedParameter to FamilyParameter
We talked about programmatically converting parameters from one to another manually before. Now let’s see how Parameter Converter of RevitAddinWizard can assist us to hit the target with just a few mouse clicks. The Parameter Converter can be found from either Revit Addin Coder sub-menu under the Tools or from Revit Addin Coder toolbar.When the menu item or the tool button is clicked, the Parameter Converter window will show up:
http://spiderinnet.typepad.com/.a/6a013489b64e31970c01538ef454f0970b-800wi
If the SharedParameter (to FamilyParameter) Converter is selected from the check list box and a class is chosen from the combo box, a SharedParameter to FamilyParameter Converter will be created and added into the class automatically. The entire converter code and necessary namespaces and class declarations have been appended below:#region Namespacesusing System;
using System.Text;
using System.Linq;
using System.Xml;
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.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;#endregion
namespace RevitAddinCS1
{
class ParameterConverter
{
public static void RawAddFamilyParamterAndSetValue(Dimension dim, FamilyManager famMan, FamilyType ft,
string name, BuiltInParameterGroup group, ParameterType type, bool isInstance, object value)
{
FamilyParameter fp = famMan.AddParameter(name, group, type, isInstance);
if( value!= null ) RawSetFamilyParameterValue(famMan, ft, fp, value);
//if (value != null) fp.SetValue(famMan, ft, value);
if( dim != null ) dim.Label = fp;
}
public static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}
public static FamilyParameter RawFindFamilyParameter(FamilyManager fm, string parameterName)
{
FamilyParameter fp = RawConvertSetToList<FamilyParameter>(fm.Parameters).
FirstOrDefault(e => e.Definition.Name.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase));
if (fp == null) throw new Exception("Invalid ParameterName Input!");
return fp;
}
public static FamilyType RawFindFamilyType(FamilyManager fm, string familyTypeName)
{
FamilyType famType = RawConvertSetToList<FamilyType>(fm.Types).
FirstOrDefault(e => e.Name.Equals(familyTypeName, StringComparison.CurrentCultureIgnoreCase));
if (famType == null) throw new Exception("Invalid FamilyTypeName Input!");
return famType;
}
public static void RawSetFamilyParameterValue(FamilyManager fm, string familyTypeName, string parameterName, object value)
{
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm,familyTypeName), RawFindFamilyParameter(fm,parameterName) , value);
}
public static void RawSetFamilyParameterValue(FamilyManager fm, FamilyType ft, string parameterName, object value)
{
RawSetFamilyParameterValue(fm, ft, RawFindFamilyParameter(fm, parameterName), value);
}
public static void RawSetFamilyParameterValue(FamilyManager fm, string familyTypeName, FamilyParameter fp, object value)
{
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm, familyTypeName), fp, value);
}
public static void RawSetFamilyParameterValue(FamilyManager fm, FamilyType ft, FamilyParameter fp, object value)
{
FamilyType curFamType = fm.CurrentType;
fm.CurrentType = ft;
try
{
switch (fp.StorageType)
{
case StorageType.None:
break;
case StorageType.Double:
if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, double.Parse(value as string));
}
else
{
fm.Set(fp, Convert.ToDouble(value));
}
break;
case StorageType.Integer:
if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, int.Parse(value as string));
}
else
{
fm.Set(fp, Convert.ToInt32(value));
}
break;
case StorageType.ElementId:
if (value.GetType().Equals(typeof(ElementId)))
{
fm.Set(fp, value as ElementId);
}
else if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, new ElementId(int.Parse(value as string)));
}
else
{
fm.Set(fp, new ElementId(Convert.ToInt32(value)));
}
break;
case StorageType.String:
fm.Set(fp, value.ToString());
break;
}
}
catch
{
throw new Exception("Invalid Value Input!");
}
finally
{
fm.CurrentType = curFamType;
}
}
public static ExternalDefinition RawFindExternalDefinition(DefinitionFile defFile, string name)
{
var v = (from DefinitionGroup dg in defFile.Groups
from ExternalDefinition d in dg.Definitions
where d.Name == name
select d);
if (v == null || v.Count() < 1)
return null;
else
return v.First();
}
public static void ConvertSharedParameterToFamilyParameter(ExternalDefinition extDef, FamilyManager famMan, bool instance, string type, object value )
{
FamilyParameter fp = famMan.AddParameter(extDef, extDef.ParameterGroup, instance);
if (value != null) RawSetFamilyParameterValue(famMan, type, fp, value);
}
}
}
We would not create test command to extercise these parameter converter code here. In case needed, please refer to earlier posts for ideas and code examples.
As mentioned, Parameter Converter of RevitAddinCodercan create all these code automatically in a second.
顶......
楼下跟上.....
谢谢老师…
看看啥…………
谢谢BIM大神…
页:
[1]