3 Replies Latest reply on Mar 28, 2017 5:00 AM by Peter Brinkhuis

    Drawing Note Macro Help: Calling out part properties in assembly drawing

    Brett Stainken

      I am looking for a macro to look into the properties of each part in an assembly, and if the part has a "filter" property with a "yes" in the value/txt expression, to display a note "flow" with an arrow for each part that has a "yes" in the filter property value.

      This is as far as I have gotten:

      Sub CreateAnote()

          Dim swApp As Object

          Dim Model As Object

          Dim str As String

         

          Const swDocPART = 1

          Const swDocASSEMBLY = 2

          Const swDocDRAWING = 3

         

      Set swApp = CreateObject("SldWorks.Application")

      Set Model = swApp.ActiveDoc

          If Model Is Nothing Then

              Exit Sub

          End If

          If (Model.GetType <> swDocDRAWING) Then

              swApp.SendMsgToUser ("Only used for drawings")

              Exit Sub

          End If

          str = "Flow>"

          Model.CreateText str, 0.09, 0.08, 0, 0.01, 0

      End Sub

       

      Thanks for your help

        • Re: Drawing Note Macro Help: Calling out part properties in assembly drawing
          Peter Brinkhuis

          If I understand it correctly, you want to:

          1. Have a drawing open with at least one assembly view
          2. Traverse all assembly parts (even sub- and subsubassemblies?)
          3. Look for the custom property "filter" in the parts (also assemblies?) to have the value "yes"
          4. Add a note "Flow>" to each part in the drawing (does the direction matter?) that has the correct property

           

          I assume there are multiple assembly traversal macro examples available, let me search for that and update the answer.

          Edit: found some reading material:

          2012 SOLIDWORKS API Help - InsertNote Method (IModelDoc2)

          2012 SOLIDWORKS API Help - Traverse Assembly at Component Level Example (VBA)

          2012 SOLIDWORKS API Help - GetRootComponent3 Method (IConfiguration)

           

          What the macro does so far is:

          1. Check if a drawing is currently open
          2. Get the selected drawing view
          3. Get the model in the drawing view (2012 SOLIDWORKS API Help - Get Document Referenced by Drawing View Example (VBA) )
          4. Make sure it's an assembly
          5. Open the assembly and activate it
          6. Get the current configuration
          7. Get the root component
          8. Todo: Get the children of that component (should return null for part document)
          9. Todo: Use the customPropertyManager to check if the cust prop is present
          10. Todo: Create array of parts found with the right property
          11. Todo: Get next component
          12. Return to drawing
          13. Todo: Add notes to found parts

           

          Option Explicit

           

          Sub CreateAnote()

              Dim swApp               As SldWorks.SldWorks

              Dim swModel             As ModelDoc2

              Dim swDrawModel         As ModelDoc2

              Dim swDraw              As DrawingDoc

              Dim swSelMgr            As SelectionMgr

              Dim swView              As View

              Dim swConf              As Configuration

              Dim swComp              As Component2

           

              Dim modelName           As String

              Dim custPropText        As String

              Dim errors              As Long

              Dim warnings            As Long

              Dim boolstatus          As Boolean

                

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

           

              If swModel Is Nothing Then Exit Sub

           

              If swModel.GetType <> swDocDRAWING Then

                  swApp.SendMsgToUser ("Please open a drawing and select a view first.")

                  Exit Sub

              End If

             

              'Set this for adding the notes later

              Set swDraw = swModel

             

              'Get selected view

              Set swSelMgr = swModel.SelectionManager

              Set swView = swSelMgr.GetSelectedObject5(1)

             

              If swView Is Nothing Then

                  swApp.SendMsgToUser ("Could not get selected drawing view.")

              End If

             

              ' Get model in view

              Set swDrawModel = swView.ReferencedDocument

             

              If swDrawModel Is Nothing Then

                  swApp.SendMsgToUser ("Could not get model in selected drawing view.")

                  Exit Sub

              End If

             

              modelName = swView.GetReferencedModelName

           

              'Check for assemblies

              If UCase(Right(modelName, 6)) <> "SLDASM" Then

                  swApp.SendMsgToUser ("This macro only works with assembly drawings.")

                  Exit Sub

              End If

            

              'Open assy

              Set swDrawModel = Nothing

              'Open assy

              Set swDrawModel = swApp.OpenDoc6(modelName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

             

              If swDrawModel Is Nothing Then

                  MsgBox ("Could not open assembly")

                  Exit Sub

              End If

             

              'Activate assy if document was already open

              Set swDrawModel = swApp.ActivateDoc3(swDrawModel.GetTitle, True, False, errors)

             

              Set swConf = swDrawModel.GetActiveConfiguration

             

              'Get root component, the assy itself

              Set swComp = swConf.GetRootComponent3(True)

             

              'Traverse tree

              'Use customPropertyManager to check

              'Add part names of found parts with custom property to array

             

              'Go back to drawing

              Set swModel = swApp.ActivateDoc3(swModel.GetTitle, True, False, errors)

             

              'Activate originally selected view, might not be necessary

              boolstatus = swDraw.ActivateView(swView.Name)

             

              'For every part in array

              'CreateText

             

              custPropText = "Flow>"

              'Model.CreateText str, 0.09, 0.08, 0, 0.01, 0

          End Sub

            • Re: Drawing Note Macro Help: Calling out part properties in assembly drawing
              Brett Stainken

              Thank you peter for responding,

              I took your code and ran it and it spit back an error on line 19

                     Set swDrawModel = swView.ReferencedDocument

                   or getting the drawing in document view?

              As per my intention with the macro yes, something that goes through all parts/sub-assemblies, see filter=yes and display flow as a note on the drawing. The arrow/direction i can manually put in with a leader.

               

              Why did you leave out the SldWorks.etc on the first few lines?

                • Re: Drawing Note Macro Help: Calling out part properties in assembly drawing
                  Peter Brinkhuis

                  You need to select a drawing view before running the macro. That was the quickest route to make this. Turns out I added the warning, but forgot to quit the macro when no view is selected. I added the exit line:

                   

                  If swView Is Nothing Then

                          swApp.SendMsgToUser ("Could not get selected drawing view.")

                          Exit Sub

                  End If

                   

                  The SldWorks. isn't necessary because the variable types (ModelDoc2 etc) are recognized anyway. If you check the macro references (Tools > References) you'll see there are some SolidWorks libraries selected that provide all of the functions related to SolidWorks, including the variable types (well, they probably are classes actually).

                   

                  You'll just need to add some code to get the assembly traversal done. The examples should really help you with that.