How to get the total size of selected ducts?

How to get the total size of selected ducts?

Sino.Pacific
Contributor Contributor
387 Views
1 Reply
Message 1 of 2

How to get the total size of selected ducts?

Sino.Pacific
Contributor
Contributor

Hi everyone,

 

I am trying to get the total size of selected ducts as the picture below. If the ducts are straight I can use the boundingbox value of each one and combine them to get the total size. But the problem is when the ducts are not not straight (for example 15 degree to the Y axis ) then the boundingbox value is not correct. 

So I hope if anyone have experience with this could please help me.

Thank you a lot 😁

 

Issue.png

 

Best Regards,

 

Cherry Truong

0 Likes
388 Views
1 Reply
Reply (1)
Message 2 of 2

jlpgy
Advocate
Advocate

This is little bit complicated:

  1. You need to traverse the <Solid> of <Duct> elements.
  2. Get all <Edge> objects, then tessellate all feature poins along the Edge.GetCurve() curve objects.
  3. Get all feature points, then sort them along a vector.
  4. Calculate the "Distance Along Vector" from the near point to far point.
        public static Pair<XYZ, XYZ> ClosestFarthestPoints(IEnumerable<XYZ> points, XYZ dir)
        {
            XYZ closest = null, farthest = null;
            foreach (var pnt in points)
            {
                if (pnt != null)
                {
                    if (closest == null || farthest == null)
                    {
                        closest = farthest = pnt;
                        continue;
                    }
                    else
                    {
                        XYZ fromClosest = pnt - closest;
                        if (fromClosest.DotProduct(dir) < 0)
                        {
                            closest = pnt;
                        }
                        else
                        {
                            XYZ fromFarthest = pnt - farthest;
                            if (fromFarthest.DotProduct(dir) > 0)
                            {
                                farthest = pnt;
                            }
                            else
                            {
                                // 调试断点
                            }
                        }
                    }
                }
            }

            if (closest == null || farthest == null)
                throw ExceptionHelper.CollectionEmpty(nameof(points));
            else
                return Pair.Create(closest, farthest);
        }
        /// <summary>
        /// 指定一个方向向量,求出沿着这个方向向量上的实体“最近、最远”点
        /// </summary>
        public static Pair<XYZ, XYZ> ClosestFarthestPoints(Solid solid, XYZ dir, int num)
        {
            solid.AssertValidSolid();
            dir.AssertNotZeroVector(nameof(dir));
            if (num <= 0)
            {
                throw ExceptionHelper.ArgumentMustBePositive(nameof(num), num.ToString());
            }

            var edgePoints = GetParamPoints(solid, num);
            var toReturn = XyzPointUtils.ClosestFarthestPoints(edgePoints, dir);   // 如果点集为空,则这里会引发异常
            return toReturn;
        }

        public static IList<XYZ> GetParamPoints(Solid solid, int num)
        {
            solid.AssertValidSolid();
            if (num <= 0)
            {
                throw ExceptionHelper.ArgumentMustBePositive(nameof(num), num.ToString());
            }

            var toReturn = new List<XYZ>();
            foreach (Edge solidEdge in solid.Edges)
            {
                Curve edgeCurve = solidEdge.AsCurve();
                if (edgeCurve != null)
                {
#if DEBUG
                    edgeCurve.AssertBound(nameof(edgeCurve));
                    edgeCurve.Length.AssertPositive(nameof(edgeCurve) + '.' + nameof(edgeCurve.Length));
#endif
                    if (edgeCurve is Line edgeLine)
                    {
                        var pnts = CurveUtils.GetParamPoints(edgeLine, 1);
                        toReturn.AddRange(pnts);
                    }
                    else
                    {
                        var pnts = CurveUtils.GetParamPoints(edgeCurve, num);
                        toReturn.AddRange(pnts);
                    }
                }
            }

            return toReturn;
        }

 

单身狗;代码狗;健身狗;jolinpiggy@hotmail.com
0 Likes