11 Replies Latest reply on May 22, 2017 9:54 AM by Korbi Anis

    GET THE DOT PRODUCT OF TWO VECTOR

    Korbi Anis

      Hi friends, I need a macro to calculate to dot product of two vectors.Thank you very much

          • Re: GET THE DOT PRODUCT OF TWO VECTOR
            Korbi Anis

            I tried to write this macro but it does not work !

            Sub Dot()

            Dim swApp As SldWorks.SldWorks

            Dim swMathUtil As SldWorks.MathUtility

            Dim swModel As SldWorks.ModelDoc2

            Dim swSelMgr As SldWorks.SelectionMgr

            Dim Normal1 As SldWorks.MathVector

            Dim Normal2 As SldWorks.MathVector

            Dim DotProd As Double

             

            Set swApp = Application.SldWorks

                Set swMathUtil = swApp.GetMathUtility

                Set swModel = swApp.ActiveDoc

                Set swSelMgr = swModel.SelectionManager

            Set Normal1 = swMathUtil.CreateVector((vVector))

            Set Normal2 = swMathUtil.CreateVector((vVector))

              DotProd = Normal1.Dot(Normal2)

             

                End Sub

              • Re: GET THE DOT PRODUCT OF TWO VECTOR
                Ivana Kolin
                Option Explicit
                Sub Dot()
                    Dim swApp As SldWorks.SldWorks
                    Dim swMathUtil As SldWorks.MathUtility
                    Dim Normal1 As SldWorks.MathVector
                    Dim Normal2 As SldWorks.MathVector
                    Dim DotProd As Double
                    Dim ptArr(2) As Double
                    Dim vVector As Variant
                    Set swApp = Application.SldWorks
                    Set swMathUtil = swApp.GetMathUtility
                    ptArr(0) = 1
                    ptArr(1) = 0
                    ptArr(2) = 0
                    vVector = ptArr
                    Set Normal1 = swMathUtil.CreateVector(vVector)
                    ptArr(0) = 0.5
                    ptArr(1) = 0
                    ptArr(2) = 0
                    vVector = ptArr
                    Set Normal2 = swMathUtil.CreateVector(vVector)
                    DotProd = Normal1.Dot(Normal2)
                End Sub
                
                
                  • Re: GET THE DOT PRODUCT OF TWO VECTOR
                    Korbi Anis

                    Thank you vey much Ivana for this macro . Just I have a simple question ! If the coordinates of the vector are defined before it means I don't like to put them in this macro , just I want to read them to explore them here , how can I do ?

                      • Re: GET THE DOT PRODUCT OF TWO VECTOR
                        Ivana Kolin

                        it is possible, but read from where?

                          • Re: GET THE DOT PRODUCT OF TWO VECTOR
                            Korbi Anis

                            Yes ,  At first I select a palanar Face called F1 and after I get the normal vector of this face called N1

                            Sub N1()

                                Dim swApp               As SldWorks.SldWorks

                                Dim swModel             As SldWorks.ModelDoc2

                                Dim swSelMgr            As SldWorks.SelectionMgr

                                Dim swFace              As SldWorks.Face2

                                Dim vNorm1               As Variant

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                Set swSelMgr = swModel.SelectionManager

                                Set swFace = swSelMgr.GetSelectedObject5(1)

                                vNorm1 = swFace.Normal

                                Debug.Print "Normal1 = (" & vNorm1(0) & ", " & vNorm1(1) & ", " & vNorm1(2) & ")"

                            swModel.ClearSelection

                                End Sub

                            After I select another planar Face called F2 And I get the normale vector oN2 ( macro like N1)

                            then I got finaly

                            the vectors Normal 1 and Normal 2 of face F1 and F2

                            Now , I want to explore these Vector to apply Dot Method ( it means I want to change " ptArray() in the macro that you sent to me with the coordinates of the vectors Normal 1 and Normal 2

                              • Re: GET THE DOT PRODUCT OF TWO VECTOR
                                Ivana Kolin

                                select 2 faces and run macro

                                 

                                Option Explicit
                                Sub Dot()
                                    Dim swApp As SldWorks.SldWorks
                                    Dim swMathUtil As SldWorks.MathUtility
                                    Dim Normal1 As SldWorks.MathVector
                                    Dim Normal2 As SldWorks.MathVector
                                    Dim DotProd As Double
                                   
                                    Set swApp = Application.SldWorks
                                    Set swMathUtil = swApp.GetMathUtility
                                    
                                    Dim swModel             As SldWorks.ModelDoc2
                                    Dim swSelMgr            As SldWorks.SelectionMgr
                                    Dim swFace              As SldWorks.Face2
                                    
                                    Set swApp = Application.SldWorks
                                    Set swModel = swApp.ActiveDoc
                                    Set swSelMgr = swModel.SelectionManager
                                    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                                  
                                    Set Normal1 = swMathUtil.CreateVector(swFace.Normal)
                                    Set swFace = swSelMgr.GetSelectedObject6(2, -1)
                                
                                
                                    Set Normal2 = swMathUtil.CreateVector(swFace.Normal)
                                    DotProd = Normal1.Dot(Normal2)
                                End Sub
                                
                                
                                  • Re: GET THE DOT PRODUCT OF TWO VECTOR
                                    Korbi Anis

                                    I selected two faces and I run the macro but it does not work !

                                      • Re: GET THE DOT PRODUCT OF TWO VECTOR
                                        Ivana Kolin

                                        because of error? Or what do you mean that it doesn't work?

                                        Option Explicit
                                        Sub Dot()
                                            Dim swApp As SldWorks.SldWorks
                                            Dim swMathUtil As SldWorks.MathUtility
                                            Dim Normal1 As SldWorks.MathVector
                                            Dim Normal2 As SldWorks.MathVector
                                            Dim DotProd As Double
                                        
                                            Set swApp = Application.SldWorks
                                            Set swMathUtil = swApp.GetMathUtility
                                           
                                            Dim swModel            As SldWorks.ModelDoc2
                                            Dim swSelMgr            As SldWorks.SelectionMgr
                                            Dim swFace              As SldWorks.Face2
                                           
                                            Set swApp = Application.SldWorks
                                            Set swModel = swApp.ActiveDoc
                                            Set swSelMgr = swModel.SelectionManager
                                            Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                                        
                                            Set Normal1 = swMathUtil.CreateVector(swFace.Normal)
                                            Set swFace = swSelMgr.GetSelectedObject6(2, -1)
                                        
                                        
                                            Set Normal2 = swMathUtil.CreateVector(swFace.Normal)
                                            DotProd = Normal1.Dot(Normal2)
                                            MsgBox "DotProd:" & DotProd
                                        End Sub
                                        
                        • Re: GET THE DOT PRODUCT OF TWO VECTOR
                          Korbi Anis

                          i dear John , I have two vector called "Normal1" and "Normal2" . I need to apply DotMethod