37 Replies Latest reply on Dec 19, 2018 6:31 PM by Cordell Hollingsworth

    How do you add Macro as Macro Feature?

    Len Richter

      Hello.

       

      I want to have a Macro run every time the assembly gets rebuilt. My understanding that this is done by adding a Macro to the feature tree as a Macro Feature.  I do not understand how to add a Macro feature from the SolidWorks help file.  Nor can I find a simple tutorial (step by step instruction) or video on how to do this. Does any know of one?

       

      I have an assembly which I have added 2 angle mates so I can by suppress 1 of the 2 mates to turn the part at that particular angle. I want the macro to run every time after I suppress 1 angle mate & unsuppress the other. Does anybody know by doing this causes a rebuild which in turn cause the macro to run?

       

      Regards,

      Len

        • Re: How do you add Macro as Macro Feature?
          Deepak Gupta

          I'm not sure how to add Macro Feature for this but can you have a macro that can suppress one mate and unsuppressing other and finally rebuild the assembly. Then you can place the macro in the SolidWorks macro folder and assign a keyboard shortcut.

           

          You can add option in the macro like a check box so that you can select which which to suppress/unsuppress or the macro can check if one is suppressed then unsuppress it and suppress it.

           

          Does it make sense??

          • Re: How do you add Macro as Macro Feature?
            Tobin Sparks

            Howdy Len,

             

            I might have something that will help you create a macro feature that will run at each rebuild (Ctrl+Q). I'll have to do it on Monday though. Have a good one :-)

            • Re: How do you add Macro as Macro Feature?
              Keith Rice

              Len,

               

              I feel your pain.  There are no tutorials out there for many aspects of the API, and no video tutorials for any aspect.  I'm trying to get a course up before too long that will address that.

               

              In the mean time, I have attached a macro that will create a macro and then run another macro on rebuild.  You must edit the information in the "Feature_Module" module so that it points to 1) the location of the .swp, 2) the module within that .swp that contains the procedure you want to run, and 3) the name of the procedure you want to run.  See the attached picture.

               

              Keith Rice

                • Re: How do you add Macro as Macro Feature?
                  Len Richter

                  Thanks Keith for your code.  It works but having problem with my macro returning nothing for " swSketch = Part.GetActiveSketch2" when it is added as a feature.  However if the macro is not added as a feature it runs fine.  I have listed the code below.  Do you have any thoughts on why this is happening?

                   

                     'Get a reference to an already running session of SolidWorks.

                     Set swApp = Application.SldWorks

                     If swApp Is Nothing Then 'Verify handle to SolidWorks.

                        'Put a msgbox here to warn the user that the macro couldn’t attach to SolidWorks.

                        MsgBox "swApp=Nothing returned for instance of SolidWorks running."

                        Exit Sub

                     End If

                   

                     'Get a reference to the active document.

                     Set Part = swApp.ActiveDoc

                     If Part Is Nothing Then 'Verify handle to Part.

                        'Put a msgbox here to warn the user that the macro couldn’t attach to Part.

                        MsgBox "Part=Nothing returned for reference to active document (Part)."

                        Exit Sub

                     End If

                   

                     'Get a reference for the selection manager for this model.

                     Set swSelMgr = Part.SelectionManager

                     If swSelMgr Is Nothing Then 'Verify handle to selection manager for this part.

                        'Put a msgbox here to warn the user that the macro couldn’t reference to SelectionManager.

                        MsgBox "swSelMgr=Nothing returned for reference to Selection Manager."

                        Exit Sub

                     End If

                   

                     'Select the Sketch.

                     boolstatus = Part.Extension.SelectByID2("3DSketch1 - Cable Iris Path@Cable Iris Connector^C2642 - CC5MPX Assembly-2@C2642 - CC5MPX Assembly", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                   

                     'Edit the sketch.

                     Part.EditSketch

                   

                     'Grab a reference to the active sketch.

                     Set swSketch = Part.GetActiveSketch2

                     'NOTE:  when this macro is added as a Macro Feature, it always returns nothing for swSketch.

                     If swSketch Is Nothing Then 'Verify handle to active sketch for this part.

                        'Put a msgbox here to warn the user that the macro couldn’t get reference to active sketch.

                        MsgBox "swSketch=Nothing returned for reference to active/edit sketch."

                        Exit Sub

                     End If

                   

                  Regards,

                  Len

                    • Re: How do you add Macro as Macro Feature?
                      Keith Rice

                      Len, is this the code for the macro that you are trying to run via the macro feature?  Otherwise I'm not really sure what you mean.  My understanding was that you were trying to run a separate macro every time that the macro feature rebuilds, in which case the above code would not be part of the macro feature.  Clarify this for me and I'll be happy to give some insight.

                       

                      Keith

                      CADSharp LLC

                        • Re: How do you add Macro as Macro Feature?
                          Len Richter

                          Hi Keith.

                           

                          Yes this is code is for the Macro that I am trying to run via the macro feature.  Ever time I suppress 1 angle mate & unsuppress the other angle mate I am hoping this will cause a rebuilt & have it run this macro code.

                           

                          Len

                            • Re: How do you add Macro as Macro Feature?
                              Keith Rice

                              Hi Len,

                               

                              Your code does not work because you cannot edit features during the rebuild function of a macro feature.  This is stated in the API Help in the "MacroFeatureData" page.  The reason being is that SolidWorks does not know how to handle rebuilds within rebuilds, and that is essentially what is happening when you try to edit a feature (even a sketch, which the API considers a type of feature) during the rebuild function of the macro feature.  You would have the same problem if you tried to edit a mate, too.  So I am afraid that macro features are not an option for what you are trying to accomplish.

                               

                              If your ultimate goal is just to do something after a rebuild, then you would want a notification that fires every time a rebuild is complete.  This is done using the RegenPostNotify2 assembly notification.  You would not want to use this for your purposes, however, because you will never be able to leave edit sketch mode!  Here's why: the macro would be listening for ANY regeneration event.  Regeneration occurs as soon as you leave your sketch.  So as soon as you left the sketch, your macro would be run again, and you would be right back in your sketch.

                               

                              If your end goal is simply to have one angle mate unsuppressed when another is suppressed, you would still need to use notifications, but there is no notification that can be fired for when a mate is suppressed, so you would want to use OnIdleNotify, which will wait until nothing else is occurring (which would be the cause after you suppressed the mate) before firing, and therefore running your other macro.

                               

                              If you desire to go this route then check out this example in the API Help to get your started.

                               

                              Keith
                              CADSharp LLC

                        • Re: How do you add Macro as Macro Feature?
                          Cordell Hollingsworth

                          Keith, awesome macro! I ran it on my part template to add a macro feature to my macro and it works great! It runs my macro on every rebuild as expected. I also ran your macro on my assembly template and it creates a macro feature just like on the part. However, in the assembly, the macro doesn't rebuild or at least doesn't run the macro, even when I press ctrl+Q.

                           

                          What is going wrong?

                          Is this because Solidworks treats assembly features differently than it treats part features? Or does it have something to do with the macro feature code? Or could it be something else?

                           

                          Thanks in advance for any advice you can give!

                        • Re: How do you add Macro as Macro Feature?
                          Scott McFadden

                          Len,

                          Is this what you are talking about?  This is the procedure I used to use.

                          Sorry, I can't help you with the macro creation, because I am not good with those.

                           

                          http://help.solidworks.com/2011/English/api/sldworksapiprogguide/GettingStarted/Assign_SolidWorks_Macro_to_Button.htm

                          • Re: How do you add Macro as Macro Feature?
                            Tobin Sparks

                            Howdy Len,

                            Starting in SW 2009 there is a way to create a macro feature that will appear in the feature tree and imbed code into your model that will run when triggered by the rebuild (Ctrl+Q) command. Are you using SW 2009 or 2010? (I have no way to test it in 2011 yet. I also just recieved a new computer (Windows 7) but am still using SW 2007 for production. I will have a test copy of 2010 loaded again soon.)

                              • Re: How do you add Macro as Macro Feature?
                                Len Richter

                                Hi Tobin.

                                 

                                I am using 2011 SP4.

                                 

                                Thanks for your reply,

                                Len

                                  • Re: How do you add Macro as Macro Feature?
                                    Tobin Sparks

                                    Howdy,

                                    Sorry about the delays. I seem to suddenly be in several whirlwinds :-)

                                    Attached is an example of how to create a macro feature.

                                    Once you run the macro it creates a feature that shows up in the feature tree.

                                    Your code in now imbedded in your model (no need to run the macro again).

                                    This example creates a point sketch and each time the rebuild command is used

                                    a msgbox lets you know the code has been triggered.

                                    Be sure to put the .bmp files in the same directory as the macro.

                                    Also the model you are using for experimenting must be a saved model.

                                    This works with SW 2010 x64 in Windows 7.

                                    You will have to change the reference libraries to your version.

                                    I currently have no way of testing this in SW 2011 :-(

                                     

                                    Hope this helps

                                      • Re: How do you add Macro as Macro Feature?
                                        Wayne Matus

                                        I have placed a CGpoint Macro Feature macro on my website for download.

                                         

                                        http://wlmservices.net/downloads.html

                                         

                                        The main sub in the macro creates a 3D sketch and a sketchpoint in the sketch at the CG of the model. It also creates the Macro Feature and makes it the sub feature of the 3D sketch. The swmRebuild function runs when the Macro Feature is rebuilt. It calculates the new center of mass and moves the sketchpoint to the new location.

                                         

                                        The reason for not attaching the macro to this message is that I am an independent contractor. I want to send traffic to my website in hopes of obtaining work.

                                         

                                        I will post the tutorial on my website as soon as it is finished.

                                          • Re: How do you add Macro as Macro Feature?
                                            Len Richter

                                            I would like to 1st thank Keith, Tobin & Wayne for your help.

                                             

                                            According to Keith, what I am trying to do can not done in the rebuilt.  I am trying to do is I have a 3 parts (camera body, lens & cable attaching to both).  The cable is between the body & lens by a means of sweep feature of a 3D sketch of a spline.  When the lens turns on the body using the angle mates it causes a rebuilt which I want after is the reset all the spline handles of that 3D spline.  This makes the cable a nice curved cable again.  Keith said that my "code does not work because you cannot edit features during the rebuild function of a macro feature.".  Also that spline handles can only be reset when you edit a 3D sketch.  I tried to get the OnIdle event as what Keith suggested but could not get it to fire.

                                             

                                            I have ran all your guys macros.  I though maybe this Wayne's "sub feature" might have some sort of solution but do not see it becuase I do not fully understand it.

                                            Len

                                              • Re: How do you add Macro as Macro Feature?
                                                Keith Rice

                                                Interesting macro!  Attached is an example of OnIdleNotify.  Just watch the immediate window and you'll see that it is firing.  Hope that helps.

                                                 

                                                Keith

                                                CADSharp LLC

                                                  • Re: How do you add Macro as Macro Feature?
                                                    Len Richter

                                                    Thanks Keith.  I did did not have the connect function.

                                                    The only problem now is it runs too often & really slows down SloidWorks.  I only want to trigger a event after only when angle mate is unsuppresed.  Is there a event that fires after unsuppressing a feature?  Or I was thinking of some how using a global variable that indicates if mate suppress then say run the routine to reset the handles only when variable set.  Is there a complete list of the events some where.  I could not really find anything on the web or help.

                                                     

                                                    Len

                                                      • Re: How do you add Macro as Macro Feature?
                                                        Keith Rice

                                                        I've had mixed results with OnIdleNotify slowing SWX down when I've used it in the past.  But I know exactly what you're talking about.  In that case, as I said in my earlier post, is there no notification for post-mate suppress/unsuppress.  However I think I remember you saying that the suppress/unsuppress actually occurs when you change configurations.  In that case, there is a notification called "swAssemblyConfigChangePostNotify".  To use this notification, go into the class module, and create a new variable:

                                                         

                                                        Dim WithEvents swAssy as SldWorks.AssemblyDoc

                                                         

                                                        Then use the drop downs at the top to choose swAssy in the first drop down and the proper notification in the second drop down.  See the attached picture.  Then you will have the function available to you to place code in.

                                                         

                                                        The notification lists are tricky to find in the Help since they are listed as enumerations.  If you want to see all of the notifications available to assembly documents, search for "swAssemblyNotify_e" in the index.  (Note: I recommend using the offline API Help)

                                                         

                                                        Keith

                                                        CADSharp LLC

                                                          • Re: How do you add Macro as Macro Feature?
                                                            Len Richter

                                                            Hi Keith.

                                                             

                                                            I did get working ywith our suggestion of using "swAssemblyConfigChangePostNotify" but now found another problem. If you now derive another assembly (now main assembly) from the my orginal assembly (now sub assembly) that has the macro in it, the macro will not run at all in the sub assembly.  The main assembly configuration changes the configuration in the subassembly which in turns should run the macro but it does not. The macro will also not run if you add the sub assembly (1 with macro it it) to a drawing & in the drawing change the configuration. I do not know if Keith or Wayne have any suggestions?

                                                             

                                                            Regards,

                                                            Len

                                                              • Re: How do you add Macro as Macro Feature?
                                                                Wayne Matus

                                                                Len,

                                                                 

                                                                I am a little confused, could you upload your macro? Reason for confusion  is you mention your are using a ConfigChangePostNotify which sounds like you are using a notifications macro but you also mention sub assembly with macro in it which sounds like a macro feature. If you are running a notifications macro, then it only will process the swAssemblyConfigChangePostNotify sub when the active document  has changed configuration. This would explain why it is not working properly when your assembly is inserted in an uperlevel assembly or from a drawing of the assembly.

                                                                  • Re: How do you add Macro as Macro Feature?
                                                                    Len Richter

                                                                    Wayne see embedded asnwers below:

                                                                     

                                                                    >>I am a little confused, could you upload your macro?

                                                                     

                                                                     

                                                                    I have 2 marcos attached. The "Macro - Run CC5MPX Iris Cable macro from macro feature" macro is the 1 Keith done to add in "Macro - CC5MPX Iris Cable Spline Reset All Handles Reset" as macro feature in the sub assembly as you figured out. Then due to running notifications macro to Then that is what is happening

                                                                     

                                                                     

                                                                    >>Reason for confusion  is you mention your are using a ConfigChangePostNotify which sounds like you are using a notifications macro

                                                                    >>but you also mention sub assembly with macro in it which sounds like a macro feature..

                                                                     

                                                                    Correct.  This is what I am doing. 

                                                                     

                                                                     

                                                                    >>If you are running a notifications macro, then it only will process the swAssemblyConfigChangePostNotify sub when the active document  has changed configuration.

                                                                     

                                                                    Is there a different solution then to execute an event like swAssemblyConfigChangePostNotify when the configuration has changed by either main assembly or in a drawing?

                                                                     

                                                                    Best Regards,

                                                                    Len

                                                                      • Re: How do you add Macro as Macro Feature?
                                                                        Wayne Matus

                                                                        The problem with it being a sub assembly is that it is not selecting the sketch since its name has changed.

                                                                         


                                                                        'Select the Sketch.  Note: "-2" indicates 2nd (<2>) instance of assembly.

                                                                        boolstatus = Part.Extension.SelectByID2("3DSketch1 - Cable Iris Path@Cable Iris Connector^C2642 - CC5MPX Assembly-2@C2642 - CC5MPX Assembly", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                                                                         

                                                                        You would now need to add the top level assembly name to the sketch name.

                                                                         

                                                                        For when it is in a drawing, you will have to handle drawing events just like it is handling assembly events. There is a swDrawingConfigChangePostNotify event, I did not test, but I am guessing that it is monitoring when user changed the referenced model's configuration in a drawing view. I did modify your macro partially for this, but not sure about the code that needs to be in this event. You may have the same issue as you have in selecting the sketch in the top level assembly.

                                                                          • Re: How do you add Macro as Macro Feature?
                                                                            Len Richter

                                                                            Hi Wayne.

                                                                             

                                                                            swDrawingConfigChangePostNotify event never fires but the only event I can seem to get to work if I force a rebuilt using <CTNL Q> is swDraw_FeatureManagerTreeRebuildNotify.

                                                                             

                                                                            Also sorry for the newbie question by I really do not fully understand how the path works to add the top level assembly "C2670 - CC5MPX Heater Assembly".  Tried following but does not work:

                                                                             

                                                                            boolstatus = Part.Extension.SelectByID2("3DSketch1 - Cable Iris Path@Cable Iris Connector^C2642 - CC5MPX Assembly-2@C2670 - CC5MPX Heater Assembly", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                                                                            boolstatus = Part.Extension.SelectByID2("3DSketch1 - Cable Iris Path@Cable Iris Connector^C2642 - CC5MPX Assembly-2@C2642 - CC5MPX Assembly@C2670 - CC5MPX Heater Assembly", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                                                                             

                                                                            Len

                                                                              • Re: How do you add Macro as Macro Feature?
                                                                                Wayne Matus

                                                                                In this example of full sketch name "Sketch1@Leg&Wheels-1@Full_Grill_Assembly/axle-1@Leg&Wheels"

                                                                                 

                                                                                "Sketch1" is the name of the sketch.

                                                                                "axle" is the name of the part file "-1" is the instance of the axle part file.

                                                                                "Legs&Wheels" is the name of the sub assembly. "-1" is its instance.

                                                                                "Full_Grill_Assembly" is the name of the top level assembly.

                                                                    • Re: How do you add Macro as Macro Feature?
                                                                      Wayne Matus

                                                                      This macro should get you going if you can live with event notifications. With any event notification macro, it runs in the backgroud, so after you start it it will continue to run until you exit SolidWorks.

                                                                       

                                                                      The macro monitors feature suppress/unsuppress events in assembly files. The event passes the feature object and if the feature was suppressed or unsuppressed. So you can check the feature name and only do what you want for the particular mate.

                                                                        • Re: How do you add Macro as Macro Feature?
                                                                          Len Richter

                                                                          Hi Wayne.

                                                                           

                                                                          I ran your macro in my assembly & whether I change configuration which suppress/unsuppress mate or do it manually the event will not swApp_ActiveDocChangeNotify() or wAssy_SuppressionStateChangeNotify() fire.  Can see MonitorSolidWorks() run ok.

                                                                           

                                                                          I did get Keith suggestion to work with swAssy_ActiveConfigChangePostNotify to work by using your swApp_ActiveDocChangeNotify() code (refer below) in events class.  Then did the same in your macro but still would not work.

                                                                           

                                                                          Private Sub Class_Initialize()
                                                                              Set swApp = Application.SldWorks 'Get a reference to an already running session of SolidWorks.

                                                                              Dim swModel As SldWorks.ModelDoc2
                                                                              'Connect to the active document.
                                                                              Set swModel = swApp.ActiveDoc
                                                                              'Check if the active document is an assembly.
                                                                              If (swModel.GetType) = swDocASSEMBLY Then
                                                                                  'Connect to the assembly doc object so that you can monitor its events.
                                                                                  Set swAssy = swModel
                                                                              End If
                                                                              Debug.Print "clsAssemblyEvents():  Start Event Notification listening."
                                                                          End Sub

                                                                           

                                                                          Private Function swAssy_ActiveConfigChangePostNotify() As Long

                                                                             Debug.Print "ActiveConfigChangePostNotify() event."

                                                                          End Function

                                                                           

                                                                          Len

                                                                            • Re: How do you add Macro as Macro Feature?
                                                                              Wayne Matus

                                                                              I did not test my macro by suppressing/unsuppressing mates by changing configurations.

                                                                               

                                                                              The reason why my macro did not work on manually suppressing/unsuppressing mates is because the swApp_ActiveDocChangeNotify sub code does not run until the user changes the active document. So if you run my macro and then open your assembly and then manually suppress a mate it will run the part of code in the  swAssy_SuppressionStateChangeNotify sub.

                                                                               

                                                                              If you want my macro to connect to the open assembly then you would need to copy the code in my swApp_ActiveDocChangeNotify and paste it into my MonitorSolidWorks sub. It would look like this.

                                                                               

                                                                              Public Sub MonitorSolidWorks()
                                                                                  Set swApp = Application.SldWorks
                                                                                  Dim swModel As SldWorks.ModelDoc2
                                                                                  ' Connect to the active document
                                                                                  Set swModel = swApp.ActiveDoc
                                                                                  ' Check if the active document is an assembly
                                                                                  If swModel.GetType = swDocASSEMBLY Then
                                                                                      ' Connect to the assembly doc object so that you can monitor its events
                                                                                      Set swAssy = swModel
                                                                                  End If
                                                                              End Sub

                                                                                • Re: How do you add Macro as Macro Feature?
                                                                                  Len Richter

                                                                                  Hi Wayne.

                                                                                   

                                                                                  Actually that is what I was doing but instead of using  MonitorSolidWorks sub, I was using the class constructor Class_Initialize().  As you know the constructor runs automatically when the class is created with the new instruction.

                                                                                   

                                                                                  Len

                                                                                    • Re: How do you add Macro as Macro Feature?
                                                                                      Wayne Matus

                                                                                      I did test and you are right, changing configurations does not fire an suppressions state change,but the manual suppressing works for me and should work for you.

                                                                                       

                                                                                      leave out was check for if the macro was started when there were no documents opened.

                                                                                       

                                                                                      Private Sub Class_Initialize()
                                                                                          Set swApp = Application.SldWorks
                                                                                          Debug.Print "Class_Initialize"
                                                                                          Dim swModel As SldWorks.ModelDoc2
                                                                                          ' Connect to the active document
                                                                                          Set swModel = swApp.ActiveDoc
                                                                                          ' Make sure there is a document active when  macro is launched
                                                                                          If Not swModel Is Nothing Then
                                                                                              Debug.Print swModel.GetTitle
                                                                                              ' Check if the active document is an assembly
                                                                                              If swModel.GetType = swDocASSEMBLY Then
                                                                                                  ' Connect to the assembly doc object so that you can monitor its events
                                                                                                  Set swAssy = swModel
                                                                                              End If
                                                                                          End If
                                                                                      End Sub

                                                                • Re: How do you add Macro as Macro Feature?
                                                                  Wayne Matus

                                                                  I have done some testing and yes, suppressing a mate does constitute a Macro Feature rebuild. So you should be able to accomplish what you want.

                                                                   

                                                                  I am working on a tutorial on Macro Features since there is no one source of information for this complex functionality. I will make it available and let you know when it is finished.

                                                                    • Re: How do you add Macro as Macro Feature?
                                                                      Wayne Matus

                                                                      I spoke a little to soon. I first thought you only needed the macro feature to toggle the two mates. If mate1 is suppressed, unsuppress mate2. I then looked closer at the thread and still had some hope. I am able to edit a sketch in the macro feature rebuild function, but the requirement is that the you have to make the sketch feature a sub feature of the macro feature. That works fine on a part file, but you cannot make a macro feature in the assembly a sub feature of a sketch in a part file.