6 Replies Latest reply on Oct 14, 2016 6:59 AM by Mike Frink

    Macro to select surface of a "cut with surface"

    Mike Price

      Hi all,

       

      I am trying to determine how to select a surface that was cut with a surface.

       

       

       

       

       

      I will be creating a surface offset from this cut surface.  Unfortunately, when I select "SurfaceCut1" and then go to "Offset Surface", it doesn't keep the face selected.

       

      Thanks!

          • Re: Macro to select surface of a "cut with surface"
            Mike Price

            I am trying different things, so excuse the mess/commented code

             

            I am trying to implement some code that Keith Rice wrote to select a face based upon a ray.  My surfaces will have vectors that off the standard XYZ vectors, so my thought is to eventually determine a vector of a cutting plane, make the cut, then use a ray to intersect the surface that was cut, then create a zero offset off of that surface.

             

            The lower portion of code commented out gets the vector.  I will be able to return those values into the ray direction values.

             

            I think this is a convoluted way to get there, but I think I'm close.....

             

            I have over 14000 hrs of Solidworks....Wish I had a 1000 hours of API development (and 1000 of VBA)...

             

            Sub main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
               
            '    'start point of ray
                dblParams(0) = 0
                dblParams(1) = 0
                dblParams(2) = 0
               
               
               
            '
                'direction of ray
                dblParams(3) = 0.500439158980997
                dblParams(4) = 0
                dblParams(5) = -0.865771706720884 'negative z direction
            '
            '    'radius of cylinder
                dblParams(6) = 200 'arbitrary value; ignored if selecting faces
            '
            '    'Only IEntity objects are accepted (vertices, edges, co-edges, loops, faces)
                Debug.Print swModel.SelectByRay(dblParams, swSelectType_e.swSelFACES)
                'Dim swModel As ModelDoc2
            '            Dim swSelMgr As SelectionMgr
            '            Dim SelectedObject As Object
            '            Dim swFace As Face2
            '            Dim swFeature As Feature
            '
            '            'swModel = swApp.ActiveDoc
            '            swSelMgr = swModel.SelectionManager
            '            SelectedObject = swSelMgr.GetPreSelectedObject()
            '            If SelectionType = swSelectType_e.swSelFACES Then
            '                swFace = SelectedObject
            '                swFeature = swFace.GetFeature
            '                Debug.Print ("Name of feature whose face you preselected: " & swFeature.Name)
            '                Debug.Print ("  Mouse over a different face, or click Debug > Stop Debugging to stop running macro")
            '                Debug.Print (" ")
            '            End If

               
            boolstatus = Part.Extension.SelectByID2(swFeature, "FACE", -2.94292601159469E-02, -1.52252586530608E-02, -5.35825918431669E-02, False, 1, Nothing, 0)
            Part.ClearSelection2 True
            boolstatus = Part.Extension.SelectByID2("", "REFSURFACE", 0, 0, 0, False, 1, Nothing, 0)
            Dim myFeature As Object
            Set myFeature = Part.FeatureManager.FeatureBossThicken(0.000254, 0, 0, False, False, True, True)
              

            '    Dim swSelMgr            As SldWorks.SelectionMgr
            '
            '    Dim swFace              As SldWorks.Face2
            '
            '    Dim vNorm              As Variant
            '
            '
            '
            '    Set swApp = Application.SldWorks
            '
            '    Set swModel = swApp.ActiveDoc
            '
            '    Set swSelMgr = swModel.SelectionManager
            '
            '    Set swFace = swSelMgr.GetSelectedObject5(1)
            '
            '
            '
            '    vNorm = swFace.Normal
            '
            '    Debug.Print "Normal = (" & vNorm(0) & ", " & vNorm(1) & ", " & vNorm(2) & ")"
                End Sub

            • Re: Macro to select surface of a "cut with surface"
              Mike Price

              I'm having trouble getting the face that is selected using swSelectType_e.swSelFACES

            • Re: Macro to select surface of a "cut with surface"
              Mike Frink

              Take a look at this example:

              2014 SOLIDWORKS API Help - Get Faces Associated with Feature Example (VBA)

               

              In your macro, if you select the surfacecut feature, this code should select the resulting face(s) and then you can perform your surface offset.