2 Replies Latest reply on May 24, 2016 2:02 AM by Alone A.

    Error while Converting VBA code into C#

    Alone A.

      Hi Everyone

      I am getting bunch of error while converting VBA code into C# code for face selection

      I am stuck with the error like nullReferenceException

      plz help me to convert the code in C#

      I am using solidworks 2015 version. Which libraries I need to import in VS for stand Alone Application.

       

      VBA Code

      **************************************************************************************************

      Sub main()

       

          Dim swApp               As SldWorks.SldWorks

          Dim swModel             As SldWorks.ModelDoc2

          Dim swSelMgr            As SldWorks.SelectionMgr

          Dim swSelData           As SldWorks.SelectData

          Dim swPart              As SldWorks.PartDoc

          Dim swBody              As SldWorks.Body2

          Dim swFace              As SldWorks.Face

          Dim swEnt               As SldWorks.Entity

          Dim bRet                As Boolean

          Dim vBodies             As Variant

         

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

          Set swPart = swModel

          Set swSelMgr = swModel.SelectionManager

          Set swSelData = swSelMgr.CreateSelectData

          vBodies = swPart.GetBodies2(swAllBodies, True)

          Set swBody = vBodies(0)

         

          Set swFace = swBody.GetFirstFace

          swModel.ClearSelection2 True

         

          Do While Not swFace Is Nothing

              Set swEnt = swFace

              ' Select through Entity

              bRet = swEnt.Select4(True, swSelData): Debug.Assert bRet

              Set swFace = swFace.GetNextFace

          Loop

      swModel.ClearSelection2 True

       

      End Sub

      ******************************************************************************************************************

        • Re: Error while Converting VBA code into C#
          Artem Taturevych

          Hi,

           

          Here is the C# equivalent developed in VSTA environment:

           

          using SolidWorks.Interop.sldworks;
          using SolidWorks.Interop.swconst;
          using System.Runtime.InteropServices;
          using System;
          
          
          namespace CS.csproj
          {
              public partial class SolidWorksMacro
              {
                  public void Main()
                  {
                      IModelDoc2 model = swApp.IActiveDoc2;
          
                      if (model is IPartDoc)
                      {
                          SelectData selData = model.ISelectionManager.CreateSelectData();
          
                          object[] bodies = (model as IPartDoc).GetBodies2(
                              (int)swBodyType_e.swAllBodies, true) as object[];
                          
                          if (bodies != null && bodies.Length > 0) 
                          {
                              IBody2 body = bodies[0] as IBody2;
          
                              model.ClearSelection2(true);
          
                              IFace2 face = body.GetFirstFace() as IFace2;
          
                              while (face != null) 
                              {
                                  (face as IEntity).Select4(true, selData);
                                  face = face.GetNextFace() as IFace2;
                              }
                          }
                      }
                  }
          
                  public SldWorks swApp;
              }
          }