//通过射线创建点,并计算距离 public double CreatePoints() { double sumDis = 0; IBody2[] body = AllBodies.ToArray(); double[] basePoint = new double[3]; double[] vectorPoint = new double[3]; try { basePoint[0] = double.Parse(txtStart_X.Text) / 1000; basePoint[1] = double.Parse(txtStart_Y.Text) / 1000; basePoint[2] = double.Parse(txtStart_Z.Text) / 1000; vectorPoint[0] = double.Parse(txtVertor_X.Text) / 1000; vectorPoint[1] = double.Parse(txtVertor_Y.Text) / 1000; vectorPoint[2] = double.Parse(txtVertor_Z.Text) / 1000; } catch (Exception exception) { Console.WriteLine(exception); MessageBox.Show("请检查坐标输入!"); return(0); } double[] rayVectorOrigins = basePoint; //起点 double[] rayVectorDirections = vectorPoint; //方向 ModelDoc2 swDoc = ((ModelDoc2)(swApp.ActiveDoc)); int numIntersectionsFound = (int)swDoc.RayIntersections(body, (object)rayVectorOrigins, (object)rayVectorDirections, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS), (double).0000001, (double).0000001); double[] points = (double[])swDoc.GetRayIntersectionsPoints(); swDoc.GetRayIntersectionsTopology(); swDoc.SketchManager.Insert3DSketch(true); swDoc.SketchManager.AddToDB = true; int tempPointIndex = 1; double[] tempLineStartPoint = new double[3]; for (int i = 0; i < points.Length; i += 9) { double[] pt = new double[] { points[i + 3], points[i + 4], points[i + 5] }; swDoc.SketchManager.CreatePoint(pt[0], pt[1], pt[2]); if (points[2] == 1) { Debug.Print("this point is on face"); if (tempPointIndex % 2 == 1) { tempLineStartPoint = new double[3]; tempLineStartPoint[0] = pt[0] * 1000.0; tempLineStartPoint[1] = pt[1] * 1000.0; tempLineStartPoint[2] = pt[2] * 1000.0; } if (tempPointIndex % 2 == 0) { var tempLineEndPoint = new double[3]; tempLineEndPoint[0] = pt[0] * 1000.0; tempLineEndPoint[1] = pt[1] * 1000.0; tempLineEndPoint[2] = pt[2] * 1000.0; sumDis = sumDis + Vector3.Distance(new Vector3(tempLineStartPoint[0], tempLineStartPoint[1], tempLineStartPoint[2]), new Vector3(tempLineEndPoint[0], tempLineEndPoint[1], tempLineEndPoint[2])); } tempPointIndex++; } //if (points[2] == 4) //{ // Debug.Print("this point is on edge"); //} //if (points[2] == 16) //{ // Debug.Print("this point is enter"); //} //if (points[2] == 32) //{ // Debug.Print("this point is exit"); //} //if (points[2] == 8) //{ // Debug.Print("this point is on vertex"); //} //if (points[2] == 1) //{ // Debug.Print("this point is on face"); //} } swDoc.SketchManager.AddToDB = false; swDoc.SketchManager.Insert3DSketch(true); return(sumDis); }
public static void pointIntersection(double[] firstPoint, Face2 firstFace, double[] secondPoint, Face2 secondFace, IBody2[] body, ModelDoc2 myModel, SldWorks mySwApplication, ref int correctIntersection, ref int correctIntersectionForPair) { var firstSurface = (Surface)firstFace.GetSurface(); var secondSurface = (Surface)secondFace.GetSurface(); correctIntersection = 0; if (firstPoint != null && secondPoint != null) { var direction = (double[])MathFunctions.MyNormalization(MathFunctions.MyVectorDifferent(secondPoint, firstPoint)); var intersection = (int) myModel.RayIntersections( body, (object)firstPoint, (object)direction, (int)(swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double)0, (double)0); // Se sono dei presenti dei punti di intersezione li salvo in un apposito vettore. if (intersection > 0) { var points = (double[])myModel.GetRayIntersectionsPoints(); var totalEntity = (Array)myModel.GetRayIntersectionsTopology(); //mySwApplication.SendMsgToUser("punti totali " + points.Length.ToString() + "intersezioni " + intersection.ToString()); if (points != null) { for (int i = 0; i < points.Length; i += 9) { double[] pt = new double[] { points[i + 3], points[i + 4], points[i + 5] }; double[] directionIntersection = (double[])MathFunctions.MyNormalization(MathFunctions.MyVectorDifferent(pt, firstPoint)); if (Math.Abs(MathFunctions.MyInnerProduct(direction, directionIntersection) - 1) < 0.01 /*&& MyDistanceTwoPoint(pt, secondPoint) > 0.001 && MyDistanceTwoPoint(pt, firstPoint) > 0.001*/) { if (firstSurface.Identity() == 4001 && secondSurface.Identity() == 4001) { if (Distance.MyDistanceTwoPoint(firstPoint, secondPoint) > Distance.MyDistanceTwoPoint(firstPoint, pt)) { correctIntersection++; correctIntersectionForPair++; } } else if (firstSurface.Identity() == 4002 && secondSurface.Identity() == 4002) { double[] closestPointToIntersectionOnFirstSurface = firstSurface.GetClosestPointOn(pt[0], pt[1], pt[2]); double[] closestPointToIntersectionOnSecondSurface = secondSurface.GetClosestPointOn(pt[0], pt[1], pt[2]); //myModel.Insert3DSketch(); //myModel.CreatePoint2(pt[0], pt[1], pt[2]); if (Distance.MyDistanceTwoPoint(firstPoint, secondPoint) > Distance.MyDistanceTwoPoint(firstPoint, pt) && Math.Abs(Distance.MyDistanceTwoPoint(closestPointToIntersectionOnFirstSurface, pt)) > 0.0001 && Math.Abs(Distance.MyDistanceTwoPoint(closestPointToIntersectionOnSecondSurface, pt)) > 0.0001) { correctIntersection++; correctIntersectionForPair++; } else { var normalFirstFace = (double[])MyNormalInPoint(firstFace, closestPointToIntersectionOnFirstSurface[0], closestPointToIntersectionOnFirstSurface[1], closestPointToIntersectionOnFirstSurface[2]); double[] dir = (double[])MathFunctions.MyNormalization(MathFunctions.MyVectorDifferent(closestPointToIntersectionOnSecondSurface, closestPointToIntersectionOnFirstSurface)); } } } } } else { mySwApplication.SendMsgToUser("Punti intersezione nulli"); } } } else { mySwApplication.SendMsgToUser("Punti nulli"); } }