![]() |
Tessellate a Body Example (C#)
Tessellate a Body Example (C#)
This example shows how to tessellate a body. '-------------------------------------------------- ' ' Preconditions: Part is open and contains a body. ' ' Postconditions: Body is tessellated. ' '-------------------------------------------------- using System; using SldWorks; namespace ConsoleApplication1 { class Class1 { /// <summary> /// Main entry point for the application /// </summary> [STAThread] static void Main(string[] args) { // Connect to SolidWorks SldWorks.SldWorks swApp = new SldWorks.SldWorksClass(); // Get the active document SldWorks.ModelDoc2 swModel = swApp.IActiveDoc2; // Get the document title String title = swModel.GetTitle(); // Cast down to a part SldWorks.PartDoc swPart = (SldWorks.PartDoc)swModel; // Variant is returned, which can be dealt with as an object // Variant represents an array of references System.Object vBodies; System.Array aBodies; // Get bodies vBodies = swPart.GetBodies2((int)SwConst.swBodyType_e.swAllBodies, false); // Cast to an array of bodies aBodies = (System.Array)vBodies; int iNumBodies = aBodies.Length; int iNumSolidBodies = 0; int iNumSheetBodies = 0; // Loop through array SldWorks.Body2 swBody; for (int b = 0; b < iNumBodies; b++) { // Get a body and apply cast swBody = (SldWorks.Body2)aBodies.GetValue(b); // Now call a method on a body int nBodyType = swBody.GetType(); if (nBodyType == (int)SwConst.swBodyType_e.swSheetBody) { iNumSheetBodies++; } if (nBodyType == (int)SwConst.swBodyType_e.swSolidBody) { iNumSolidBodies++; SldWorks.Face2 swFace = null; SldWorks.Tessellation swTessellation = null; bool bResult = false; // Pass in null so the whole body will be tessellated swTessellation = (Tessellation)swBody.GetTessellation(null); // // Set up the Tessellation object // Required data swTessellation.NeedFaceFacetMap = true; swTessellation.NeedVertexParams = true; swTessellation.NeedVertexNormal = true; // How to handle matches across common edges swTessellation.MatchType = (int)SwConst.swTesselationMatchType_e.swTesselationMatchFacetTopology; // Do it bResult = swTessellation.Tessellate(); // Get the number of vertices and facets int iNumVertices = swTessellation.GetVertexCount(); int iNumFacets = swTessellation.GetFacetCount(); // Now get the facet data per face int[] aFacetIds; int iNumFacetIds; int[] aFinIds; int[] aVertexIds; double[] aVertexCoords1; double[] aVertexCoords2; // // Add sketch for this body // Speed things up swModel.SetAddToDB(true); swModel.SetDisplayWhenAdded(false); // Insert sketch swModel.Insert3DSketch2(false); // Loop over faces swFace = (Face2)swBody.GetFirstFace(); while (swFace != null) { aFacetIds = (int[])swTessellation.GetFaceFacets(swFace); iNumFacetIds = aFacetIds.Length; for (int iFacetIdIdx = 0; iFacetIdIdx < iNumFacetIds; iFacetIdIdx++) { aFinIds = (int[])swTessellation.GetFacetFins(aFacetIds[iFacetIdIdx]); // There should always be three fins per facet for (int iFinIdx= 0; iFinIdx < 3; iFinIdx++) { aVertexIds = (int[])swTessellation.GetFinVertices(aFinIds[iFinIdx]); // Should always be two vertices per fin aVertexCoords1 = (double[])swTessellation.GetVertexPoint(aVertexIds[0]); aVertexCoords2 = (double[])swTessellation.GetVertexPoint(aVertexIds[1]); // Create a line swModel.CreateLine2(aVertexCoords1[0], aVertexCoords1[1], aVertexCoords1[2], aVertexCoords2[0], aVertexCoords2[1], aVertexCoords2[2]); } } swFace = (Face2)swFace.GetNextFace(); } // Close sketch swModel.Insert3DSketch2(true); // Clear selection for next pass swModel.ClearSelection2(true); // Restore settings swModel.SetAddToDB (false); swModel.SetDisplayWhenAdded(true); } } return; } } } |
所有的时间均为北京时间。 现在的时间是 03:09 PM. |