public static bool hitMainBodyBefore(double[] rayOrigin, double[] rayDirection, double[] hitPointToCompareTo) { List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])mainBody.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(mainBody.Transform2); bodies.Add(tempBody); } int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)rayOrigin, (object)rayDirection, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS | swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double).0000001, (double).0000001); double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); if (numIntersectionsFound == 0) { return(false); } int lengthOfOneReturn = 9; for (int i = 0; i < numIntersectionsFound; i++) { byte intersectionType = (byte)horrifyingReturn[i * lengthOfOneReturn + 2]; if ((intersectionType & (byte)swRayPtsResults_e.swRayPtsResultsENTER) == 0) { // we need it to be just entry rays continue; } double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; if (distanceAlongRay(rayOrigin, new double[] { x, y, z }) < distanceAlongRay(rayOrigin, hitPointToCompareTo)) { return(true); } } return(false); }
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"); } }
//通过射线创建点,并计算距离 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 double distanceFromFlagToCamera() { if (Utilities.distanceBetween(belongsTo, camera.fieldOfView) < 0) { return(Utilities.distanceBetween(belongsTo, camera.fieldOfView)); } swFeatureMgr.EditRollback((int)swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, ""); swAssembly.EditRebuild(); IMathPoint centreOfFlagBase = Utilities.getNamedPoint("centre of flag base", belongsTo); IMathPoint centreOfFlagTip = Utilities.getNamedPoint("centre of flag top", belongsTo); double[] dataBase = centreOfFlagBase.ArrayData; double[] dataTip = centreOfFlagTip.ArrayData; double[] flagDir = new double[] { dataTip[0] - dataBase[0], dataTip[1] - dataBase[1], dataTip[2] - dataBase[2] }; List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])camera.fieldOfView.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(camera.fieldOfView.Transform2); bodies.Add(tempBody); } int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)dataTip, (object)flagDir, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS | swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double).0000001, (double).0000001); if (numIntersectionsFound == 0) { return(0); } double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); int lengthOfOneReturn = 9; double[] intersectionPoint = new double[3]; for (int i = 0; i < numIntersectionsFound; i++) { byte intersectionType = (byte)horrifyingReturn[i * lengthOfOneReturn + 2]; if ((intersectionType & (byte)swRayPtsResults_e.swRayPtsResultsENTER) == 0) { // we need it to be just entry rays continue; } double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; // check to see if the distance along the ray is shorter? we want the shortest distance! intersectionPoint[0] = x; intersectionPoint[1] = y; intersectionPoint[2] = z; break; } return(Utilities.distanceFormula(intersectionPoint, dataTip)); }