using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using SEPD.BuidingElectricity.CommonHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SEPD.BuidingElectricity.SchemaForRoom.ExternalCommands
{
class LinkDocInfo
{
public Document docT { get; set; }//建筑链接文件
public Document doc { get; set; }//当前工程文件
public Document docS { get; set; }//结构链接文件
public Document docE { get; set; }//电气链接文件
public RevitLinkInstance revitLinkInstanceT { get; set; }
public List<Element> allRooms { get; set; }
public List<Element> allDoors { get; set; }
public List<Element> allStrcturalFrames { get; set; }
public List<Element> allDeviceElements { get; set; }
public List<FamilySymbol> allLights { get; set; }
public List<Element> allLineStyles { get; set; }
public List<string> selectedLineStyles { get; set; }
public List<Element> LinkedLevels { get; set; }
public List<Wall> AllWalls { get; set; }
public List<Wall> allWall { get; set; }
public List<Element> allFloors { get; set; }
public List<Grid> Grids { get; set; }
public LinkDocInfo (){
}
//====================================================
/// <summary>
/// 获取建筑链接文件
/// </summary>
/// <param name="commandData"></param>
/// <returns></returns>
public Document GetLinkDocT(ExternalCommandData commandData)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
#region 获取建筑链接文件 Document instanceDoc instanceDocumentT
Document instanceDocumentT = null;
FilteredElementCollector InstanceCollectorT = new FilteredElementCollector(doc);
ElementClassFilter InstanceFilterT = new ElementClassFilter(typeof(RevitLinkInstance));
InstanceCollectorT = InstanceCollectorT.WherePasses(InstanceFilterT);
foreach (RevitLinkInstance Instance in InstanceCollectorT)
{
if (Instance.GetLinkDocument().Title.Contains("T") || Instance.GetLinkDocument().Title.Contains("建筑"))
{
revitLinkInstanceT = Instance;
instanceDocumentT = Instance.GetLinkDocument();
break;
}
}
docT = instanceDocumentT;
#endregion
return docT;
}
/// <summary>
/// 将文档中链接的结构模型提取出来
/// </summary>
/// <param name="commandData"></param>
/// <returns></returns>
public Document GetLinkDocS(ExternalCommandData commandData) {
Document doc = commandData.Application.ActiveUIDocument.Document;
#region 将文档中链接的结构模型提取出来
//将文档中链接的结构模型提取出来
RevitLinkInstance current_Structural_Instance = null;
FilteredElementCollector all_Linked_Structural_Doc = new FilteredElementCollector(doc);
ElementCategoryFilter cat_Linked_Structural_Filter = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
all_Linked_Structural_Doc = all_Linked_Structural_Doc.WherePasses(cat_Linked_Structural_Filter);
var filtered_Structural_List = from Rvt_Links in all_Linked_Structural_Doc
where Rvt_Links.Name.Contains("结构") || Rvt_Links.Name.Contains("S")
select Rvt_Links as RevitLinkInstance;
foreach (RevitLinkInstance RLI in filtered_Structural_List)
{
if (RLI != null)
{
current_Structural_Instance = RLI;
break;
}
}
if (current_Structural_Instance == null)
{
// MessageBox.Show("如需使用避让功能,需链接结构模型");
}
else
{
Document linked_Structural_Doc = current_Structural_Instance.GetLinkDocument();
docS = linked_Structural_Doc;
}
return docS;
#endregion
}
/// <summary>
/// 将文档中链接的电气模型提取出来
/// </summary>
/// <param name="commandData"></param>
/// <returns></returns>
public Document GetLinkDocE(ExternalCommandData commandData) {
Document doc = commandData.Application.ActiveUIDocument.Document;
#region 将文档中链接的电气模型提取出来
RevitLinkInstance current_Device_Instance = null;
FilteredElementCollector all_Linked_Electrical_Doc = new FilteredElementCollector(doc);
ElementCategoryFilter cat_Linked_Electrical_Filter = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
all_Linked_Electrical_Doc = all_Linked_Electrical_Doc.WherePasses(cat_Linked_Electrical_Filter);
var filtered_Electrical_List = from Rvt_Links in all_Linked_Electrical_Doc
where Rvt_Links.Name.Contains("电气") || Rvt_Links.Name.Contains("E")
select Rvt_Links as RevitLinkInstance;
foreach (RevitLinkInstance RLI in filtered_Electrical_List)
{
if (RLI != null)
{
current_Device_Instance = RLI;
break;
}
}
if (current_Device_Instance == null)
{
// MessageBox.Show("如需使用避让功能,需链接电气模型");
}
else
{
Document linked_Device_Doc = current_Device_Instance.GetLinkDocument();
docE = linked_Device_Doc;
}
#endregion
return docE;
}
/// <summary>
/// 获得链接文件中所有房间
/// </summary>
/// <param name="commandData"></param>
/// <param name="instanceDocumentT"></param>
/// <returns></returns>
public List<Element> GetLinkRooms(ExternalCommandData commandData,Document instanceDocumentT) {
#region 获得链接文件中所有房间 List<Element> rooms Rooms
List<Element> Rooms = new List<Element>();
if (instanceDocumentT != null)
{
FilteredElementCollector InstanceRoomCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Rooms);
FilteredElementCollector InstanceFloorCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Floors);
FilteredElementCollector InstanceRoofCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Roofs);
Rooms = InstanceRoomCollector.ToList();
// floors = InstanceFloorCollector.ToList();
// roofs = InstanceRoofCollector.ToList();
//MessageBox.Show(Rooms.Count.ToString());
}
allRooms = Rooms;
#endregion
return Rooms;
}
public List<Element> GetLinkRooms( Document instanceDocumentT)
{
#region 获得链接文件中所有房间 List<Element> rooms Rooms
List<Element> Rooms = new List<Element>();
if (instanceDocumentT != null)
{
FilteredElementCollector InstanceRoomCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Rooms);
FilteredElementCollector InstanceFloorCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Floors);
FilteredElementCollector InstanceRoofCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Roofs);
Rooms = InstanceRoomCollector.ToList();
// floors = InstanceFloorCollector.ToList();
// roofs = InstanceRoofCollector.ToList();
//MessageBox.Show(Rooms.Count.ToString());
}
allRooms = Rooms;
#endregion
return Rooms;
}
/// <summary>
/// 获得链接文件中所有门
/// </summary>
/// <param name="commandData"></param>
/// <param name="instanceDocumentT"></param>
/// <returns></returns>
public List<Element> GetLinkDoors(ExternalCommandData commandData,Document instanceDocumentT) {
#region 获得链接文件中所有门 List<Element> Doors Doors
List<Element> Doors = new List<Element>();
if (instanceDocumentT != null)
{
FilteredElementCollector InstanceDoorsCollector = new FilteredElementCollector(instanceDocumentT).
OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance));
Doors = InstanceDoorsCollector.ToList();
//MessageBox.Show(Rooms.Count.ToString());
allDoors = Doors;
}
#endregion
return Doors;
}
/// <summary>
/// 获得当前工程内所有灯具信息
/// </summary>
/// <param name="commandData"></param>
/// <param name="doc"></param>
/// <returns></returns>
public List<FamilySymbol> GetCurrentLIG( Document doc) {
#region 获得当前工程内所有灯具信息 class LightObjectInfo LightObjectInfos Lights
//筛选
List<FamilySymbol> Lights = new List<FamilySymbol>();
FilteredElementCollector all_Elements = new FilteredElementCollector(doc);
ElementClassFilter class_Filter = new ElementClassFilter(typeof(FamilySymbol));
all_Elements = all_Elements.WherePasses(class_Filter);
var filtered_List = from sy in all_Elements
where sy.Name.Length > 3
where sy.Name.Substring(0, 3) == "LIG" || sy.Name.Contains("灯") || sy.Name.Contains("照明")
select sy as FamilySymbol;
Lights = filtered_List.ToList<FamilySymbol>();
allLights = Lights;
#endregion
return Lights;
}
/// <summary>
/// 获得链接文件中所有线型
/// </summary>
/// <param name="commandData"></param>
/// <returns></returns>
public List<Element> GetCurrentLineStyle(ExternalCommandData commandData,Document doc) {
#region 获得链接文件中所有线型
FilteredElementCollector allElements = new FilteredElementCollector(doc);
ElementClassFilter classFilter = new ElementClassFilter(typeof(GraphicsStyle));
ElementCategoryFilter catFilter1 = new ElementCategoryFilter(BuiltInCategory.OST_Lines);
allElements = allElements.WherePasses(classFilter);
selectedLineStyles = allElements.Where(e => e.Name.Contains("照明")).Select(e => e.Name).ToList();
allLineStyles = allElements.Select(e => e as Element).ToList();
#endregion
return allLineStyles;
}
/// <summary>
/// 此处应该寻找链接文件内的墙
/// </summary>
/// <param name="commandData"></param>
/// <param name="instanceDocumentT"></param>
/// <returns></returns>
public List<Wall> GetLinkWalls(ExternalCommandData commandData,Document instanceDocumentT) {
#region 此处应该寻找链接文件内的墙
FilteredElementCollector wallCollector = new FilteredElementCollector(instanceDocumentT);
ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
wallCollector = wallCollector.WherePasses(wallFilter);
List<Wall> allWall = new List<Wall>();
foreach (Element wall in wallCollector)
{
Wall w = wall as Wall;
allWall.Add(w == null ? null : w);
}
AllWalls = allWall;
#endregion
return allWall;
}
public List<Wall> GetLinkWalls( Document instanceDocumentT)
{
#region 此处应该寻找链接文件内的墙
FilteredElementCollector wallCollector = new FilteredElementCollector(instanceDocumentT);
ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
wallCollector = wallCollector.WherePasses(wallFilter);
List<Wall> allWall = new List<Wall>();
foreach (Element wall in wallCollector)
{
Wall w = wall as Wall;
allWall.Add(w == null ? null : w);
}
AllWalls = allWall;
#endregion
return allWall;
}
/// <summary>
/// 建筑链接文件内所有轴网
/// </summary>
/// <param name="commandData"></param>
/// <param name="instanceDocumentT"></param>
/// <returns></returns>
public List<Grid> GetLinkGrids(ExternalCommandData commandData , Document instanceDocumentT) {
#region 建筑链接文件内所有轴网
List<Element> grids = RevitFilterMethod.RevitClassFilterMethod<Grid>(instanceDocumentT);
Grids = grids.Select(g => g as Grid).ToList();
#endregion
return Grids;
}
/// <summary>
/// 从电气链接文件里拿到所有的楼板
/// </summary>
/// <param name="commandData"></param>
/// <param name="docE"></param>
/// <returns></returns>
public List<Element> GetLinkFloors(ExternalCommandData commandData , Document docE) {
#region 从电气链接文件里拿到所有的楼板 allFloors
List<Element> linkedFloors = new List<Element>();
if (docE != null)
{
linkedFloors = RevitFilterMethod.RevitCategoryFilterMethod(docE, BuiltInCategory.OST_Floors);
allFloors = linkedFloors;
}
else { MessageBox.Show("未检测到电气模型"); }
#endregion
return linkedFloors;
}
/// <summary>
/// 从建筑链接文件中过滤出需要的标高
/// </summary>
/// <param name="commandData"></param>
/// <param name="docT"></param>
/// <returns></returns>
public List<Element> GetLinkLevel(ExternalCommandData commandData, Document docT) {
#region 从建筑链接文件中过滤出需要的标高
//从建筑链接文件中过滤出需要的标高
List<Element> linked_Architecture_LV = null;
FilteredElementCollector all_Linked_Architecture_LV = new FilteredElementCollector(docT);
ElementClassFilter cat_Linked_Architecture_LV_Filter = new ElementClassFilter(typeof(Level));
all_Linked_Architecture_LV = all_Linked_Architecture_LV.WherePasses(cat_Linked_Architecture_LV_Filter);
linked_Architecture_LV = all_Linked_Architecture_LV.ToList();
LinkedLevels = linked_Architecture_LV;
#endregion
return linked_Architecture_LV;
}
/// <summary>
/// 从电气链接文件里拿到所有的实例
/// </summary>
/// <param name="commandData"></param>
/// <param name="docE"></param>
/// <returns></returns>
public List<Element> GetLinkInstances(ExternalCommandData commandData,Document docE) {
#region 从电气链接文件里拿到所有的实例
List<Element> linkedDevices = new List<Element>();
if (docE != null) {
linkedDevices = RevitFilterMethod.RevitClassFilterMethod<FamilyInstance>(docE);
allDeviceElements = linkedDevices;
}
else { MessageBox.Show("未检测到电气模型"); }
#endregion
return linkedDevices;
}
public List<Element> GetLinkWindows(ExternalCommandData commandData, Document docT) {
List<Element> linkedWindows = new List<Element>();
if (docT != null) {
linkedWindows = RevitFilterMethod.RevitCategoryFilterMethod(docT, BuiltInCategory.OST_Windows);
}
return linkedWindows;
}
public void getLink(ExternalCommandData commandData ) {
Document doc = commandData.Application.ActiveUIDocument.Document;
#region 获取建筑链接文件 Document instanceDoc instanceDocumentT
Document instanceDocumentT = null;
FilteredElementCollector InstanceCollectorT = new FilteredElementCollector(doc);
ElementClassFilter InstanceFilterT = new ElementClassFilter(typeof(RevitLinkInstance));
InstanceCollectorT = InstanceCollectorT.WherePasses(InstanceFilterT);
foreach (RevitLinkInstance Instance in InstanceCollectorT)
{
if (Instance.GetLinkDocument().Title.Contains("T") || Instance.GetLinkDocument().Title.Contains("建筑"))
{
revitLinkInstanceT = Instance;
instanceDocumentT = Instance.GetLinkDocument();
break;
}
}
docT = instanceDocumentT;
#endregion
//------------------------------------------------------
#region 将文档中链接的结构模型提取出来
//将文档中链接的结构模型提取出来
RevitLinkInstance current_Structural_Instance = null;
FilteredElementCollector all_Linked_Structural_Doc = new FilteredElementCollector(doc);
ElementCategoryFilter cat_Linked_Structural_Filter = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
all_Linked_Structural_Doc = all_Linked_Structural_Doc.WherePasses(cat_Linked_Structural_Filter);
var filtered_Structural_List = from Rvt_Links in all_Linked_Structural_Doc
where Rvt_Links.Name.Contains("结构") || Rvt_Links.Name.Contains("S")
select Rvt_Links as RevitLinkInstance;
foreach (RevitLinkInstance RLI in filtered_Structural_List)
{
if (RLI != null)
{
current_Structural_Instance = RLI;
break;
}
}
if (current_Structural_Instance == null)
{
// MessageBox.Show("如需使用避让功能,需链接结构模型");
}
else
{
Document linked_Structural_Doc = current_Structural_Instance.GetLinkDocument();
docS = linked_Structural_Doc;
}
#endregion
//------------------------------------------------------
#region 将文档中链接的电气模型提取出来
RevitLinkInstance current_Device_Instance = null;
FilteredElementCollector all_Linked_Electrical_Doc = new FilteredElementCollector(doc);
ElementCategoryFilter cat_Linked_Electrical_Filter = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
all_Linked_Electrical_Doc = all_Linked_Electrical_Doc.WherePasses(cat_Linked_Electrical_Filter);
var filtered_Electrical_List = from Rvt_Links in all_Linked_Electrical_Doc
where Rvt_Links.Name.Contains("电气") || Rvt_Links.Name.Contains("E")
select Rvt_Links as RevitLinkInstance;
foreach (RevitLinkInstance RLI in filtered_Electrical_List)
{
if (RLI != null)
{
current_Device_Instance = RLI;
break;
}
}
if (current_Device_Instance == null)
{
// MessageBox.Show("如需使用避让功能,需链接电气模型");
}
else
{
Document linked_Device_Doc = current_Device_Instance.GetLinkDocument();
docE = linked_Device_Doc;
}
#endregion
//------------------------------------------------------
//------------------------------------------------------
#region 获得链接文件中所有房间 List<Element> rooms Rooms
List<Element> Rooms = new List<Element>();
if (instanceDocumentT != null)
{
FilteredElementCollector InstanceRoomCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Rooms);
FilteredElementCollector InstanceFloorCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Floors);
FilteredElementCollector InstanceRoofCollector = new FilteredElementCollector(instanceDocumentT).OfCategory(BuiltInCategory.OST_Roofs);
Rooms = InstanceRoomCollector.ToList();
// floors = InstanceFloorCollector.ToList();
// roofs = InstanceRoofCollector.ToList();
//MessageBox.Show(Rooms.Count.ToString());
}
allRooms = Rooms;
#endregion
#region 获得当前工程内所有灯具信息 class LightObjectInfo LightObjectInfos Lights
//筛选
List<FamilySymbol> Lights = new List<FamilySymbol>();
FilteredElementCollector all_Elements = new FilteredElementCollector(doc);
ElementClassFilter class_Filter = new ElementClassFilter(typeof(FamilySymbol));
all_Elements = all_Elements.WherePasses(class_Filter);
var filtered_List = from sy in all_Elements
where sy.Name.Length > 3
where sy.Name.Substring(0, 3) == "LIG" || sy.Name.Contains("灯") || sy.Name.Contains("照明")
select sy as FamilySymbol;
Lights = filtered_List.ToList<FamilySymbol>();
allLights = Lights;
#endregion
#region 获得链接文件中所有门 List<Element> Doors Doors
List<Element> Doors = new List<Element>();
if (instanceDocumentT != null)
{
FilteredElementCollector InstanceDoorsCollector = new FilteredElementCollector(instanceDocumentT).
OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance));
Doors = InstanceDoorsCollector.ToList();
//MessageBox.Show(Rooms.Count.ToString());
allDoors = Doors;
}
#endregion
#region 获得链接文件中所有线型
FilteredElementCollector allElements = new FilteredElementCollector(doc);
ElementClassFilter classFilter = new ElementClassFilter(typeof(GraphicsStyle));
ElementCategoryFilter catFilter1 = new ElementCategoryFilter(BuiltInCategory.OST_Lines);
allElements = allElements.WherePasses(classFilter);
selectedLineStyles = allElements.Where(e => e.Name.Contains("照明")).Select(e => e.Name).ToList();
allLineStyles = allElements.Select(e => e as Element).ToList();
#endregion
#region 此处应该寻找链接文件内的墙
FilteredElementCollector wallCollector = new FilteredElementCollector(instanceDocumentT);
ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
wallCollector = wallCollector.WherePasses(wallFilter);
List<Wall> allWall = new List<Wall>();
foreach (Element wall in wallCollector)
{
Wall w = wall as Wall;
allWall.Add(w == null ? null : w);
}
AllWalls = allWall;
#endregion
#region 建筑链接文件内所有轴网
List<Element> grids = RevitFilterMethod.RevitClassFilterMethod<Grid>(instanceDocumentT);
Grids = grids.Select(g => g as Grid).ToList();
#endregion
#region 从建筑链接文件中过滤出需要的标高
//从建筑链接文件中过滤出需要的标高
List<Element> linked_Architecture_LV = null;
FilteredElementCollector all_Linked_Architecture_LV = new FilteredElementCollector(docT);
ElementClassFilter cat_Linked_Architecture_LV_Filter = new ElementClassFilter(typeof(Level));
all_Linked_Architecture_LV = all_Linked_Architecture_LV.WherePasses(cat_Linked_Architecture_LV_Filter);
linked_Architecture_LV = all_Linked_Architecture_LV.ToList();
LinkedLevels = linked_Architecture_LV;
#endregion
//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------
#region 从电气链接文件里拿到所有的楼板 allFloors
if (docE != null)
{
List<Element> linkedFloors = new List<Element>();
linkedFloors = RevitFilterMethod.RevitCategoryFilterMethod(docE, BuiltInCategory.OST_Floors);
allFloors = linkedFloors;
}
else { MessageBox.Show("未检测到电气模型"); }
#endregion
#region 从电气链接文件里拿到所有的实例
if (docE != null)
{
List<Element> linkedDevices = new List<Element>();
linkedDevices = RevitFilterMethod.RevitClassFilterMethod<FamilyInstance>(docE);
allDeviceElements = linkedDevices;
}
else { MessageBox.Show("未检测到电气模型"); }
#endregion
}
}
}
使用方法:
LinkDocInfo linkDocInfo = new LinkDocInfo();
Document docT = linkDocInfo.GetLinkDocT(commandData);
//List<Wall> AllWalls = linkDocInfo.AllWalls;
List<Wall> AllWalls = linkDocInfo.GetLinkWalls(commandData, docT);
//List<Grid> Grids = linkDocInfo.Grids;
List<Grid> Grids = linkDocInfo.GetLinkGrids(commandData, docT);
//List<Element> allRooms = linkDocInfo.allRooms;
List<Element> allRooms = linkDocInfo.GetLinkRooms(commandData, docT);
//RevitLinkInstance revitLinkInstanceT = linkDocInfo.revitLinkInstanceT;