cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Winfrom App Using invoke The Creo viewer Control AxpviewLib.Axpview to see 3D drawing

MurphyMin
12-Amethyst

Winfrom App Using invoke The Creo viewer Control AxpviewLib.Axpview to see 3D drawing

1.Set the reference of the control

MurphyMin_0-1686204502768.png

2.Using axpview to open the .ed or .pvz drawing

Action<string> actionDelegate = x => { this.axpview1.sourceUrl = x; };
OpenFileDialog openFileDialog = new OpenFileDialog()
{
	InitialDirectory = @"D:\2023\parts\",
	Filter = @"Creo Viewer Files(*.pvz)|*.pvz|All files(*.*)|*.*"
};

DialogResult result1 = openFileDialog.ShowDialog();
if (result1 == DialogResult.OK && !string.IsNullOrWhiteSpace(openFileDialog.FileName))
{
	path = openFileDialog.FileName;
}
Task.Run(() =>
{
	axpview1.BeginInvoke(actionDelegate, path);
});

3.set the bg color for the axpview 

int hexValue = (129 << 16) | (156 << 😎 | 243;
//rgb(129,156,243) to hex color
string hexColorCode = $"0x{hexValue:X6}";
axpview1.backgroundcolor = hexColorCode;

4.Get the view location and then set all the default views location

//1.If you wanna to get the view location should coding like that
string str = axpview1.GetViewLocation();
//2.The result is like that "-0.993508 -0.0579201 0.0979185 0 0.0025464 //0.849163 0.528125 0 -0.113737 0.524945 -0.843502 0 2.36994 29.8653 -27.6611 1"
//got is, this string is build by the 4x4 matrix 
//3.Actually you can set the view location via the matrix you get 
 axpview1.SetViewLocation("-0.993508 -0.0579201 0.0979185 0 0.0025464 0.849163 0.528125 0 -0.113737 0.524945 -0.843502 0 2.36994 29.8653 -27.6611 1");
//you can get all the default views' location and set the new location by code

 5.But what if you want to rotating,moving,scaling the view?
5.1 Rotating matrixs

//firstly, you should know some basic knowledges about the matrix operation
//you can get the formal rotating matrix like the below codes in internet
/// <summary>
/// X轴旋转基准矩阵
/// </summary>
/// <param name="ang"></param>
/// <returns></returns>
public static Matrix4x4 GetXRotateMatrix(double ang)
{
	Matrix4x4 matrix4X4 = new Matrix4x4(1, 0, 0, 0,
										0, Math.Cos(ang).ToFloat(), -Math.Sin(ang).ToFloat(), 0,
										0, Math.Sin(ang).ToFloat(), Math.Cos(ang).ToFloat(), 0,
										0, 0, 0, 1
										);
	return matrix4X4;
}
/// <summary>
/// Y轴旋转基准矩阵
/// </summary>
/// <param name="ang"></param>
/// <returns></returns>
public static Matrix4x4 GetYRotateMatrix(double ang)
{
	Matrix4x4 matrix4X4 = new Matrix4x4(Math.Cos(ang).ToFloat(), 0, Math.Sin(ang).ToFloat(), 0,
										0, 1, 0, 0,
										-Math.Sin(ang).ToFloat(), 0, Math.Cos(ang).ToFloat(), 0,
										0, 0, 0, 1
										);
	return matrix4X4;
}
/// <summary>
/// z轴旋转基准矩阵
/// </summary>
/// <param name="ang"></param>
/// <returns></returns>
public static Matrix4x4 GetZRotateMatrix(double ang)
{
	Matrix4x4 matrix4X4 = new Matrix4x4(Math.Cos(ang).ToFloat(), -Math.Sin(ang).ToFloat(), 0, 0,
										Math.Sin(ang).ToFloat(), Math.Cos(ang).ToFloat(), 0, 0,
										0, 0, 1, 0,
										0, 0, 0, 1
										);
	return matrix4X4;
}

 5.2 For example , we want rotate the view around the axis "Y" with degree 180

//1.you should transform the angle from degree to radian
double angy = 180 * (Math.PI / 180);
//2.The the rotating matrix for axis Y(in step 5.1)
Matrix4x4 yMatrix = GetYRotateMatrix((float)angy);
//3.Then get the original view location matrix string, then transform it to 4X4 matrix
string oriMatrix2 = axpview1.GetViewLocation();
var list2 = oriMatrix2.Split(" ".ToCharArray());
Matrix4x4 matrix4X4v2 = ListToMatrix(list2);
//4.Then you got the new view matrxi by these two matrix 
Matrix4x4 destMatrixv2 = Matrix4x4.Multiply(matrix4X4v2, yMatrix);
//5.Transform the new matrix to the matrix string the axpview need
string strv2 = MatrixToString(destMatrixv2);
//6.Set new view location and Refresh
axpview1.SetViewLocation(strv2);
axpview1.Refresh();

5.2 Moving operation

/// <summary>
/// 用户指定轴移动,默认X轴
/// </summary>
/// <param name="axpview1"></param>
/// <param name="moveAxis"></param>
/// <param name="moveVal"></param>
public static void MoveUserStep(Axpview axpview1, string moveAxis, double moveVal)
{
	string oriMatrix = axpview1.GetViewLocation();
	var list = oriMatrix.Split(" ".ToCharArray());

	Matrix4x4 matrix4X4 = ListToMatrix(list);

	Matrix4x4 moveMatrix;
	switch (moveAxis)
	{
		case "X":
			moveMatrix = GetMoveMatrix(moveVal, 0, 0);
			break;
		case "Y":
			moveMatrix = GetMoveMatrix(0, moveVal, 0);
			break;
		case "Z":
			moveMatrix = GetMoveMatrix(0, 0, moveVal);
			break;
		default:
			moveMatrix = GetMoveMatrix(moveVal, 0, 0);
			break;
	}

	Matrix4x4 destMatrix = Matrix4x4.Multiply(matrix4X4, moveMatrix);
	string str = MatrixToString(destMatrix);
	axpview1.SetViewLocation(str);
	axpview1.Refresh();
}

5.3 Moving operation matrix

/// <summary>
/// 平移基矩阵
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
public static Matrix4x4 GetMoveMatrix(double x, double y, double z)
{
	Matrix4x4 matrix4X4 = new Matrix4x4(1, 0, 0, 0,
										0, 1, 0, 0,
										0, 0, 1, 0,
										x.ToFloat(), y.ToFloat(), z.ToFloat(), 1
										);
	return matrix4X4;
}

5.4 Scaling operation

/// <summary>
/// 缩放矩阵
/// </summary>
/// <param name="scale"></param>
/// <returns></returns>
public static Matrix4x4 GetScaleMatrix(double scale)
{
//actually the scale elements should be scale.toFloat(),however,in this control should be reciprocal
	Matrix4x4 matrix4X4 = new Matrix4x4(1 / scale.ToFloat(), 0, 0, 0,
									   0, 1 / scale.ToFloat(), 0, 0,
									   0, 0, 1 / scale.ToFloat(), 0,
									   0, 0, 0, 1
									   );


	return matrix4X4;
}

5.5 Scaling operation

/// <summary>
/// 用户指定比例等比例缩放
/// </summary>
/// <param name="axpview1"></param>
/// <param name="scale"></param>
public static void SetUserScale(Axpview axpview1, double scale)
{
	string oriMatrix = axpview1.GetViewLocation();
	var list = oriMatrix.Split(" ".ToCharArray());

	Matrix4x4 matrix4X4 = ListToMatrix(list);

	Matrix4x4 scaleMatrix = GetScaleMatrix(scale);

	Matrix4x4 destMatrix = Matrix4x4.Multiply(matrix4X4, scaleMatrix);

	string str = MatrixToString(destMatrix);

	axpview1.SetViewLocation(str);
	axpview1.Refresh();
}

 

Finally, all those codes should be add the checking logic for the user input params by youself

 

 

0 REPLIES 0
Announcements
NEW Creo+ Topics: PTC Control Center and Creo+ Portal


Top Tags