If you want to get the angle between the workplane, you can just get the
angle between their normals. But, depending on how you view it, this answer
could be one of the two possible angle between the vectors. If this is not
important, here's the macro that gets the angle between their normals:
Public Sub GetAngle()
Dim ActiveAssemDoc As AssemblyDocument
Set ActiveAssemDoc = ThisApplication.ActiveDocument
Dim AssemXYWorkPlane As WorkPlane
Set AssemXYWorkPlane = ActiveAssemDoc.ComponentDefinition.WorkPlanes(3)
Dim FirstOcc As ComponentOccurrence
Set FirstOcc = ActiveAssemDoc.ComponentDefinition.Occurrences(1)
If FirstOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
Dim FirstOccPartDef As AssemblyComponentDefinition
Set FirstOccPartDef = FirstOcc.Definition
Dim FirstOccXYWorkPlane As WorkPlane
Set FirstOccXYWorkPlane = FirstOccPartDef.WorkPlanes(3)
Dim FirstOccXYWorkPlaneProxy As WorkPlaneProxy
FirstOcc.CreateGeometryProxy FirstOccXYWorkPlane,
FirstOccXYWorkPlaneProxy
Dim AngleInRadians As Double
AngleInRadians =
AssemXYWorkPlane.Plane.Normal.AngleTo(FirstOccXYWorkPlaneProxy.Plane.Normal)
Dim PI As Double
PI = 4 * Math.Atn(1)
Dim AngleInDegrees As Double
AngleInDegrees = AngleInRadians * 180 / PI
End If
End Sub
But, this angle between any two vectors as returned by the "Vector.AngleTo"
method is always less than 180 deg irrespective of the directions of the
vectors. The angle returned is the included angle between the vectors and
hence, is always less than 180 deg.
This angle is the smaller of "theta" and "360-theta" (where "theta" is the
angle that you would consider when calculating the dot product or the cross
product between the vectors), this applies irrespective of how your
coordinate system is defined.
If you want your angles to be returned in a particular "sense"
(anti-clockwise or clockwise) then you could define this "sense" as a
direction vector. With this direction defined, you could compare the cross
product of the vectors (say Vector1 and Vector2) between which you want to
measure the angle to this direction vector, if they are the same then the
angle is Vector1.AngleTo(Vector2), but, if opposite in direction, then the
angle is (2*pi) - Vector1.AngleTo(Vector2).
As an example, let us consider that all vectors lie on the XY plane. If you
are looking at the XY plane in such a manner that the positive X direction
points to the right and the positive Y direction points towards the top,
then the Z direction would be pointing out of the plane towards you. Given
this direction of viewing the XY plane, and if you want to measure the
angles between all vectors that might be drawn on the XY planes in the
anti-clockwise direction only, then the steps are:
1) Define the "sense" or direction vector corresponding to the
anti-clockwise measurement; in this case it would be the Z vector.
2) In order to find the angle between any two vectors, e.g. Vector1 and
Vector2, find the cross product of the two vectors, i.e. Vector1 x Vector2,
if the direction of the cross product vector is the same as the direction
vector (in this case the Z direction), then the angle between them in the
anti-clockwise direction is Vector1.AngleTo(Vector2) but, if the direction
of the cross-product is opposite that of the direction vector, then the
angle between the vectors is 2*pi - Vector1.AngleTo(Vector2).
Here is the VBA code:
Public Sub AngleWithDirectionSense()
Dim pi As Double
pi = 4 * Atn(1)
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
'if you have many vectors on a plane and want to measure angles in the
same sense:e.g anticlockwise
'use two vectors to set up the sense
Dim oXDir As Vector
Set oXDir = oTransGeom.CreateVector(1, 0, 0)
Dim oYDir As Vector
Set oYDir = oTransGeom.CreateVector(0, 1, 0)
'set up direction as anti-clockwise
Dim oDirSense As Vector
Set oDirSense = oXDir.CrossProduct(oYDir)
'use above or reverse it for clockwise:
' oDirSense.X = -(oDirSense.X)
' oDirSense.Y = -(oDirSense.Y)
' oDirSense.Z = -(oDirSense.Z)
'now use the sense to measure angles between any other vectors on the
plane
'define the first vector
Dim oVector1 As Vector
Set oVector1 = oTransGeom.CreateVector(1, 1, 0)
'define the second vector
Dim oVector2 As Vector
Set oVector2 = oTransGeom.CreateVector(1, -1, 0)
'get the crossproduct
Dim oCrossProd As Vector
Set oCrossProd = oVector1.CrossProduct(oVector2)
'now determine the angle between Vector1 and Vector2 in the
anti-clockwise direction
Dim AngleRadians As Double
If oCrossProd.DotProduct(oDirSense) < 0 Then
AngleRadians = 2 * pi - oVector1.AngleTo(oVector2)
Else
AngleRadians = oVector1.AngleTo(oVector2)
End If
Dim AngleDegrees As Double
AngleDegrees = AngleRadians * 180 / pi
End Sub
-Venkatesh Thiyagarajan.
wrote in message news:5346853@discussion.autodesk.com...
Hi All,
I have an assembly with a sub assembly, and I want to write some code to
measure the angle of the sub assembly YZ plane with respects to the parent
assembly YZ plane.
I have tried to use getangle but I am getting an invalid call error.
Any ideas on the best way to do this?
Thanks,
Regards,
Mark.