2 Replies Latest reply on Feb 2, 2016 8:57 PM by Jason Taylor

    C# Null Reference - Solidworks Dimension

    Jason Taylor

      I am trying to create parts by modifying dimensions on a template part that I have created. I attempted to modify the code from the SolidWorks help website for modifying dimensions with C#. However, I am getting a Null Reference exception when it gets to the line that edits the dimension. Here is the help example I based my code off of: 2013 SOLIDWORKS API Help - Change Dimension Example (C#)

       

      Here is an example of the code I'm trying to use:

              static public void ColdForm()

              {

                  var swApp = new SldWorks.SldWorks();

                  swApp.Visible = true;

       

                  ModelDoc2 swModel;

                  Feature swFeature;

                  SelectionMgr swSelMgr;

                  Dimension swDim;

                  bool boolstatus;

                  string filename;

       

                  filename = "C:\\Users\\Jason\\Documents\\Coldform Templates\\10x25z14.sldprt";

                  swModel = swApp.OpenDoc(filename, (int)swDocumentTypes_e.swDocPART);

       

                  boolstatus = swModel.Extension.SelectByID2("Hole1Sketch", "SKETCH", 0, 0, 0, false, 0, null, 0);

                  swModel.EditSketch();

                  swModel.ClearSelection2(true);

                  boolstatus = swModel.Extension.SelectByID2("HoleDia1@Hole1Sketch", "DIMENSION",

                                      6.02225734887301, 0.182771494811241, -6.62411254208879E-02, false, 0, null, 0);

       

                  swSelMgr = (SelectionMgr)swModel.SelectionManager;

                  swFeature = (Feature)swSelMgr.GetSelectedObject6(0, -1);

       

                  swDim = (Dimension)swFeature.Parameter("HoleDia1@Hole1Sketch"); 

                     \\^This is the line VS highlights when it throws the error - specifically (swDim)

                  swDim.SetSystemValue2(.01905, (int)swSetValueInConfiguration_e.swSetValue_InThisConfiguration);

       

                  swModel.EditRebuild3();

              }

       

      Full Error message:

      An unhandled exception of type 'System.NullReferenceException' occurred

      Additional information: Object reference not set to an instance of an object.

       

      I feel like the code is almost an exact copy of the code listed in the help example. Can anyone let me know what I'm doing wrong?

       

      Thanks!

        • Re: C# Null Reference - Solidworks Dimension
          Artem Taturevych

          swFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);

           

          Selection index is 1-based. Make also sure that your feature is selected and dimension name is correct.

           

          You can also replace this with:

           

          swDim = (Dimension)swModel.Parameter("HoleDia1@Hole1Sketch");

           

          and no need to select the feature because the name of the parameter already includes feature name

           

          Thanks,

          Artem

            • Re: C# Null Reference - Solidworks Dimension
              Jason Taylor

              Thank you for your help, still got the error when I changed the index to 1. However, I tried 2 and it worked. It doesn't make sense to me at the moment, because I was thinking that since I had the line "swModel.ClearSelection2(true);" in there, there would be only 1 selection. Nevertheless, the code is doing what I want it to do, so I won't complain. Thanks for your help in pointing me to the selection index!