8 Replies Latest reply on Aug 17, 2018 2:00 PM by Sam Pike

    Editing Assembly Part Linear pattern

    Sam Pike

      Hello, another newbie question here.  I have came up to a similar problem which I just encountered in the past.

      I am trying to edit the dimensions D1 and D3  of a linear pattern that is on a part that is in an assembly.

      The name of the part names always change.

      The assembly has about 20 parts in it. If I run this code alone on just the part it works great.

      I cannot seam to get it to work while I am inside of the assembly though. I also would not prefer the user to find or preselect the pattern. I plan on rolling this into a form after I get this working. Please help.

       

      Dim swApp as sldworks.sldworks

      Dim swPart as sldworks.ModelDoc2

      Dim swDim as Variant

      Dim swDim2 as Variant

       

      Sub main ()

       

      Set swApp = Application.sldworks

      Set swPart = swApp.ActiveDoc

       

      Set swDim = swPart.Parameter("D1@LPattern1")

      swDim.Value = 2

       

      Set swDim = swPart.Parameter("D3@LPattern1")

      swDim2.Value = 2

       

      swPart.ForceRebuild3 (True)

      End Sub

        • Re: Editing Assembly Part Linear pattern
          Josh Brady

          You need to really actually understand what ever single line of your macro does.  And doesn't do.

           

          Look at "Set swPart = swApp.ActiveDoc".

          That makes the variable swPart refer to the active document.  Which, when you have an assembly active, is the assembly.

          "Set swDim = swPart.Parameter(...." says "Hey, swPart!" (which is the assembly, remember?) "Gimme your dimension called 'D1@LPattern1'".  Well, guess what.  The assembly doesn't own that dimension.  It's owned by the part.  swPart doesn't know what the heck you're talking about.  You need to get the ModelDoc2 that actually owns D1@....

           

          So, how is the macro supposed to know which part to search out for the dimension you want?  Your whole assembly can be full of parts that each have their own D1@LPattern1.  Are you pre-selecting the part you want to change?  If so, you have to tell your macro to look at what you've selected.  Every ModelDoc2 has what's called a SelectionManager.  It keeps track of what is selected and gives you programmatic access to it.  So, if you've preselected the part that you want to change its D1@LPattern1, you have to ask swPart for its SelectionManager.  Then ask the SelectionManager for the selected object (well, actually the selectedobjectscomponent), then you ask the component for its ModelDoc2, then you ask THAT modeldoc2 for its Parameter("D1@LPattern1")

            • Re: Editing Assembly Part Linear pattern
              Sam Pike

              Very good explanation. I always enjoy your responses as they are well explained.

              Unfortunately I do not want to preselect it And that is where I am getting even more lost.

              I can easily change the pattern name if that would uncomplicate it a little?

                • Re: Editing Assembly Part Linear pattern
                  Josh Brady

                  So... Just to make sure I'm on the right page...

                   

                  You have an assembly open.

                  It has a bunch of parts in it.

                  Some of these parts have linear patterns in them.

                  You want to change the dimensions in the parts that drive the linear pattern.

                   

                  Is that right?  If so, you have to think about how the macro can know which part you want to change dimensions in.  If you are sitting in front of this assembly on your screen, how do you decide yourself which part contains the dimension you want to modify? 

                    • Re: Editing Assembly Part Linear pattern
                      Sam Pike

                      Close.

                      I have an assembly open.

                      It has a bunch of parts in it.

                      Some of them have linear patterns. One part in particular has a special named linear pattern, I will call it BoltHoleLinearPattern. I want to update the D1 and D3 dimensions of the linear pattern in only one direction.

                       

                      D1 and D3 are the dimension names for the number of instances and distance that is given by Solidworks. Therefore I am only changing the dimensions of the linear pattern.

                       

                      So if I was to do it manually, I would expand the tree of all the parts In the assembly.

                       

                      Then I would start looking thru all the features of the individual parts one by one. And I am looking  for a specially named linear pattern called BoltHoleLinearPattern feature Once I found it, I would click on the part, to put it in edit mode, scroll to the BoltHoleLinearPattern, and edit the pattern.

                       

                      I would then change the numbers for the number of instances (which I believe is the D1) and the distance for that pattern (which I believe is D3)

                       

                      Then click out of editing the part, and do a rebuild.

                       

                      The assembly and parts are in a templete that gets Pack N Go for each new design. Names of all parts/assembly are changed during the Pack N Go Process but the feature names will remain the same. Therefore that is why I will change the name of the Pattern, so it is distinct.

                       

                      Hopefully that wasn’t too long of an explanation!

                        • Re: Editing Assembly Part Linear pattern
                          Josh Brady

                          Yep... That makes sense.  So basically it sounds like you need to make the macro do that exact same thing.  I think the key for understanding here is this... When you edit the part in the assembly, you're not actually changing or editing the assembly.  If you opened the part by itself and edited the pattern, the assembly wouldn't care when you open it.  It would just rebuild itself. 

                           

                          Lots of times in SolidWorks API, the way you find "things" is by looking at every single "thing" until you find the one that looks like what you want.  Sometimes that's done by traversing the feature tree, sometimes it's by iterating over all the "things" you get from a certain Get"Things" call.

                           

                          In this case, you're starting from the assembly.  You're looking for a certain component.  Well, actually, you're looking for the dimension of a certain ModelDoc2 that is a component of this assembly that contains a feature named "BoltHoleLinearPattern"

                           

                          Probably the most efficient way to do this is to get all of the assembly's components in an array using the assembly's GetComponents function.  Then, you will look at each Component2 in that array using a loop.  The Component2 object has a function called "FeatureByName".  Since you're looking for a certain name, this will work great.  If FeatureByName returns Nothing, go on to the next component.  If it's not Nothing, you found what you want.  So you get that Component2's ModelDoc2.  Then you can ask it for the Parameter("D1@BoltHoleLinearPattern"), and change that Dimension object's value.

                            • Re: Editing Assembly Part Linear pattern
                              Sam Pike

                              Ok, I am treading water here with some of the basics. Here is my attempt but I am running into errors.

                              Could you help untangle me?

                               

                              Option Explicit

                              Dim swApp As SldWorks.SldWorks
                              Dim swModel As SldWorks.ModelDoc2
                              Dim swAssy As SldWorks.AssemblyDoc
                              Dim swComp As SldWorks.Component2
                              Dim swDim As Variant
                              Dim swDim2 As Variant

                              Sub main()

                              Set swApp = Application.SldWorks
                              Set swModel = swApp.ActiveDoc
                              Set swAssy = swModel

                              Dim vComponents As Variant
                              vComponents = swAssy.GetComponents(True)
                              Set swComp = vComponents(0)

                              If swAssy.FeatureByName = BoltHoleLinearPattern Then
                                  Set swComp = swAssy
                                  'D1 equals instance
                                  Set swDim = swModel.Parameter("D1@BoltHoleLinearPattern")
                                  swDim.Value = 4

                                  'D3 equals distance
                                  Set swDim2 = swModel.Parameter("D3@BoltHoleLinearPattern")
                                  swDim2.Value = 2

                                  swModel.ForceRebuild3 (True)
                                 
                              If Not swAssy.FeatureByName Is Nothing Then

                              Loop

                              End If

                              End Sub

                                • Re: Editing Assembly Part Linear pattern
                                  Josh Brady

                                  Sam,

                                   

                                  I would highly recommend that you take some tutorials on VBA itself.  It will help you wrap your head around the basics, as you mentioned. The errors you're getting currently aren't even related to SolidWorks - they're basic syntax and logic flow.  It's really hard to learn all 3 things at once (how to program, how to use VBA, and SolidWorks API). 

                                   

                                  Then head over to CadSharp.com and get all you can from there related to SolidWorks API.  The SolidWorks object model is really big and complex, but it's valuable to learn.

                                   

                                   

                                   

                                  Option Explicit

                                  Dim swApp As SldWorks.SldWorks
                                  Dim swModel As SldWorks.ModelDoc2
                                  Dim swAssy As SldWorks.AssemblyDoc
                                  Dim swComp As SldWorks.Component2
                                  Dim swDim As Variant
                                  Dim swDim2 As Variant

                                  Sub main()

                                  Set swApp = Application.SldWorks
                                  Set swModel = swApp.ActiveDoc
                                  Set swAssy = swModel

                                  Dim vComponents As Variant
                                  vComponents = swAssy.GetComponents(True)

                                  'You need to start a loop here.  You don't have a loop. 

                                  dim i as long

                                  for i = 0 to ubound(vcomponents)
                                  Set swComp = vComponents(i)

                                  If swAssy.FeatureByName = BoltHoleLinearPattern Then this isn't how this function works.   Check help file.
                                  Set swComp = swAssy    Not sure what this line was supposed to do?

                                  if  not (swcomp.featurebyname("BoltHoleLinearPattern") is nothing) then

                                       'if the featurebyname does not return Nothing, that means we found the component whose

                                       'underlying ModelDoc2 we want to modify.

                                       set swModel = swcomp.getmodeldoc2
                                        'D1 equals instance
                                        Set swDim = swModel.Parameter("D1@BoltHoleLinearPattern")
                                        swDim.Value = 4

                                        'D3 equals distance
                                        Set swDim2 = swModel.Parameter("D3@BoltHoleLinearPattern")
                                        swDim2.Value = 2

                                        swModel.ForceRebuild3 (True)

                                       exit for 'we found what we wanted and did what we wanted. No need to keep going through the loop

                                  end if

                                  next i

                                  If Not swAssy.FeatureByName Is Nothing Then

                                  Loop

                                  End If

                                  End Sub

                                    • Re: Editing Assembly Part Linear pattern
                                      Sam Pike

                                      Thanks for all your help and patience. Yes I am looking into and trying to find some help for the basic stuff.

                                      I ended up getting the code working(by your graces). I had to change one small thing though. I had to change the swModel.ForceReuild3 to swAssy.ForceRebuild3. Anyways thanks for your help.

                                       

                                      Here is the final macro. Hopefully it can help someone else out.

                                      Option Explicit

                                      Dim swApp As SldWorks.SldWorks
                                      Dim swModel As SldWorks.ModelDoc2
                                      Dim swAssy As SldWorks.AssemblyDoc
                                      Dim swComp As SldWorks.Component2
                                      Dim swDim As Variant
                                      Dim swDim2 As Variant

                                      Sub main()

                                      Set swApp = Application.SldWorks
                                      Set swModel = swApp.ActiveDoc
                                      Set swAssy = swModel

                                      Dim vComponents As Variant
                                      vComponents = swAssy.GetComponents(True)
                                      Dim i As Long
                                      For i = 0 To UBound(vComponents)
                                      Set swComp = vComponents(i)

                                      If Not (swComp.FeatureByName("BoltHoleLinearPattern") Is Nothing) Then
                                          Set swModel = swComp.GetModelDoc2
                                          'D1 equals instance
                                          Set swDim = swModel.Parameter("D1@BoltHoleLinearPattern")
                                          swDim.Value = 4

                                          'D3 equals distance
                                          Set swDim2 = swModel.Parameter("D3@BoltHoleLinearPattern")
                                          swDim2.Value = 0.75
                                         
                                          swAssy.ForceRebuild3 (True)

                                      End If
                                      Next i

                                      End Sub