True angle in cutlist with API

Can somebody please point me in the right direction for a macro that traverses a weldment cut list and for each profile in the cut list look at both ends of the profile to check their angles. Check if their angles are according to the same plane or twisted/rotated 90° and also if the angles are 45° or 135°

See also my first question about this but without API

https://forum.solidworks.com/message/1013032?commentID=1013032#comment-1013032

SolidworksApi/macros
Answers 15
JC
JC 2021-04-08 Best Answer

what you probably need to do to approach this, is to use reference the triad coordinate system.

if your models are drawn with this in mind you can get the 3D angles relative to the coordinate system.

reference lines on the part, they have an axis in 2019 SOLIDWORKS API Help - LineParams Property (ICurve) 

the adjacent part also has line params.

Take the two vectors and calculate the angle between them.

So if the line vector = 0,0,1 it is going positive to Z
If it is 0,0,-1 it is negative Z

1,0,0 is positive Y

So it could be .25,0.25,0.5 (they always add up to 1, that is what a normalized vector is) which has angles to each vector
To get the angle from one vector to another

create cartesian vectors

dim XVect as mathvector '(1,0,0)

Dim YVect as mathVector '(0,1,0)

Dim ZVect as MathVector '(0,0,1)

use the macro below to get started. this will get the angle between two edges. make sure they are lines.

sketch segments could work using sketchLine.GetCurve
2019 SOLIDWORKS API Help - GetCurve Method (ISketchSegment) 
All you need are 3 numbers defining an axis to figure out angles to eachother or to the  3 Cartesian default vectors

Dim swApp As SldWorks.SldWorks<br>Dim Pi As Double<br>Sub main()<br>    Pi = 3.14159265359<br>    Set swApp = Application.SldWorks<br>    Dim Swmodel As ModelDoc2<br>    Set Swmodel = swApp.ActiveDoc<br><br>    Dim Edge1 As Edge<br>    Dim Edge2 As Edge<br>    Dim Mutil As MathUtility<br>    Set Mutil = swApp.GetMathUtility<br><br>    Dim Selmgr As SelectionMgr<br>    Set Selmgr = Swmodel.SelectionManager<br><br>    Set Edge1 = Selmgr.GetSelectedObject6(1, -1)<br>    Set Edge2 = Selmgr.GetSelectedObject6(2, -1)<br>    Dim edge1Data As Variant<br>    Dim Edge2Data As Variant<br>    Dim Edge1Axis(2) As Double<br>    Dim Edge2Axis(2) As Double<br>    edge1Data = Edge1.GetCurve.LineParams<br>    Edge2Data = Edge2.GetCurve.LineParams<br>    For i = 0 To 2<br>        Edge1Axis(i) = edge1Data(i + 3)<br>        Edge2Axis(i) = Edge2Data(i + 3)<br>    Next<br>    Dim Edge1Vec As MathVector<br>    Dim Edge2Vec As MathVector<br><br>    Set Edge1Vec = Mutil.CreateVector(Edge1Axis)<br>    Set Edge2Vec = Mutil.CreateVector(Edge2Axis)<br>    Dim angleBetween As Double<br>    angleBetween = GetAngleBetweenVectors(Edge1Vec, Edge2Vec)<br>    'Angle between is in radians<br>    angleBetween = RadiansToDegrees(angleBetween)<br>    Debug.Print "Angle between edges = " & angleBetween<br><br><br><br>End Sub<br>Function DegreesToRadians(ByVal Degrees As Double) As Double<br>    DegreesToRadians = ((Degrees * Pi) / 180)<br>End Function<br>Function RadiansToDegrees(ByVal Radians As Double) As Double<br>    RadiansToDegrees = ((Radians * 180) / Pi)<br>End Function<br>Function GetAngleBetweenVectors(ByVal Vec1 As MathVector, ByVal Vec2 As MathVector) As Double<br>    Dim Vec1Data As Variant<br>    Dim Vec2Data As Variant<br>    If Not Vec1 Is Nothing And Not Vec2 Is Nothing Then<br>        Vec1Data = Vec1.ArrayData<br>        Vec2Data = Vec2.ArrayData<br>        Dim FromDotTo As Double<br>        FromDotTo = Vec1.Dot(Vec2)<br>        If FromDotTo <> 0 Then<br>        Dim FromTimesTo As Double<br>        FromTimesTo = Vec1.GetLength * Vec2.GetLength<br><br>            If FromTimesTo = 0 Then<br>                GetAngleBetweenVectors = 0#<br>            Else<br>                Dim val As Double<br>                val = Arccos(1)<br><br><br>                GetAngleBetweenVectors = Arccos(FromDotTo / FromTimesTo)<br>                End If<br>        Else<br>          If (Vec2Data(0) <> 0 Or Vec2Data(1) <> 0 Or Vec2Data(2) <> 0) And (Vec1Data(0) <> 0 Or Vec1Data(1) <> 0 Or Vec1Data(2) <> 0) Then<br>                    GetAngleBetweenVectors = Pi / 2<br>                Else<br>                    GetAngleBetweenVectors = 0<br>                End If<br>        End If<br>    Else<br>            Debug.Print "Vectors empty"<br><br>    End If<br><br>End Function<span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span></span>

Jacob Corder

www.cadsharp.com

FE
FE 2021-04-08

Thank you for this ‌.

I saw your reaction about this on an other post and realized that this is not simple but our draftsmen have to figure this our for every frame they draw and I would like to assist them with this with API so that they don't lose time and sleep over this. If you have further information about this and are willing to share I would be most grateful.

Frank

EK
EK 2021-04-08

Hallo Frank,

That is a very steep hill to climb. A rectangular profile can have 10 possible permutations.
Gettting the angle is your least worry. How it relates to your profile sketch is the hard part.
Now throw in different style profile i.e. rounds and you can see how this will get out of hand quickly.

Cut saws that can do angled cuts in both horizontal and vertical plane have
often different cut limits per plane. That means some profile you need
to lay down with the long side while others you laydown short side.
Especially if you cut multiples...really hard task to organize.

comment imagecomment image
FE
FE 2021-04-08

The macro I have so far is attached.

Not really sure how to interpret the immediate window results. Are the 3 angles referring to X,Y and Z?

How can the twisted cuts be interpreted?

The macro does not traverse every profile and I can not find how to get the correct results by traversing through the whole body. Only the first 7 profiles are traversed and not all 8.

RJ
RJ 2021-04-08

Planar faces are parallel if there normal are same or opposite.

Check if vNorm1(0), vNorm1(1) and vNorm1(2) is equal to vNorm2(0), vNorm2(1) and vNorm2(2) respectively or to -1*vNorm2(0), vNorm2(1), vNorm2(2) respectively.

FE
FE 2021-04-08

I was using vNorm = swFace.Normal and the 3 values as result but don't know where to go from there

vNorm = swFace.Normal

Debug.Print "Normal = (" & vNorm(0) & ", " & vNorm(1) & ", " & vNorm(2) & ")"

RJ
RJ 2021-04-08

Frank,

My bad there. It is Face2::Normal(). Please use that.

Hope it helps!!!

Rajat Jain

FE
FE 2021-04-08

Thank you ‌ for helping.

Step 1: Got there

Step 2: Got there just now

Step 3: Can not find Surface::Normal (ISurface Interface)

Frank

RJ
RJ 2021-04-08

Hello,

I am sure Jacob's suggestion shall help you in solving your problem. You can use the following tip to quickly get the cut faces: -

  1. Get all faces of Body using Body2::GetFaces().
  2. Traverse through each face and select all planar faces only. For this, get Surface from Face2 using Face2::GetSurface() and then use Surface::IsPlanar() to get the boolean.
  3. Now get normal to each planar surface using Face2::Normal(). Please note that this function results valid normal only for planar faces.
  4. Compare normal of the planar faces for parallel (or colinear) normal. The side faces of structural members shall have corresponding planar face which is parallel (or colinear) on the other side. Remove these faces. You shall be left with end faces only.
  5.  As a simple test, you can also select the faces with minimum area from the list if normal faces instead of step 4. Usually, these are the faces which are the end faces.

Hope it helps!!!

Rajat Jain

FE
FE 2021-04-08

I will read up on those help files tomorrow

Thanks ‌

JC
JC 2021-04-08

well you need to get the body.

Evaluate all faces IBody2:GetFaces

determine what faces are the cut ones through analysis. there will be some logic you will figure out i'm sure.

take those 2 cut faces

get the angle between them.

FE
FE 2021-04-08

then what you need to do is 

 

Take the cut faces.

But how do I get the cut face without selecting it manually?

In IFace2 I could not find a clue to getting the cut faces of each part

JC
JC 2021-04-08

then what you need to do is 

Take the cut faces.

use IFace;Normal to get its direction

Face1Vec =IMathUtiliity:CreateVector(IFace:Normal)

Face2Vec = IMathUtiliity:CreateVector(IFace:Normal)

Then you can pass those vectors to the same function GetAngleBetweenVectors.(Face1Vec,Face2Vec)

Jacob Corder

www.cadsharp.com

FE
FE 2021-04-08

I will try to struggle through this. It looks like a way to get there but with my limited SW API knowledge the road is still looooong.

Myself I was thinking more in the line of traversing through the entire cut list and for each part in that cut list comparing the start and end face of the profile in relation to themselves and to the sketch line it originated from. But also this sounds a bit more like magic then reality to me now.

Thank you ‌ for your reaction