|
@@ -0,0 +1,165 @@
|
|
|
|
+/* ==============================================================================
|
|
|
|
+ * 功能描述:CreateFacilityRevitUtils
|
|
|
|
+ * 创 建 者:Garrett
|
|
|
|
+ * 创建日期:2019/10/23 10:24:09
|
|
|
|
+ * ==============================================================================*/
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Text.RegularExpressions;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using Autodesk.Revit.DB;
|
|
|
|
+using Autodesk.Revit.UI;
|
|
|
|
+using Autodesk.Revit.DB.Structure;
|
|
|
|
+using Saga.PlugIn.ModelCheck;
|
|
|
|
+using SAGA.DotNetUtils.Others;
|
|
|
|
+using SAGA.RevitUtils.Extends;
|
|
|
|
+using SAGA.RevitUtils.Windows;
|
|
|
|
+using FWindSoft.Revit;
|
|
|
|
+using SAGA.DotNetUtils;
|
|
|
|
+
|
|
|
|
+namespace Saga.PlugIn.CreateFacility
|
|
|
|
+{
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// CreateFacilityRevitUtils
|
|
|
|
+ /// </summary>
|
|
|
|
+ public class CreateFacilityRevitUtils
|
|
|
|
+ {
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 由资产id查找模型中的设备
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="code"></param>
|
|
|
|
+ /// <param name="propertyId"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static string FindFacility(Document doc,string code, string propertyId)
|
|
|
|
+ {
|
|
|
|
+ var facilities = DocumentExtension.GetElements<FamilyInstance>(doc);
|
|
|
|
+ string id = null;
|
|
|
|
+ foreach (FamilyInstance facility in facilities)
|
|
|
|
+ {
|
|
|
|
+ var familyName = facility.GetFamilyName();
|
|
|
|
+ if (familyName.StartsWith(code))
|
|
|
|
+ {
|
|
|
|
+ var parameter = ParameterExtend.GetParameterString(facility, CreateFacilityConst.PropertyID);
|
|
|
|
+ if (parameter == propertyId)
|
|
|
|
+ {
|
|
|
|
+ id = facility.Id.ToString();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 定位模型
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="id"></param>
|
|
|
|
+ public static void FocusFacility(string id)
|
|
|
|
+ {
|
|
|
|
+ RevitCore.UIApp.SetShowElements(new ElementId(id.ToInt()));
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 自动创建设备
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="doc"></param>
|
|
|
|
+ /// <param name="code"></param>
|
|
|
|
+ /// <param name="location"></param>
|
|
|
|
+ /// <param name="propertyId"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static string CreateFacility(Document doc, MFacilityClass facilityClass, MXYZ location, string propertyId)
|
|
|
|
+ {
|
|
|
|
+ FamilyInstance fi = null;
|
|
|
|
+
|
|
|
|
+ using (Transaction trans = new Transaction(doc, "自动创建模型"))
|
|
|
|
+ {
|
|
|
|
+ trans.Start();
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ FamilySymbol fs = GetFamilySymbol(doc, facilityClass.code);
|
|
|
|
+ if (fs == null)
|
|
|
|
+ {
|
|
|
|
+ WinTipMissFamily win=new WinTipMissFamily($"{facilityClass.code}-{facilityClass.name}");
|
|
|
|
+ win.ShowDialog();
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (IsBasePointFamily(fs))
|
|
|
|
+ {
|
|
|
|
+ //使用BIMLocation中的坐标,mm转为英寸
|
|
|
|
+ XYZ xyz = new XYZ(location.X.ToDouble().ToApi(), location.Y.ToDouble().ToApi(),
|
|
|
|
+ location.Z.ToDouble().ToApi());
|
|
|
|
+ xyz = xyz == null ? XYZ.Zero : xyz.ConvertToApi();
|
|
|
|
+ var level = GetCurFloorLevel(doc);
|
|
|
|
+ //传标高,z值设置为零
|
|
|
|
+ xyz = xyz.NewZ(0);
|
|
|
|
+ fi = doc.Create.NewFamilyInstance(xyz, fs, level, StructuralType.NonStructural);
|
|
|
|
+ //假如定位点和中心点不一致,平移设备,将设备中心点移到定位点上
|
|
|
|
+ doc.Regenerate();
|
|
|
|
+ fi.SetSharedParameter(CreateFacilityConst.PropertyID, propertyId);
|
|
|
|
+ XYZ centerXyz = FWindSoft.Revit.ElementExtension.GetLocationPoint(fi).NewZ(0);
|
|
|
|
+ if (!centerXyz.IsEqual2(xyz))
|
|
|
|
+ {
|
|
|
|
+ XYZ vector = xyz - centerXyz;
|
|
|
|
+ doc.MoveElement(fi.Id, vector);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ trans.Commit();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ MessageShowBase.Infomation("自动创建只能创建基于点的族,请查看族类型");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ trans.RollBack();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return fi?.Id.ToString();
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 判断是否为基于点的设备
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="fs"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ private static bool IsBasePointFamily(FamilySymbol fs)
|
|
|
|
+ {
|
|
|
|
+ bool result = false;
|
|
|
|
+ Family fa = fs?.Family;
|
|
|
|
+ if (fa == null) return result;
|
|
|
|
+ result = fa.FamilyPlacementType == FamilyPlacementType.OneLevelBased;
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static Level GetCurFloorLevel(Document doc)
|
|
|
|
+ {
|
|
|
|
+ var view = DocumentExtension.GetElements<ViewPlan>(doc).FirstOrDefault(t => t.GenLevel?.Name != null && t.Name?.IndexOf("-saga") > -1 && t.ViewType == ViewType.FloorPlan);
|
|
|
|
+ Level level = view?.GenLevel ?? doc.GetLevels().FirstOrDefault();
|
|
|
|
+ return level;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 由code获取FamilySymbol
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="doc"></param>
|
|
|
|
+ /// <param name="code"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static FamilySymbol GetFamilySymbol(Document doc,string code)
|
|
|
|
+ {
|
|
|
|
+ Func<string, bool> filterFamily = (t) =>
|
|
|
|
+ {
|
|
|
|
+ return Regex.IsMatch(t, $@"{code}{ModelCheckConst.IsFamilyCode}");
|
|
|
|
+ };
|
|
|
|
+ List<Family> listFamily = doc.GetFamilys();
|
|
|
|
+ var fa = listFamily.FirstOrDefault(t => filterFamily(t.Name));
|
|
|
|
+ if (fa == null) return null;
|
|
|
|
+ List<FamilySymbol> symbolList = FamilyExtension.GetFamilySymbols(fa);
|
|
|
|
+ var fs = symbolList?.FirstOrDefault();
|
|
|
|
+ if (fs != null && !fs.IsActive)
|
|
|
|
+ fs.Activate();
|
|
|
|
+ return fs;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|