Understanding matrix transformations

Understanding matrix transformations

eric.mathews7BV6E
Contributor Contributor
629 Views
5 Replies
Message 1 of 6

Understanding matrix transformations

eric.mathews7BV6E
Contributor
Contributor

Using standard .Net math object like Sin, Cos and Tan, a line starting at (0, 0) and extending to the right will have a degree angle of 0°. The rotation is counter clock wise and thus a line at 270° will be pointing straight down. So, lets consider a line that starts at (0,0) and extends to (0,-12).

 

When I apply this logic to a sketch created on a workplane, it inverts the line to be vertical from (0,0) to (0, 12). Is there a way to fix this using a matrix object? 

0 Likes
630 Views
5 Replies
Replies (5)
Message 2 of 6

Michael.Navara
Advisor
Advisor

Transformation matrices are very useful for manipulation with objects in 2D and 3D space. But in the sketch you can use much simpler methods Sketch.MoveSketchObjects and Sketch.RotateSketchObjects which provides simple transformation in 2D space.

But be careful if you don't forgot some geometrical constrains on the sketch line. (Vertical constraint in your sample.)

 

 

EDIT:

Code sample

Dim sketch As PlanarSketch = ThisApplication.ActiveEditObject
Dim objectsToRotate = ThisApplication.TransientObjects.CreateObjectCollection
objectsToRotate.Add(sketch.SketchLines(1))
Dim centerPoint = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
Dim angle As Double = 3 / 2 * PI
Dim copy = False
Dim removeConstraints = True
sketch.RotateSketchObjects(objectsToRotate, centerPoint, angle, copy, removeConstraints)
0 Likes
Message 3 of 6

eric.mathews7BV6E
Contributor
Contributor

I can rotate, mirror and move points before they are inserted into the sketch. I'm trying to come up with a bullet proof way to feed in 2d points and get 2d sketch points that match my expectations.

 

ILine2D _sketchLine2d = new ILine2D(0, 0, 10, 12);            
PlanarSketch _sketch = GetSketch(); // On the YZ Plane. 

Matrix _modelMatrix = _sketch.ModelToSketchTransform;

// By this logic, _xAxisTemp == Z Vector && _yAxisTemp == X Vector && _zAxisTemp == Y Vector.
_modelMatrix.GetCoordinateSystem(out Point _origin1, out Vector _xAxisTemp, out Vector _yAxisTemp, out Vector _zAxisTemp);

// Using the above information the matrix coordinate system is reset.
_modelMatrix.SetCoordinateSystem(_origin1, _yAxisTemp, _zAxisTemp, _xAxisTemp);

// Now, all the vectors come out as the correct axes. 
_modelMatrix.GetCoordinateSystem(out Point _origin1A, out Vector _xAxisTempA, out Vector _yAxisTempA, out Vector _zAxisTempA);

// Create the start/end points of the line.
Point _pt1 = _tg.CreatePoint(_sketchLine2d.StartPoint.X, _sketchLine2d.StartPoint.Y);
Point _pt2 = _tg.CreatePoint(_sketchLine2d.EndPoint.X, _sketchLine2d.EndPoint.Y);

// This is where I'd expect the points to be transformed, that is; the values  are changed, by the new matrix. 
// But, nothing happens. The values don't change.
_pt1.TransformBy(_modelMatrix);
_pt2.TransformBy(_modelMatrix);

// These points are wrong.
Point2d _2dPt1 = _sketch.ModelToSketchSpace(_pt1);
Point2d _2dPt2 = _sketch.ModelToSketchSpace(_pt2);
0 Likes
Message 4 of 6

Michael.Navara
Advisor
Advisor

I'm sorry, I don't understand what do you try to do. transformation matrices works well, when you need to recalculate coordinates from one coordinate system to another. For example:

 

1) You know the 3D point in global coordinate system (GCS) and you want to get its coordinates in sketch coordinate system (SCS). You can use ModelToSketchTransform matrix to calculate its coordinates (ignore Z value, it is always 0).

 

ModelToSketchTransform_Sample

Similar to project point, but without reference to original geometry

 

Dim part As PartDocument =ThisDoc.Document
Dim targetSketch As PlanarSketch = part.ComponentDefinition.Sketches("TargetSketch1")
Dim workPoint1 As WorkPoint = part.ComponentDefinition.WorkPoints("Work Point1")

Dim pt1 = workPoint1.Point
pt1.TransformBy(targetSketch.ModelToSketchTransform)

Dim pt1_2D As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(pt1.X, pt1.Y)

targetSketch.SketchPoints.Add(pt1_2D, True)​

 

 

 

2) You can manipulate with objects within coordinate system.

DrawHexagon_Sample

Move point from one vertex of hexagon to the next one and draw the lines between them 

 

Dim part As PartDocument =ThisDoc.Document
Dim targetSketch As PlanarSketch = part.ComponentDefinition.Sketches("HexagonSketch1")

Dim pt1_2D As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0,0)
Dim startSketchPoint As SketchPoint
Dim endSketchPoint As SketchPoint

Dim mtxTranslation As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
Dim vectorTranslation As Vector2d = ThisApplication.TransientGeometry.CreateVector2d(1,0)
mtxTranslation.SetTranslation(vectorTranslation)

Dim mtxRotation As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
Dim center As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0,0)
mtxRotation.SetToRotation(PI / 3, center)

Dim mtxNextVertex As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
mtxNextVertex.PostMultiplyBy(mtxRotation)
mtxNextVertex.PostMultiplyBy(mtxTranslation)

startSketchPoint = targetSketch.SketchPoints.Add(pt1_2D)

For i = 1 To 6
	pt1_2D.TransformBy(mtxNextVertex)
	endSketchPoint = targetSketch.SketchPoints.Add(pt1_2D)
	targetSketch.SketchLines.AddByTwoPoints(startSketchPoint, endSketchPoint)
	startSketchPoint = endSketchPoint
Next​

 

 

 

 

Message 5 of 6

eric.mathews7BV6E
Contributor
Contributor

I appreciate your help. However; I'm not quite there yet. Let me explain it another way. I went into the sketch options and turned on the "Coordinate System Indicator". You see the global UCS on the left and the sketch UCS on the right. According to the help file: "The default Autodesk Inventor coordinate system follows the conventional rectangular Cartesian system with the positive X direction to the right, the positive Y direction upwards, and the positive Z direction towards you." Here in lies the problem. The X-Axis isn't pointing to the right. it's point up. The Y-Axis isn't pointing up, it's pointing to the left. Just to be clear this is looking at the sketch from the "positive" side. This is why adding a point (5, 5) goes to the top left instead of the top right. So, at this point, I'm not 100% sure what I need that will allow me to add points with a "conventional rectangular Cartesian system...".

 Dual UCS.PNG

0 Likes
Message 6 of 6

Michael.Navara
Advisor
Advisor

Directions up, down left right has no sense. Because you can change the orientation of the SCS. See the picture below

 

2024-02-01_8-22-00.png

 

If you want to calculate coordinates, you need to choose one coordinate system (usually GCS) and all calculations needs to be done in this CS. Then you can transform the coordinates to target CS (in your case it is the SCS)

I have a lot of experience with working with transformations and in my opinion this is the best practice.

 

0 Likes