AnsweredAssumed Answered

C# Null Reference - Solidworks Dimension

Question asked by Jason Taylor on Feb 2, 2016
Latest reply on Feb 2, 2016 by 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!

Outcomes