Create Pipe from Points in list

zrodgersTSSSU
Advocate
Advocate

Create Pipe from Points in list

zrodgersTSSSU
Advocate
Advocate

Hey Everyone, Im trying to create pipes from a list of points. however i want to create the pipes between only between the closest points. Right now it will connect the points from the element in the order that it gets created.

 

EX:

zrodgersTSSSU_0-1623783990918.png

i know im not iterating through the points correctly to do so. but i cant figure out search the list for the closest points and then draw the pipe between those...

 

Thanks ahead of time for any responses/guidance.

 

Here is my code so far:

if (form.ShowDialog() == DialogResult.OK)
                {
                    //gets the level from form
                    ElementId slvlId = null;
                    if (form.sLevel.SelectedIndex > -1)
                    {
                        slvlId = (form.sLevel.SelectedItem as Level).Id;
                    }


                    //gets the system type from form
                    ElementId sysTypeId = null;
                    if (form.pSysType.SelectedIndex > -1)
                    {
                        sysTypeId = (form.pSysType.SelectedItem as PipingSystemType).Id;
                    }


                    //gets the pipe type from form
                    ElementId pTypeId = null;
                    if (form.pType.SelectedIndex > -1)
                    {
                        pTypeId = (form.pType.SelectedItem as PipeType).Id;
                    }

                    using (Transaction tx = new Transaction(doc))
                    {
                        tx.Start("Create Pipes");

                        List<XYZ> points = new List<XYZ>();

                        foreach (ElementId elemId in selectedIds)
                        {
                            Element elem = uidoc.Document.GetElement(elemId);

                            //bounding box around solid
                            BoundingBoxXYZ elemBB = elem.get_BoundingBox(doc.ActiveView);
                            //gets center of bounding box
                            XYZ bbCentroid = (elemBB.Max + elemBB.Min) / 2;
                            //add centroid points to list
                            points.Add(bbCentroid);
                        }

                        XYZ startPt = null;
                        XYZ endPt = null;
                        foreach (XYZ pt in points)
                        {
                            startPt = pt;
                            if (startPt != null && endPt != null)
                            {
                                Pipe pipe = Pipe.Create(doc, sysTypeId, pTypeId, slvlId, startPt, endPt);
                                endPt = pt;
                            }
                            else
                            {
                                endPt = pt;
                            }
                        }

                        tx.Commit();
                    }
                }
0 Likes
Reply
Accepted solutions (1)
679 Views
3 Replies
Replies (3)

mhannonQ65N2
Advocate
Advocate

It looks like you might want to create a Minimum Spanning Tree. You could try implementing this algorithm on the points and creating pipe for each edge in the resulting tree.

0 Likes

zrodgersTSSSU
Advocate
Advocate

I have read it and it seems that would be the same as using the distance to method. I just dont know how to structure the loops to iterate the points and compare them to eachother....

0 Likes

zrodgersTSSSU
Advocate
Advocate
Accepted solution

Here is the solution that worked for me for anyone who needs it in the future!!

 

 

                        foreach (XYZ pt in points)
                        {
                            XYZ closestPoint = null;
                            foreach (XYZ point2 in points) //loop to find closest point
                            {
                                if(pt != point2)
                                {
                                    if (closestPoint == null)
                                    {
                                        closestPoint = point2;
                                    }
                                    if (pt.DistanceTo(point2) < pt.DistanceTo(closestPoint))
                                    {
                                        closestPoint = point2;
                                    }

                                }
                                
                            }
//create pipe here
}

 

 

 

0 Likes