野火论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始


查看: 11287|回复: 5

[原创] 零件模式下自定义坐标系,输出点坐标

[复制链接]

2

主题

7

回帖

0

威望

实习生

Rank: 1

积分
11
发表于 2013-1-6 14:08 | 显示全部楼层 |阅读模式
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)

From_PTC_Example_CreateDimension.rar

31.62 KB, 下载次数: 59

程序源文件

0

主题

9

回帖

0

威望

实习生

Rank: 1

积分
11
发表于 2016-5-14 10:55 | 显示全部楼层
这么好的帖子,为什么没人顶起呢
回复 支持 反对

使用道具 举报

0

主题

2

回帖

0

威望

实习生

Rank: 1

积分
2
发表于 2017-6-11 11:11 | 显示全部楼层
厉害
回复 支持 反对

使用道具 举报

22

主题

636

回帖

6

威望

高工

Rank: 4

积分
735
发表于 2019-5-27 09:17 | 显示全部楼层
虽然感觉像自己要找的如何更改坐标系的一种解决办法 但是看到这么多代码 瞬间云里雾里 搞不清楚这个到底是怎么样的一个功能!还请楼主能贴图示意下 感谢
回复 支持 反对

使用道具 举报

21

主题

310

回帖

6

威望

工程师

Rank: 3Rank: 3

积分
409
发表于 2019-5-28 05:29 | 显示全部楼层
注释做得很好
回复 支持 反对

使用道具 举报

0

主题

11

回帖

0

威望

实习生

Rank: 1

积分
14
发表于 2019-12-17 15:41 | 显示全部楼层
厉害了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

本站为非营利性站点,部分资源为网友搜集或发布,仅供学习和研究使用,如用于商业用途,请购买正版。站内所发布的资源,如有侵犯你的权益,请联系我们,本站将立即改正或删除。

QQ|手机版|小黑屋|野火论坛(©2007~2024) ( 苏ICP备11036728号-2 )苏公网安备 32039102000103号

GMT+8, 2024-3-29 15:50 , Processed in 0.169449 second(s), 32 queries .

快速回复 返回顶部 返回列表