39 Replies Latest reply on Oct 26, 2018 2:41 AM by Gertjan Van Dijk

    Grab Items from Selection and Rebuild Them

    Matt Peneguy

      So, I'm trying to create a macro to rebuild parts or assemblies I have selected in the Feature Tree.  I've figured out how to iterate through the selected items, but I'm not sure of the code to grab them and rebuild them.  Here's what I have so far:

      Sub main()
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swSelMgr As SldWorks.SelectionMgr
          Dim SelType1 As Long
          Dim i As Integer
      
      
          Dim SelCount As Long
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swSelMgr = swModel.SelectionManager
          SelCount = swSelMgr.GetSelectedObjectCount2(-1)
          For i = 1 To SelCount
              
              
              SelType1 = swSelMgr.GetSelectedObjectType3(i, -1)
              MsgBox (SelType1)
              
          Next i
          
      End Sub
      
        • Re: Grab Items from Selection and Rebuild Them
          Josh Brady

          Can you upload an example assembly that doesn't already rebuild correctly when you rebuild the assembly?  I would like to make sure I understand the interrelationships to avoid actually hurting performance while trying to help...

            • Re: Grab Items from Selection and Rebuild Them
              Matt Peneguy

              The plan is to have it just rebuild only the items I have selected in the Feature Tree.  So, if it works how I'm hoping, it shouldn't be slower than actually opening the sub assembly. 

              Unfortunately, attaching the entire ASM would be difficult it's over 1GB.  But I have attached one of the smaller sub assemblies.

                • Re: Grab Items from Selection and Rebuild Them
                  Josh Brady

                  OK... I assume we're looking at the "...Ropes Asm".  What would I change in this assembly to cause components to need updating but not actually update until they're rebuilt in their own window?

                    • Re: Grab Items from Selection and Rebuild Them
                      Matt Peneguy

                      Let me see if I can get further up the tree.  What drives it is the Ropes Sketch.  And that is driven down from the top level sketch because the ropes link the bridge to the counterweight.

                      • Re: Grab Items from Selection and Rebuild Them
                        Matt Peneguy

                        Are you concerned that more will rebuild and save than what is selected?

                          • Re: Grab Items from Selection and Rebuild Them
                            Josh Brady

                            Matt Peneguy wrote:

                             

                            Are you concerned that more will rebuild and save than what is selected?

                            For one, I want to understand why they don't already rebuild when they should... but then also if I write something I want to be able to tell if it's really doing the thing that you want. 

                             

                            I'm a little worried that it may not actually work... The Remarks section on ModelDoc2::EditRebuild3 says "This method only works in-context of the active document".  The Rebuild method of the ModelDocExtension looks like it only works for assemblies and drawings... kinda weird.  Then there's ForceRebuild3, but you're concerned with performance, and that one may be overkill (and therefore a performance hog)

                              • Re: Grab Items from Selection and Rebuild Them
                                Matt Peneguy

                                I tried to make a pared down assembly and everything is rebuilding 3 levels deep.  I wonder what the cutoff is where SW stops cascading those changes.

                                ForceRebuild3(true) looks to be what I need.  I'm trying to push these changes down the tree without opening the files. I may take what Gertjan Van Dijk posted with ForceRebuild3 and see what happens in my test assembly.  Before I kludge this all together, is this what I'd do to save the file, also?

                                boolstatus = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)

                          • Re: Grab Items from Selection and Rebuild Them
                            Deepak Gupta

                            Matt Peneguy wrote:

                             

                            The plan is to have it just rebuild only the items I have selected in the Feature Tree. So, if it works how I'm hoping, it shouldn't be slower than actually opening the sub assembly.

                            Unfortunately, attaching the entire ASM would be difficult it's over 1GB. But I have attached one of the smaller sub assemblies.

                            The way I would think it to open the selected components but invisibly, rebuild, save and close them. I'll try something tomorrow.

                             

                            And by the when you do pack and go, make sure you uncheck appearances, etc. as they get added to the zip file increasing the file size.

                              • Re: Grab Items from Selection and Rebuild Them
                                Matt Peneguy

                                Deepak Gupta wrote:

                                 

                                The way I would think it to open the selected components but invisibly, rebuild, save and close them. I'll try something tomorrow.

                                Thanks Deepak.  That may be the case.  Opening and loading into memory may be necessary.

                                Deepak Gupta wrote:

                                 

                                And by the when you do pack and go, make sure you uncheck appearances, etc. as they get added to the zip file increasing the file size.

                                I'll keep that in mind next time I upload files.

                                • Re: Grab Items from Selection and Rebuild Them
                                  Josh Brady

                                  Deepak Gupta wrote:

                                   

                                  The way I would think it to open the selected components but invisibly, rebuild, save and close them. I'll try something tomorrow.

                                   

                                   

                                  Unless the components are suppressed or lightweight, you can't open them invisibly because they're already open invisibly.  This is why it's particularly confusing to me why opening the part in its own window makes it rebuild differently. 

                                   

                                  You could get a pointer to the ModelDoc2 object for each selected component, but (as I mentioned) EditRebuild supposedly only works with the active document per the documentation.  Of course, it's possible that the docs are incorrect (I've sent in several corrections in the past...) but that's why I want to have some file to play with that reproduces the problem to make sure the correct thing is getting solved.

                                    • Re: Grab Items from Selection and Rebuild Them
                                      Matt Peneguy

                                      Like I said above I am not good at VBA, but is there anyway to select the Model from the Feature Tree and do a ForceRebuild3?  Change out the swModel with the selected components?

                                      bRet = swModel.ForceRebuild3(False)

                                        • Re: Grab Items from Selection and Rebuild Them
                                          Josh Brady

                                          Well... Here it is... Hope it's helpful.  You can muck around with switching between editrebuild3 and forcerebuild, along with the false/true args of forcerebuild.  If you don't like the msgbox messages you can delete 'em.

                                           

                                          Dim swApp As SldWorks.SldWorks
                                          Dim swDoc As SldWorks.ModelDoc2
                                          Dim swSelMgr As SldWorks.SelectionMgr
                                          Dim swComp As SldWorks.Component2
                                          Dim stWatch As Double
                                          'Dim aDocs As Variant
                                          Dim cDocs As Collection
                                          Dim i As Long
                                          
                                          
                                          Sub main()
                                              Set swApp = Application.SldWorks
                                              Set swDoc = swApp.ActiveDoc
                                              Dim swAssy As SldWorks.AssemblyDoc
                                              Set swAssy = swDoc
                                              Set swSelMgr = swDoc.SelectionManager
                                              'swAssy.ResolveAllLightWeightComponents False
                                              Set cDocs = New Collection
                                              'aDocs = swApp.GetDocuments
                                              For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
                                                  Set swComp = swSelMgr.GetSelectedObjectsComponent4(i, -1)
                                                  If Not swComp Is Nothing Then
                                                      cDocs.Add swComp.GetModelDoc2
                                                  End If
                                              Next i
                                              Debug.Print cDocs.Count
                                              MsgBox "About to rebuild " & cDocs.Count & " documents."
                                              stWatch = Timer
                                              For i = 1 To cDocs.Count
                                                  Set swDoc = cDocs(i)
                                                  Debug.Print "Rebuilding " & swDoc.GetTitle
                                                  'swDoc.EditRebuild3
                                                  swDoc.ForceRebuild3 False
                                              Next i
                                              Set cDocs = Nothing
                                              MsgBox "Rebuilt in " & Round(Timer - stWatch, 2) & "s"
                                          End Sub
                                          
                                            • Re: Grab Items from Selection and Rebuild Them
                                              Matt Peneguy

                                              Cool.  Thanks So Much! I need to make a couple changes to my main ASM. After that, I'll take that for a test drive and see what it does on that test ASM...And I'm linking to this thread from the Top Ten List.

                                              • Re: Grab Items from Selection and Rebuild Them
                                                Matt Peneguy

                                                Josh,

                                                The macro is throwing this error with EditRebuild3 and with ForceRebuild3:

                                                I tried to change

                                                Dim swAssy As SldWorks.AssemblyDoc

                                                to

                                                Dim swAssy As SldWorks.ModelDoc2

                                                thinking it may fix it.  But I still get the same error.

                                                  • Re: Grab Items from Selection and Rebuild Them
                                                    Josh Brady

                                                    How fast did you grab the code?  Where is "set cDocs = Nothing"?

                                                     

                                                    That's a sorta "cleanup" line that I added really late.  I posted the code, then I noticed that I had put that line right in the dang middle instead of way slap down at the end.  I fixed the post pretty quick but you may have got hold of it too early...

                                                      • Re: Grab Items from Selection and Rebuild Them
                                                        Matt Peneguy

                                                        That line is 3rd from the last. But, I'll grab the code again and try it.

                                                          • Re: Grab Items from Selection and Rebuild Them
                                                            Matt Peneguy

                                                            Yeah, it's still got problems at the swDoc on my end, same error:

                                                              • Re: Grab Items from Selection and Rebuild Them
                                                                Josh Brady

                                                                OK... How many docs does it say it's about to rebuild?

                                                                Also.... Are you selecting lightweight components?

                                                                  • Re: Grab Items from Selection and Rebuild Them
                                                                    Matt Peneguy

                                                                    Yes they are lightweight and it doesn't appear to matter how many ASMs I grab.  Is lightweight going to be an issue?  That's really the point most of these sub asms will be lightweight.

                                                                      • Re: Grab Items from Selection and Rebuild Them
                                                                        Matt Peneguy

                                                                        Set them to resolved and it worked

                                                                        I guess worse case scenario I could have it set Lightweight to Resolved in the macro?

                                                                        • Re: Grab Items from Selection and Rebuild Them
                                                                          Josh Brady

                                                                          OK.  Here's the thing.  All you should need to do is set them to resolved instead of lightweight.  I'm pretty sure you don't need this macro.

                                                                           

                                                                          When components are lightweight, SW just loads a bare representation of the components into memory.  It can't rebuild lightweight components because it hasn't loaded the "recipe", it's just got the dang finished cake.

                                                                           

                                                                          When you open a document in its own window, it loads the rest of the document into memory.  Only then can it recognize that, since you change the cake type from birthday to red velvet, it needs to rebuild with cream cheese icing instead of buttercream.

                                                                           

                                                                          This version will resolve only the components that you selected... However, you'll probably get better results by just right-clicking and choose "set to resolved" and then hit rebuild on the assembly...

                                                                           

                                                                          Sub main()
                                                                              Set swApp = Application.SldWorks
                                                                              Set swDoc = swApp.ActiveDoc
                                                                              Dim swAssy As SldWorks.AssemblyDoc
                                                                              Set swAssy = swDoc
                                                                              Set swSelMgr = swDoc.SelectionManager
                                                                              'swAssy.ResolveAllLightWeightComponents False
                                                                              Set cDocs = New Collection
                                                                              'aDocs = swApp.GetDocuments
                                                                              For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
                                                                                  Set swComp = swSelMgr.GetSelectedObjectsComponent4(i, -1)
                                                                                  If Not swComp Is Nothing Then
                                                                                      swComp.SetSuppression2 swComponentFullyResolved
                                                                                      cDocs.Add swComp.GetModelDoc2
                                                                                  End If
                                                                              Next i
                                                                              Debug.Print cDocs.Count
                                                                              MsgBox "About to rebuild " & cDocs.Count & " documents."
                                                                              stWatch = Timer
                                                                              For i = 1 To cDocs.Count
                                                                                  Set swDoc = cDocs(i)
                                                                                  Debug.Print "Rebuilding " & swDoc.GetTitle
                                                                                  'swDoc.EditRebuild3
                                                                                  swDoc.ForceRebuild3 False
                                                                              Next i
                                                                              Set cDocs = Nothing
                                                                              MsgBox "Rebuilt in " & Round(Timer - stWatch, 2) & "s"
                                                                          End Sub
                                                                          
                                                                            • Re: Grab Items from Selection and Rebuild Them
                                                                              Matt Peneguy

                                                                              Josh Brady wrote:

                                                                               

                                                                              OK. Here's the thing. All you should need to do is set them to resolved instead of lightweight. I'm pretty sure you don't need this macro.

                                                                               

                                                                              When components are lightweight, SW just loads a bare representation of the components into memory. It can't rebuild lightweight components because it hasn't loaded the "recipe", it's just got the dang finished cake.

                                                                               

                                                                              When you open a document in its own window, it loads the rest of the document into memory. Only then can it recognize that, since you change the cake type from birthday to red velvet, it needs to rebuild with cream cheese icing instead of buttercream.

                                                                              I discussed it with John Stoltzfus and I thought he was of the same opinion.  At some point these changes don't get pushed down to the subassemblies, lightweight or not.  If that is not the case, then I need to do is have the macro toggle lightweight off then back on for the selected subassemblies.  Unfortunately I don't see anything for AssemblyDoc's swDoc that supports that.

                                                                            • Re: Grab Items from Selection and Rebuild Them
                                                                              Josh Brady

                                                                              Matt Peneguy wrote:

                                                                               

                                                                              Yes they are lightweight and it doesn't appear to matter how many ASMs I grab. Is lightweight going to be an issue? That's really the point most of these sub asms will be lightweight.

                                                                              Small complaint... Your Top Ten post shows the component of interest as already resolved, not lightweight.  Thus this rabbit trail.  I'd have just told you at that point you could just resolve (which is already in the RMB menu) and rebuild... 

                                                                              • Re: Grab Items from Selection and Rebuild Them
                                                                                Deepak Gupta

                                                                                Matt Peneguy wrote:

                                                                                 

                                                                                Yes they are lightweight and it doesn't appear to matter how many ASMs I grab. Is lightweight going to be an issue? That's really the point most of these sub asms will be lightweight.

                                                                                May be we can use the macro to resolve them rebuild and make light weight again

                                                                                  • Re: Grab Items from Selection and Rebuild Them
                                                                                    Josh Brady

                                                                                    Deepak Gupta wrote:

                                                                                     

                                                                                    Matt Peneguy wrote:

                                                                                     

                                                                                    Yes they are lightweight and it doesn't appear to matter how many ASMs I grab. Is lightweight going to be an issue? That's really the point most of these sub asms will be lightweight.

                                                                                    May be we can use the macro to resolve them rebuild and make light weight again

                                                                                    Unless you're bumping up against the top end of your RAM, it's probably going to be a worse hit to performance to be popping them in and out of lightweight all willy nilly...

                                                                                      • Re: Grab Items from Selection and Rebuild Them
                                                                                        Matt Peneguy

                                                                                        Josh Brady wrote:

                                                                                         

                                                                                        Deepak Gupta wrote:

                                                                                         

                                                                                        Matt Peneguy wrote:

                                                                                         

                                                                                        Yes they are lightweight and it doesn't appear to matter how many ASMs I grab. Is lightweight going to be an issue? That's really the point most of these sub asms will be lightweight.

                                                                                        May be we can use the macro to resolve them rebuild and make light weight again

                                                                                        Unless you're bumping up against the top end of your RAM, it's probably going to be a worse hit to performance to be popping them in and out of lightweight all willy nilly...

                                                                                        It's a lot better than what I'm doing now which is opening each sub in succession.

                                                                                        I made a change at the top level and I'm going to bump the Lightweight on and off down the feature tree and test this.

                                                                                          • Re: Grab Items from Selection and Rebuild Them
                                                                                            Matt Peneguy

                                                                                            1-2Z6Y3OX wrote:

                                                                                             

                                                                                            Josh Brady wrote:

                                                                                             

                                                                                            Deepak Gupta wrote:

                                                                                             

                                                                                            Matt Peneguy wrote:

                                                                                             

                                                                                            Yes they are lightweight and it doesn't appear to matter how many ASMs I grab. Is lightweight going to be an issue? That's really the point most of these sub asms will be lightweight.

                                                                                            May be we can use the macro to resolve them rebuild and make light weight again

                                                                                            Unless you're bumping up against the top end of your RAM, it's probably going to be a worse hit to performance to be popping them in and out of lightweight all willy nilly...

                                                                                            It's a lot better than what I'm doing now which is opening each sub in succession.

                                                                                            I made a change at the top level and I'm going to bump the Lightweight on and off down the feature tree and test this.

                                                                                            Josh Brady,

                                                                                            You were correct at least for 3 levels deep.  I made a change and it updated with the parts set to resolved.

                                                                                  • Re: Grab Items from Selection and Rebuild Them
                                                                                    Deepak Gupta

                                                                                    Might be stupid question but did you selected any component before running the macro? BTW macro works fine for me.

                                                                        • Re: Grab Items from Selection and Rebuild Them
                                                                          Deepak Gupta

                                                                          Josh Brady wrote:

                                                                           

                                                                          Unless the components are suppressed or lightweight, you can't open them invisibly because they're already open invisibly. This is why it's particularly confusing to me why opening the part in its own window makes it rebuild differently.

                                                                          By invisible I mean using this method: swapp.DocumentVisible

                                                                            • Re: Grab Items from Selection and Rebuild Them
                                                                              Josh Brady

                                                                              Deepak Gupta wrote:

                                                                               

                                                                              Josh Brady wrote:

                                                                               

                                                                              Unless the components are suppressed or lightweight, you can't open them invisibly because they're already open invisibly. This is why it's particularly confusing to me why opening the part in its own window makes it rebuild differently.

                                                                              By invisible I mean using this method: swapp.DocumentVisible

                                                                              As far as I can tell from the documentation, that's a setting that you call in SW before you open a document.  Then, when you open the document, it's not visible.  Thing is, the documents are already open.  They're already (to use the wording of the help file) retrieved.   I'm pretty sure you can't OpenDoc7 a document that's already open as a component of an assembly.  You'll get an error that the document is already open.  Of course, you could open it in a second, invisible instance of SW, but that instance wouldn't be able to see the top-level assembly change that is trying to drive the rebuild.

                                                                    • Re: Grab Items from Selection and Rebuild Them
                                                                      Gertjan Van Dijk

                                                                      You might want to first "save" the selected items somewhere, before you start rebuilding, since you might accidentally change the selection set.

                                                                       

                                                                      Dim selectionSet as new List(of object)

                                                                      for i = 1 to SelCount

                                                                          selectionSet.Add(selMgr.GetSelectedObject6(i,-1)

                                                                      next i

                                                                       

                                                                      This way, you are certain to always have the initially selected items available.

                                                                       

                                                                      then, you can

                                                                       

                                                                      Dim selObj as object

                                                                      for j = 0 to selectionSet.count

                                                                           selObj = selectionSet(j)

                                                                         

                                                                           'do the actions here

                                                                      next j

                                                                        • Re: Grab Items from Selection and Rebuild Them
                                                                          Matt Peneguy

                                                                          I follow the logic, but I'm not very good with VBA.  The following line is not working.

                                                                          Dim selectionSet as New List(swModel)

                                                                          Here's what I've got so far:

                                                                          Sub main()

                                                                              Dim swApp As SldWorks.SldWorks

                                                                              Dim swModel As SldWorks.ModelDoc2

                                                                              Dim swSelMgr As SldWorks.SelectionMgr

                                                                              Dim SelCount As Integer

                                                                              Dim i As Integer

                                                                              Dim j As Integer

                                                                              Dim selObj As Object

                                                                              Dim selectionSet1 As New L

                                                                           

                                                                           

                                                                              Set swApp = Application.SldWorks

                                                                              Set swModel = swApp.ActiveDoc

                                                                              Set swSelMgr = swModel.SelectionManager

                                                                              SelCount = swSelMgr.GetSelectedObjectCount2(-1)

                                                                              For i = 1 To SelCount

                                                                                  selectionSet1.Add (swSelMgr.GetSelectedObject6(i, -1))

                                                                                  MsgBox ("i " & i)

                                                                              Next i

                                                                              For j = 0 To selectionSet1.Count

                                                                               selObj = selectionSet1(j)

                                                                               MsgBox ("j " & j)

                                                                           

                                                                           

                                                                              Next j

                                                                          End Sub