多项功能优化

This commit is contained in:
GG Z
2024-12-22 10:26:12 +08:00
parent 77655c9ef5
commit 83b846f15f
66 changed files with 5424 additions and 2927 deletions

View File

@@ -70,24 +70,6 @@ public static class CollectorAssist
: FilteredElementCollector.IsViewValidForElementIteration(view.Document, view.Id)
? new FilteredElementCollector(view.Document, view.Id)
: throw new ArgumentException($"视图{view.Id}不可应用收集器");
public static FilteredElementCollector OfModelCollector(this Document doc)
{
if (doc == null)
{
throw new ArgumentNullException(nameof(doc), "文档为空");
}
var li = doc.OfCollector()
.WhereElementIsNotElementType()
.Where(
e => e is TopographySurface ||
(e.CanHaveTypeAssigned() &&
e.HasPhases() &&
e.Category is { Parent: null, CategoryType: CategoryType.Model } &&
e is not Panel &&
e is not Mullion &&
e is not RevitLinkInstance)).Select(e => e.Id).ToList();
return new FilteredElementCollector(doc, li);
}
/// <summary>
/// 文档的元素收集器
/// </summary>
@@ -135,18 +117,19 @@ public static class CollectorAssist
/// </summary>
/// <param name="collector">收集器</param>
/// <returns>过滤元素收集器</returns>
public static FilteredElementCollector OfInstance(this FilteredElementCollector collector) =>
public static FilteredElementCollector OfInstances(this FilteredElementCollector collector) =>
collector.WhereElementIsNotElementType();
/// <summary>
/// 过滤项目文件的模型元素
/// </summary>
/// <returns>元素集合</returns>
public static IEnumerable<Element> OfModelElements(this FilteredElementCollector collector) =>
collector
public static FilteredElementCollector OfModelCollector(this Document doc)
{
if (doc == null)
{
throw new ArgumentNullException(nameof(doc), "文档为空");
}
var li = doc.OfCollector()
.WhereElementIsNotElementType()
.Where(
e => e is TopographySurface ||
e is DirectShape ||
(e.CanHaveTypeAssigned()
&& e.IsValidObject
&& e.HasPhases()
@@ -154,6 +137,46 @@ public static class CollectorAssist
&& e.Category is { Parent: null, CategoryType: CategoryType.Model }
&& e is not Panel
&& e is not Mullion
&& e is not RevitLinkInstance)).Select(e => e.Id).ToList();
return new FilteredElementCollector(doc, li);
}
/// <summary>
/// 过滤项目文件最顶层的模型元素
/// </summary>
/// <returns>元素集合</returns>
public static IEnumerable<Element> OfParentModelElements(this Document doc) =>
doc.OfCollector()
.WhereElementIsNotElementType()
.Where(
e => e is TopographySurface ||
e is DirectShape ||
(e.CanHaveTypeAssigned()
&& e.IsValidObject
&& e.HasPhases()
&& e.get_BoundingBox(null) != null
&& e.Category is { CategoryType: CategoryType.Model, AllowsBoundParameters: true }
&& (e is HostObject || e.Category.Parent == null)
&& e is not Panel
&& e is not Mullion
&& e is not RevitLinkInstance)
);
/// <summary>
/// 过滤项目文件的所有模型元素
/// </summary>
/// <returns>元素集合</returns>
public static IEnumerable<Element> OfAllModelElements(this Document doc) =>
doc.OfCollector()
.WhereElementIsNotElementType()
.Where(
e => e is TopographySurface ||
e is DirectShape ||
(e.CanHaveTypeAssigned()
&& e.IsValidObject
&& e.HasPhases()
&& e.ViewSpecific == false
&& e.Category is { CategoryType: CategoryType.Model, AllowsBoundParameters: true }
&& e.get_BoundingBox(null) != null
&& e is not RevitLinkInstance)
);
}

View File

@@ -21,7 +21,7 @@ public static class ConnectorAssist
return null;
}
var document = first.Owner.Document;
var isUnion = false; //活接头
var isSameSize = false; //相同规格、尺寸
if (first.Domain != second.Domain)
{
throw new ArgumentException("无法连接不同类型的管线");
@@ -35,14 +35,14 @@ public static class ConnectorAssist
{
var shape = first.Shape;
//判断管径是否一致
isUnion = shape is ConnectorProfileType.Oval or ConnectorProfileType.Rectangular
isSameSize = shape is ConnectorProfileType.Oval or ConnectorProfileType.Rectangular
? Math.Abs(first.Width - second.Width) < 0.0001 && Math.Abs(first.Height - second.Height) < 0.0001
: Math.Abs(first.Radius - second.Radius) < 0.0001;
}
break;
}
case Domain.DomainPiping:
isUnion = Math.Abs(first.Radius - second.Radius) < 0.0001;
isSameSize = Math.Abs(first.Radius - second.Radius) < 0.0001;
break;
case Domain.DomainCableTrayConduit:
{
@@ -50,11 +50,11 @@ public static class ConnectorAssist
{
if (second.Owner is Conduit)
{
isUnion = Math.Abs(first.Radius - second.Radius) < 0.0001;
isSameSize = Math.Abs(first.Radius - second.Radius) < 0.0001;
}
if (second.Owner is CableTray)
{
isUnion = Math.Abs(first.Width - second.Width) < 0.0001 && Math.Abs(first.Height - second.Height) < 0.0001;
isSameSize = Math.Abs(first.Width - second.Width) < 0.0001 && Math.Abs(first.Height - second.Height) < 0.0001;
}
}
break;
@@ -62,11 +62,11 @@ public static class ConnectorAssist
}
try
{
//平行创建过渡件和活接头
//平行时,创建过渡件和活接头
if (first.CoordinateSystem.BasisZ.IsParallelTo(second.CoordinateSystem.BasisZ))
{
//活接头创建
if (isUnion) //管径一致时
//管径一致时,根据情况创建活接头
if (isSameSize)
{
//有一个是族的时候,始终直接连接,不创建活接头
if (first.Owner is MEPCurve ^ second.Owner is MEPCurve)
@@ -75,29 +75,40 @@ public static class ConnectorAssist
{
first.ConnectTo(second);
}
return null;
}
if (first.Owner is MEPCurve && second.Owner is MEPCurve) //都是管线
//都是管线
if (first.Owner is MEPCurve && second.Owner is MEPCurve)
{
return document.Create.NewUnionFitting(first, second);
}
//都是族的连接件
if (first.Owner is FamilyInstance && second.Owner is FamilyInstance)
{
first.ConnectTo(second);
}
return null;
}
//过渡件,管径不一致时
if (first.Owner is MEPCurve)
else
{
return document.Create.NewTransitionFitting(first, second);
}
if (second.Owner is MEPCurve)
{
return document.Create.NewTransitionFitting(second, first);
//管径不一致时,创建过渡件
if (first.Owner is MEPCurve)
{
return document.Create.NewTransitionFitting(first, second);
}
if (second.Owner is MEPCurve)
{
return document.Create.NewTransitionFitting(second, first);
}
//都是族的连接件
if (first.Owner is FamilyInstance && second.Owner is FamilyInstance)
{
first.ConnectTo(second);
}
return null;
}
}
//不平行则创建弯头
return document.Create.NewElbowFitting(first, second);
}
catch (Exception)
@@ -130,9 +141,9 @@ public static class ConnectorAssist
//垂直主管的分支管,为了创建默认三通
var verticalLine = Line.CreateBound(intersect, intersect + (3.0 * vertical));
//延长管线到交点处
branch.Owner.SetLocationCurve((branch.Owner.GetLocCurve() as Line).ExtendLine(intersect));
connector.Owner.SetLocationCurve((connector.Owner.GetLocCurve() as Line).ExtendLine(intersect));
connector1.Owner.SetLocationCurve((connector1.Owner.GetLocCurve() as Line).ExtendLine(intersect));
branch.Owner.SetLocationCurve((branch.Owner.GetCurve() as Line).ExtendLine(intersect));
connector.Owner.SetLocationCurve((connector.Owner.GetCurve() as Line).ExtendLine(intersect));
connector1.Owner.SetLocationCurve((connector1.Owner.GetCurve() as Line).ExtendLine(intersect));
//复制支管创建垂直主管的管线
var verticalMEPCurveId = ElementTransformUtils.CopyElement(document, branch.Owner.Id, XYZ.Zero).FirstOrDefault();
var verticalMEPCurve = document.GetElement(verticalMEPCurveId) as MEPCurve;
@@ -160,10 +171,10 @@ public static class ConnectorAssist
document.Regenerate();
//Connector branchFarConn = branch.Owner.GetConnectors().GetFarthestConnector(intersect);
//Line newBranch = Line.CreateBound(branchFarConn.Origin, teeBranchConn.Origin);
var l = branch.Owner.GetLocCurve() as Line;
var l = branch.Owner.GetCurve() as Line;
var newBranchLine = l.ExtendLine(teeBranchConn!.Origin);
branch.Owner.SetLocationCurve(newBranchLine);
//Line line = branch.Owner.GetLocCurve() as Line;
//Line line = branch.Owner.GetCurve() as Line;
//if (newBranch.Direction.IsAlmostEqualTo(line.Direction))
//{
// (branch.Owner.Location as LocationCurve).Curve = newBranch;
@@ -302,7 +313,7 @@ public static class ConnectorAssist
Domain.DomainHvac when connector.Shape == ConnectorProfileType.Rectangular => (connector.Width * 0.2) + (connector.Height * 0.2),
Domain.DomainHvac when connector.Shape == ConnectorProfileType.Oval => (connector.Width * 0.2) + (connector.Height * 0.2),
Domain.DomainHvac when connector.Shape == ConnectorProfileType.Round => connector.Radius * 0.5,
Domain.DomainPiping => connector.Radius * 0.5,
Domain.DomainPiping => connector.Radius * 4,
Domain.DomainCableTrayConduit => 1,
_ => 0.5
};

View File

@@ -39,8 +39,8 @@ public static class MEPAssist
public static Dictionary<MEPCurve, XYZ> GetNearestPoints(this MEPCurve mepCurve, MEPCurve mepCurve1)
{
Dictionary<MEPCurve, XYZ> dictionary = [];
var line1 = mepCurve.GetLocCurve() as Line;
var line2 = mepCurve1.GetLocCurve() as Line;
var line1 = mepCurve.GetCurve() as Line;
var line2 = mepCurve1.GetCurve() as Line;
var a = line1.Direction.DotProduct(line2.Direction);
var b = line1.Direction.DotProduct(line1.Direction);
var c = line2.Direction.DotProduct(line2.Direction);
@@ -76,7 +76,6 @@ public static class MEPAssist
/// </summary>
/// <param name="mepCurve">用来复制的管线</param>
/// <param name="line">定位线</param>
/// <param name="canNewElbow">可以创建弯头</param>
/// <returns></returns>
public static MEPCurve CopyAndSetLocationCurve(this MEPCurve mepCurve, Line line)
{
@@ -193,7 +192,7 @@ public static class MEPAssist
document.Regenerate();
//投影点
if (
electricMepCurve.GetLocCurve() is not Line line
electricMepCurve.GetCurve() is not Line line
|| electricMepCurve is FlexDuct
|| electricMepCurve is FlexPipe
|| electricMepCurve is PipeInsulation
@@ -205,7 +204,7 @@ public static class MEPAssist
}
var projectPoint = line.Project(point).XYZPoint;
if ((electricMepCurve.GetLocCurve() as Line).IsInsideEx(point, application.ShortCurveTolerance))
if ((electricMepCurve.GetCurve() as Line).IsInsideEx(point, application.ShortCurveTolerance))
{
var startLine = Line.CreateBound(line.GetEndPoint(0), projectPoint);
var endLine = Line.CreateBound(projectPoint, line.GetEndPoint(1));
@@ -377,12 +376,12 @@ public static class MEPAssist
public static void ConnectTo(this MEPCurve mainMepCurve, MEPCurve branchMepCurve)
{
var document = mainMepCurve.Document;
var unboundBranchCurve = branchMepCurve.GetLocCurve();
var unboundBranchCurve = branchMepCurve.GetCurve();
unboundBranchCurve.MakeUnbound();
var unboundMainCurve = mainMepCurve.GetLocCurve();
var unboundMainCurve = mainMepCurve.GetCurve();
unboundMainCurve.MakeUnbound();
var intersection = unboundBranchCurve.IntersectionPoint(unboundMainCurve) ?? throw new InvalidOperationException("主次分支管线不存在交点");
var line = mainMepCurve.GetLocCurve() as Line;
var line = mainMepCurve.GetCurve() as Line;
//根据交点在管线内的位置,判断是形成弯头还是三通
if (line.IsInsideEx(intersection, 0.5)) //三通
{
@@ -459,9 +458,9 @@ public static class MEPAssist
{
var doc = pipe.Document;
var v = doc.ActiveView as View3D;
List<T> li = new();
List<T> li = [];
//线段长度
var pline = pipe.GetLocCurve() as Line;
var pline = pipe.GetCurve() as Line;
var p1 = pline.GetEndPoint(0);
var p2 = pline.GetEndPoint(1);
//线段方向

View File

@@ -709,7 +709,7 @@ public static class SpatialAssist
/// </summary>
/// <param name="instance">以点定位的实例</param>
/// <returns></returns>
public static Curve GetLocCurve(this Element instance)
public static Curve GetCurve(this Element instance)
{
return instance.Location is LocationCurve lc ? lc.Curve : null;
}
@@ -1348,6 +1348,8 @@ public static class SpatialAssist
{
throw new ArgumentNullException(nameof(curve));
}
//var sourceCurve=instance.GetCurve();
if (instance.Location is LocationCurve lc)
{
try