Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross product of vectors - macro

Status
Not open for further replies.

picia

Mechanical
Mar 24, 2006
26
Hi, I wan to build a macro which give me new vector using function MathVector.Cross . One of input vector is a normal of surface (for example plane) and a second is one of axis of origin. So, I want obtain vector emergent with this two inputs vectors... If this possible?If You have any ideas please describe... thanks
 
Replies continue below

Recommended for you

I have simple program where I have a vector which represent normal of slected plane.Now I want to make a cross product between nthis vector and for example one vector of origin (x or y or z).How make taht?please help me...Below is a source code:
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFace As SldWorks.Face2
Dim swSurf As SldWorks.Surface
Dim vPlane As Variant
Dim swMathUtil As SldWorks.MathUtility
Dim nVector(2) As Double
Dim vVector As Variant
Dim swFlatNorm As SldWorks.MathVector
Dim ArrayData As Variant

Set swApp = Application.SldWorks
Set swMathUtil = swApp.GetMathUtility
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swFace = swSelMgr.GetSelectedObject5(1)
Set swSurf = swFace.GetSurface

If swSurf.IsPlane Then
vPlane = swSurf.PlaneParams
swApp.SendMsgToUser " Selected Surface - PLANE " _
& vbCrLf & vbCrLf & " Normal = (" & vPlane(0) & ", " & vPlane(1) & ", " & vPlane(2) & ")" _
& vbCrLf & vbCrLf & " Root = (" & vPlane(3) * 1000# & ", " & vPlane(4) * 1000# & ", " & vPlane(5) * 1000# & ") mm" _
& vbCrLf & vbCrLf & " File = " & swModel.GetPathName
End If

nVector(0) = vPlane(0): nVector(1) = vPlane(1): nVector(2) = vPlane(2):
vVector = nVector
Set swFlatNorm = swMathUtil.CreateVector((vVector))
ArrayData = swFlatNorm.ArrayData
swApp.SendMsgToUser " Selected Surface - PLANE " _
& vbCrLf & vbCrLf & " Vector of normal = (" & ArrayData(0) & ", " & ArrayData(1) & ", " & ArrayData(2) & ")"
GoTo CleanUp

CleanUp:
Set swApp = Nothing
Set swMathUtil = Nothing
Set swModel = Nothing
Set swSelMgr = Nothing
Set swFace = Nothing
End Sub
 
You've got 99% of what you need here. All that remains is for you to do the cross product. Declare a variable as

Dim swCPVector As As SldWorks.MathVector

Then, once you've created swFlatNorm with swMathUtil.CreateVector you can cross it with a (1,1,1) vector like so:

set swCPVector = swFlatNorm.Cross(swMathUtil.CreateVector(Array(1,1,1)))

 
Sorry, brain fart. What you want is more like:

Code:
Dim swCPwithX as SldWorks.MathVector
Dim swCPwithY as SldWorks.MathVector
Dim swCPwithZ as SldWorks.MathVector
.
.
.
.
set swCPwithX = swFlatNorm.Cross(swMathUtil.CreateVector(Array(1,0,0)))
set swCPwithY = swFlatNorm.Cross(swMathUtil.CreateVector(Array(0,1,0)))
set swCPwithZ = swFlatNorm.Cross(swMathUtil.CreateVector(Array(0,0,1)))
 
Oh, Thanks handleman.I amke something like You... but your proposition is good. I think about this, that one of component to cross product (You proposs swCPwithX or swCPwithY or swCPwithZ) will be perpendicular to normal of selected surafce... I try use function in MathVector but there are nothing... (I think so..) How make taht?For example: if normal of surface is (1,0,0) that vector which I use to cross product is (0,1,0) or (0,0,1).If You know how make it, please help... Thanks
 
I'm don't understand what you are asking. The result "swCPwithX" will be perpendicular to the plane normal and to the X axis of the part coordinate system. Similar for swCPwithY and swCPwithZ. I'm not sure what end result you seek.

 
Yes, You have right handleman.But this is only good when normal is (0,0,1) or (0,1,0) or (1,0,0).And how willbe perpendicular vector when normal willbe for example (0.4251, 0, 0.687)?
 
I'm afraid I still don't understand what you are trying to do. Why are you interested in finding the cross product?
 
So, I want obtain a coordinates system where one of axis is normal to surface(vector of normal). Next axis must be a orthogonal to this normal. When I have this two vectors I can obtain third vector using cross product...
 
One vector is not enough to define a coordinate system. If your normal vector is the X-axis of your new coordinate system then the Y and Z axes can still be any direction along the surface. You will have to do two cross products. The first one will be with one of the part's axes and the other will be a cross of the result and the original normal vector. Something like:
Code:
dim MyNewXaxis as SldWorks.MathVector
dim MyNewYaxis as SldWorks.MathVector
dim MyNewZaxis as SldWorks.MathVector
.
.
.
.
Set MyNewXaxis = swFlatNorm
set MyNewYaxis = MyNewXaxis.Cross(swMathUtil.CreateVector(Array(1,0,0)))
'[check here to make sure that the value of 
'MyNewYaxis is not (0,0,0).  If so, cross MyNewXaxis
'with (0,1,0) to get value for MyNewYaxis
Set MyNewZaxis = MyNewXaxis.cross(MyNewYaxis)
 
Ok handleman, Your proposition is good.Thanks for Your time and patience:)!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor