GET THE DOT PRODUCT OF TWO VECTOR

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

SolidworksApi macros
Answers 11
KA
KA 2017-05-22

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

KA
KA 2017-05-22

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

IK
IK 2017-05-22
<p>Option Explicit</p><p>Sub Dot()</p><p>    Dim swApp As SldWorks.SldWorks</p><p>    Dim swMathUtil As SldWorks.MathUtility</p><p>    Dim Normal1 As SldWorks.MathVector</p><p>    Dim Normal2 As SldWorks.MathVector</p><p>    Dim DotProd As Double</p><p>    Dim ptArr(2) As Double</p><p>    Dim vVector As Variant</p><p>    Set swApp = Application.SldWorks</p><p>    Set swMathUtil = swApp.GetMathUtility</p><p>    ptArr(0) = 1</p><p>    ptArr(1) = 0</p><p>    ptArr(2) = 0</p><p>    vVector = ptArr</p><p>    Set Normal1 = swMathUtil.CreateVector(vVector)</p><p>    ptArr(0) = 0.5</p><p>    ptArr(1) = 0</p><p>    ptArr(2) = 0</p><p>    vVector = ptArr</p><p>    Set Normal2 = swMathUtil.CreateVector(vVector)</p><p>    DotProd = Normal1.Dot(Normal2)</p><p>End Sub</p><p></p>
KA
KA 2017-05-22

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 ?

IK
IK 2017-05-22

it is possible, but read from where?

KA
KA 2017-05-22

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

IK
IK 2017-05-22

select 2 faces and run macro

<p>Option Explicit</p><p>Sub Dot()</p><p>    Dim swApp As SldWorks.SldWorks</p><p>    Dim swMathUtil As SldWorks.MathUtility</p><p>    Dim Normal1 As SldWorks.MathVector</p><p>    Dim Normal2 As SldWorks.MathVector</p><p>    Dim DotProd As Double</p><p>   </p><p>    Set swApp = Application.SldWorks</p><p>    Set swMathUtil = swApp.GetMathUtility</p><p>    </p><p>    Dim swModel             As SldWorks.ModelDoc2</p><p>    Dim swSelMgr            As SldWorks.SelectionMgr</p><p>    Dim swFace              As SldWorks.Face2</p><p>    </p><p>    Set swApp = Application.SldWorks</p><p>    Set swModel = swApp.ActiveDoc</p><p>    Set swSelMgr = swModel.SelectionManager</p><p>    Set swFace = swSelMgr.GetSelectedObject6(1, -1)</p><p>  </p><p>    Set Normal1 = swMathUtil.CreateVector(swFace.Normal)</p><p>    Set swFace = swSelMgr.GetSelectedObject6(2, -1)</p><p></p><p></p><p>    Set Normal2 = swMathUtil.CreateVector(swFace.Normal)</p><p>    DotProd = Normal1.Dot(Normal2)</p><p>End Sub</p><p></p>
KA
KA 2017-05-22

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

IK
IK 2017-05-22

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

<p>Option Explicit</p><p>Sub Dot()</p><p>    Dim swApp As SldWorks.SldWorks</p><p>    Dim swMathUtil As SldWorks.MathUtility</p><p>    Dim Normal1 As SldWorks.MathVector</p><p>    Dim Normal2 As SldWorks.MathVector</p><p>    Dim DotProd As Double</p><p></p><p>    Set swApp = Application.SldWorks</p><p>    Set swMathUtil = swApp.GetMathUtility</p><p>   </p><p>    Dim swModel            As SldWorks.ModelDoc2</p><p>    Dim swSelMgr            As SldWorks.SelectionMgr</p><p>    Dim swFace              As SldWorks.Face2</p><p>   </p><p>    Set swApp = Application.SldWorks</p><p>    Set swModel = swApp.ActiveDoc</p><p>    Set swSelMgr = swModel.SelectionManager</p><p>    Set swFace = swSelMgr.GetSelectedObject6(1, -1)</p><p></p><p>    Set Normal1 = swMathUtil.CreateVector(swFace.Normal)</p><p>    Set swFace = swSelMgr.GetSelectedObject6(2, -1)</p><p></p><p></p><p>    Set Normal2 = swMathUtil.CreateVector(swFace.Normal)</p><p>    DotProd = Normal1.Dot(Normal2)</p><p>    MsgBox "DotProd:" & DotProd</p><p>End Sub</p>
KA
KA 2017-05-22

Now It's Okey Ivana . thank you very much   and Sorry if I If I asked you many questions