public static void ClassifyFace(Face2 face, ref List <MyPlane> listOfMyPlane, ref List <MySphere> listOfMySphere, ref List <MyCone> listOfMyCone, ref List <MyCylinder> listOfMyCylinder, ref List <MyTorus> listOfMyTorus) { var faceSurface = (Surface)face.GetSurface(); if (faceSurface.IsPlane()) { var newMyPlane = CreateMyPlaneFromFace(face); listOfMyPlane.Add(newMyPlane); } if (faceSurface.IsSphere()) { var newMySphere = CreateMySphereFromFace(faceSurface); listOfMySphere.Add(newMySphere); } if (faceSurface.IsCone()) { var newMyCone = CreateMyConeFromFace(faceSurface); listOfMyCone.Add(newMyCone); } if (faceSurface.IsCylinder()) { var newMyCylinder = CreateMyCylinderFromFace(faceSurface, face); listOfMyCylinder.Add(newMyCylinder); } if (faceSurface.IsTorus()) { var newMyTorus = CreateMyTorusFromFace(faceSurface); listOfMyTorus.Add(newMyTorus); } }
/// <summary> /// 创建面对象 /// </summary> /// <param name="face"></param> /// <returns></returns> private static SWFace CreateSWFace(Face2 face) { if (face == null) { return(null); } //复制Face信息 SWFace swFace = new SWFace(); //ID swFace.ID = face.GetFaceId(); //包围盒 swFace.BoundingBox = CreateSWBoundingBox(face.GetBox()); //三角面片 //int tessTriCount = face.GetTessTriangleCount(); swFace.TessTriangles = face.GetTessTriangles(false); swFace.TessNormals = face.GetTessNorms(); //OutPutTessTriangles(tessTriCount, arrTriangles); /* * if (arrTriangles != null) { * for (int i = 0; i < tessTriCount; i += 9) { * swFace.TessTriangles.Add(CreateSWTriangle(arrTriangles, i)); * } * } */ //几何信息 swFace.Surface = CreateSWSurface(face.GetSurface()); return(swFace); }
/// <summary> /// The my get normal for plane face. /// </summary> /// <param name="firstFace"> /// The first face. /// </param> /// <param name="firstPoint"> /// The first point. /// </param> /// <returns> /// The <see cref="double[]"/>. /// </returns> public static double[] MyGetNormalForPlaneFace(Face2 firstFace, out double[] firstPoint) { var firstSurface = (Surface)firstFace.GetSurface(); var firstNormal = new double[3]; firstPoint = new double[3]; Array firstValuesPlane = null; if (firstSurface.IsPlane()) { firstValuesPlane = firstSurface.PlaneParams; Array.Copy(firstValuesPlane, 0, firstNormal, 0, 3); Array.Copy(firstValuesPlane, 3, firstPoint, 0, 3); } // Pongo il verso della normale alla superficie concorde con quello della faccia. if (!firstFace.FaceInSurfaceSense()) { firstNormal.SetValue((double)firstValuesPlane.GetValue(0), 0); firstNormal.SetValue((double)firstValuesPlane.GetValue(1), 1); firstNormal.SetValue((double)firstValuesPlane.GetValue(2), 2); } else { firstNormal.SetValue(-(double)firstValuesPlane.GetValue(0), 0); firstNormal.SetValue(-(double)firstValuesPlane.GetValue(1), 1); firstNormal.SetValue(-(double)firstValuesPlane.GetValue(2), 2); } return(firstNormal); }
public FaceGeom(Face2 swface) { SwFace = swface; Surface = (Surface)SwFace.GetSurface(); ListeSwFace.Add(SwFace); switch ((swSurfaceTypes_e)Surface.Identity()) { case swSurfaceTypes_e.PLANE_TYPE: Type = eTypeFace.Plan; GetInfoPlan(); break; case swSurfaceTypes_e.CYLINDER_TYPE: Type = eTypeFace.Cylindre; GetInfoCylindre(); break; case swSurfaceTypes_e.EXTRU_TYPE: Type = eTypeFace.Extrusion; GetInfoExtrusion(); break; default: break; } }
public static double[] KLMyGetNormalForPlaneFace(Face2 firstFace, out double[] firstPoint, double[,] transformMatrix) { var firstSurface = (Surface)firstFace.GetSurface(); var firstNormal = new double[3]; firstPoint = new double[3]; Array firstValuesPlane = null; if (firstSurface.IsPlane()) { firstValuesPlane = firstSurface.PlaneParams; Array.Copy(firstValuesPlane, 0, firstNormal, 0, 3); Array.Copy(firstValuesPlane, 3, firstPoint, 0, 3); } // Pongo il verso della normale alla superficie concorde con quello della faccia. if (!firstFace.FaceInSurfaceSense()) { firstNormal.SetValue((double)firstValuesPlane.GetValue(0), 0); firstNormal.SetValue((double)firstValuesPlane.GetValue(1), 1); firstNormal.SetValue((double)firstValuesPlane.GetValue(2), 2); } else { firstNormal.SetValue(-(double)firstValuesPlane.GetValue(0), 0); firstNormal.SetValue(-(double)firstValuesPlane.GetValue(1), 1); firstNormal.SetValue(-(double)firstValuesPlane.GetValue(2), 2); } if (transformMatrix != null) { double[] firstNormalAffine = { (double)firstNormal.GetValue(0), (double)firstNormal.GetValue(1), (double)firstNormal.GetValue(2), 1 }; double[] firstPointAffine = { (double)firstPoint.GetValue(0), (double)firstPoint.GetValue(1), (double)firstPoint.GetValue(2), 1 }; var newFirstNormal = Matrix.Multiply(transformMatrix, firstNormalAffine); var newFirstPoint = Matrix.Multiply(transformMatrix, firstPointAffine); firstNormal.SetValue((double)newFirstNormal.GetValue(0), 0); firstNormal.SetValue((double)newFirstNormal.GetValue(1), 1); firstNormal.SetValue((double)newFirstNormal.GetValue(2), 2); firstPoint.SetValue((double)newFirstPoint.GetValue(0), 0); firstPoint.SetValue((double)newFirstPoint.GetValue(1), 1); firstPoint.SetValue((double)newFirstPoint.GetValue(2), 2); } return(firstNormal); }
// Try to see if the SolidWorks Body2 shape is a pure sphere. // If so,return true. If not a sphere, return false. // If it is a sphere, in 'radius' and 'center' one can get // the sphere data. public static bool SWbodyToSphere(Body2 swBody, ref double radius, ref Point3D center) { bool issphere = false; if (swBody.GetFaceCount() == 1) { object[] mfaces = (object[])swBody.GetFaces(); Face2 sphface = (Face2)mfaces[0]; Surface msurf = (Surface)sphface.GetSurface(); if (msurf.IsSphere()) { issphere = true; double[] sphpar = (double[])msurf.SphereParams; radius = sphpar[3]; center.X = sphpar[0]; center.Y = sphpar[1]; center.Z = sphpar[2]; } } return(issphere); }
public static Array MyNormalInPoint(Face2 face, double x, double y, double z) { var normal = new double[3]; var mySurf = (Surface)face.GetSurface(); double[] firstEvalutation = mySurf.EvaluateAtPoint(x, y, z); // --> EvaluateAtPoint restituisce la normale alla superficie in un punto! if (face.FaceInSurfaceSense()) { normal.SetValue((double)firstEvalutation.GetValue(0), 0); normal.SetValue((double)firstEvalutation.GetValue(1), 1); normal.SetValue((double)firstEvalutation.GetValue(2), 2); } else { normal.SetValue(-(double)firstEvalutation.GetValue(0), 0); normal.SetValue(-(double)firstEvalutation.GetValue(1), 1); normal.SetValue(-(double)firstEvalutation.GetValue(2), 2); } return(normal); }
// Try to see if the SolidWorks Body2 shape is a pure box. // If so,return true. If not a box, return false. // If it is a box, in 'corner' and 'Dx,Dy,Dx' one can get // the box main corner and the three departing edges. public static bool SWbodyToBox(Body2 swBody, ref Point3D corner, ref Vector3D Ex, ref Vector3D Ey, ref Vector3D Ez) { bool isbox = false; if (swBody.GetFaceCount() == 6) { if (swBody.GetEdgeCount() == 12) { if (swBody.GetVertexCount() == 8) { object[] mfaces = (object[])swBody.GetFaces(); object[] medges = (object[])swBody.GetEdges(); object[] mverts = (object[])swBody.GetVertices(); isbox = true; // rejective test 1: are all faces as planes? bool allplanes = true; for (int i = 0; i < 6; i++) { Face2 sphface = (Face2)mfaces[i]; Surface msurf = (Surface)sphface.GetSurface(); if (!msurf.IsPlane()) { allplanes = false; } } if (allplanes) { isbox = true; Vector3D[] pnts = new Vector3D[8]; for (int ip = 0; ip < 8; ip++) { pnts[ip] = new Vector3D(((double[])((Vertex)mverts[ip]).GetPoint())[0], ((double[])((Vertex)mverts[ip]).GetPoint())[1], ((double[])((Vertex)mverts[ip]).GetPoint())[2]); } Vector3D pC = pnts[0]; int X_id = 0; int Y_id = 0; int Z_id = 0; Vector3D pX = pnts[0]; Vector3D pY = pnts[0]; Vector3D pZ = pnts[0]; Vector3D Dx = (pX - pC); Vector3D Dy = (pY - pC); Vector3D Dz = (pZ - pC); // rejective test 1: are there 3 points that define an orthogonal trihedron with pC corner? bool found_corner = false; for (int xi = 1; xi < 8; xi++) { for (int yi = xi + 1; yi < 8; yi++) { for (int zi = yi + 1; zi < 8; zi++) { pX = pnts[xi]; pY = pnts[yi]; pZ = pnts[zi]; Dx = (pX - pC); Dy = (pY - pC); Dz = (pZ - pC); if ((Math.Abs(Vector3D.DotProduct(Dx, Dy)) < Dx.Length * (1e-5)) && (Math.Abs(Vector3D.DotProduct(Dy, Dz)) < Dx.Length * (1e-5)) && (Math.Abs(Vector3D.DotProduct(Dz, Dx)) < Dx.Length * (1e-5))) { X_id = xi; Y_id = yi; Z_id = zi; found_corner = true; break; } } if (found_corner) { break; } } if (found_corner) { break; } } if (!found_corner) { isbox = false; } double box_tol = Dx.Length * (1e-5); //System.Windows.Forms.MessageBox.Show(" Dx " + Dx + "\n" + " Dy " + Dy + "\n" + " Dz " + Dz); // rejective test 2: is there a point opposite to pC? Vector3D pE = pC + Dx + Dy; bool found_E = false; int E_id = 0; for (int ip = 1; ip < 8; ip++) { Vector3D ds = pE - pnts[ip]; if (ds.Length < box_tol) { found_E = true; E_id = ip; } } if (!found_E) { isbox = false; } // rejective test 3: are other four points aligned? Vector3D norm = Vector3D.CrossProduct(Dx, Dy); norm.Normalize(); Vector3D[] ptsA = new Vector3D[4]; bool[] aligned = new bool[4]; double[] dists = new double[4]; aligned[0] = aligned[1] = aligned[2] = aligned[3] = false; ptsA[0] = pC; ptsA[1] = pX; ptsA[2] = pY; ptsA[3] = pnts[E_id]; for (int iA = 0; iA < 4; iA++) { for (int ip = 1; ip < 8; ip++) { if ((ip != E_id) && (ip != X_id) && (ip != Y_id)) { Vector3D D = pnts[ip] - ptsA[iA]; if ((Vector3D.CrossProduct(D, norm)).Length < box_tol) { aligned[iA] = true; dists[iA] = Vector3D.DotProduct(D, norm); } } } } if ((aligned[0] != true) || (aligned[1] != true) || (aligned[2] != true) || (aligned[3] != true)) { isbox = false; } if ((Math.Abs(dists[0] - dists[1]) > box_tol) || (Math.Abs(dists[0] - dists[2]) > box_tol) || (Math.Abs(dists[0] - dists[3]) > box_tol)) { isbox = false; } // return geom.info if (isbox) { corner = new Point3D(pC.X, pC.Y, pC.Z); Ex = Dx; Ey = Dy; Ez = norm * dists[0]; } } } } } return(isbox); }
// Try to see if the SolidWorks Body2 shape is a pure convex hull. // If so,return true. If not a convex hull, return false. // If it is a convex hull, in 'vertexes' get all the points. public static bool SWbodyToConvexHull(Body2 swBody, ref Point3D[] vertexes, int maxvertexes) { bool ishull = false; if (swBody.GetVertexCount() <= maxvertexes) { object[] mfaces = (object[])swBody.GetFaces(); object[] medges = (object[])swBody.GetEdges(); object[] mverts = (object[])swBody.GetVertices(); ishull = true; // rejective test 1: are all faces as planes? bool allplanes = true; for (int i = 0; i < swBody.GetFaceCount(); i++) { Face2 aface = (Face2)mfaces[i]; Surface msurf = (Surface)aface.GetSurface(); if (!msurf.IsPlane()) { allplanes = false; } } if (!allplanes) { return(false); } // rejective test 2: are all edges as straight lines? bool allstraightedges = true; for (int i = 0; i < swBody.GetEdgeCount(); i++) { Edge aedge = (Edge)medges[i]; Curve mcurve = (Curve)aedge.GetCurve(); if (!mcurve.IsLine()) { allstraightedges = false; } } if (!allstraightedges) { return(false); } // rejective test 3: are there holes as in tori? // Use Euler formula v + f - e = 2 -2*genus if (swBody.GetVertexCount() + swBody.GetFaceCount() - swBody.GetEdgeCount() != 2) { return(false); } if (ishull) { vertexes = new Point3D[swBody.GetVertexCount()]; for (int ip = 0; ip < swBody.GetVertexCount(); ip++) { vertexes[ip] = new Point3D(((double[])((Vertex)mverts[ip]).GetPoint())[0], ((double[])((Vertex)mverts[ip]).GetPoint())[1], ((double[])((Vertex)mverts[ip]).GetPoint())[2]); } } } return(ishull); }
// Try to see if the SolidWorks Body2 shape is a pure cylinder. // If so,return true. If not a cylinder, return false. // If it is a cylinder, in 'P1' and 'P2' and 'radius' one can get // the two ends and the radius. public static bool SWbodyToCylinder(Body2 swBody, ref Point3D P1, ref Point3D P2, ref double radius) { bool iscyl = false; if (swBody.GetFaceCount() == 3) { if (swBody.GetEdgeCount() == 2) { object[] mfaces = (object[])swBody.GetFaces(); object[] medges = (object[])swBody.GetEdges(); object[] mverts = (object[])swBody.GetVertices(); int iplane = 0; Surface surf_cyl = null; Surface surf_planeA = null; Surface surf_planeB = null; for (int i = 0; i < 3; i++) { Face2 mface = (Face2)mfaces[i]; Surface msurf = (Surface)mface.GetSurface(); if (msurf.IsCylinder()) { surf_cyl = msurf; } if (msurf.IsPlane()) { if (iplane == 0) { surf_planeA = msurf; } if (iplane == 1) { surf_planeB = msurf; } iplane++; } if ((surf_cyl != null) && (surf_planeA != null) && (surf_planeB != null)) { iscyl = true; double[] cylpar = (double[])surf_cyl.CylinderParams; Vector3D cyl_C = new Vector3D(cylpar[0], cylpar[1], cylpar[2]); Vector3D cyl_D = new Vector3D(cylpar[3], cylpar[4], cylpar[5]); double cyl_rad = cylpar[6]; double cyl_tol = (1e-6); double[] pApar = (double[])surf_planeA.PlaneParams; Vector3D pA_N = new Vector3D(pApar[0], pApar[1], pApar[2]); Vector3D pA_C = new Vector3D(pApar[3], pApar[4], pApar[5]); double[] pBpar = (double[])surf_planeB.PlaneParams; Vector3D pB_N = new Vector3D(pBpar[0], pBpar[1], pBpar[2]); Vector3D pB_C = new Vector3D(pBpar[3], pBpar[4], pBpar[5]); // Rejective test 1: cyl axis & norms ofplanes are not aligned? if ((Vector3D.CrossProduct(cyl_D, pA_N)).Length > cyl_tol) { iscyl = false; } if ((Vector3D.CrossProduct(cyl_D, pB_N)).Length > cyl_tol) { iscyl = false; } // return geom.info if (iscyl) { radius = cyl_rad; P1 = (Point3D)(cyl_C + cyl_D * (Vector3D.DotProduct((pA_C - cyl_C), cyl_D))); P2 = (Point3D)(cyl_C + cyl_D * (Vector3D.DotProduct((pB_C - cyl_C), cyl_D))); } } else { iscyl = false; } } } } return(iscyl); }
public static bool samePlane(Face2 firstFace, Face2 secondFace, SldWorks swApp) { var firstSurf = (Surface)firstFace.GetSurface(); var secondSurf = (Surface)secondFace.GetSurface(); var firstParameters = (Array)firstSurf.PlaneParams; var secondParameters = (Array)secondSurf.PlaneParams; var firstNormal = new double[3]; var firstPoint = new double[3]; var secondNormal = new double[3]; var secondPoint = new double[3]; Array.Copy(firstParameters, 0, firstNormal, 0, 3); Array.Copy(firstParameters, 3, firstPoint, 0, 3); Array.Copy(secondParameters, 0, secondNormal, 0, 3); Array.Copy(secondParameters, 3, secondPoint, 0, 3); if (!firstFace.FaceInSurfaceSense()) { firstNormal.SetValue(-(double)firstNormal.GetValue(0), 0); firstNormal.SetValue(-(double)firstNormal.GetValue(1), 1); firstNormal.SetValue(-(double)firstNormal.GetValue(2), 2); } if (!secondFace.FaceInSurfaceSense()) { secondNormal.SetValue(-(double)secondNormal.GetValue(0), 0); secondNormal.SetValue(-(double)secondNormal.GetValue(1), 1); secondNormal.SetValue(-(double)secondNormal.GetValue(2), 2); } var results = Math.Abs(Matrix.InnerProduct(firstNormal, secondNormal) - 1); var normalPrint = String.Format("{0} {1} {2} --- {3} {4} {5} = {6}", firstNormal[0], firstNormal[1], firstNormal[2], secondNormal[0], secondNormal[1], secondNormal[2], results); var firstEquation = new double[4] { (double)firstNormal.GetValue(0), (double)firstNormal.GetValue(1), (double)firstNormal.GetValue(2), -(double)firstNormal.GetValue(0) * (double)firstPoint.GetValue(0) - (double)firstNormal.GetValue(1) * (double)firstPoint.GetValue(1) - (double)firstNormal.GetValue(2) * (double)firstPoint.GetValue(2), }; var secondEquation = new double[4] { (double)secondNormal.GetValue(0), (double)secondNormal.GetValue(1), (double)secondNormal.GetValue(2), -(double)secondNormal.GetValue(0) * (double)secondPoint.GetValue(0) - (double)secondNormal.GetValue(1) * (double)secondPoint.GetValue(1) - (double)secondNormal.GetValue(2) * (double)secondPoint.GetValue(2), }; var equationPrint = String.Format("Eq: {0}x {1}y {2}z = {3}", firstEquation[0], firstEquation[1], firstEquation[2], firstEquation[3]); /* * if (Math.Abs(Accord.Math.Matrix.InnerProduct(firstNormal, secondNormal) - 1) < 0.001) * { * swApp.SendMsgToUser("Normale uguale"); * return false; * } */ //return firstEquation.Equals(secondFace); if (Math.Abs(firstEquation[0] - secondEquation[0]) < 0.01 && Math.Abs(firstEquation[1] - secondEquation[1]) < 0.01 && Math.Abs(firstEquation[2] - secondEquation[2]) < 0.01 && Math.Abs(firstEquation[3] - secondEquation[3]) < 0.01) { swApp.SendMsgToUser("Equazione uguale"); 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"); } }
private Double DiametreMm(Face2 face) { Surface s = face.GetSurface(); return(DiametreMm(s)); }
/// <summary> /// 创建面对象 /// </summary> /// <param name="face"></param> /// <returns></returns> private static SWFace CreateSWFace(Face2 face) { if (face == null) return null; //复制Face信息 SWFace swFace = new SWFace(); //ID swFace.ID = face.GetFaceId(); //包围盒 swFace.BoundingBox = CreateSWBoundingBox(face.GetBox()); //三角面片 //int tessTriCount = face.GetTessTriangleCount(); swFace.TessTriangles = face.GetTessTriangles(false); swFace.TessNormals = face.GetTessNorms(); //OutPutTessTriangles(tessTriCount, arrTriangles); /* if (arrTriangles != null) { for (int i = 0; i < tessTriCount; i += 9) { swFace.TessTriangles.Add(CreateSWTriangle(arrTriangles, i)); } } */ //几何信息 swFace.Surface = CreateSWSurface(face.GetSurface()); return swFace; }