Client/Desktop/KMBIM3.0/23.10.16/Cmd/Hanger/View3DHanger.cs

328 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using KDCS.Utils;
namespace KMBIM.Revit.Tools.Cmd.Hanger
{
internal class LevelComparer : IComparer<Autodesk.Revit.DB.Level>
{
public int Compare(Autodesk.Revit.DB.Level x, Autodesk.Revit.DB.Level y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
if (Util.fuzzyEqual(x.ProjectElevation, y.ProjectElevation))
{
return 0;
}
if (x.ProjectElevation < y.ProjectElevation)
{
return -1;
}
return 1;
}
}
public class View3DHanger
{
private bool IsEqualView(View3D view3d, string view_name)
{
if (!view3d.IsTemplate)
{
return view3d.Name == view_name;
}
return false;
}
private Level AnchorLevel;
private List<Autodesk.Revit.DB.Level> m_levels;
private List<double> m_elevations;
private const double m_c = 10.0;
private Document _Document;
private Autodesk.Revit.DB.ReferenceIntersector referenceIntersect;
private List<Autodesk.Revit.DB.Level> GetLevels()
{
return this.m_levels;
}
private void SetLevels(List<Autodesk.Revit.DB.Level> levels)
{
this.m_levels = levels;
}
public void SetAncholLevel(Level level)
{
AnchorLevel = level;
}
private List<double> GetElevations()
{
return this.m_elevations;
}
private void SetElevations(List<double> A_0)
{
this.m_elevations = A_0;
}
public View3DHanger()
{
AnchorLevel = null;
SetLevels((List<Autodesk.Revit.DB.Level>)null);
SetElevations((List<double>)null);
referenceIntersect = null;
}
public View3DHanger(Document A_0)
{
_Document = A_0;
referenceIntersect = null;
Init(A_0);
}
private void GetReferenceIntersector()
{
if (referenceIntersect != null)
{
return;
}
List<BuiltInCategory> list = new List<BuiltInCategory>();
list.Add(BuiltInCategory.OST_Floors);
list.Add(BuiltInCategory.OST_Roofs);
list.Add(BuiltInCategory.OST_StructuralFraming);
list.Add(BuiltInCategory.OST_StructuralTruss);
list.Add(BuiltInCategory.OST_StructuralFoundation);
list.Add(BuiltInCategory.OST_Stairs);
string viewName = "View3DHanger";
View3D view3D = null;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(_Document);
FilteredElementIterator iter = filteredElementCollector.OfClass(typeof(View3D)).GetElementIterator();
iter.Reset();
while (iter.MoveNext())
{
View3D tmp_view3d = iter.Current as View3D;
if (null != tmp_view3d && !tmp_view3d.IsTemplate)
{
if (tmp_view3d.Name == viewName)
{
view3D = tmp_view3d;
break;
}
}
}
if (view3D == null)
{
IEnumerable<ViewFamilyType> viewFamilyTypes = from elem in new
FilteredElementCollector(_Document).OfClass(typeof(ViewFamilyType))
let type = elem as ViewFamilyType
where type.ViewFamily == ViewFamily.ThreeDimensional
select type;
view3D = View3D.CreateIsometric(_Document, viewFamilyTypes.First().Id);
if (view3D == null)
{
return;
}
view3D.Name = viewName;
foreach (BuiltInCategory item in list)
{
Util.SetCategoryHidden(_Document, view3D, item);
}
if (view3D.CanModifyViewDiscipline())
{
view3D.Discipline = ViewDiscipline.Coordination;
}
if (view3D.CanModifyDetailLevel())
{
view3D.DetailLevel = Autodesk.Revit.DB.ViewDetailLevel.Fine;
}
if (view3D.CanModifyDisplayStyle())
{
view3D.DisplayStyle = DisplayStyle.Realistic;
}
view3D.CropBoxActive = false;
view3D.CropBoxVisible = false;
view3D.IsSectionBoxActive = false;
}
List<Autodesk.Revit.DB.ElementFilter> list2 = new List<Autodesk.Revit.DB.ElementFilter>();
foreach (BuiltInCategory item2 in list)
{
list2.Add(new Autodesk.Revit.DB.ElementCategoryFilter(item2));
}
referenceIntersect = new Autodesk.Revit.DB.ReferenceIntersector(new LogicalOrFilter(list2), FindReferenceTarget.Face, view3D);
referenceIntersect.FindReferencesInRevitLinks = true;
}
private void Init(Document doc)
{
List<Autodesk.Revit.DB.Level> list = new List<Autodesk.Revit.DB.Level>();
foreach (Autodesk.Revit.DB.Level item in new Autodesk.Revit.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).OfType<Autodesk.Revit.DB.Level>())
{
if (item != null)
{
list.Add(item);
}
}
list.Sort(new LevelComparer());
SetLevels(list);
SetElevations(new List<double>());
for (int i = 0; i < GetLevels().Count; i++)
{
GetElevations().Add(GetLevels()[i].ProjectElevation);
}
}
public bool GetIntersectionElevation(Autodesk.Revit.DB.XYZ origin, bool isDown, out double elevation)
{
elevation = 0.0;
GetReferenceIntersector();
if (referenceIntersect == null)
{
return false;
}
Autodesk.Revit.DB.XYZ direction = new Autodesk.Revit.DB.XYZ(0.0, 0.0, (!isDown) ? 1 : (-1));
Autodesk.Revit.DB.ReferenceWithContext referenceWithContext = referenceIntersect.FindNearest(origin, direction);
if (referenceWithContext == null)
{
return false;
}
Reference reference = referenceWithContext.GetReference();
if (reference == null)
{
return false;
}
elevation = reference.GlobalPoint.Z;
return true;
}
public bool GetAnchorElevation(Autodesk.Revit.DB.XYZ location, bool isSupport, out double elevation)
{
elevation = 0.0;
if (GetElevations().Count == 0)
{
return false;
}
if (isSupport && Util.LT_ET(GetElevations()[0], location.Z))
{
return false;
}
if (!isSupport && Util.GT_ET(GetElevations()[GetElevations().Count - 1], location.Z))
{
return false;
}
for (int i = 0; i < GetElevations().Count; i++)
{
if (isSupport && Util.LT_ET(GetElevations()[i], location.Z))
{
elevation = GetElevations()[i - 1];
return true;
}
if (!isSupport && Util.GT(GetElevations()[i], location.Z))
{
elevation = GetElevations()[i];
return true;
}
}
if (isSupport)
{
elevation = GetElevations()[GetElevations().Count - 1];
}
else
{
if (AnchorLevel != null) elevation = AnchorLevel.Elevation;
else elevation = GetElevations()[0];
}
return true;
}
public bool GetAnchorElevation(Autodesk.Revit.DB.XYZ location, bool isSupport, EnumAnchorInstallBase AnchorInstallBase, out Autodesk.Revit.DB.Level level, out double locationElevation, out double anchoroElevation)
{
int num = 0;
var levels = GetLevels();
for (num = 0; num < levels.Count && !(location.Z < levels[num].ProjectElevation); num++)
{
}
if (num == 0)
{
level = levels[0];
}
else
{
level = levels[num - 1];
}
locationElevation = location.Z - level.ProjectElevation;
anchoroElevation = double.NaN;
double elevation = 0.0;
if (AnchorInstallBase == EnumAnchorInstallBase.Structrual)
{
//구조체
if (GetIntersectionElevation(location, isSupport, out elevation))
{
anchoroElevation = elevation - level.ProjectElevation;
}
}
else if (GetAnchorElevation(location, isSupport, out elevation))
{
if (AnchorLevel != null) anchoroElevation = AnchorLevel.Elevation - level.ProjectElevation;
else anchoroElevation = elevation - level.ProjectElevation;
}
return true;
}
public Dictionary<Autodesk.Revit.DB.ElementId, double> a(Autodesk.Revit.DB.Level A_0)
{
Dictionary<Autodesk.Revit.DB.ElementId, double> dictionary = new Dictionary<Autodesk.Revit.DB.ElementId, double>();
if (GetLevels() == null || GetElevations() == null || GetLevels().Count == 0)
{
return dictionary;
}
for (int i = 0; i < GetLevels().Count; i++)
{
if (A_0.Id == GetLevels()[i].Id)
{
if (i < GetLevels().Count - 1)
{
dictionary.Add(GetLevels()[i].Id, GetElevations()[i + 1] - GetElevations()[i]);
}
if (i > 0)
{
dictionary.Add(GetLevels()[i - 1].Id, GetElevations()[i] - GetElevations()[i - 1]);
}
break;
}
}
return dictionary;
}
}
}