6 Replies Latest reply on Jul 22, 2018 1:03 PM by Deepak Gupta

    Needs a macro to Show mass in Kg

    Sumit Rana

      Hi everyone,

      I'm new to macros and been constantly learning about them from forum, YouTube and Cadsharp. Coming to the question i needs a macro to put a note in drawing showing part/assembly weight in kg if the weight is above 1000gm and showing the weight in kg if it's below 1000gm.

       

      I'm aware of creating equation in part level then attach it with the custom property then link the note in drawing to custom property. But i can't do this with all the assembly parts.

        • Re: Needs a macro to Show mass in Kg
          Solid Air

          Do you already have a note in your drawing that displays mass of the model?

           

          Do you have some idea on how you would like to get the mass in not?

           

          Where do you want to locate the note (or do you not need that)?

            • Re: Needs a macro to Show mass in Kg
              Sumit Rana

              Yes I already have the note which is linked to the mass property of the part.

              Location of the note does not matter (well actually it does but I don't want you to do the whole work for me I just needs the code later I can modify it as per my requirements)

               

              Sumit

                • Re: Needs a macro to Show mass in Kg
                  Solid Air

                  I assume you know the basics of getting the SolidWorks object and object of the active document (in this case a drawing).  Next you need to get the model object of the part/assembly from a drawing view.  After you have the model object, you then get its mass property object to find the model's mass.  Now you  analyze the mass to determine if the mass is greater or less than 1000 grams so you know what units to display in your note.  After that it is a matter of creating the note.  I can help you with getting to the mass part but I have not programmed creating a note on a drawing in a long time (but I found I could record creating one which will get you close except for controlling its placement on the drawing).  Below is code for getting mass.

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim swDoc As SldWorks.ModelDoc2
                  Dim swDrwDoc As SldWorks.DrawingDoc
                  Dim swView As SldWorks.View
                  Dim swViewModel As SldWorks.ModelDoc2
                  Dim swMassProp As SldWorks.MassProperty
                  
                  Sub Main
                  
                      Dim ModelMass As Double
                  
                      Set swApp = Application.SldWorks
                      Set swDoc = swApp.ActiveDoc
                      
                      Set swDrwDoc = swDoc
                      
                      'call twice because first view is drawing format
                      Set swView = swDrwDoc.GetFirstView
                      Set swView = swView.GetNextView
                      
                      Set swViewModel = swView.ReferencedDocument
                      
                      Set swMassProp = swViewModel.Extension.CreateMassProperty
                          
                      'mass is returned in KG
                      ModelMass = swMassProp.Mass
                  
                  End Sub
                  
              • Re: Needs a macro to Show mass in Kg
                Deepak Gupta

                I would suggest you to use equations rather than the macro. And better add the equation into the part template.