【ProToolkit范例之五】输出Asm的架构

时间:2021-06-21 19:41:31

在ProE中,Asm档一般很小,因为将prt或子asm档组立进去的话,并不是文件拷贝而是相当于只是以指针的形式存储。

以下示例用于访问组立档并以txt文件的形式输出组立档的架构。

【ProToolkit范例之五】输出Asm的架构【ProToolkit范例之五】输出Asm的架构

源码下载:http://download.csdn.net/detail/wangyao1052/4995636

代码如下:

//-----------------------------------------------------------------------------
//*****************************************************************************
//-----------------------------------------------------------------------------
#pragma region

#include <ProToolkit.h>
#include <ProUtil.h>
#include <ProMenuBar.h>
#include <ProMenu.h>
#include <ProMode.h>
#include <ProMdl.h>
#include <ProSolid.h>
#include <ProAsmcomp.h> 
#include <ProArray.h>
#include <ProModelitem.h>
#include <ProFeature.h>
#include <ProFeatType.h>
#include <ProLayer.h>





// 函数声明
uiCmdAccessState TestAccess(uiCmdAccessMode access_mode);
uiCmdAccessState AsmOutputTestAccess(uiCmdAccessMode access_mode);

int Test(uiCmdCmdId  command,
         uiCmdValue *p_value,
         void       *p_push_command_data);
int AsmOutputTest(uiCmdCmdId  command,
                  uiCmdValue *p_value,
                  void       *p_push_command_data);

ProError _VisitAsmAllComponent_(ProFeature* p_feature,
                                ProError status,
                                ProAppData app_data);

extern "C" int user_initialize()
{
    ProError err;
    ProFamilyName MsgFile;
    ProStringToWstring(MsgFile, "Test_Msg.txt");

    // 添加菜单TestMenu
    err = ProMenubarMenuAdd("TestMenu", "TestMenu", "Help", PRO_B_TRUE, MsgFile);

    // 添加命令和Menu Item
    uiCmdCmdId cmd_id1;

    err = ProCmdActionAdd("Test", Test, uiProeImmediate, TestAccess, 
        PRO_B_FALSE, PRO_B_FALSE, &cmd_id1);

    err = ProMenubarmenuPushbuttonAdd("TestMenu", "Test", "Test", "Test_Help",
        NULL, PRO_B_TRUE, cmd_id1, MsgFile);

    uiCmdCmdId cmd_id2;

    err = ProCmdActionAdd("AsmOutputTest", AsmOutputTest, uiProeImmediate, AsmOutputTestAccess, 
        PRO_B_FALSE, PRO_B_FALSE, &cmd_id2);

    err = ProMenubarmenuPushbuttonAdd("TestMenu", "AsmOutputTest", "AsmOutputTest", "AsmOutputTest_Help",
        NULL, PRO_B_TRUE, cmd_id2, MsgFile);

    return 0;
}


extern "C" void user_terminate()
{     
}

int Test(uiCmdCmdId  command,
         uiCmdValue *p_value,
         void       *p_push_command_data)
{
    AfxMessageBox(TEXT("Just For Test"));
    return 0;
}

struct AsmTreeNode
{
    char name[PRO_NAME_SIZE];
    AsmTreeNode *pParent;           // 父节点
    vector<AsmTreeNode> vChilds;  // 子节点的集合

    AsmTreeNode()
    {
        memset(name, 0, sizeof(name));
        pParent = NULL;
    }
};

void OutputAsm(FILE *fp, AsmTreeNode *pNode, int nLayer)
{
    for (int i=0; i<nLayer; ++i)
    {
        fprintf(fp, "--");
    }
    fprintf(fp, "%s\n", pNode->name);

    nLayer++;
    for (int i=0; i<pNode->vChilds.size(); ++i)
    {
        OutputAsm(fp, &(pNode->vChilds[i]), nLayer);
    }
    nLayer--;
}

int AsmOutputTest(uiCmdCmdId  command,
                  uiCmdValue *p_value,
                  void       *p_push_command_data)
{
    ProError err;
    ProMdl mdl_curr;
    ProMdlType mdl_type;
    
    // 获取当前模型并判断是不是组立档案
    err = ProMdlCurrentGet(&mdl_curr);
    err = ProMdlTypeGet(mdl_curr, &mdl_type);
    if (PRO_MDL_ASSEMBLY != mdl_type)
    {
        AfxMessageBox(TEXT("当前模型不是组立档案!"));
        return -1;
    }

    // 获取当前组立档的名字
    ProName mdl_name;
    ProMdlNameGet(mdl_curr, mdl_name);

    // 构造节点
    AsmTreeNode head;
    ProWstringToString(head.name, mdl_name);

    // 递归访问组立档下的所有Component
    err = ProSolidFeatVisit((ProSolid)mdl_curr, _VisitAsmAllComponent_, NULL, &head);

    // 输出
    FILE *fp = fopen("D:\\123.txt", "a");
    OutputAsm(fp, &head, 0);
    fclose(fp);

    return 0;
}

uiCmdAccessState TestAccess(uiCmdAccessMode access_mode)
{
    return ACCESS_AVAILABLE;
}

uiCmdAccessState AsmOutputTestAccess(uiCmdAccessMode access_mode)
{
    ProError err;
    ProMode  mode;

    err = ProModeCurrentGet(&mode);
    if (err != PRO_TK_NO_ERROR)
    {
        return ACCESS_UNAVAILABLE;
    }

    if (mode == PRO_MODE_ASSEMBLY)
    {
        return ACCESS_AVAILABLE;
    }
    else
    {
        return ACCESS_UNAVAILABLE;
    }
}

ProError _VisitAsmAllComponent_(ProFeature* p_feature,
                                ProError status,
                                ProAppData app_data)
{
    AsmTreeNode *pParent = (AsmTreeNode*)app_data;

    // 判断是不是ProAsmComp
    ProError err;
    ProFeattype feat_type;
    ProFeatureTypeGet(p_feature, &feat_type);
    if (PRO_FEAT_COMPONENT != feat_type)
    {
        return PRO_TK_NO_ERROR;
    }
    
    // 构造节点
    AsmTreeNode node;
    ProMdl mdl_asmcomp;
    ProAsmcompMdlGet(p_feature, &mdl_asmcomp);

    ProName mdl_name;
    ProMdlNameGet(mdl_asmcomp, mdl_name);
    ProWstringToString(node.name, mdl_name);

    // 递归遍历子节点
    err = ProSolidFeatVisit((ProSolid)mdl_asmcomp, _VisitAsmAllComponent_, NULL, &node);

    // 和父节点关联
    node.pParent = pParent;
    pParent->vChilds.push_back(node);

    return PRO_TK_NO_ERROR;
}

#pragma endregion
//-----------------------------------------------------------------------------
//*****************************************************************************
//-----------------------------------------------------------------------------