12 Replies Latest reply on Nov 5, 2012 2:20 PM by Mark Coville

    Export custom properties of selected parts

    Mark Coville

      Hello All,

       

      I have been trying to create this macro for awhile with no luck. I want to export specific custom properties from all the parts I select in an assembly by click and drag on the screen. The goal eventually would be to export the custom properties "ShippingWeight" & "OperatingWeight" of the 50 or so parts I selected into excel so I can get totals. Here is what I have so far but I am lost. Thanks in advance.

       

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim selmgr As SldWorks.SelectionMgr
      Dim comp As SldWorks.Component2
      Dim cpm As CustomPropertyManager
      Dim valOut              As String
      Dim resolvedValOut      As String

      Sub main()

          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set selmgr = swModel.SelectionManager
        
          For ii = 1 To selmgr.GetSelectedObjectCount
              Dim partinfo As String
              Dim Part As SldWorks.ModelDoc2
              Set comp = selmgr.GetSelectedObject3(ii)
              Set Part = comp.GetModelDoc2
              Set cpm = swModel.Extension.CustomPropertyManager("")
              partinfo = "Name:" + comp.Name + vbNewLine + _
                         "Config:" + comp.ReferencedConfiguration + vbNewLine + _
                          "Size:" + cpm.Get2("Size", valOut, resolvedValOut)
          
              MsgBox (partinfo)
          Next

      End Sub

        • Re: Export custom properties of selected parts
          Keith Rice

          Mark,

           

          You'll need to use Microsoft.Office.Interop.Excel to work with Excel from VB.NET. Check out this example.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: Export custom properties of selected parts
              Mark Coville

              Hi Keith,

               

              Thanks for the reply. I actually found an example that you wrote that exports all the custom properties to excel but I'm just not there yet. Before I can even think about exporting to excel I need to get a configuration specific property into a message box. I am a newbie and I have to work in small chunks. Am I even close to getting what I need? I know it's a step back but if I replace Get2 with Get then it runs but I don't get a value for "Size" just blank.

               

              Thanks Again,

              Mark

                • Re: Export custom properties of selected parts
                  Keith Rice

                  Cool. Just to make it clear: the example I wrote was probably in VBA, not VB.NET. It looks like you are using VB.NET, so I think you'll need to check out the link in my first post in this thread.

                   

                  Whether you are trying to get document level or configuration level custom properties, you use the same interface and therefore the same methods. The difference is that is when you get the ICustomPropertyManager pointer for the current model, you need to specify Empty (or a zero length strength) or the configuration's name for the argument in IModelDocExtension::CustomPropertyManager.

                   

                  So in your example you have this:

                   

                  Set cpm = swModel.Extension.CustomPropertyManager("")

                   

                  That gets the document level custom property manager. This is how you get the custom prop manager for a configuration called "test":

                   

                  Set cpm = swModel.Extension.CustomPropertyManager("test")

                   

                  Simple as that. To traverse all of the configurations you will need all of the configuration names, and to get that you can use IModelDoc2::GetConfigurationNames.

                   

                  I hope this helps.

                   

                  Keith

                  Video Tutorials for the SolidWorks API

              • Re: Export custom properties of selected parts
                Artem Taturevych

                I would not suggest you to use direct Excel APIs in case you have a big data to store (hundreds or thousands of custom properties fields). Because this will run into a significant performance issues. There are two solutions for this:

                 

                a) Output the data to comma separated CSV format which is supported by Excel. Use simple text output techniques in this case.

                b) Use ODBC or OleDB to store the data in Excel format directly.

                 

                Sure the above has no sense if you like to provide the custom formatting of your data in Excel.

                ______________________

                Regards,

                Artem Taturevych

                Application Engineer at Intercad

                http://intercad.com.au/

                Tel: +61 2 9454 4444

                  • Re: Export custom properties of selected parts
                    Mark Coville

                    Keith - I followed your advice and it worked the only issue I have before I can look into excel is if I select a part it gives me the part data but if i select a sub-assembly it gives me the sub-assembly data. How do I force it to give me the individual part data of the parts that are in the sub-assembly.

                     

                    Artem - Thanks for your reply, I will definately look into those option as well when I get there.

                     

                     

                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim selmgr As SldWorks.SelectionMgr
                    Dim comp As SldWorks.Component2
                    Dim cpm As CustomPropertyManager
                    Dim valOut              As String
                    Dim resolvedValOut      As String
                    Dim config As String

                    Sub main()

                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        Set selmgr = swModel.SelectionManager
                      
                        For ii = 1 To selmgr.GetSelectedObjectCount
                            Dim partinfo As String
                            Dim Part As SldWorks.ModelDoc2
                            Set comp = selmgr.GetSelectedObject3(ii)
                            Set Part = comp.GetModelDoc2
                            config = comp.ReferencedConfiguration
                            Set cpm = Part.Extension.CustomPropertyManager(config)
                           
                            partinfo = "Description: " + cpm.Get("Long Description") + vbNewLine + _
                            "Size: " + cpm.Get("Size")
                        
                            MsgBox (partinfo)
                        Next

                    End Sub

                  • Re: Export custom properties of selected parts
                    Ivana Kolin

                    avoid use of obsolete features

                     

                    Option Explicit

                    Sub main()

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                        Dim selmgr As SldWorks.SelectionMgr

                        Dim swComp As SldWorks.Component2

                       

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set selmgr = swModel.SelectionManager

                        Dim ii As Integer

                      

                        For ii = 1 To selmgr.GetSelectedObjectCount

                            Set swComp = selmgr.GetSelectedObject6(ii, -1)

                            processAssy swComp

                        Next

                    End Sub

                     

                     

                    Sub processAssy(swComp As SldWorks.Component2)

                        Dim swmod As SldWorks.ModelDoc2

                        Dim swAssy As SldWorks.AssemblyDoc

                        Dim vComponents As Variant

                        Dim vComp As Variant

                       

                        Set swmod = swComp.GetModelDoc2

                        

                        If swmod.GetType = swDocASSEMBLY Then

                            Set swAssy = swmod

                            vComponents = swAssy.GetComponents(False)

                       

                            For Each vComp In vComponents

                                Set swComp = vComp

                                processAssy swComp

                            Next

                            Exit Sub

                        End If

                        If swmod.GetType = swDocPART Then

                            processPart swComp

                        End If

                       

                    End Sub

                    Sub processPart(swComp As SldWorks.Component2)

                        Dim cpm As CustomPropertyManager

                        Dim valOut              As String

                        Dim resolvedValOut      As String

                        Dim sizeValOut              As String

                        Dim sizeResolvedValOut      As String

                       

                        Dim swmod As SldWorks.ModelDoc2

                        Dim partinfo As String

                        Set swmod = swComp.GetModelDoc2

                     

                     

                        Set cpm = swmod.Extension.CustomPropertyManager(swComp.ReferencedConfiguration)

                        cpm.Get4 "Long Description", False, valOut, resolvedValOut

                        cpm.Get4 "Size", False, sizeValOut, sizeResolvedValOut

                     

                     

                        partinfo = "Description: " + valOut + vbNewLine + _

                        "Size: " + sizeValOut

                     

                     

                        MsgBox (partinfo)

                     

                     

                    End Sub

                      • Re: Export custom properties of selected parts
                        Mark Coville

                        Thank you everyone,

                         

                        Now I just have to get it into excel!

                          • Re: Export custom properties of selected parts
                            Ivana Kolin

                            Option Explicit

                            Const MyPath As String = "C:\output.csv"

                            Dim fNum As Integer

                             

                             

                            Private Declare Function ShellExecute Lib "shell32.dll" _

                                Alias "ShellExecuteA" (ByVal Hwnd As Long, _

                                ByVal lpOperation As String, ByVal lpFile As String, _

                                ByVal lpParameters As String, _

                                ByVal lpDirectory As String, _

                                ByVal nShowCmd As Long) As Long

                             

                             

                            Public Function OpenLocation(ByVal WhichFilePath As String, Optional sParams As String = "", Optional sStartIn As String = vbNullString, Optional lngOpenMode As Long = 1) As Long

                                OpenLocation = ShellExecute(0, "Open", WhichFilePath, sParams, sStartIn, lngOpenMode)

                            End Function

                            Sub main()

                                Dim swApp As SldWorks.SldWorks

                                Dim swModel As SldWorks.ModelDoc2

                                Dim selmgr As SldWorks.SelectionMgr

                                Dim swComp As SldWorks.Component2

                             

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                Set selmgr = swModel.SelectionManager

                                Dim ii As Integer

                                fNum = FreeFile()

                                Open MyPath For Output As fNum

                                For ii = 1 To selmgr.GetSelectedObjectCount

                                    Set swComp = selmgr.GetSelectedObject6(ii, -1)

                                    processAssy swComp

                                Next

                                Close #fNum

                                If OpenLocation("excel.exe", MyPath, "", 1) < 32 Then

                                    'Failed to open

                                Else

                                    'Opened

                                End If

                             

                             

                            End Sub

                             

                             

                            Sub processAssy(swComp As SldWorks.Component2)

                                Dim swmod As SldWorks.ModelDoc2

                                Dim swAssy As SldWorks.AssemblyDoc

                                Dim vComponents As Variant

                                Dim vComp As Variant

                             

                                Set swmod = swComp.GetModelDoc2

                             

                                If swmod.GetType = swDocASSEMBLY Then

                                    Set swAssy = swmod

                                    vComponents = swAssy.GetComponents(False)

                             

                                    For Each vComp In vComponents

                                        Set swComp = vComp

                                        processAssy swComp

                                    Next

                                    Exit Sub

                                End If

                                If swmod.GetType = swDocPART Then

                                    processPart swComp

                                End If

                             

                            End Sub

                            Sub processPart(swComp As SldWorks.Component2)

                                Dim cpm As CustomPropertyManager

                                Dim valOut              As String

                                Dim resolvedValOut      As String

                                Dim sizeValOut              As String

                                Dim sizeResolvedValOut      As String

                             

                                Dim swmod As SldWorks.ModelDoc2

                                Dim partinfo As String

                                Set swmod = swComp.GetModelDoc2

                             

                             

                                Set cpm = swmod.Extension.CustomPropertyManager(swComp.ReferencedConfiguration)

                                cpm.Get4 "Long Description", False, valOut, resolvedValOut

                                cpm.Get4 "Size", False, sizeValOut, sizeResolvedValOut

                             

                             

                                partinfo = "Description: " + valOut + vbNewLine + _

                                "Size: " + sizeValOut

                               

                                Print #fNum, valOut & ";" & sizeValOut

                             

                             

                             

                             

                            End Sub