这是一个典型的增加新类型(Duplicate()来实现),并且修改类型的参数值。这里一个特殊的情况是这里的两个参数值都是Revit的接头类型对象。实际上保存在这两个参数中的是ElementId类型的值,指向实际的两种接头类型。
在参数中如何修改参数存储类型是对象Id的值呢? 请看下面的代码。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Autodesk.Revit .DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit .ApplicationServices;
using Autodesk.Revit.Attributes ;
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[RegenerationAttribute(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document rvtDoc = app.ActiveUIDocument.Document;
PipeType pipeType = null;
List<Autodesk.Revit.DB.Element> ptypes = new List<Autodesk.Revit.DB.Element>();
FilteredElementCollector collector = new FilteredElementCollector(rvtDoc);
collector.OfCategory(BuiltInCategory.OST_PipeCurves);
collector.OfClass(typeof(ElementType));
ptypes.AddRange(collector.ToElements());
foreach (Element ptype in ptypes)
{
if (string.Compare(ptype.Name, "standard", true) == 0)
{
pipeType = ptype as PipeType;
break;
}
}
if (pipeType != null)
{
string name = "ADNNewType";
Transaction trans = new Transaction(rvtDoc, "ExComm");
trans.Start();
Autodesk.Revit.DB.ElementType newType = pipeType.Duplicate(name);
trans.Commit();
if (newType != null)
{
Transaction t = new Transaction(rvtDoc, "ExComm1");
t.Start();
ElementId id1 = FindFamilyType(rvtDoc, typeof(FamilySymbol), "Elbow - Threaded - MI - Class 150", "Standard", BuiltInCategory.OST_PipeFitting).Id;
ElementId id2 = FindFamilyType(rvtDoc, typeof(FamilySymbol), "Tee - Threaded - MI - Class 150", "Standard", BuiltInCategory.OST_PipeFitting).Id;
//change Elbow parameter value
newType.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOW_PARAM).Set(id1);
//change Tee parameter value
newType.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM).Set(id2);
//change "Preferred unction Type" parameter value
newType.get_Parameter(BuiltInParameter.RBS_CURVETYPE_PREFERRED_BRANCH_PARAM).Set(0);
t.Commit();
}
}
return Result.Succeeded;
}
//====================================================================
// Helper Functions
//====================================================================
/// <summary>
/// Helper function: find an element of the given type, name, and category(optional)
/// You can use this, for example, to find a specific wall and window family with the given name.
/// e.g.,
/// FindFamilyType(_doc, GetType(WallType), "Basic Wall", "Generic - 200mm")
/// FindFamilyType(_doc, GetType(FamilySymbol), "M_Single-Flush", "0915 x 2134mm", BuiltInCategory.OST_Doors)
/// </summary>
public static Element FindFamilyType(Document rvtDoc, Type targetType,
string targetFamilyName, string targetTypeName, Nullable<BuiltInCategory> targetCategory)
{
// First, narrow down to the elements of the given type and category
var collector = new FilteredElementCollector(rvtDoc).OfClass(targetType);
if (targetCategory.HasValue)
{
collector.OfCategory(targetCategory.Value);
}
// Parse the collection for the given names
// Using LINQ query here.
var targetElems =
from element in collector
where element.Name.Equals(targetTypeName) &&
element.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM).
AsString().Equals(targetFamilyName)
select element;
// Put the result as a list of element fo accessibility.
IList<Element> elems = targetElems.ToList();
// Return the result.
if (elems.Count > 0)
{
return elems[0];
}
return null;
}
}
这篇文章与本博中的这个文章有些类似。不过这里修改的是参数值类型不一样,而且是针对管道类型。
在Revit中,如何编程创建新类型(如窗户或墙) http://blog.csdn.net/joexiongjin/article/details/6191299