Hide Table of Contents

Tessellate a Body Example (C#)

//--------------------------------------------------

// This example shows how to tessellate a body.

//

// Preconditions:

//     (1) The specified document exists.

//     (2) Ensure that the namespace matches your C# project name.

//

// Postconditions:

//     (1) Part body is tessellated.

//     (2) Inspect the Immediate Window.

//

//--------------------------------------------------

using System;

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System.Diagnostics;

namespace TessellateABody_CSharp.csproj

{

    public partial class SolidWorksMacro

    {

        public SldWorks swApp;

        public void Main()

        {

            int errors = 0;

            int warnings = 0;

            // Get the active document

            ModelDoc2 swModel = swApp.OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\drive shaft plate.sldprt", (int)swDocumentTypes_e.swDocPART, 0, "", ref errors, ref warnings);

            // Get the document title

            Debug.Print(swModel.GetTitle());

            // Cast down to a part

            PartDoc swPart = (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)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

            Body2 swBody;

            for (int b = 0; b < iNumBodies; b++)

            {

                // Get a body and apply cast

                swBody = (Body2)aBodies.GetValue(b);

                // Now call a method on a body

                int nBodyType = swBody.GetType();

                if (nBodyType == (int)swBodyType_e.swSheetBody)

                {

                    iNumSheetBodies++;

                }

                if (nBodyType == (int)swBodyType_e.swSolidBody)

                {

                    iNumSolidBodies++;

                    Face2 swFace = null;

                    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

                    swTessellation.NeedFaceFacetMap = true;

                    swTessellation.NeedVertexParams = true;

                    swTessellation.NeedVertexNormal = true;

                    swTessellation.ImprovedQuality = true;

                    Debug.Print("Tessellation is configured for higher-quality data output.");

                    // How to handle matches across common edges

                    swTessellation.MatchType = (int)swTesselationMatchType_e.swTesselationMatchFacetTopology;

                    // Do it

                    bResult = swTessellation.Tessellate();

                    // Get the number of vertices and facets

                    Debug.Print("Number of vertices: " + swTessellation.GetVertexCount());

                    Debug.Print("Number of facets: " + 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);

                    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;

        }

    }

}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Tessellate a Body Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2013 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.