Ouedraogo Ismael
Ouedraogo Ismael

Reputation: 11

Finding hole in Solidworks parts

I'm trying to create an application in C# using Solidworks API that can help me to detect holes in SolidWorks parts. I'm very new to C# and this is like a new experiment.

So far I wrote a code that does that but it's doesn't do completely what I want him to do. It actually selects all cylindrical parts and not only the holes.

Also, is there a way using C# to make the difference between a hole ( complete circular edges) and a hole ( not complete circular edges or a hole cut at the edges of a body )

Here is my code and I'll try to post some pictures too to make it clear.

namespace ColorCylFaces
{
    class Program
    {
        static void Main(string[] args)
        {
            SldWorks.SldWorks mySwApp;
            ModelDoc2 swDoc;
            Surface swSurface;
            PartDoc swPart;
            object[] faces;
            Face2[] swFaces;
            object[] bodies;
            Body2[] swBodies;
            Boolean boolResult;
            double[] EvalData = new double[3];

            double[] dPt = new double[3];

            double[] dNormVec = new double[3];

            var vCylParams = new double[3];

            object[] Orgn = new object[3];

            object[] reyvec = new object[3];

            Object[] DirVec = new object[3];

            var Bounds = new double[3];

            bool[] intersc = new bool[11];

            int arr;
            try
            {
                mySwApp = new SldWorks.SldWorks();
            }
            catch (Exception)
            {
                MessageBox.Show("La connexion à Solidworks a échoué!");
                return;
                throw;
            }
            swDoc = mySwApp.ActiveDoc;
            if (swDoc == null)
            {
                MessageBox.Show("La prochaine fois, ouvrez une pièce!");
                return;
            }
            Debug.Print("Nom du fichier: " + swDoc.GetPathName());
            if (swDoc.GetType() != (int)swDocumentTypes_e.swDocPART)
            {
                MessageBox.Show("Le document actif n'est pas un Part. \n La prochaine fois, ouvrez une pièce, rien d'autre!");
                return;
            }
            swPart = (PartDoc)swDoc;
            bodies = swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);

            List<IBody2> bodyList = new List<IBody2>();
            foreach (IBody2 item in bodies)
            {
                bodyList.Add(item);
            }

                foreach (Body2 swBody in bodies)
            {
                faces = swBody.GetFaces();
               
                foreach (Face2 swFace in faces)
                {
                    swSurface = swFace.GetSurface();
                    boolResult = swSurface.IsCylinder();
                    if (boolResult == true)
                    {
                        vCylParams = swSurface.CylinderParams;

                        Orgn[0] = vCylParams[0];

                        Orgn[1] = vCylParams[1];

                        Orgn[2] = vCylParams[2];

                        reyvec[0] = Math.Abs(Math.Abs(vCylParams[3]) - 1);

                        reyvec[1] = Math.Abs(Math.Abs(vCylParams[4]) - 1);

                        reyvec[2] = Math.Abs(Math.Abs(vCylParams[5]) - 1);

                        arr = swDoc.RayIntersections(

                            bodyList.ToArray(),

                            (object)Orgn,

                            (object)reyvec,

                            (int)swRayPtsOpts_e.swRayPtsOptsTOPOLS,

                            0.1,

                            1);

                        double[] colorInfo = swFace.MaterialPropertyValues;
                        if (colorInfo == null)
                        {

                            colorInfo = swDoc.MaterialPropertyValues;
                        }
                        colorInfo[0] = 1.0;
                        colorInfo[1] = 1.0;
                        colorInfo[2] = 1.0;
                        swFace.MaterialPropertyValues = colorInfo;
                    }
                }
            }
        }
    }
}

Upvotes: 1

Views: 1015

Answers (1)

Sinue
Sinue

Reputation: 395

I found an article on Codestack related to this topic. There is an example on how to differentiate between an actual hole and a boss. You can find it here: https://www.codestack.net/solidworks-api/geometry/determine-hole-boss/

An other solution I can think of could be reading all hole wizard features by traversing the features inside the feature-tree. But this would only find holes created with the hole wizard.

Upvotes: 2

Related Questions