5 Replies Latest reply on Oct 5, 2012 4:24 AM by Yong Ning

    How to use API to measure the specified area?

    Yong Ning

      a0.jpg

       

      I need measure the specified area (Green).

      The method is API.

       

      Thanks.

        • Re: How to use API to measure the specified area?
          Artem Taturevych

          Use IMeasure interface. Here is the example:

           

          http://help.solidworks.com/2012/English/api/sldworksapi/Measure_Selected_Entities_Example_VB.htm

          ______________________

          Regards,

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

          • Re: How to use API to measure the specified area?
            Keith Rice

            Yong, you could use the IMeasure interface:

             

            =========

             

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim swMeasure As SldWorks.Measure

             

            Sub main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swMeasure = swModel.Extension.CreateMeasure

                swMeasure.Calculate Nothing

                Debug.Print swMeasure.Area & " m^2"

            End Sub

             

            =========

             

            Or you could IFace2::GetArea.

             

            =========

             

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim swSelMgr As SldWorks.SelectionMgr

            Dim swFace As SldWorks.Face2

             

            Sub main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swSelMgr = swModel.SelectionManager

                Set swFace = swSelMgr.GetSelectedObject6(1, -1)

                Debug.Print Round(swFace.GetArea, 8) & " m^2"

            End Sub

             

            =========

             

            Hope this helps.

             

            Keith

            Video Tutorials for the SolidWorks API

              • Re: How to use API to measure the specified area?
                Yong Ning

                Thank your code

                ---------------------

                I use Solidworks 2006,

                I need code is

                 

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swSelMgr As SldWorks.SelectionMgr

                Dim swFace As SldWorks.Face2

                 

                Sub main()

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    Set swSelMgr = swModel.SelectionManager

                    Set swFace = swSelMgr.GetSelectedObject6(1, -1)

                    Debug.Print Round(swFace.GetArea, 8) & " m^2"

                End Sub

                 

                -----------------------

                 

                Set swMeasure = swModel.Extension.CreateMeasure →Must use Solidworks 2010(2011 , 2012, 2013)

                  • Re: How to use API to measure the specified area?
                    Yong Ning

                    Solidworks API help file  is trouble

                     

                    'Sweep Planar Loop Along Vector Example (VB)

                    'This example shows how to sweep a planar loop along a vector.

                     

                    '---------------------------------

                    '

                    ' Preconditions: Model document is open and a planar face is selected.

                    '

                    ' Postconditions: Planar loop is swept along the specified vector.

                    '

                    '---------------------------------

                    Option Explicit

                    Sub main()

                        Dim swApp                   As SldWorks.SldWorks

                        Dim swModel                 As SldWorks.ModelDoc2

                        Dim swSelMgr                As SldWorks.SelectionMgr

                        Dim swFace                  As SldWorks.Face2

                        Dim swLoop                  As SldWorks.Loop2

                        Dim vSweepOutput            As Variant

                        Dim swBody                  As SldWorks.Body2

                        Dim swStopFace(1)           As SldWorks.Face2

                       

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swSelMgr = swModel.SelectionManager

                        Set swFace = swSelMgr.GetSelectedObject5(1)

                        Set swLoop = swFace.GetFirstLoop: Debug.Assert 1 = swFace.GetLoopCount

                       

                        vSweepOutput = swLoop.SweepPlanarLoop(0#, 0#, 0.01, 0#)

                        Debug.Assert Not IsEmpty(vSweepOutput)

                        Debug.Assert 2 = UBound(vSweepOutput)

                       

                        Set swBody = vSweepOutput(0)

                        Set swStopFace(0) = vSweepOutput(1)

                        Set swStopFace(1) = vSweepOutput(2)

                       

                        Debug.Print "Original face area     = " & swFace.GetArea '* 1000000# '& " mm^2"

                        'Debug.Print "  Stop face 1 area     = " & swStopFace(0).GetArea * 1000000# & " mm^2"

                        'Debug.Print "  Stop face 2 area     = " & swStopFace(1).GetArea * 1000000# & " mm^2"

                    End Sub

                    '---------------------------------

                     

                    http://www.icax.org/article-4106-1.html

                • Re: How to use API to measure the specified area?
                  Yong Ning

                  aa0.jpg

                   

                  Option Explicit
                      Dim swApp As SldWorks.SldWorks
                      Dim Part As SldWorks.ModelDoc2
                      Dim swSketchSeg                 As SldWorks.SketchSegment
                      Dim SwSkSeg As SldWorks.SketchSegment
                      Dim SwFace As SldWorks.Face2
                            
                      Sub main()
                               
                      Dim ThisFile As String
                      Dim Methods(8) As String
                      Dim Names As Variant
                      Dim Types As Variant
                      Dim Values As Variant
                      Dim editBody As Body2
                      Dim swmacrofeaturebydefault As Variant
                      ''
                      Dim swSelMgr As SldWorks.SelectionMgr

                      ''
                                  
                     
                      Set swApp = Application.SldWorks
                      Set Part = swApp.ActiveDoc
                      Set editBody = Nothing
                      ''
                      Set swSelMgr = Part.SelectionManager
                      Set SwFace = swSelMgr.GetSelectedObject5(1)
                      Debug.Print SwFace.GetArea
                      Debug.Print


                     
                      'Debug.Print Round(swSketchSeg.GetLength * 1000, 2)
                     
                      'Set SwSkSeg = swSketchSeg
                      
                     
                      'Define the routines to call
                      ThisFile = swApp.GetCurrentMacroPathName
                      Methods(0) = ThisFile
                      Methods(1) = "CalArea"
                      Methods(2) = "swmRebuild"
                      Methods(3) = ThisFile
                      'Methods(4) = "MacroFeature_Module1"
                      Methods(4) = "CalArea"
                      Methods(5) = "swmEditDefinition"
                      Methods(6) = "" 'A security routine is optional
                      Methods(7) = ""
                      Methods(8) = ""

                      Names = Empty
                      Types = Empty
                      Values = Empty
                               
                      'Insert the macro feature
                      Part.FeatureManager.InsertMacroFeature "CalArea", "", (Methods), Names, Types, Values, editBody, swmacrofeaturebydefault
                     
                  End Sub

                  Function swmRebuild(app As Variant, Part As Variant, Feature As Variant) As Variant
                         
                      Dim Definition As SldWorks.MacroFeatureData
                      Dim Objects As Variant
                      Dim ObjTypes As Variant
                      Dim SelMarks As Variant
                      Dim DrwViews As Variant
                      ''
                     
                      Set Definition = Feature.GetDefinition

                      Definition.GetSelections2 Objects, ObjTypes, SelMarks, DrwViews
                      'Set swSketchSeg = Objects(0)
                      Set SwFace = Objects(0)
                  'Debug.Print swSketchSeg.GetLength

                      Dim vol As Variant
                      Dim conv As Variant
                      Dim convRounded As String
                      Dim prop As Boolean
                     
                      'Debug.Print SwSkSeg.GetID
                     
                      prop = Part.DeleteCustomInfo2("", "Area")
                      Dim Var
                      Var = Round(SwFace.GetArea, 4)
                      prop = Part.AddCustomInfo3("", "Area", 30, "Area:" & Str(Var) + "Square Meter")
                  End Function

                  Function swmEditDefinition(app As Variant, Part As Variant, Feature As Variant) As Variant
                  End Function