9 Replies Latest reply on Sep 9, 2015 2:44 PM by Mohammed Gh

    RayIntersections

    Mohammed Gh

      Hi,

       

      I am trying to get the result (x,y, and z coordinate) of vertical projection of two points on a knitted surface.
      RayIntersections and GetRayIntersectionsPoints look promising but there no good documentation. Even on the solidworks api help website.

       

       

      Attached the file I am working on and below is the script.


      Could you please look at the script and tell me what is wrong with it and/or if you have a better aproche or a good documentation about how to get the coordinate of the vertical projection of the two points on the surface.

      Thanks.

       

       

       

       

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swBody(0) As SldWorks.Body2

          Dim boolstatus  As Boolean

         

      Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager

              boolstatus = swModel.Extension.SelectByID2("surface", "REFSURFACE", 0, 0, 0, False, 0, Nothing, 0)

             

              Set swBody(0) = swSelMgr.GetSelectedObject6(1, -1)  ''

              Dim PointsArray(5) As Double

       

              dPtArr(0) = -1.14329

              dPtArr(1) = 0.11616

              dPtArr(2) = 0.0017

               dPtArr(3) = -37.9361194

               dPtArr(4) = -4.57

               dPtArr(5) = 0.54

       

               Dim dVecArr(2) As Double

       

              dVecArr(0) = 0

              dVecArr(1) = -1

              dVecArr(2) = 0

               

              Const hitRadius As Double = 0.0000095

       

              Const offset As Double = 0.0000001

       

              Dim nNoOfInterestions As Integer

       

              Debug.Print swModel.RayIntersections(swBody, dPtArr, dVecArr, swRayPtsOptsTOPOLS, hitRadius, offset)

       

          

       

              Dim vPts As Variant

       

              vPts = swModel.GetRayIntersectionsPoints

       

          

       

          End Sub

        • Re: RayIntersections
          Ivana Kolin

          sw api is working with meters. Are those values in meters?

                  dPtArr(0) = -1.14329

                  dPtArr(1) = 0.11616

                  dPtArr(2) = 0.0017

                   dPtArr(3) = -37.9361194

                   dPtArr(4) = -4.57

                   dPtArr(5) = 0.54

            • Re: RayIntersections
              Mohammed Gh

              Hi Ivana,

               

              Thanks for your reply.

               

              Yes, They are in meters. I learned that the hard way.

               

              I am still working on it step by step to see where the problem is.

               

              When I select the surface by name using:
                boolstatus = swModel.Extension.SelectByID2("surface", "REFSURFACE", 0, 0, 0, False, 0, Nothing, 0)

               
                 then assign selection to swBody:       set swBody = swSelMgr.GetSelectedObject6(1, -1)   

              I get an error that say "Can't assign to array".

               


              How to fix that or how to convert suface to body2 type and assign it to swbody?

               

               

              Thanks

            • Re: RayIntersections
              Ivana Kolin
              Option Explicit
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swBody(0) As SldWorks.Body2
              Dim boolstatus As Boolean
              Dim dPtArr(2) As Double
              Dim swFeat As SldWorks.Feature
              Sub main()
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  Set swSelMgr = swModel.SelectionManager
                  boolstatus = swModel.Extension.SelectByID2("surface", "REFSURFACE", 0, 0, 0, False, 0, Nothing, 0)
                  Set swFeat = swSelMgr.GetSelectedObject6(1, -1)     ''
                  Set swBody(0) = swFeat.GetBody
                  Dim PointsArray(5) As Double
                  dPtArr(0) = -1.14329
                  dPtArr(1) = 0.11616
                  dPtArr(2) = 0.0017
                  '         dPtArr(3) = -37.9361194
                  '         dPtArr(4) = -4.57
                  '         dPtArr(5) = 0.54
                  Dim dVecArr(2) As Double
                  dVecArr(0) = 0
                  dVecArr(1) = -1
                  dVecArr(2) = 0
                  Const hitRadius As Double = 0.0000095
                  Const offset As Double = 0.0000001
                  Dim nNoOfInterestions As Integer
                  Debug.Print swModel.RayIntersections(swBody, dPtArr, dVecArr, swRayPtsOptsTOPOLS, hitRadius, offset)
                  Dim vPts As Variant
                  vPts = swModel.GetRayIntersectionsPoints
                  Debug.Print vPts(0), vPts(1), vPts(2), vPts(3), vPts(4), vPts(5)
              End Sub
              
              
                • Re: RayIntersections
                  Mohammed Gh

                  Hi Ivana,

                   

                  I ran it as a first try and got some result.   I will check the accuracy of the result tomorrow and get back to you.

                   

                  Thank you for taking time to answer my question.

                   

                  M.

                    • Re: RayIntersections
                      Mohammed Gh

                      It is accurate and that exactly what I need right now.

                       

                      Thank you Ivana.

                       

                      M

                        • Re: RayIntersections
                          Ivana Kolin

                          Your're welcome

                            • Re: RayIntersections
                              Mohammed Gh

                              Hi Ivana,

                               

                              The program above works very well When the body that I want to project on is a "REFSURFACE" type.

                               

                              The body I want to select to project on a set of points is  "BODYFEATURE" or "SURFACEBODY" type.

                               

                              When I run the following code I get error saying: "Object doesn't support this property or method"


                              the selection goes well but  the getbody2 function does not work and that what I need to use for  the RayIntersections function.

                                      

                              Thanks

                               

                                     

                               

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

                              Dim swApp As Object

                              Dim Model As SldWorks.ModelDoc2

                              Dim SelMgr As SldWorks.SelectionMgr

                              Dim Feat As SldWorks.Feature

                              Dim Body(0) As SldWorks.Body2

                              Dim boolstatus As Boolean

                              Dim longstatus As Long, longwarnings As Long

                               

                              Sub main()

                                          Set swApp = Application.SldWorks

                                          Set Model = swApp.ActiveDoc

                                          Set SelMgr = Model.SelectionManager

                                          boolstatus = Model.Extension.SelectByID2("Surface-Knit2", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

                                          Debug.Print "selection state: " & boolstatus

                                          Debug.Print SelMgr.GetSelectedObjectType3(1, -1)

                                          Set swFeat = SelMgr.GetSelectedObject6(1, -1)

                                                  

                                          Set Body(0) = swFeat.GetBody      '' It stops right here "Object doesn't support this property or method"

                                          Debug.Print Body(0).Name

                              End Sub

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

                                • Re: RayIntersections
                                  Ivana Kolin
                                  Option Explicit
                                  '-----------------------------------------------------------------------------------------------------------------------------------------------------------------
                                  Sub main()
                                      Dim swApp                                     As SldWorks.SldWorks
                                      Dim Model                                     As SldWorks.ModelDoc2
                                      Dim SelMgr                                    As SldWorks.SelectionMgr
                                      Dim boolstatus                                As Boolean
                                      Dim swSelObj                                  As Object
                                      Dim swFeat                                    As SldWorks.Feature
                                      Dim swBody                                    As SldWorks.Body2
                                      Set swApp = Application.SldWorks
                                      Set Model = swApp.ActiveDoc
                                      Set SelMgr = Model.SelectionManager
                                      boolstatus = Model.Extension.SelectByID2("Surface-Knit2", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                                      Debug.Print "selection state: " & boolstatus
                                      Debug.Print SelMgr.GetSelectedObjectType3(1, -1)
                                      Set swSelObj = SelMgr.GetSelectedObject6(1, -1)
                                      If TypeOf swSelObj Is Body2 Then
                                          Set swBody = swSelObj
                                      End If
                                      If TypeOf swSelObj Is Feature Then
                                          Set swFeat = swSelObj
                                          Set swBody = swFeat.GetBody
                                      End If
                                      If swBody Is Nothing Then
                                          Debug.Print "  Unknown object"
                                      Else
                                          Debug.Print swBody.Name
                                      End If
                                  End Sub