|
VC++6.0及Pro/E3.0下二次开发,选择自定义坐标系,输出创建点的坐标。
代码如下:
//包含头文件
#include "roToolkit.h"
#include "roMenu.h"
#include "roUtil.h"
#include "ProMenubar.h"
#define PRO_USE_VAR_ARGS 1//在VC6.0下promessagedisplay()出现参数不对问题时,加上该语句
#include <ProMessage.h>
#include <ProSolid.h>
#include <ProWindows.h>
#include "ProFeatType.h"
#include <ProFeature.h>
#include "ProMdl.h"
#include "ProSolid.h"
#include "ProWstring.h"
#include <ProArray.h>
#include <ProObjects.h>
#include <ProAsmcomp.h>
#include <ProMessage.h>
#include <ProSolid.h>
#include <ProArray.h>
#include "ProDrawing.h"
#include "ProPoint.h"
#include "ProAnnotation.h"
#include <ProCsys.h>
#include <ProModelitem.h>
// ProUtilVectorCopy所需头文件
/*#include <UtilMath.h>*///有时直接包含该头文件,编译会出问题,这是可直接将所需函数复制到代码中使用,可解决问题
//检验函数TEST_CALL_REPORT所需头文件
//#include "UtilNames.h"
#define max_num 3//自定义宏
int CGetCSYSListDlg::OnUsrDimPoints() // CGetCSYSListDlg是本人自定义的对话框类;
{
// TODO: Add your control notification handler code here
ProError status;
ProSelection *sel_p,*sel_c, csys_sel;
int n_sel, n_points,CurWin;
ProSolid solid;
ProVector point,point1;
ProModelitem model_item1[max_num],model_item2;
ProPoint p_point[max_num];
ProCsys p_csys;
ProCsysdata *csys_data;
ProFileName messagefile;
ProGeomitemdata *csys_geomdata;
ProMatrix from_csys,to_csys,trf;
ProAsmcomppath csys_comppath;
ProGeomitem csys_geom;
ProStringToWstring(messagefile,"msg.txt");
//选择自定义坐标系并获得该坐标系的相对于原坐标系变换矩阵
ProMessageDisplay(messagefile,"选择自定义坐标系");
ProSelect("csys",1,NULL, NULL, NULL, NULL,&sel_c,&n_sel);
if(status == PRO_TK_USER_ABORT || n_sel < 1)
return(0);
ProSelectionModelitemGet(sel_c[0], &model_item2);//获得选择项的模型项
status = ProCsysInit( (ProSolid) model_item2.owner,model_item2.id, &p_csys);//获得选择坐标系的句柄p_csys。
status = ProCsysDataGet(p_csys, &csys_geomdata);
csys_data = csys_geomdata->data.p_csys_data;
// TEST_CALL_REPORT("ProCsysDataGet()","OnUsrDimPoints()",//检验函数,包含在头文件#include "UtilNames.h"内,源文件在
//proe安装目录~proeWildfire3.0\protoolkit\protk_appls\pt_examples\pt_utils内
// status, status != PRO_TK_NO_ERROR);
ProMatrixInit(csys_data->x_vector,csys_data->y_vector,csys_data->z_vector,
csys_data->origin, from_csys);
ProUtilMatrixInvert(from_csys, to_csys);//to_csys,该矩阵就是我们需要的变换矩阵
//选择创建的点,max_num为自定义的宏,决定点的最大数目
status = ProSelect("point", max_num, NULL, NULL, NULL, NULL, &sel_p, &n_sel);
if(status == PRO_TK_USER_ABORT || n_sel < 1)
return(0);
for (int i=0;i<n_sel;i++)
{
status = ProSelectionModelitemGet(sel_p, &model_item1);//获得各个选中点的模型项
}
for (int j=0;j<n_sel;j++)
{
if (model_item1[j].type==PRO_POINT)
{
status = ProPointInit( (ProSolid) model_item1[j].owner,model_item1[j].id,&p_point[j]);//获取点的句柄
status = ProPointCoordGet(p_point[j], point1);
ProPntTrfEval(point1, to_csys, point1);//面向零件的
//显示点x,y,z坐标
char w1[100];
sprintf(w1,"%f",point1[0]);//显示点的x坐标值
AfxMessageBox(w1);
}
}
ProGeomitemdataFree(&csys_geomdata);
ProWindowCurrentGet(&CurWin);//窗口处理函数
ProWindowRepaint(CurWin);
ProWindowActivate(CurWin);
return (1);
}
//程序中用的函数,这些函数是从proe安装包内自带的部分复制而来
double *VectorCopy(
double from[3],
double to[3])
{
if(from == NULL)
to[0] = to[1] = to[2] = 0.0;
else
{
to[0] = from[0];
to[1] = from[1];
to[2] = from[2];
}
return(to);
}
static double identity_matrix[4][4] = { {1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0}
};
void ProUtilMatrixCopy(
double input[4][4],
double output[4][4])
{
int i,j;
if(input == NULL)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
output[j] = identity_matrix[j];
}
else
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
output[j] = input[j];
}
}
int ProUtilMatrixInvert(
double m[4][4],
double output[4][4])
{
double vec[3], scale_sq, inv_sq_scale;
int i,j;
/*--------------------------------------------------------------------*\
If the matrix is null, return the identity matrix
\*--------------------------------------------------------------------*/
if(m == NULL)
{
ProUtilMatrixCopy(NULL, output);
return(1);
}
/*--------------------------------------------------------------------*\
Obtain the matrix scale
\*--------------------------------------------------------------------*/
vec[0] = m[0][0];
vec[1] = m[0][1];
vec[2] = m[0][2];
scale_sq = vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2];
/*--------------------------------------------------------------------*\
Check whether there is an inverse, and if not, return 0
\*--------------------------------------------------------------------*/
if(scale_sq < (.000000001 * .000000001))
return(0);
/*--------------------------------------------------------------------*\
Need the inverse scale squared
\*--------------------------------------------------------------------*/
inv_sq_scale = 1.0 / scale_sq;
/*--------------------------------------------------------------------*\
The orientation vectors
\*--------------------------------------------------------------------*/
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
output[j] = m[j] * inv_sq_scale;
output[j][3] = 0.0;
}
/*--------------------------------------------------------------------*\
The shift vectors
\*--------------------------------------------------------------------*/
for(i=0;i<3;i++)
{
output[3] = 0.0;
for(j=0;j<3;j++)
output[3] -= m[j] * m[3][j] * inv_sq_scale;
}
output[3][3] = 1.0;
return(1);
}
程序编写时遇到的问题及解决办法:
(1)使用promessagedisplay(),参数不对的问题,参考http://wenku.baidu.com/view/488dade1524de518964b7d5e.html。
(2)使用proutil中函数,出现内存泄露问题,解决办法,将其中用到的函数复制到代码中,例如本例中用到的ProUtilMatrixCopy()函数,就是从proe安装目录~proeWildfire3.0\protoolkit\protk_appls\pt_examples\pt_utils中UtilMath.c中复制来的。
(3)程序最后需要添加上窗口处理函数 ProWindowCurrentGet();ProWindowRepaint();ProWindowActivate();否则会出现选择的坐标系、点等一直处于选择状态灯问题,具体用法参见各类资料。
(4)最后附上源程序,供大家参考。
From_PTC_Example_CreateDimension.rar
(31.62 KB, 下载次数: 62)
|
|