123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /* ==============================================================================
- * 功能描述: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;
- }
- }
- }
|