123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using Autodesk.Revit.DB;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FWindSoft.Revit.Mep
- {
- /// <summary>
- /// 构件初始坐标接口(匹配connector的连接点)
- /// </summary>
- public interface IFittingCoordinate
- {
- /// <summary>
- /// 获取指定实例的连接点坐标
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- Tuple<XYZ, XYZ> GetCoordinate(FamilyInstance fi);
- }
- /// <summary>
- /// 连接件坐标生成工厂
- /// </summary>
- public static class FittingCoordinateFactory
- {
- /// <summary>
- /// 根据内置分类,获取相应的构件坐标
- /// </summary>
- /// <param name="builtCategory"></param>
- /// <returns></returns>
- public static IFittingCoordinate CreateFittingCoordinate(BuiltInCategory builtCategory)
- {
- if (builtCategory == BuiltInCategory.OST_ConduitFitting)
- {
- return new ConduitFittingCoordinate();
- }
- else if (builtCategory == BuiltInCategory.OST_PipeFitting)
- {
- return new PipeFittingCoordinate();
- }
- return null;
- }
- /// <summary>
- /// 获取实例的坐标体系
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- public static Tuple<XYZ, XYZ> GetCoordinate(FamilyInstance fi)
- {
- var builtCategory = fi.GetBuiltInCategory();
- var realFittingCoordinate = CreateFittingCoordinate(builtCategory);
- return realFittingCoordinate?.GetCoordinate(fi);
- }
- }
- /// <summary>
- /// 线管连接件坐标
- /// </summary>
- internal class ConduitFittingCoordinate : IFittingCoordinate
- {
- public Tuple<XYZ, XYZ> GetCoordinate(FamilyInstance fi)
- {
- return new Tuple<XYZ, XYZ>(XYZ.BasisX, XYZ.BasisY);
- }
- }
- /// <summary>
- /// 水管管连接件坐标
- /// </summary>
- internal class PipeFittingCoordinate : IFittingCoordinate
- {
- public Tuple<XYZ, XYZ> GetCoordinate(FamilyInstance fi)
- {
- var connectors = fi.GetConnectors(Domain.DomainPiping);
- Connector primaryConnector = connectors.FirstOrDefault(c => c.GetMEPConnectorInfo().IsPrimary);
- Connector secondConnector = null;
- foreach (Connector connector in connectors)
- {
- if (connector == primaryConnector)
- {
- continue;
- }
- if (!connector.CoordinateSystem.BasisZ.IsParallel(primaryConnector.CoordinateSystem.BasisZ))
- {
- secondConnector = connector;
- break;
- }
- }
- if(primaryConnector==null||secondConnector==null)
- {
- return null;
- }
- #region 特殊处理
- try
- {
- if (connectors.Count == 2)
- {
- secondConnector.Angle = Math.PI / 2;
- }
- else if (connectors.Count == 3)
- {
- secondConnector.Angle = Math.PI / 2;
- }
- }
- catch (Exception e)
- {
- //如果没有参照角可能会发生异常;四通设置90度有问题。设置0度是不是可以?
- //throw;
- }
- #endregion
-
- var firstDirection = primaryConnector.CoordinateSystem.BasisZ;
- var secondDirection = secondConnector.CoordinateSystem.BasisZ;
- if (!firstDirection.IsVertical(secondDirection))
- {
- var useSecond = secondDirection - secondDirection.DotProduct(firstDirection) * firstDirection;
- secondDirection = useSecond.Normalize();
- }
- return new Tuple<XYZ, XYZ>(firstDirection, secondDirection);
- }
- }
- }
|