Revit二次开发入门:第七章元素的创建与修改
本章内容
1.创建元素
-
创建模型元素
- 使用Document.Create或对应的模型元素的静态函数创建
- 使用Element子类的静态方法创建:DB.Document.Create返回Creation.Document
-
新的Create API趋势
- Creation.Document下面不方便查找
- 创建方法在相关的类里面
-
创建族
-
创建族类型
- 在创建族的时候创建类型
- 使用ElementType的Duplicate(复制)方法
2.项目和族文件的创建
族实例创建
- 基于族的实例创建
- 根据族的模板及参数决定
- 族实例最佳经验
接口 | 说明 | 例子 |
---|---|---|
NewFamilyInstance(Face face,Line position,FamilySymbol symbol) | 创建基于线和面的实例 | 基于线和免得族(线性加固等) |
NewFamilyInstance(Face face,XYZ location,XYZ referenceDirection,FamilySymbol symbol) | 指定位置和方向来创建基于面的实例 | 基于面的族 |
NewFamilyInstance(XYZ location,FamilySymbol symbol,StructuralType structuralType) | 指定位置来创建实例 | 家具、植物、汽车 |
NewFamilyInstance(XYZ location,FamilySymbol symbol,Element host,StructuralType structuralType) | 指定位置来创建宿主实例 | m门、窗、家具、结构基础、柱、梁(在修改梁的LocationCurve.Curve的endpoints之前,这个梁是没有长度的)、支柱(同梁) |
NewFamilyInstance(XYZ location,FamilySymbol symbol,XYZ referenceDirection,Element host,StructuralType structuralType) | 指定位置与方向来创建宿主实例 | 家具、植物、汽车 |
NewFamilyInstance(XYZ location,FamilySymbol symbol,Level level,StructuralType structuralType) | 指定位置来创建基于标高的实例 | l梁、柱(在修改梁的LocationCurve.Curve的endpoints之前,这个梁是没有长度的)、支柱(同梁) |
NewFamilyInstance(XYZ location,FamilySymbol symbol,Element host,Level level,StructuralType structuralType) | 指定位置来创建基于标高与宿主的实例 | 门、窗 |
NewFamilyInstance(Curve curve,FamilySymbol symbol,Level level,StructuralType structuralType) | 指定曲线来创建基于标高的实例 | l梁、支柱 |
-
在族中创建
- Document.IsFamilyDocument判断先
- Document.FamilyCreate进行创建
- Dimension,Form等
3.元素的导入导出
- 数据导入:Import
方法 | 描述 |
---|---|
Import(string,GBXMLIportOptions) | 将一个绿色建筑导入到文件中 |
Import(string,SATImportOptions,View) | 将一个SAT文件导入到文件中 |
Import(String,SKPimportOptions,View) | 将一个SKP文件导入到文件中 |
Import(string,DGNImportOptions,View) | 将一个DGN文件导入到文件中 |
Import(string,DWGImportOptions,View,Element) | 将一个DWG或者DXF文件导入到文件中 |
Import(string,ImageImportOptions,View,Element) | 将一个图片文件或位图导入到文件中 |
- 数据导出:Export
Name | Description | |
---|---|---|
![]() |
Export(String, String, MassGBXMLExportOptions) | Exports a gbXML file from a mass model document. |
![]() |
Export(String, String, GBXMLExportOptions) | Export the model in gbXML (green-building) format. |
![]() |
Export(String, String, IFCExportOptions) | Exports the document to the Industry Standard Classes (IFC) format. |
![]() |
Export(String, String, NavisworksExportOptions) | Exports a Revit project to the Navisworks .nwc format. |
![]() |
Export(String, String, ViewSet, DWFExportOptions) | Exports the current view or a selection of views in DWF format. |
![]() |
Export(String, String, ViewSet, DWFXExportOptions) | Exports the current view or a selection of views in DWFX format. |
![]() |
Export(String, String, ViewSet, FBXExportOptions) | Exports the document in 3D-Studio Max (FBX) format. |
![]() |
Export(String, String, ICollection<(Of «‘(ElementId>)»), DGNExportOptions) | Exports a selection of views in DGN format. |
![]() |
Export(String, String, ICollection<(Of «‘(ElementId>)»), DWGExportOptions) | Exports a selection of views in DWG format. |
![]() |
Export(String, String, ICollection<(Of «‘(ElementId>)»), DXFExportOptions) | Exports a selection of views in DXF format. |
![]() |
Export(String, String, ICollection<(Of «‘(ElementId>)»), SATExportOptions) | Exports the current view or a selection of views in SAT format. |
4.元素的移动、复制、镜像
ElementTransformUtils:
-
移动:
API提供了移动元素的方法,可以把一个或者几个元素从一个地方移动到另一个地方。从精确程度来说,和UI的移动命令是一样的。
方法 描述 MoveElement(Document,ElementId,XYZ) 使用给定的平移变换移动一个元素 MoveElement(Document,Icollection<ElementId>,XYZ) 使用给定的平移变化移动元素集 -
镜像
-
旋转
-
拷贝
Revit API提供复制元素的方法
5.实例练习
实例练习1:
元素的基本创建:
- 创建一个结构墙
元素的位置变动:
- 实现墙体的复制、移动、镜像
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace CreateElement {
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class SolidTest : IExternalCommand {
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Transaction t1 = new Transaction(doc, "T1");
t1.Start();
Wall wall = Wall.Create(doc, Line.CreateBound(new XYZ(), new XYZ(0, 10, 0)), Level.Create(doc, 0).Id,
false);//注意这里需要返回Id,而不是LevelId
t1.Commit();
TaskDialog.Show("T1", wall.Id.ToString());
Transaction t2 = new Transaction(doc, "copy");
t2.Start();
ElementTransformUtils.CopyElement(doc, wall.Id, new XYZ(10, 0, 0));
t2.Commit();
TaskDialog.Show("T2", "Copy Successed!");
Transaction t3 = new Transaction(doc, "Move");
t3.Start();
ElementTransformUtils.MoveElement(doc, wall.Id, new XYZ(10, 20, 0));
t3.Commit();
TaskDialog.Show("T3", "移动完成");
Transaction t4 = new Transaction(doc, "Mirror");
t4.Start();
if (ElementTransformUtils.CanMirrorElement(doc, wall.Id)) {
Plane pl = Plane.CreateByNormalAndOrigin(new XYZ(0, -1, 0), XYZ.Zero);
ElementTransformUtils.MirrorElement(doc, wall.Id, pl);
}
t4.Commit();
TaskDialog.Show("T4", "Mirror !");
return Result.Succeeded;
}
}
}
###
实例练习2:
- 尝试不同类型元素的创建方法:
- 创建一个拉伸实体(正方体)
- 创建一个自定义族类型
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Create2 {
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class CreateBox : IExternalCommand {
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Transaction t1 = new Transaction(doc, "Box");
t1.Start();
Curve c1 = Line.CreateBound(new XYZ(), new XYZ(0, 10, 0));
Curve c2 = Line.CreateBound(new XYZ(0, 10, 0), new XYZ(10, 10, 0));
Curve c3 = Line.CreateBound(new XYZ(10, 10, 0), new XYZ(10, 0, 0));
Curve c4 = Line.CreateBound(new XYZ(10, 0, 0), new XYZ(0, 0, 0));
CurveArray curveArray = new CurveArray();
curveArray.Append(c1);
curveArray.Append(c2);
curveArray.Append(c3);
curveArray.Append(c4);
CurveArrArray curveArr = new CurveArrArray();
curveArr.Append(curveArray);
doc.FamilyCreate.NewExtrusion(true, curveArr,
SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), XYZ.Zero)), 10);
doc.FamilyManager.NewType("UCD");
t1.Commit();
return Result.Succeeded;
}
}
}