Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Rotate sketchblock

taher.basha
Enthusiast

Rotate sketchblock

taher.basha
Enthusiast
Enthusiast

I want to rotate a sketch block using vb.net or C# 

 

It's just to read the current position and rotate to some angle

0 Likes
Reply
464 Views
9 Replies
Replies (9)

taher.basha
Enthusiast
Enthusiast

I have tried this but is there any other method as well?

0 Likes

Michael.Navara
Advisor
Advisor
0 Likes

taher.basha
Enthusiast
Enthusiast

I want to basically

1. Insert(from other sketch),

2. rotate and

3. position a block as per user inputs of X and Y value.

I could be able to insert and  rotate it with the solution you posted above but I couldn't be able to position it since the sketchblock coordinates reading the insertion position which is offset from origin. After rotation I want to modify sketchblock coordinates to x=0,y=0

0 Likes

Michael.Navara
Advisor
Advisor

Can you share your code? May be this can be fixed easily.

0 Likes

J.Pelfrene
Enthusiast
Enthusiast

I did something like this before:

 

Public Sub Main()	
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oSketch As PlanarSketch = oPartDoc.ComponentDefinition.Sketches.Item(1)	
	If Not oPartDoc.SketchActive Then oSketch.Edit
	
	Dim oSketchObjects As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
		
	For Each oEnt In oSketch.SketchBlocks
		oSketchObjects.Add(oEnt)
	Next

	Dim oCen As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0,0)
	oSketch.RotateSketchObjects(oSketchObjects, oCen, Math.PI/2, False, True)	

	Dim allMinPoints As List(Of Point2d) = oSketch.SketchEntities.Cast(Of SketchEntity).Select(Function(e) e.RangeBox.MinPoint).OrderBy(Function(p) p.X).ToList()
	Dim minX = allMinPoints.First().X
	Dim minY = allMinPoints.OrderBy(Function(p) p.Y).First().Y
	
	Dim oVec As Vector2d = ThisApplication.TransientGeometry.CreateVector2d(-minX/2,-minY/2)
	oSketch.MoveSketchObjects(oSketchObjects, oVec)
	
End Sub

 

Angle is set to 90° counterclockwise (Math.PI/2). Change this for the angle you need.

 

I still don't really understand why it is necessary to add the factor 1/2 for the vector coordinates, but that way seems to work well for positioning the sketch neatly in the 1st quadrant.

taher.basha
Enthusiast
Enthusiast

TransientGeometry tg = invApp.TransientGeometry;

Matrix2d mx = tg.CreateMatrix2d();

//mx= skb[cbLetters.SelectedIndex].Transformation;

//---------------
double x = Convert.ToDouble(txtBoxXDir.Text)/10;
double y = Convert.ToDouble(txtBoxYDir.Text)/10;
double angle = Convert.ToDouble(txtBoxAngle.Text);
double oRad = (angle / 180) * 3.14159265358979;


mx.SetToRotation(oRad, tg.CreatePoint2d(0, 0));
skb[cbLetters.SelectedIndex].Transformation=mx;

mx.SetTranslation(tg.CreateVector2d(x, y));
skb[cbLetters.SelectedIndex].Transformation=mx;

invApp.ActiveDocument.Update();

0 Likes

taher.basha
Enthusiast
Enthusiast

I did almost the same with below code

 

TransientGeometry tg = invApp.TransientGeometry;

Matrix2d mx = tg.CreateMatrix2d();

//mx= skb[cbLetters.SelectedIndex].Transformation;

//---------------
double x = Convert.ToDouble(txtBoxXDir.Text)/10;
double y = Convert.ToDouble(txtBoxYDir.Text)/10;
double angle = Convert.ToDouble(txtBoxAngle.Text);
double oRad = (angle / 180) * 3.14159265358979;


mx.SetToRotation(oRad, tg.CreatePoint2d(0, 0));
skb[cbLetters.SelectedIndex].Transformation=mx;

mx.SetTranslation(tg.CreateVector2d(x, y));
skb[cbLetters.SelectedIndex].Transformation=mx;

invApp.ActiveDocument.Update();

 

 

but can i get some reference for Matrix and transformation 

0 Likes

taher.basha
Enthusiast
Enthusiast

I did somthing like this

 

static void Rotate(SketchBlock sketchBlock)
{
    TransientGeometry tg = InvAccess.InvApp.TransientGeometry;

    Matrix2d mx1 = tg.CreateMatrix2d();

    mx1= sketchBlock.Transformation.Copy();

 

    double oRad = Math.PI/2;

 

    Matrix2d mx2 = tg.CreateMatrix2d();

    mx2.SetToRotation(oRad, tg.CreatePoint2d(0, 0));

    mx1.PreMultiplyBy(mx2);

 

    sketchBlock.Transformation=mx1;

 

    PartAccess.Model.prtDoc.Update();

}

 

which is luckly working. 

and I will again explain the scenario. I have a block say A which is vertical at the center and I want to rotate it to 90deg and move it to y=somevalue(say 20) 

0 Likes