5 Replies Latest reply on Oct 15, 2016 5:33 PM by Amen Allah Jlili

    C# macro to add new custom properties

    Gene Massion

      I’m in the  process of trying to learn how to program Solidworks macros in C#.  I am pretty comfortable with C# but am having some difficulty with the SW API. My first task is pretty easy, I just want to write a macro that will allows me to add some predefined custom properties to every part in an assembly.  First step is to just add the custom properties to one, already open SW part.  I thought this example http://help.solidworks.com/2014/English/api/sldworksapi/Get_Custom_Properties_for_Configuration_Example_CSharp.htm was pretty close to what I need for my first step but I don’t seem to be able to get it to run.  I just copied it off the web page into Visual Studio Tools for Applications.   It compiles fine but I get a “The operation could not be completed. The parameter is incorrect” error. 

       

      Any ideas would be greatly appreciated.  Thanks

       

      Here's the code from the example

       

      using SolidWorks.Interop.sldworks;
      using SolidWorks.Interop.swconst;
      using System.Runtime.InteropServices;
      using System;
      using System.Diagnostics;
      namespace GetCustProperties_CSharp.csproj
      {
         
      partial class SolidWorksMacro
          {

             
      ModelDoc2 swModel;
             
      Configuration config;
             
      CustomPropertyManager cusPropMgr;
             
      int lRetVal;
             
      object[] vPropNames;
             
      object vPropNamesObject;
             
      object vPropTypes;
             
      object vPropValues;
             
      string ValOut;
             
      string ResolvedValOut;
             
      bool wasResolved;
             
      object resolved;
             
      int nNbrProps;
             
      int j;

             
      int custPropType;

             
      public void Main()
              {
                  swModel = (
      ModelDoc2)swApp.ActiveDoc;
                  config = (
      Configuration)swModel.GetActiveConfiguration();

                  cusPropMgr = config.CustomPropertyManager;

                  lRetVal = cusPropMgr.Add3(
      "ADATE", swCustomInfoType_e.swCustomInfoDate, "4-13-59", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);
                  lRetVal = cusPropMgr.Get5(
      "ADATE", false, out ValOut, out ResolvedValOut, out wasResolved);
                  lRetVal = cusPropMgr.Set2(
      "ADATE", "4-13-89");

                 
      // Get the number of custom properties for this configuration
                  nNbrProps = cusPropMgr.Count;
                 
      Debug.Print("Number of properties for this configuration:            " + nNbrProps);

                 
      // Get the names of the custom properties
                  vPropNames = (object[])cusPropMgr.GetNames();

                 
      // For each custom property, print its type, value, and resolved value
                  for (j = 0; j <= nNbrProps - 1; j++)
                  {
                      lRetVal = cusPropMgr.GetAll2(
      ref vPropNamesObject, ref vPropTypes, ref vPropValues, ref resolved);
                      custPropType = cusPropMgr.GetType2((
      string)vPropNames[j]);
                     
      Debug.Print("    Name, swCustomInfoType_e value, and resolved value:  " + vPropNames[j] + ", " + custPropType + ", " + ResolvedValOut);
                  }

                  lRetVal = cusPropMgr.Delete2(
      "ADATE");
                 
      // Get the number of custom properties for this configuration
                  nNbrProps = cusPropMgr.Count;
                 
      Debug.Print("Number of properties for this configuration:            " + nNbrProps);
              }
             
      public SldWorks swApp;
          }
      }

        • Re: C# macro to add new custom properties
          Viktor Bovzdarenko

          Hi Gene,

          Seems like the code in this example was converted directly from VBA to C# without adjusting it to run from visual studio.

          In order to run it from visual studio you should first connect correctly to SolidWorks application and reference required libraries. Here is the example: SolidWorks API Stand-Alone Application - YouTube

          After you get an instance of SldWorks swApp the code above could be used.

          Regards

          Viktor

            • Re: C# macro to add new custom properties
              Gene Massion

              Well that was helpful.  I duplicated a C# macro from the help tutorials and it works.  Then I added in what I think is the code to add a custom property.  I'm having trouble getting the .Add3 command to work and it seems like I don't know have to use the built in enumerations.  .Add3 needs a value for the custom field type.  If I use swCustomInfoType_e.swCustomInfoText it won't compile and the error is "Error 3 Argument '2': cannot convert from 'SolidWorks.Interop.swconst.swCustomInfoType_e' to 'int'  ".  If I cast it to an (int) or track down the actual value of the enumeration and use it, I get this run time error: " Error = Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

               

              Thanks in advance for the help

               

              Here's my current macro code:

               

                        ModelDoc2 swDoc = null;

                          PartDoc swPart = null;

                          DrawingDoc swDrawing = null;

                          AssemblyDoc swAssembly = null;

                          bool boolstatus = false;

                          int longstatus = 0;

                          int longwarnings = 0;

                          swDoc = ((ModelDoc2)(swApp.ActiveDoc));

               

                          Configuration config;

                          CustomPropertyManager cusPropMgr;

                          int retValue;

               

                          config = (Configuration)swDoc.GetActiveConfiguration();

                          cusPropMgr = config.CustomPropertyManager;

               

                          //This won't compile.  Error = Error 3 Argument '2': cannot convert from 'SolidWorks.Interop.swconst.swCustomInfoType_e' to 'int'       

                          retValue = cusPropMgr.Add3("New custom property", swCustomInfoType_e.swCustomInfoText, "my new custom property value",          swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);

                          Console.WriteLine("Return value = " + retValue.ToString());

               

                          //This won't run.  Error = Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

                          retValue = cusPropMgr.Add3("New custom property", (int)swCustomInfoType_e.swCustomInfoText, "my new custom property value", (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);

                          Console.WriteLine("Return value = " + retValue.ToString());

               

                          //This won't run.  Error = Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

                          retValue = cusPropMgr.Add3("New custom property", 30, "my new custom property value", 1);

                          Console.WriteLine("Return value = " + retValue.ToString());

                      }

               

               

                      /// <summary>

                      ///  The SldWorks swApp variable is pre-assigned for you.

                      /// </summary>

                      public SldWorks swApp;

                  }

              }

                • Re: C# macro to add new custom properties
                  Viktor Bovzdarenko

                  try to use int value for every enumerator:

                  (int)swCustomInfoType_e.swCustomInfoText
                  and the rest seems to be fine, just be sure that all variables are declared

                  int retValue = 0;

                  retValue = cusPropMgr.Add3("New custom property Name", (int)swCustomInfoType_e.swCustomInfoText,

                                      "New custom property Val", (int)swCustomPropertyAddOption_e.swCustomPropertyReplaceValue);

              • Re: C# macro to add new custom properties
                Gene Massion

                I probably wasn't clear.  I tried all 3 calls to Add3 with the errors shown in my code above. Casting the enumerations to int is the second try in my code above and gave the Error = Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

                 

                I'm not familiar with VB but I'll give it a try with it as there are more examples and resources.  I sure would like to figure out the issue with the C# approach though.