
Re: Construct a MathTransform
John Alexander Sep 22, 2017 8:44 AM (in response to Amen Allah Jlili)You want to compute the math transform for a coordinate system located at "Coordinate system 1" without creating the "Coordinate System 1" feature?
I don't know if this would actually be any faster than using the existing coordinate system feature. I would understand the motivation if the coordinate system doesn't exist yet.
Transformation matrix data:
a b c . n 
d e f . o 
g h i . p 
. 
j k l . m 
Translation
{j,k,l} is the vector describing the translation. You would need to get the position of that "Coordinate system 1" with respect to the part's origin. i.e. {x,y,z} = {j,k,l}
The documentation indicates that {n,o,p} are unused, n=0, o=0, p=0.
Scale
m = 1.0
I've only ever used this with drawings.
Rotation
The 3x3 submatrix (ai) are the rotational components. I believe each column (or row) in that rotation matrix can be thought of as a unit vector aligned with an axis of the coordinate system being rotated into/out of. I've convinced myself of this because, R I = R leads me to believe that the columns of a rotation matrix can be thought of as the normal vectors aligned with the resulting coordinate system axes. If this isn't true, the following comments about building the rotation matrix are incorrect.
{a,b,c}  unit vector aligned with xaxis
{d,e,f,}  unit vector aligned with yaxis
{g,h,i}  unit vector aligned with zaxis
I think this is correct, otherwise it would be the transpose of this. i.e. {a,d,g}  xaxis.
If that green face is planar, you can get its normal vector with respect to the part's origin.
2012 SOLIDWORKS API Help  Normal Property (IFace2)
You can assign that to {d,e,f} = {y1,y2,y3}. Where {y1,y2,y3} are the x,y,z components of the IFace.Normal(). I say {d,e,f} because your image shows the Yaxis perpendicular to the surface.
Next you need to determine either the Xaxis or the Zaxis and then compute the last term. How you will determine the Xaxis or Zaxis isn't clear from the image. It looks like maybe X is aligned with that sketch line.
Assuming you get the xaxis from the sketch line (might have to compute it from startpointendpoint), you can get the zaxis as the cross product of your xaxis and yaxis. Because x and y are already unit vectors, this product should still be a unit vector.
z = x x y
You could either compute this or build MathVectors for x, and y and set z = x.cross(y).
Finally, with the big array of 16 doubles built, you pass it to swMathUtil.CreateMathTransform
2012 SOLIDWORKS API Help  CreateTransform Method (IMathUtility)
**If you follow these steps exactly, the result will likely be wrong. It is probably necessary to change the direction of the translation (set all the terms negative), transpose the rotation, or invert the whole thing before you get the transform you are looking for.

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 8:50 AM (in response to John Alexander)I'll give this a try.

Re: Construct a MathTransform
Jacob Corder Sep 22, 2017 3:50 PM (in response to Amen Allah Jlili)what i do with all this fun math stuff is use methods found in
https://msdn.microsoft.com/enus/library/system.windows.media.media3d(v=vs.110).aspx
https://msdn.microsoft.com/enus/library/system.windows.media.media3d.vector3d(v=vs.110).aspx
Matrix3D Structure (System.Windows.Media.Media3D)
you can also get the source code or somewhat like it online if you search for Point3d or Vector3D or matrix3D
once i discoveded these methods, i tell ya. they took over all the calculating in my program.
I'm trying to show an alternate to creating solidworks vectors from solidworks math
you will just need to make the final MathVectors or just create the matrix
Cross
Public Function CrossProduct(ByVal VecX As Double, ByVal VecY As Double, ByVal VecZ As Double) As Double()
'Vector that Is Orthogonal between 2 vectors
Return new double(){(Y * VecZ)  (Z * VecY)}, {(Z * VecX)  (X * VecZ)}, {(X * VecY)  (Y * VecX)}
End Function

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 6:00 PM (in response to Jacob Corder)Thanks Jacob.

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 6:02 PM (in response to Jacob Corder)On a different note, have you ever tried to get the rotational direction of a circular arc (IEdge)? You'll be the star of the show if you could figure this out.

Re: Construct a MathTransform
Jacob Corder Sep 22, 2017 6:08 PM (in response to Amen Allah Jlili)Challenge accepted.

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 6:10 PM (in response to Jacob Corder)Choose whatever perspective you like (Either you look down on the face or you're looking from below). You may want to take a look at the Vector Axis of the CircleParams too.

Re: Construct a MathTransform
Jacob Corder Sep 22, 2017 6:44 PM (in response to Amen Allah Jlili)My thoughts are getting the lenght of the chord, and comparing it to the start vertex to end vertex. It will work for all besides exactly a half circle.

Re: Construct a MathTransform
Jacob Corder Sep 22, 2017 6:45 PM (in response to Jacob Corder)Or a random true false. It should be 50% accurate hehehehe

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 6:46 PM (in response to Jacob Corder)Been there done. Got the TShirt. Move on to another approach.... you've accepted the challenge

Re: Construct a MathTransform
Jacob Corder Sep 22, 2017 6:56 PM (in response to Amen Allah Jlili)I'm not at a computer to work on it until Monday.... I have accepted the challenge... And am sticking to it. I will admit defeat probably on Monday.

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 6:59 PM (in response to Jacob Corder)And I personally wish all the best. Your victory is my victory and so is the defeat





Re: Construct a MathTransform
John Alexander Sep 22, 2017 8:29 PM (in response to Amen Allah Jlili)Is the circleparams axis not aligned with the startvertex>endvertex sense by the right hand rule? Looks like the underlying curveparams "Sense" property might invert that relationship?
2012 SOLIDWORKS API Help  CircleParams Property (ICurve)

Re: Construct a MathTransform
Amen Allah Jlili Sep 22, 2017 8:33 PM (in response to John Alexander)2012 SOLIDWORKS API Help  Sense Property (ICurveParamData) merely gets whether the curve and edge are in the same direction. It's not reliable.

Re: Construct a MathTransform
John Alexander Sep 25, 2017 7:00 AM (in response to Amen Allah Jlili)If you adopt the convention that the angle of the arc starts at Startvertex and ends at EndVertex  and that the cross product of the vectors from the centerpoint to each point (start and end) respectively should align with the axis, i think this will get you what you want.
Dim center as mathpoint
Dim axis as MathVector
Dim V1 as mathvector
Dim V2 as mathvector
Dim V3 as mathvector
Dim sensemultiplier as integer
Dim projection as double
Dim angle_is_more_than_pi as boolean
Dim angle as double
const cPrecision = 0.00000001
'check if it is aligned
if curveParamDAta.Sense then
sensemultiplier = 1.0
else
sensemultiplier = 1.0
end if
'turn everything into a mathpoint or mathvector
center = MathPoint(circleparams[0],circleparams[1],circleparams[2])
axis = MathVector(circleparams[3],circleparams[4],circleparams[5])
startpoint = MathPoint(startvertex) 'from vertex coordinates
endpoint = MathPoint(endvertex) 'from vertex coordinates
'compute the vectors from centerpoint to startpoint and from centerpoint to endpoint respectively
V1 = MathVector(startpoint  center) 'might have to use mathpoint.subtractpoint or something like that
V2 = MathVector(endpoint  center)
V1 = V1.normalise() 'don't care about the radius, just want the unit vector
V2 = V2.normalise() 'don't care about the radius, just want the unit vector
V3 = V1.Cross(V2) 'this should point in the same direction as the axis. It might also point in the opposite direction depending on the 'Sense'
'project the vector from the cross product onto the axis vector. This will be 1 if they are perfectly aligned 1 if their alignments are flipped, somewhere between the two (1,1) if they are not perfectly aligned.
projection = axis.dot(V3)
'if the magnitude is 1 or 1 within some cPrecision
if (projection*projection  1) < cPrecision then
if projection * sensemultiplier < 0 then
angle_is_more_than_pi = True
else
angle_is_more_than_pi = False
end if
else
'either solidworks lied about the centerpoint, the precision is too conservative, or I suck at math
end if
angle = acos(V1 .dot(V2)) 'minimum angle between the vectors
'do one more comparison to see if it needs to be flipped
if angle_is_more_than_pi then
angle = 2 * pi  angle
end if
Acos() can be stolen from: ASin, ACos, ACot, ASec, ACsc  Missing inverse trig functions
I haven't had a chance to test this approach. It is possible that curveparamdata's sense property doesn't have any impact on the orientation of the axis orientation but i can't figure out how else it would be used. Like, why would it exist if it had no bearing on the Edge?









Re: Construct a MathTransform
Amen Allah Jlili Sep 23, 2017 5:37 PM (in response to Amen Allah Jlili)A quick workaround to the question would be to get to use the NormalTo command and then get the MathTransform from the active model view.