April 12, 2016

SheetMetalFolder2 extension of SheetMetalFolder for those who want to change thickness or radius

For people who understood the purpose of this article from the headline, here you go GitHub 


Don't you just hate it when the bug is in the API and not your code? It's the last place you'd think is responsible for the error, right?

For the past few days, I was trying to change the thickness from the SheetMetalFolder interface (NOT the SheetMetalFeature) as part of a new feature in my PowerSM add-in. Apparently, when you get the SheetMetalFeatureData from a SheetMetalFolder, applying modification to the folder feature doesn't seem to result in anything even if the ModifyDefinition method returns true.

After staring cluelessly at my computer's screen for hours, I decided to it to the SolidWorks forum where my chances of solving question are a little better. After posting the question, people were quick to post their answers. Few answers in, a SolidWorks Guru, Deepak Gupta, check his blog here, posted that changing the thickness from this question is an SPR, which in SolidWorks gibber gabber means something like the API people are working on it.

As a final attempt, I took to LinkedIn API education group where a gentleman by the name of Artem, instructed me to a method that does exactly what I want.

I wrote the following extension of the ISheetMetalFolder (You can find the code at GitHub. The link is above) that uses a decorator patterned constructor based on the SheetMetalFolder feature. The premise is simple: A public method called Thickness changes the thickness of the Sheet Metal Folder using dimensions as contrary to the convention method that uses SheetMetalFeatureData. A similar method does the same thing for the bend radius.

Warning: Before you can get a feature's IDisplayDimension, use IModelDoc2::SetUserPreferenceToggle and swDisplayFeatureDimensions to display them.

Below is the code for the class and below is a demo:

using System;
using System.Collections.Generic;
using System.Text;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace SheetMetalFolder2
{
    public class SheetMetalFolder2
    {
        private SheetMetalFolder swSheetMetalFolder;
        private Feature swFeat;
        public SheetMetalFolder2(SheetMetalFolder SheetMetalFolder) 
        
        {
            if (SheetMetalFolder != null) 
            {
                swSheetMetalFolder = SheetMetalFolder;
                swFeat = (Feature)swSheetMetalFolder.GetFeature();
               
            }
            else
            {
                throw new NullReferenceException("Constructor has a null parameter.");
            }
        }

        // Thickness is in mm
        public bool Thickness(double Thickness) 
        
        {
            DisplayDimension swDisplayDimension = (DisplayDimension)swFeat.GetFirstDisplayDimension();
            while (swDisplayDimension != null) 
            {
                // the display dimension of the thickness is not radial or diametric
                if (swDisplayDimension.GetType() != (int)swDimensionType_e.swRadialDimension && swDisplayDimension.GetType() != (int)swDimensionType_e.swDiameterDimension) 
                {
                    Dimension swDimension = (Dimension)swDisplayDimension.GetDimension();
                    // thickness dimension has reference points and a direction
                    if (swDimension.ReferencePoints != null) 
                    {
                        
                        object[] swMathPointsObj = (object[])swDimension.ReferencePoints;
                        foreach (object swMathPointObj in swMathPointsObj)
                        {
                            MathPoint swMathPoint = (MathPoint)swMathPointObj;
                            if (!IsPointZero(swMathPoint)) 
                            {
                               
                               int retval = swDimension.SetSystemValue2(Thickness / 1000.0, 0);
                               if (retval == 0)
                                   return true;
                               return false;
                               
                               
                                
                            }
                        }                     
                    }
                }
                
                // Get next dimension
                swDisplayDimension = (DisplayDimension)swFeat.GetNextDisplayDimension(swDisplayDimension);
            }
            return false;
        }
        // BendRadius is in mm
        public bool BendRadius(double BendRadius)
        {
            DisplayDimension swDisplayDimension = (DisplayDimension)swFeat.GetFirstDisplayDimension();
            while (swDisplayDimension != null)
            {
                // the radius dimension is radial or diametric
                if (swDisplayDimension.GetType() == (int)swDimensionType_e.swRadialDimension || swDisplayDimension.GetType() == (int)swDimensionType_e.swDiameterDimension)
                {
                    Dimension swDimension = (Dimension)swDisplayDimension.GetDimension();

                    int retval = swDimension.SetSystemValue2(BendRadius / 1000.0, 0);
                                if (retval == 0)
                                    return true;
                                return false; 
                }

                // Get next dimension
                swDisplayDimension = (DisplayDimension)swFeat.GetNextDisplayDimension(swDisplayDimension);
            }
            return false;
        }
        
       private bool IsPointZero(MathPoint mathpoint) 
       {
               double sum = 0;
               Double[] DoubleArray = (Double[])mathpoint.ArrayData;
               foreach (double d in DoubleArray)
               {
                   sum = sum + d;
               }
               
               if (sum > 0.0001) 
               {
                   return false;
               }
               return true;
       }
         
    }
}

Here's a demo:

public void Main()
        {
          
            ModelDoc2 swModelDoc = (ModelDoc2)swApp.ActiveDoc;
            SelectionMgr swSelectionManager = (SelectionMgr)swModelDoc.SelectionManager;
            Feature swFeature = (Feature)swSelectionManager.GetSelectedObject6(1,-1);
            SheetMetalFolder swSheetMetalFolder = (SheetMetalFolder)swFeature.GetSpecificFeature2();



            // Declare swSheetMetalFolder2 using decorator pattern
            SheetMetalFolder2 swSheetMetalFolder2  = new SheetMetalFolder2(swSheetMetalFolder);
            // change thickness 
            swSheetMetalFolder2.Thickness(20);

            // change bend radius
            swSheetMetalFolder2.BendRadius(3);

            // rebuild is necessary to get effects
            swModelDoc.ForceRebuild3(true);
        }

        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;