5 Replies Latest reply on Dec 1, 2017 3:47 AM by Fabian Mayer

    VBA macro to get the extrusion size

    Darren Smith

      Can anybody help???

       

       

      Is it possible, using a VBA macro to get the extrusion size?

       

       

      For example,

       

       

      Say I have created a simple rectangular sketch. Then I extrude the sketch so it becomes 12.7mm thick.

       

       

      How can I get the extrusion size (12.7mm in this case) using a VBA macro?

       

       

      Many thanks in advance!!!!

       

       

      Darren

        • Re: VBA macro to get the extrusion size
          Deepak Gupta

          Use GetDepth to get the feature length (the result would be in Meters) and then multiple the value with 1000 to get the value in MM

          • Re: VBA macro to get the extrusion size
            Keith Rice

            This will get the extrude depth in the first direction.

             

            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim swPart As SldWorks.PartDoc
            Dim swFeat As SldWorks.Feature
            Dim swExtrudeData As SldWorks.ExtrudeFeatureData2
            
            Sub main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                Set swPart = swModel
                Set swFeat = swPart.FeatureByName("Boss-Extrude1")
                Set swExtrudeData = swFeat.GetDefinition
                swExtrudeData.AccessSelections swModel, Nothing
                Debug.Print swExtrudeData.getdepth(True)
                swExtrudeData.ReleaseSelectionAccess
            End Sub
            

             

            Keith

            SolidWorks API Tutorials

              • Re: VBA macro to get the extrusion size
                Fabian Mayer

                I used the macro to get the extruded depth in a part document.

                Now I want to use it for a part in an assembly:

                1) Activate the specific part

                2) Return the extruded depth

                3) Reactivate the assembly

                 

                I managed to activate/deactivate the part, but I did not get how to adapt the macro above to get the extruded depth in this context. I am guessing that I need to adapt:

                1. Set swModel = swApp.ActiveDoc 
                2. Set swPart = swModel 

                But how?

                  • Re: VBA macro to get the extrusion size
                    Nilesh Patel

                    Hi Fabian,

                     

                    If you just want to get the depth of the extrude feature of a part, try following codes:

                     

                    Option Explicit
                    
                    
                    Dim swApp           As SldWorks.SldWorks
                    Dim swModel         As SldWorks.ModelDoc2
                    Dim swPart          As SldWorks.PartDoc
                    Dim swFeat          As SldWorks.Feature
                    Dim swExtrudeData   As SldWorks.ExtrudeFeatureData2
                    Dim swSelMgr        As SldWorks.SelectionMgr
                    Dim swComp          As SldWorks.Component2
                    Dim swCompModel     As SldWorks.ModelDoc2
                      
                    Sub main()
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        If swModel.GetType <> swDocumentTypes_e.swDocASSEMBLY Then Exit Sub
                        Set swSelMgr = swModel.SelectionManager
                        Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)
                        If swComp Is Nothing Then Exit Sub
                        Set swCompModel = swComp.GetModelDoc2
                        Set swPart = swCompModel
                        Set swFeat = swPart.FeatureByName("Boss-Extrude1")
                        Set swExtrudeData = swFeat.GetDefinition
                        swExtrudeData.AccessSelections swCompModel, Nothing
                        Debug.Print swExtrudeData.GetDepth(True) * 1000
                        swExtrudeData.ReleaseSelectionAccess
                    End Sub
                    

                     

                    You will need to select the part in the assembly before running the macro

                     

                    Regards,

                    Nilesh