在Revit中,如何编程创建新类型(如窗户或墙)

2025年03月13日 00:53
有2个网友回答
网友(1):

在Revit中创建对象用Autodesk.Revit.Creation.Document 和 Autodesk.Revit.Creation.Application类中的New***()方法来创建对象。比如创建墙用Autodesk.Revit.Creation.Document.NewWall() 方法。可是我们确无法从Creation命名空间中的Document类中找到方法来创建墙类型。那能用编程方式创建新类型吗? Revit是没有提供方法来直接创建一个类型。因为类型的属性和参数很多,是吧? 如果有一个方法来创建,那参数列表得十分长才能清楚定义出这个类型。所以Revit API不直接创建一个新类型,而是从一个已有类型中复制一个类型,所有的属性和参数都从原类型中获得,然后你根据需要修改一些属性、参数的值,实现想要的类型。 Revit所有的类型类都从ElementType类派生。ElementType类提供了Duplicate() 方法来复制类型。调用此函数,在当前模型文件中添加一个指定名称的类型,就会创建指定名称的类型。从ElementType派生的类都实现了Duplicate() 方法,所以所有的类型都可以用Duplicate()来创建,然后修改类型的属性、参数即可。 所以可以从WallType类型的Duplicate()方法创建一个墙类型,从FamilySymbol.Duplicate() 方法创建一个窗户类型。 请看下面代码示例创建一个标注样式类型: public class RevitCommand : IExternalCommand{public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements){UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; //get an exising dimension type. FilteredElementCollector collector = new FilteredElementCollector(doc); collector.OfClass (typeof(DimensionType)); DimensionType dimType = null; foreach(Element elem in collector){ if(elem.Name == "Linear Dimension Style"){dimType = elem as DimensionType ;break;}}DimensionType newType = dimType.Duplicate("NewType"); if(newType != null){Transaction trans = new Transaction(doc, "ExComm"); trans.Start(); newType.get_Parameter(BuiltInParameter.LINE_PEN).Set(2);//you can change more here.

网友(2):

可以从WallType类型的Duplicate()方法创建一个墙类型,从FamilySymbol.Duplicate() 方法创建一个窗户类型。 请看下面代码示例创建一个标注样式类型:
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;

//get an exising dimension type.
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass (typeof(DimensionType));
DimensionType dimType = null;
foreach(Element elem in collector)
{

if(elem.Name == "Linear Dimension Style")
{
dimType = elem as DimensionType ;
break;
}
}
DimensionType newType = dimType.Duplicate("NewType");
if(newType != null)
{
Transaction trans = new Transaction(doc, "ExComm");
trans.Start();
newType.get_Parameter(BuiltInParameter.LINE_PEN).Set(2);
//you can change more here.
doc.Regenerate();
trans.Commit();
}
return Result.Succeeded ;
}
}