11 Replies Latest reply on Dec 3, 2017 9:00 PM by Solid Air

    Adding Custom Properties to a  Part

    Shawn Murphy

      I'm trying to add a macro to automate adding custom properties
      to a part files.   As you can see in the
      video, the macro recorder seems to be getting hung up.  I have never done API programming, but a
      little BASIC years ago.  I’m started
      using “CADSharp.com” yesterday. I’m not looking to learn a lot of VBA at this
      time, other than getting this macro to work.

        • Re: Adding Custom Properties to a  Part
          Solid Air

          This question has been asked many times.  Have you done a search in the forum on custom properties?

          • Re: Adding Custom Properties to a  Part
            Swme Eng

            I've never gotten the macro recorder to work, if you are interested in learning VBA, then definitely go through CAD Sharp's website, if not, and all you care about is quickly creating custom properties, then do take a loot at the Property Tab Builder.

             

             

            Basically, you would need to cast the custom prop manager object, then you can use the add and get methods to work with properties. In my experience, you can fully automate the custom property process for assigning stuff like BOM information to parts, and not have to worry about data entry/mistakes associated with the property tab builder.

              • Re: Adding Custom Properties to a  Part
                Shawn Murphy

                Hi Everyone,

                 

                I still need help, I've got a lot of hours into this chasing in circles.  I've looked at all the free videos on CADSharp, YouTube, Online Help, API help.  While I did learn a lot, code is so form and syntax senvitve.  Once someone shows me how to get this code working to change one custom properity, I think it's all down hill from there.  Deepak, I did look at your example.  This is where I currently am, still can't get the custom properity populated. Yes, it's avalible in the pull down choices.  Sorry for all the interation, but I also have 10 more I did not copy in.  BTW, API help stated AddCustomInfo2 Method  is obsoleted by CustomPropertyManager Property , which is even more confusing me.  I'll run the risk of using the old code for now.

                 

                I would hazourd the a guess, this is the question everyone else is asking as well.  I feel if you help me with this, it would answer the question for grate many users.

                 

                This is the line that won't run "'swModel.AddCustomInfo2 "Mass", swCustomInfoText, Chr(34) & "SW-Mass" & "@" & swModel.GetTitle & ".sldprt" & Chr(34)"

                 

                Dim swApp As SldWorks.SldWorks
                Dim swModel As ModelDoc2
                Dim swModelDocExt As ModelDocExtension
                Dim swCustProp As CustomPropertyManager
                Dim sFileName As String, Path As String
                Dim Longstatus As Long, longwarnings As Long
                Dim Val As String, nProp As String
                Dim ValOut As String
                Dim retVal As String

                'Dim Part As SldWorks.ModelDoc2

                'Dim instance As ICustomPropertyManager
                'Dim FieldName As System.String
                'Dim FieldType As System.Integer
                'Dim FieldValue As System.String
                'Dim OverwriteExisting As System.Integer
                'Dim value As System.Integer
                'Set FieldName = "Materail-Presto"

                'value = instance.Add3(FieldName, FieldType, FieldValue, OverwriteExisting)

                    Sub main()
                        'swModel.AddCustomInfo2 "Mass", swCustomInfoText, Chr(34) & "SW-Mass" & "@" & swModel.GetTitle & ".sldprt" & Chr(34)
                        swModel.AddCustomInfo2 ("Mass", swCustomInfoText, "SW-Mass" & "@" & swModel.GetTitle & ".sldprt")
                'Set swApp = Application.SldWorks
                End Sub

                  • Re: Adding Custom Properties to a  Part
                    Solid Air

                    First, I do not see in your code where you get the SolidWorks or Model objects; a must before you can use AddCustomInfo2.  But like you found out, it is an obsoleted call.  Below is an example from from the API help using Add3.  Of course since you are trying to write to the custom tab and not the configuration specific tab, you will need to alter the code to get the custom property manager.

                     

                    '---------------------------------------------------------------------------
                    '
                    Preconditions:
                    ' 1. Open a part document.
                    ' 2. Open the Immediate
                    window.
                    '
                    ' Postconditions:
                    ' 1. Adds a date custom property to the
                    part's configuration.
                    ' 2. Deletes the custom property from the
                    configuration.
                    ' 3. Examine the Immediate
                    window.
                    '---------------------------------------------------------------------------

                    Option Explicit

                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As
                    SldWorks.ModelDoc2
                    Dim config As SldWorks.Configuration
                    Dim cusPropMgr As
                    SldWorks.CustomPropertyManager
                    Dim lRetVal As Long
                    Dim vPropNames As
                    Variant
                    Dim vPropTypes As Variant
                    Dim vPropValues As Variant
                    Dim ValOut
                    As String
                    Dim ResolvedValOut As String
                    Dim wasResolved As Boolean
                    Dim
                    resolved As Variant
                    Dim nNbrProps As Long
                    Dim j As Long
                    Dim
                    custPropType As Long

                    Sub main()

                        Set swApp = Application.SldWorks
                        Set swModel
                    = swApp.ActiveDoc
                        Set config =
                    swModel.GetActiveConfiguration
                      

                        Set cusPropMgr = config.CustomPropertyManager
                      

                        lRetVal = cusPropMgr.Add3("ADATE",
                    swCustomInfoType_e.swCustomInfoDate, "4-13-59",
                    swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                        lRetVal =
                    cusPropMgr.Get5("ADATE", False, ValOut, ResolvedValOut,
                    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
                        lRetVal =
                    cusPropMgr.GetAll2(vPropNames, vPropTypes, vPropValues, resolved)

                      ' For each custom property, print its type, value, and
                    resolved value
                        For j = 0 To nNbrProps - 1
                            custPropType =
                    cusPropMgr.GetType2(vPropNames(j))
                            Debug.Print "    Name,
                    swCustomInfoType_e value, and resolved value:  " & vPropNames(j) & ", ";
                    custPropType & ", " & ResolvedValOut
                        Next j

                        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

                    End Sub