Is there a reliable method to determine angle between two lines?

Is there a reliable method to determine angle between two lines?

AlexFielder
Advisor Advisor
913 Views
8 Replies
Message 1 of 9

Is there a reliable method to determine angle between two lines?

AlexFielder
Advisor
Advisor

Say for instance I have the following sketch lines:

2021-12-01 10_12_02-Window.png

I know that using the API I can measure the angle between them and (in this case) the API will return 105° because that is the smallest possible angle.

 

If, however I position these same lines such that the angle is > 180°:

2021-12-01 10_16_22-Window.png

The API will return 90° because again that is the smallest angle between these two lines.

 

What I need to find however is the angle in a specific direction (in this case clockwise, but not always).

 

Assume for a moment that the first line to be measured is always the green line in the position(s) below, what API call can I make use of to achieve this?

2021-12-01 10_24_56-Window.png

Thanks,

 

Alex.

 

PS. I have tried using the following:

Dim Angle As Double = Measure.Angle(Object1.Name, Object2.Name)
Dim OtherAngle as Double = Vector1.AngleTo(Vector2)

0 Likes
Accepted solutions (2)
914 Views
8 Replies
Replies (8)
Message 2 of 9

Michael.Navara
Advisor
Advisor

If you have two lines joined with one point, you can calculate Vector3D from common point to free end for both lines (v1, v2). X and Y coordinate of this vectors is numerically the same as Vector2D coordinate of CommomPoint.VectorTo(EndPoint). Z coordinate is 0.

Then you can calculate cross product of this two vectors (v1 x v2 = v3) and check v3.Z coordinate of result.

If v3.Z>0 then minimal angle of v1 and v2 is your result.

If v3.Z>0 then 2*PI - minimal angle of v1 and v2 is your result 

0 Likes
Message 3 of 9

AlexFielder
Advisor
Advisor

Thanks @Michael.Navara this is useful. Did you mean that if one of those calculations is less than zero though? 

0 Likes
Message 4 of 9

Michael.Navara
Advisor
Advisor

Yes of course 😀

I hope this is correct:

If v3.Z>0 then minimal angle of v1 and v2 is your result.

If v3.Z<0 then 2*PI - minimal angle of v1 and v2 is your result 

0 Likes
Message 5 of 9

AlexFielder
Advisor
Advisor

Thanks @Michael.Navara looking back over my original post I realise I goofed with the initial image.

 

Here it is updated with what I am attempting to achieve:

2021-12-02 10_05_43-Window.png

This boils down to "simple" trigonometry and after some testing, iterating 5° intervals on ALL the positions  of the angle to X axis and Angle between "walls" the best my code can correctly place is ~70% of positions.

 

The data from my testing I have yet to revisit due to time (in order to narrow down the failures), but because it had gotten extremely frustrating not being able to nail 100% of positions I have since moved on to the next part of this program which requires me to place a component at point (2) in the image above, aligned with the blue line. This bit I can do just fine.

0 Likes
Message 6 of 9

J-Camper
Advisor
Advisor
Accepted solution

@AlexFielder,

 

You can get definitive Clockwise Directed Angles by drawing an arc between the lines.  The arc will always be drawn counterclockwise by default, this can be overridden upon creation but knowing the default direction is enough.  Try the following:

Dim pDef As PartComponentDefinition = TryCast(ThisDoc.Document.ComponentDefinition, PartComponentDefinition)
If IsNothing(pDef) Then Exit Sub
	
Dim oSk As PlanarSketch = pDef.Sketches.Item(1)

Dim Line(1) As SketchLine
Line(0) = oSk.SketchLines.Item(1) 
Line(1) = oSk.SketchLines.Item(2)

Dim Arc As SketchArc
Arc = oSk.SketchArcs.AddByCenterStartEndPoint(Line(0).EndSketchPoint, Line(0).StartSketchPoint, Line(1).EndSketchPoint)

Dim directedAngle(1) As Double
directedAngle(0) = Arc.StartAngle*(180/Math.PI)
directedAngle(1) = 360 - directedAngle(0)

Arc.Delete

Logger.Trace("CounterClockwise: " & directedAngle(0) & ", Clockwise: " & directedAngle(1))

This rule assumes you are: (1) local in a Part Document, (2) The first Planar sketch is the sketch you are looking for, (3) There are 2 sketch lines joined @ endpoint of line 1 & startpoint of line 2.  The rule deletes the arc after getting the angle values.

 

Let me know if you have any questions, or if this is not working as intended.

 

Message 7 of 9

AlexFielder
Advisor
Advisor

@J-Camper now THAT I like. I need to run this code in an assembly, so I assume it would require a sketch in the assembly to function correctly which is no great challenge (I think 🤔)

 

I am currently busy frankensteining together everything that works so far into one "workflow" but I will test this out over the next few days.

 

If I can't use a sketch in an Assembly, I assume that client graphics could achieve the same result? I have previously used them to draw an arc or two so could repurpose that code (I think)

0 Likes
Message 8 of 9

J-Camper
Advisor
Advisor

@AlexFielder,

 

I made a mistake in the code I posted:

For some reason I had "Arc.StartAngle" instead of "Arc.SweepAngle" in the snippet I posted.  You will want to use the sweep angle.

 

It does work in assembly sketches, just change the ComponentDefinition type, but I should clarify that the "Clockwise" direction is only true when viewing from the normal side of the plane [for origin planes: YZ Plane viewed from Right face of view cube, XZ is Top, & XY is Front]. 

0 Likes
Message 9 of 9

AlexFielder
Advisor
Advisor
Accepted solution

@J-Camper you're a life-saver! Thank you for sharing this oh-so-simple solution.

 

Here is the code I ended up with (in my test part file at least):

Dim oSketch As PlanarSketch = PartDef.Sketches.Add(PartDef.WorkPlanes(3))
Dim oExtWall As SketchLine = oSketch.AddByProjectingEntity(InventorExteriorWall)
Dim oAdjWall As SketchLine = oSketch.AddByProjectingEntity(InventorAdjoiningWall)
Dim Arc As SketchArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oExtWall.StartSketchPoint, oExtWall.EndSketchPoint, oAdjWall.StartSketchPoint)
Dim directedAngle(1) As Double
directedAngle(0) = Arc.SweepAngle * (180/Math.PI)
RotationAngle = directedAngle(0) / 2
oSketch.Delete

 

Now to apply it to my assembly 😉

 

 

PS. I know this works 100% of the time as I just ran a test of ~2000 positions- all of which passed.

 

0 Likes