using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Creat3DView
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
UIApplication uiapp = uidoc.Application;
Selection sel = uidoc.Selection;
View view = commandData.View;
List<Reference> referlist = sel.PickObjects(ObjectType.Element, "選擇構件").ToList();
List<ElementId> ids = new List<ElementId>();
//聲明一個視圖集合
ViewSet views = new ViewSet();//特點是集合内容在一個視圖檔案内
//聲明一個視圖list集合
List<View3D> view3Ds = new List<View3D>();
//建立事件并啟動事件
Transaction ts = new Transaction(doc, "建立圍框");
ts.Start();
foreach (var reference in referlist)
{
Element ele = doc.GetElement(reference);
BoundingBoxXYZ boundingBox = ele.get_BoundingBox(view);
Transform transform = Transform.Identity;
BoundingBoxXYZ box = new BoundingBoxXYZ();
box.Transform = transform;
box.Min = boundingBox.Min;
box.Max = boundingBox.Max;
View3D view3D = View3D.CreateIsometric
(doc, new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>().Where(m => m.ViewFamily == ViewFamily.ThreeDimensional)
.First().Id);
//設定三維剖面框
view3D.SetSectionBox(box);
views.Insert(view3D);
view3Ds.Add(view3D);
}
ts.Commit();
FBXExportOptions options = new FBXExportOptions();
string fileFolder = @"D:\作品準備\FBX導出";
string fileName = "水廠項目三維構件綜合";
doc.Export(fileFolder, fileName, views, options);
for (int i = 0; i < view3Ds.Count; i++)
{
ViewSet viewSet = new ViewSet();
viewSet.Insert(view3Ds[i]);
string filename = "水廠項目三維構件" + i;
FBXExportOptions pts = new FBXExportOptions();
doc.Export(fileFolder, filename, viewSet, pts);
}
return Result.Succeeded;
}
}
}
導出檔案格式參考文檔
關于導出各檔案格式API使用執行個體