[VBA/C# CE] Calculate Intersection Points between two Lines in term of XY with VBA COM

This code was written few years ago, and nowaday it is still found very useful to VBA/C# because Bentley provides no function for that.

Function GetIntersectionPoints can return you a intersection point but when you rotate the view, the output will be changed. (unless you know how to fix the Matrix3d)

Function Ray3dRay3dIntersectXY will return you a Boolean, but from the VBA Help it is hard for us to understand how to get out the intersection point.

So I copied the code to here for easy reference. (not sure if there any bug for you guys)

GetIntersectionPoints

      public static Point3d GetLineIntersectXY1(Point3d Line1_Point1, Point3d Line1_Point2, Point3d Line2_Point1, Point3d Line2_Point2)
        {
            //Equation of Line : y = mx + c
            Application app = Bentley.MicroStation.InteropServices.Utilities.ComApp;
            Point3d result = app.Point3dFromXYZ(0, 0, 0);
            double m1 = 0;
            double m2 = 0;
            double c1 = 0;
            double c2 = 0;
            double Temp = 0;
            
            if ((Line1_Point2.X - Line1_Point1.X != 0) && (Line2_Point2.X - Line2_Point1.X != 0))
            {
                m1 = (Line1_Point2.Y - Line1_Point1.Y) / (Line1_Point2.X - Line1_Point1.X);
                m2 = (Line2_Point2.Y - Line2_Point1.Y) / (Line2_Point2.X - Line2_Point1.X);
                c1 = Line1_Point1.Y - (m1 * Line1_Point1.X);
                c2 = Line2_Point1.Y - (m2 * Line2_Point1.X);

                if (0 == m2) //Cannot divide a value by zero in Mathematics
                {
                    Temp = m1;
                    m1 = m2;
                    m2 = Temp;
                    Temp = c1;
                    c1 = c2;
                    c2 = Temp;
                }

                result.X = (c1 - c2) / (m2 - m1);
                result.Y = (m1 * c2 / m2 - c1) / (m1 / m2 - 1);
                result.Z = 0;
            }
            else if ((Line1_Point2.X - Line1_Point1.X == 0) && (Line2_Point2.X - Line2_Point1.X != 0))
            {
                m2 = (Line2_Point2.Y - Line2_Point1.Y) / (Line2_Point2.X - Line2_Point1.X);
                c2 = Line2_Point1.Y - (m2 * Line2_Point1.X);

                result.X = Line1_Point1.X;
                result.Y = (m2 * Line1_Point1.X) + c2;
                result.Z = 0;
            }
            else if ((Line1_Point2.X - Line1_Point1.X != 0) && (Line2_Point2.X - Line2_Point1.X == 0))
            {
                m1 = (Line1_Point2.Y - Line1_Point1.Y) / (Line1_Point2.X - Line1_Point1.X);
                c1 = Line1_Point1.Y - (m1 * Line1_Point1.X);

                result.X = Line2_Point1.X;
                result.Y = (m1 * Line2_Point1.X) + c1;
                result.Z = 0;
            }
            else
            {
                //System.Windows.Forms.MessageBox.Show("Error!");
            }
            return result;
        }

        public static Point3d GetLineIntersectXY2(bool IsVertical, double X, double m1, double c1, double m2 = 0, double c2 = 0)
        {
            //y = mx + c
            Application app = Bentley.MicroStation.InteropServices.Utilities.ComApp;
            Point3d result = app.Point3dFromXYZ(0, 0, 0);
            if (IsVertical == false)
            {
                if ((m2 - m1 != 0) && (m2 - c1 != 0) && (m1 / m2 - 1 != 0))
                {
                    result.X = (c1 - c2) / (m2 - m1);
                    if (m2 != 0)
                    {
                        result.Y = ((m1 * c2 / m2) - c1) / ((m1 / m2) - 1);
                    }
                    else
                    {
                        //  y = (c2 - (m2 / m1 * c1)) / (1 - (m2 / m1));
                        result.Y = c2; 
                    }
                }
            }
            else //Intersecting with a vertical line
            {
                result.X = X;
                result.Y = (m1 * X) + c1;
            }
            return result;
        }

Parents
  • Bentley provides no function for that

    No function for what?  There are plenty of geometric functions for .NET with MicroStation CONNECT.  Unfortunately, the help documents are split between five different files.  Take a look at help files BentleyGeometryNET and BentleyGeometryNET.structs.

    With C++ and .NET, geometrical calculations can be abstracted from explicit DGN elements.  Many elements, for example, can be decomposed to CurveVectors or CurvePrimitives.

    If you're still using VBA for geometric calculations then you need to examine the .NET API in detail.  Most of the time, .NET classes are wrappers around C++.  As Jan often recommends, start with C++ MicroStationAPI help, which is a single document and therefore easier to navigate and search than the fragmented .NET help documentation.  Usually, the C++ help provides more information than the .NET help.

     
    Regards, Jon Summers
    LA Solutions

  • Actually, there were only Point3d belong to VBA COM. Anyway, just use it if you want to keep codes clean, lol..... That was useful to design lines of hand railing which required massive lines intersections. 

  • There was only Point3d belong to VBA COM

    Point3d is the VBA name for DPoint3d available in both C++ and .NET.

     
    Regards, Jon Summers
    LA Solutions

Reply Children
No Data