3 Replies Latest reply on Jan 23, 2017 12:58 PM by Brian Smith

    Handling multiple file types within a macro / macro feature

    Kevin Collins

      Hey all,


      I came to you guys because I know you're the smartest SW API users around!

      I have a bounding box macro feature designed to find the overall dimensions of a part and export them to the custom properties.

      The macro feature is embedded in the part template and it works great, for the most part.

      The macro feature is also programmed to re-order itself to the bottom of the tree so that any and all changes to the part are captured.


      After multiple rounds of error handling, I have the macro running to the point where it no longer errors out in any scenario I am likely to run across.

      That said, however, I do have an issue.  If I change dimensions of a part within the part file, the updated values export without issue.

      The point at which I run into a problem is if I try editing a part within an assembly.

      There are no errors, however, the values will not update unless I manually go into edit feature on the part file outside of the assembly and rebuild the feature from there.

      I think I understand why this is happening.  The following line of code tells SW to skip everything if the active file type is an assembly or drawing file:


      The problem is, if I remove this line of code, or comment it out, the part template which contains the feature will error out if an assembly or drawing file are open at the time of trying to create a new part.


      Does anyone know of an easy way around this?


      Does anyone know if the method "Getbodies2" will work on assembly or drawing files, or is this specific to part documents?


      I've attached the macro in it's current state if anyone can shed any light it would be appreciated.  I am being pushed to have this up and running by the end of the week.


      Thanks all!

        • Re: Handling multiple file types within a macro / macro feature
          Artem Taturevych

          Hi Kevin,


          When macro feature rebuilds the pointer to the feature is returned in the rebuild function


          Function swmRebuild(app As Variant, model As Variant, feat As Variant) As Variant


          The pointer is returned in the context of where the macro feature rebuilds. So if it rebuilds when the part is active than this is a part context. If this is rebuilt within the assembly component than this is an assembly context. That means that you can differentiate and find where it rebuilds.


          The IFeature interface can be cast to IEntity which has a GetComponent method.


          So if you call the feat.GetComponent and the returned result is not null that your macro feature is being rebuilt in the context of the assembly and you can find the model doc from the component. Something like that:


          Function swmRebuild(app As Variant, model As Variant, feat As Variant) As Variant


              Set swApp = app

              Set swModel = model


              Dim swComp As SldWorks.Component2

              Set swComp = feat.GetComponent


              If Not swComp Is Nothing Then

                  Set swModel = swComp.GetModelDoc2

              End If


              mainTestMacro2 swModel


          End Function