Revit二次开发入门:第七章元素的创建与修改

本章内容

1.创建元素

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梁、支柱

3.元素的导入导出

方法 描述
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) 将一个图片文件或位图导入到文件中
  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:

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;
        }
    }
}