Hide Table of Contents

Get Distance Between Entities Example (C#)

This example shows how to get the minimum and maximum distances between face and edge entities.

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

// Preconditions:

// Open <SOLIDWORKS_install_dir>\samples\tutorial\multibody\multi_inter.sldprt.

//

// Postconditions:

// 1. Observe the Immediate Window.

//    The minimum distance between the two face entities is 0 mm.

//    The maximum distance between the face and edge entity is

//    96.6801751904066 mm.

// 2. Click Sketch4 in the FeatureManager design tree.

//    A sketch line is created to depict the maximum distance between the

//    face and edge entities.

//

// NOTE: Do not save the part as it is used in SOLIDWORKS tutorials.

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

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Diagnostics;

namespace GetDistanceBetweenEntities_CSharp.csproj

{

    partial class SolidWorksMacro

    {

        ModelDoc2 swDoc;

        SelectionMgr swSM;

        Face2 swFace;

        Edge swEdge;

        Entity swTop1;

        Entity swTop2;

        bool bMin;

        long retval;

        double dist;

        double[] varParam;

        object varPos1;

        object varPos2;

        double[] Pos1 = new double[3];

        double[] Pos2 = new double[3];

        int caseType;

        bool boolstatus;

        public void Main()

        {

            varParam = null;

            swDoc = (ModelDoc2)swApp.ActiveDoc;

            swSM = (SelectionMgr)swDoc.SelectionManager;

            for (caseType = 1; caseType <= 2; caseType++)

            {

                switch (caseType)

                {

                    case 1:

                        FaceFaceMinDistance();

                        break;

                    case 2:

                        FaceEdgeMaxDistance();

                        break;

                    default:

                        System.Windows.Forms.MessageBox.Show("Enter proper case");

                        break;

                }

            }

        }

        public void SetParameterForEdge()

        {

            double[] startPt = new double[3]; ;

            double[] endPt = new double[3]; ;

            double[] startPara = new double[2]; ;

            double[] endPara = new double[2]; ;

            swEdge = (Edge)swSM.GetSelectedObject6(2, -1);

            startPt = (double[])((Vertex)swEdge.GetStartVertex()).GetPoint();

            endPt = (double[])((Vertex)swEdge.GetEndVertex()).GetPoint();

            startPara = (double[])swEdge.GetParameter(startPt[0], startPt[1], startPt[2]);

            endPara = (double[])swEdge.GetParameter(endPt[0], endPt[1], endPt[2]);

            double[] paramDl = new double[2];

            paramDl[0] = startPara[0];

            paramDl[1] = endPara[0];

            varParam = paramDl;

        }

        public void SetParameterForFace()

        {

            swFace = (Face2)swSM.GetSelectedObject6(2, -1);

            Surface swSurface = default(Surface);

            swSurface = (Surface)swFace.GetSurface();

            double[] varBox = new double[6];

            varBox = (double[])swFace.GetBox();

            double[] varLowParam = new double[2];

            double[] varHighParam = new double[2];

            varLowParam = (double[])swSurface.ReverseEvaluate(varBox[0], varBox[1], varBox[2]);

            varHighParam = (double[])swSurface.ReverseEvaluate(varBox[3], varBox[4], varBox[5]);

            double[] paramD2 = new double[4];

            paramD2[0] = varLowParam[0];

            paramD2[1] = varLowParam[1];

            paramD2[2] = varHighParam[0];

            paramD2[3] = varHighParam[1];

            varParam = paramD2;

        }

        public void CreateLine()

        {

            SketchManager swSkM = default(SketchManager);

            SketchSegment skSegment = default(SketchSegment);

            swSkM = swDoc.SketchManager;

            swDoc.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, false, 0, null, 0);

            swSkM.InsertSketch(true);

            skSegment = swSkM.CreateLine(Pos1[0], Pos1[1], Pos1[2], Pos2[0], Pos2[1], Pos2[2]);

            swDoc.ClearSelection2(true);

            swSkM.InsertSketch(true);

        }

        public void FaceFaceMinDistance()

        {

            swDoc.ClearSelection();

            boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.07448317477082, -0.04390354307787, 0.08443247713632, false, 0, null, 0);

            boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.05640517674067, -0.005486808589922, 0.06500000000005, true, 0, null, 0);

            SetParameterForFace();

            if ((swSM.GetSelectedObjectCount() == 2))

            {

                swTop1 = (Entity)swSM.GetSelectedObject6(1, -1);

                swTop2 = (Entity)swSM.GetSelectedObject6(2, -1);

                bMin = true;

                retval = swTop1.GetDistance(swTop2, bMin, varParam, out varPos1, out varPos2, out dist);

                Pos1 = (double[])varPos1;

                Pos2 = (double[])varPos2;

                Debug.Print("GetDistance return value (0 = success) : " + retval);

                Debug.Print("Face1 coordinate: " + Pos1[0] + "," + Pos1[1] + "," + Pos1[2]);

                Debug.Print("Face2 coordinate: " + Pos2[0] + "," + Pos2[1] + "," + Pos2[2]);

                Debug.Print("Minimum Distance between two faces = " + dist * 1000 + " mm");

                Debug.Print("");

                CreateLine();

            }

        }

        public void FaceEdgeMaxDistance()

        {

            swDoc.ClearSelection();

            boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.0712080569869, -0.04996892464504, 0.08163440548356, false, 0, null, 0);

            boolstatus = swDoc.Extension.SelectByID2("", "EDGE", -0.04898677039967, 0.0004196506486664, 0.06476403488529, true, 0, null, 0);

            SetParameterForEdge();

            if ((swSM.GetSelectedObjectCount() == 2))

            {

                swTop1 = (Entity)swSM.GetSelectedObject6(1, -1);

                swTop2 = (Entity)swSM.GetSelectedObject6(2, -1);

                bMin = false;

                retval = swTop1.GetDistance(swTop2, bMin, varParam, out varPos1, out varPos2, out dist);

                Pos1 = (double[])varPos1;

                Pos2 = (double[])varPos2;

                Debug.Print("GetDistance return value (0 = success) : " + retval);

                Debug.Print("Face coordinate: " + Pos1[0] + "," + Pos1[1] + "," + Pos1[2]);

                Debug.Print("Edge coordinate: " + Pos2[0] + "," + Pos2[1] + "," + Pos2[2]);

                Debug.Print("Maximum Distance between face and edge = " + dist * 1000 + " mm");

                Debug.Print("");

                CreateLine();

            }

        }

        public SldWorks swApp;

    }

}



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:   Get Distance Between Entities 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) 2015 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.