5 Replies Latest reply on Sep 9, 2015 6:29 PM by Kevin Sturge

    How to select a body and other fun stuff

    Kevin Sturge

      Hello, In the following macro I would like to select the (only) solid body and use that as the object to rotate with the (move copy action)

      For those curious about the purpose... well basically we use 2.5 axis mills, and we want some of the parts in question to be "wedged" about the center of gravity along the y axis, by a user input driven value... that way the part can be re-raised to zero on the z and trick the machine into thinking that the empty space under the wedged section should be solid...

      I've gotten pretty far on my lonesome.

      If you're curious about the userform I used i would be glad to add pictures and the code for that ( basically just shooting values to the main proc. )


      So I know that the rotate section works (fantastically) when I name the solid body something but it is illogical to rename every solid body every time. So My Question is:

      1.) Can I choose to rename a solid body to something before selecting it? (would solve all my problems)

      2.) If not how do I select the Soid body (or all solid bodies Since there will only ever be one) and use the "Move copy" action against it?



      ' **********************************************************************************************

      ' Macro used to rotate the Part in question about the specified point (COG)
      '     which should be defined by cog macro.
      '      the origin should be moved to the cog so it is just Rotating about the y




      '     Part is a .sldprt

      '     The solid body  may be named a wide array of things due to the possibility of having to use the mirror macro beforehand




      '     Part can be rotated with the "copy move" actions

      '     There is only one Solid body in the doc




      '     Teh part is rotated by the radian equivalent of the user input degrees or millimeters(lift)


      ' **********************************************************************************************

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Public Y As Double

      Public X As Double


      'Show the userform


      Sub GUI()


      End Sub


      'Proceedure to grab values and appy the change from degrees to radians.


      Public Sub MakeWedge(ByVal o As Boolean, ByVal t As String)

      'MsgBox Userinput & o & t

      Dim mm_Option As Boolean

      mm_Option = o

      Dim result As Double

      If mm_Option = True Then

          result = t * 1.5 * 0.0174532925


          result = t * 0.0174532925

      End If


      MsgBox result

      Call rotate(result)

      End Sub



      Sub rotate(ByVal res As Double)


      The two methods I am exploring




      'this works if the solid body is named something

      boolstatus = Part.Extension.SelectByID2("SolidBodyName", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("SolidBodyName", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0)




      'Does this work to select all bodies...?

      'boolstatus = Part.Extension.SketchBoxSelect("0.000000", "0.000000", "0.000000", "0.000000", "0.000000", "0.000000")





      '"Move Copy" action


      'Dim myFeature As Object

      myFeature = Part.FeatureManager

      Set myFeature = swSelData.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, res, 0, False, 1)

      End Sub

          • Re: How to select a body and other fun stuff
            Josh Brady

            Here's a macro that selects all faces of all bodies that adjoin an edge that is shorter than the input value.  It shows you how to programmatically get a variable that represents a body.  Once you have that variable, .Select


            Dim swApp As SldWorks.SldWorks

            Dim swDoc As SldWorks.ModelDoc2

            Dim myPart As SldWorks.PartDoc

            Dim myBods As Variant

            Dim myFaces As Variant

            Dim myEdges As Variant

            Dim myBod As SldWorks.Body2

            Dim myFace As SldWorks.Face2

            Dim myEdge As SldWorks.Edge

            Dim myCurve As SldWorks.Curve

            Dim dStart As Double

            Dim dEnd As Double

            Dim bClosed As Boolean

            Dim bPeriodic As Boolean

            Dim i As Long

            Dim j As Long

            Dim k As Long

            Dim cLen As Double

            Dim MinEdge As Double

            Dim bGotParms As Boolean

            Sub main()


            Set swApp = Application.SldWorks

            Set swDoc = swApp.ActiveDoc

            If swDoc.GetType = swDocPART Then

                Set myPart = swDoc


                MsgBox "Parts only!"

                Exit Sub

            End If


            MinEdge = CDbl(InputBox("Enter minimum edge length in mm", "Select faces of small edges", "0.2"))

            MinEdge = MinEdge / 1000


            If MsgBox("Clear existing selections?", vbYesNo) = vbYes Then

                swDoc.ClearSelection2 True

            End If


            myBods = myPart.GetBodies2(swSolidBody, True)

            For i = 0 To UBound(myBods)

                Set myBod = myBods(i)

                myFaces = myBod.GetFaces

                For j = 0 To UBound(myFaces)

                    Set myFace = myFaces(j)

                    myEdges = myFace.GetEdges

                    For k = 0 To UBound(myEdges)

                        Set myEdge = myEdges(k)

                        Set myCurve = myEdge.GetCurve

                        bGotParms = myCurve.GetEndParams(dStart, dEnd, bClosed, bPeriodic)

                        cLen = myCurve.GetLength3(dStart, dEnd)

                        'Debug.Print bGotParms, dStart, dEnd, bClosed, bPeriodic, cLen

                        If cLen < MinEdge Then

                            myFace.Select4 True, Nothing

                            Exit For

                        End If

                    Next k

                Next j

            Next i

            End Sub