12 Replies Latest reply on Aug 9, 2018 5:06 PM by Josh Brady

    Macro to Create Folders by parts' volume/weights

    Michael Fernando

      I got a large imported assembly through Parasolid with over 2000 parts. I would like to create folders in the assembly FeatureManager with similar parts so that I could simplify this assembly by suppressing the folders with insignificant components scattered all over.

      Currently I use Assembly Visualisation’s Mass/volume property to bulk select them and create folders; this is a manual process which takes a long time to complete.

      I’m sure someone has thought about this process before. Has anyone made a macro to automate this process? 

        • Re: Macro to Create Folders by parts' volume/weights
          Deepak Gupta

          Which version of SOLIDWORKS you're using? If 2017 or above then try Shorten Assembly FeatureManager Tree

           

          Other option would be to save the assembly as part file and convert that part into weldment. Now take advantage of cut list to group them

          • Re: Macro to Create Folders by parts' volume/weights
            Michael Fernando

            I’ve created sub assemblies, but the part names are just a serial # without an order. Hence, I would like to create folders within those subassemblies containing parts with similar part volumes. Folder name could be “Vol_##.###” in ascending order where I could check the values and cleanup further.

             

            I’m not a programmer and is this possible? Maybe I should submit an ER for this.

              • Re: Macro to Create Folders by parts' volume/weights
                Josh Brady

                This is certainly possible.  I doubt an ER will get any traction at all because the macro is simple and the number of people who need it is likely quite small.

                 

                Can you post an example parasolid? Preferably one that makes surfaces as you mentioned...

                  • Re: Macro to Create Folders by parts' volume/weights
                    Michael Fernando

                    Here is a simplified sub-assembly.

                      • Re: Macro to Create Folders by parts' volume/weights
                        Michael Fernando

                        Please try on this Sub-assembly with Virtual components.

                          • Re: Macro to Create Folders by parts' volume/weights
                            Josh Brady

                            Here you go:

                            Dim swApp As SldWorks.SldWorks
                            Dim swDoc As SldWorks.ModelDoc2
                            Dim swAssy As SldWorks.AssemblyDoc
                            Dim swPart As SldWorks.PartDoc
                            Dim swFMgr As SldWorks.FeatureManager
                            Dim FeatColl As Collection
                            Dim aBods As Variant
                            Dim swFace As SldWorks.Face2
                            Dim VolColl As Collection
                            Dim SurfColl As Collection
                            Dim aFeats As Variant
                            Dim swFeat As SldWorks.Feature
                            Dim swMref As SldWorks.MateReference
                            Dim swComp As SldWorks.Component2
                            Dim swCompDoc As SldWorks.ModelDoc2
                            Dim aProps As Variant
                            Dim boolstatus As Boolean
                            Dim aComps() As SldWorks.Component2
                            Dim tmpVol As Double
                            Dim tmpSurf As Double
                            Dim i As Long
                            Dim j As Long
                            Dim longstatus As Long, longwarnings As Long
                            
                            
                            Dim VOLTOL As Double
                            Dim SURFTOL As Double
                            
                            
                            Sub main()
                            'This is the tolerance to allow for calculation variability
                            VOLTOL = (1 / 1000 / 1000 / 1000 / 1000 / 1000 / 1000) * 500 'This is 500 cubic microns
                            SURFTOL = (1 / 1000 / 1000 / 1000 / 1000) * 800  'This is 800 square microns
                            Set swApp = Application.SldWorks
                            
                            
                            Set swDoc = swApp.ActiveDoc
                            Set swAssy = swDoc
                            Set swFMgr = swDoc.FeatureManager
                            Set FeatColl = New Collection
                            Set VolColl = New Collection
                            Set SurfColl = New Collection
                            aFeats = swFMgr.GetFeatures(True)
                            'First let's load up a collection
                            For i = 0 To UBound(aFeats)
                                'Debug.Print aFeats(i).Name, aFeats(i).GetTypeName
                                If aFeats(i).GetTypeName = "Reference" Then
                                    FeatColl.Add aFeats(i)
                                    Set swCompDoc = aFeats(i).GetSpecificFeature.GetModelDoc
                                    aProps = swCompDoc.Extension.GetMassProperties2(0, longstatus, False)
                                    If longstatus = 2 Then
                                        If swCompDoc.GetType <> swDocPART Then
                                            MsgBox "This assembly contains subasseblies that have no solid body.  UR2 CRA Z. I give up."
                                            Exit Sub
                                        Else
                                            Set swPart = swCompDoc
                                            tmpSurf = 0
                                            aBods = swPart.GetBodies2(-1, False)
                                            On Error Resume Next
                                            For j = 0 To UBound(aBods)
                                                Set swFace = aBods(j).GetFirstFace
                                                tmpSurf = tmpSurf + swFace.GetArea
                                            Next j
                                            On Error GoTo 0
                                            VolColl.Add 0
                                            SurfColl.Add tmpSurf
                                        End If
                                    Else
                                        VolColl.Add aProps(3)
                                        SurfColl.Add aProps(4)
                                    End If
                                End If
                            Next i
                            
                            
                            swDoc.FeatureManager.EnableFeatureTree = False
                            While FeatColl.Count > 0
                                tmpVol = VolColl(1)
                                tmpSurf = SurfColl(1)
                                FeatColl(1).Select False
                                ReDim aComps(0)
                                Set aComps(0) = FeatColl(1).GetSpecificFeature2
                                FeatColl.Remove 1
                                VolColl.Remove 1
                                SurfColl.Remove 1
                                i = 1
                                Do While i < FeatColl.Count + 1
                                    If (Abs(VolColl(i) - tmpVol) < VOLTOL) And (Abs(SurfColl(i) - tmpSurf) < SURFTOL) Then
                                        ReDim Preserve aComps(UBound(aComps) + 1)
                                        Set aComps(UBound(aComps)) = FeatColl(i).GetSpecificFeature2
                                        FeatColl.Remove i
                                        VolColl.Remove i
                                        SurfColl.Remove i
                                    Else
                                        i = i + 1
                                    End If
                                    If i > FeatColl.Count Then Exit Do
                                Loop
                                Set swFeat = swFMgr.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
                                If tmpVol = 0 Then
                                    swFeat.Name = "Surf_" & Round(tmpSurf * 1000 * 1000, 2)
                                Else
                                    swFeat.Name = "Vol_" & Round(tmpVol * 1000 * 1000 * 1000, 2)
                                End If
                                swAssy.ReorderComponents aComps, swFeat, swReorderComponents_LastInFolder
                            Wend
                            MsgBox "Folderizing Complete"
                            swDoc.FeatureManager.EnableFeatureTree = True
                            End Sub
                            
                    • Re: Macro to Create Folders by parts' volume/weights
                      Josh Brady

                      Sorting is possible.  It may be interesting enough to someone that they will do it.