装配_遍历装配树_获取和设置组件属性

/********************************************************************************
** (C) Copyright 2025, ZWSOFT Co., LTD. (Guangzhou) All Rights Reserved.
*********************************************************************************/
#include "pch.h"
#include <algorithm>

//已经后台激活过的零件
vector<svxCompName> activatedParts{};

void 装配_遍历装配树_获取和设置组件属性(svxEntPath* pComponentPath)
{
//获取组件级别
WriteMessage("组件级别 = %d", pComponentPath->Count / 2);

#if VX_API_VERSION >= 2900
//获取组件的名称(含序号后缀)
char componentNameWithOrder[256]{};
ZF_CALL(cvxCompNameWithOrderNumberGet(pComponentPath, componentNameWithOrder, sizeof(componentNameWithOrder)));
WriteMessage("组件名(含序号后缀) = %s", componentNameWithOrder);
#endif

//组件名称(右击组件-实体信息 才能看到)
vxLongName name;
ZF_CALL(cvxEntNameByPath(pComponentPath, name));
WriteMessage("name:%s", name);

//获取组件对应的零件
svxCompName compName = { 0 };
ZF_CALL(cvxCompInqPartByPath(pComponentPath, compName.File, compName.Part));
WriteMessage("组件对应的文件名 = %s,根对象名 = %s", compName.File, compName.Part);
vxLongPath filePath = { 0 };
ZF_MIX(cvxPathFindByFileName(compName.File, filePath, sizeof(filePath)));
WriteMessage("组件【%s】对应的文件全路径 = %s", compName.Part, filePath);
//判断组件后台文件是否丢失
int isLost = -1;
ZF_CALL(cvxCompIsFileLost(pComponentPath, &isLost));
if (isLost == TRUE)
//if (filePath[0] == 0)
{
//后台文件丢失
WriteMessage("组件【%s】后台文件丢失,将被移除", compName.Part);
//移除组件
ZF_CALL(cvxEntErase(pComponentPath));
}

//更改组件名(及其后台文件名)
//vxLongPath newFileName{};
//sprintf_s(newFileName, "%s_1.Z3PRT", compName.Part);
//vxRootName newRootName{};
//sprintf_s(newRootName, "%s_1", compName.Part);
//ZF_CALL(cvxRootRenameMultiByLongPath(1, &compName.File, &compName.Part, &newFileName, &newRootName, TRUE));

//高亮
svxColor color = { 255, 153, 0 };
ZF_VOID(cvxCompHighlight(pComponentPath, &color));
WriteMessage("设置高亮颜色 = (R:%d, G:%d, B:%d)", color.r, color.g, color.b);
//取消高亮
ZF_VOID(cvxCompUnHighlight(pComponentPath));
WriteMessage("取消高亮");
//重生成组件
//ZF_CALL(cvxCompRegen(pComponentPath, 1));
//WriteMessage("重生成组件【%s】", compName.Part);

//设置是否轻量化
ZF_CALL(cvxCompLightweightSetByPath(pComponentPath, 1, TRUE, TRUE));
//获取是否是轻量化组件
int isLightweight = -1;
ZF_CALL(cvxCompIsLightweightByPath(pComponentPath, &isLightweight));
WriteMessage("组件【%s】【%s】轻量化组件", compName.Part, isLightweight == TRUE ? "是" : "不是");

//获取父组件
svxEntPath parentPath = {};
ZF_MIX(cvxEntPathGetParentComp(pComponentPath, &parentPath));
if (parentPath.Count > 0) {
vxLongName parentName;
ZF_CALL(cvxEntNameByPath(&parentPath, parentName));
WriteMessage("组件【%s】的父组件是【%s】", compName.Part, parentName);
}
else {
WriteMessage("组件【%s】的父组件是【顶层节点】", compName.Part);
}

//获取组件所使用的配置
int idConfiguration = 0;
ZF_CALL(cvxCompCfgIdGet(pComponentPath, &idConfiguration));
WriteMessage("组件【%s】所使用配置 = %d", compName.Part, idConfiguration);
////获取配置名称
//char nameActiveConfiguration[128];
//ZF_CALL(cvxEntName(idConfiguration, nameActiveConfiguration, sizeof(nameActiveConfiguration)));
//WriteMessage("组件【%s】当前激活的配置为【%s】", compName.Part, nameActiveConfiguration);
//设置组件所使用的配置
ZF_CALL(cvxCompCfgIdSet(pComponentPath, idConfiguration));
WriteMessage("设置组件【%s】所使用配置 = %d", compName.Part, idConfiguration);

//判断组件是否是通过装配特征生成的(比如装配阵列)
int isFromAsmFtr = -1;
ZF_CALL(cvxCompFromAsmFtr(pComponentPath, &isFromAsmFtr));
WriteMessage("组件【%s】【%s】通过装配特征生成的", compName.Part, isFromAsmFtr == TRUE ? "是" : "不是");

//设置自动重生成状态
int autoRegen = 2;//0=无;1=装配前重生成;2=装配后重生成
//ZF_CALL(cvxAsmAutoRegenStateSetByPath(pComponentPath, autoRegen));
//获取自动重生成状态
autoRegen = -1;
ZF_CALL(cvxAsmAutoRegenStateGetByPath(pComponentPath, &autoRegen));
WriteMessage("组件【%s】的自动重生成状态 = %d", compName.Part, autoRegen);

//获取当前激活的组件
//svxEntPath activeComponentPath = {};
//ZF_CALL(cvxEntPathInqActive(&activeComponentPath));
//vxLongName activeComponentName;
//ZF_CALL(cvxEntNameByPath(&activeComponentPath, activeComponentName));
//WriteMessage("当前激活的组件名字 = 【%s】", activeComponentName);
//激活当前组件(测试完成后请注释代码,以免影响后续代码执行)
//ZF_CALL(cvxCompEditPartByPath(pComponentPath));

//获取位置(基于总装配的绝对坐标系)
svxMatrix position = { 0 };
ZF_CALL(cvxEntMatrix2(*pComponentPath, &position));

//后台激活组件对应的零件,某些操作必须激活组件对应的零件才能进行,如更改零件的表达式等,如不需要,请忽略
bool 有需要激活组件的操作 = false;
if (有需要激活组件的操作 &&
std::find_if(
activatedParts.begin(),
activatedParts.end(),
[compName](svxCompName it)//Lambda表达式,如果已经激活过就不再激活了,多个相同组件激活一次就行了
{
if (strcmp(compName.File, it.File) == 0 && strcmp(compName.Part, it.Part) == 0)
{
return true;
}
return false;
})
== activatedParts.end())
{
activatedParts.emplace_back(compName);
//进入激活状态
if (0 == ZF_CALL(cvxRootActivate2(compName.File, compName.Part)))
{
//请在下面添加对零件的操作的代码
// ...略
//退出激活状态
ZF_CALL(cvxRootActivate2(NULL, NULL));
}
}
WriteMessage("=========================");
}

void 装配_遍历装配树_一次性获取所有_svxEntPath版本()
{
activatedParts.clear();
//获取当前装配的所有级别的子组件
int componentNumber = 0;
svxEntPath* pComponentPaths = NULL;
ZF_CALL(cvxCompInqPaths(NULL, -1, FALSE, &componentNumber, &pComponentPaths));
WriteMessage("当前装配的所有级别的子组件数量 = %d", componentNumber);
for (int i = 0; i < componentNumber; i++)
{
装配_遍历装配树_获取和设置组件属性(pComponentPaths + i);
实用函数_输出_svxEntPath(pComponentPaths[i]);
}
ZF_VOID(cvxMemFree((void**)&pComponentPaths));
}

posted @   张永全-PLM顾问  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示