Punch

// // PunchCreation.cpp // // #include "stdafx.h" #include "Punch.h" #include "PunchCreation.h" #include "Udf.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #include #include #include #define PUNCH_LIBRARY_PATH "D://CAX_WORK_166//MechDesign//library//" #define PUNCH_UDF_PATH "D://CAX_WORK_166//MechDesign//udf//" #define PROJECT_NAME "TEST" // // Construction/Destruction // CPunchCreation::CPunchCreation() { } CPunchCreation::~CPunchCreation() { } // // 訪問特徵中所有幾何信息 // ProError VisitAllFeatGeomitems(ProGeomitem *p_handle, ProError status, ProAppData app_data) { ProArrayObjectAdd((ProArray*)app_data, -1, 1, p_handle); return PRO_TK_NO_ERROR; } // // 將坐標系特徵轉換為幾何信息 // ProError CsysFeatureToModelitem(ProFeature &csysFeat, ProGeomitem &csysItem) { ProError status; ProGeomitem *csysItems; status = ProArrayAlloc(0, sizeof(ProGeomitem), 1, (ProArray*)&csysItems); status = ProFeatureGeomitemVisit(&csysFeat, PRO_CSYS, VisitAllFeatGeomitems, NULL, &csysItems); if (status != PRO_TK_NO_ERROR) { return PRO_TK_USER_ABORT; } int itemCount; status = ProArraySizeGet(csysItems, &itemCount); if (status != PRO_TK_NO_ERROR || itemCount < 1) { return PRO_TK_USER_ABORT; } csysItem = csysItems[0]; return PRO_TK_NO_ERROR; } // // 獲取組立下所有檔案 // ProError GetAllFeatParts(ProFeature* p_feature, ProError status, ProAppData app_data) { ProFeattype featType; ProFeatStatus featStatus; status = ProFeatureStatusGet(p_feature, &featStatus); if (status != PRO_TK_NO_ERROR || featStatus != PRO_FEAT_ACTIVE) { return PRO_TK_NO_ERROR; } status = ProFeatureTypeGet(p_feature, &featType); if (status != PRO_TK_NO_ERROR || featType != PRO_FEAT_COMPONENT) { return PRO_TK_NO_ERROR; } ProMdl compMdl; status = ProAsmcompMdlGet(p_feature, &compMdl); if (status != PRO_TK_NO_ERROR) { return PRO_TK_NO_ERROR; } ProMdlType mdlType; status = ProMdlTypeGet(compMdl, &mdlType); if (status != PRO_TK_NO_ERROR) { return PRO_TK_NO_ERROR; } status = ProArrayObjectAdd((ProArray*)app_data, PRO_VALUE_UNUSED, 1, &compMdl); if (mdlType == PRO_MDL_ASSEMBLY) { status = ProSolidFeatVisit((ProSolid)compMdl, GetAllFeatParts, NULL, (ProAppData)app_data); } return PRO_TK_NO_ERROR; } // // 訪問檔案中所有尺寸 // ProError VisitAllDims(ProDimension* p_dimension, ProError status, ProAppData app_data) { status = ProArrayObjectAdd((ProArray*)app_data, -1, 1, p_dimension); return PRO_TK_NO_ERROR; } // // 獲取檔案中的所有尺寸 // void GetAllDimensions(ProMdl asmMdl, ProAppData allDims) { ProError status; int nPartCount(0); ProMdl* allParts; status = ProArrayAlloc(0, sizeof(ProMdl), 1, (ProArray*)&allParts); status = ProArrayObjectAdd((ProArray*)&allParts, -1, 1, &asmMdl); status = ProSolidFeatVisit((ProSolid)asmMdl, GetAllFeatParts, NULL, (ProAppData)&allParts); status = ProArraySizeGet((ProArray)allParts, &nPartCount); if (status != PRO_TK_NO_ERROR || nPartCount <= 0) { return; } for (int i = 0;i < nPartCount; ++i) { status = ProSolidDimensionVisit((ProSolid)allParts[i], PRO_B_FALSE, VisitAllDims, NULL, allDims); } } // // 創建Punch沖壓出來的特徵 // ProError CPunchCreation::CreatePunch( const string &punchName, vector &punchDims, ProSelection &punchOwnerSel, ProSelection *&pointSels, ProSelection &zOrientSel, ProSelection &xOrientSel) { ProError status; _punchName = punchName; _punchDims = punchDims; _punchOwnerSel = punchOwnerSel; _pointSels = pointSels; _zOrientSel = zOrientSel; _xOrientSel = xOrientSel; status = DownloadPunchFromLibrary(); if (status != PRO_TK_NO_ERROR) { return status; } status = CreatePunchCsys(); if (status != PRO_TK_NO_ERROR) { return status; } status = InsertPunchUdf(); if (status != PRO_TK_NO_ERROR) { return status; } status = RenamePunch(); status = SavePunch(); return PRO_TK_NO_ERROR; } // // 從標準庫中下載標準沖頭並進行尺寸驅動 // ProError CPunchCreation::DownloadPunchFromLibrary() { ProError status; ProPath punchFullPathW; char punchFullPathS[PRO_PATH_SIZE]; sprintf(punchFullPathS, "%s%s", PUNCH_LIBRARY_PATH, _punchName.c_str()); ProStringToWstring(punchFullPathW, punchFullPathS); status = ProMdlLoad(punchFullPathW, PRO_MDL_UNUSED, PRO_B_FALSE, &_punchMdl); if (status != PRO_TK_NO_ERROR) { return status; } if (_punchDims.size() < 1) { return PRO_TK_NO_ERROR; } int nDimCount = 0; ProDimension* allDims; status = ProArrayAlloc(0, sizeof(ProDimension), 1, (ProArray*)&allDims); GetAllDimensions(_punchMdl, (ProAppData)&allDims); status = ProArraySizeGet((ProArray)allDims, &nDimCount); for (int k = 0;k < nDimCount; ++k) { ProName dimName; char name[32]; status = ProDimensionSymbolGet(&allDims[k], dimName); ProWstringToString(name, dimName); // 驅動PUNCH的尺寸 for (int i = 0; i < _punchDims.size(); ++i) { if (!stricmp(_punchDims[i].Name.c_str(), name)) { ProDimensionValueSet(&allDims[k], _punchDims[i].Value); } } } // 重生檔案 ProSolidRegenerate((ProSolid)_punchMdl, PRO_REGEN_NO_FLAGS); return PRO_TK_NO_ERROR; } // // 根據用戶選擇的點/Z向參考面/X向參考面創建坐標系 // ProError CPunchCreation::CreatePunchCsys() { ProError status; int pointNum = 0; status = ProArraySizeGet(_pointSels, &pointNum); if (status != PRO_TK_NO_ERROR || pointNum < 1) { return PRO_TK_USER_ABORT; } ProAsmcomppath cmppath; status = ProSelectionAsmcomppathGet(_punchOwnerSel, &cmppath); status = ProArrayAlloc(0, sizeof(ProSelection), 1, (ProArray*)&_punchCsyses); for (int i = 0; i < pointNum; ++i) { UdfData udfData; ProSelectionCopy(_punchOwnerSel, &udfData.Owner); // UDF參考 UdfRef udfRef1("origin_point", _pointSels[i]); udfData.AddRef(udfRef1); UdfRef udfRef2("z_orient", _zOrientSel); udfData.AddRef(udfRef2); UdfRef udfRef3("x_orient", _xOrientSel); udfData.AddRef(udfRef3); // UDF其他信息 udfData.Path = PUNCH_UDF_PATH; udfData.Name = "punch_csys.gph"; // 創建UDF ProGroup udf; status = CreateUdf(udfData, &udf); if (status != PRO_TK_NO_ERROR) { continue; } // 獲取坐標系 ProFeature *udfFeats; status = ProGroupFeaturesCollect(&udf, &udfFeats); if (status != PRO_TK_NO_ERROR) { continue; } ProSelection csysSel; ProGeomitem csysItem; CsysFeatureToModelitem(udfFeats[1], csysItem); status = ProSelectionAlloc(&cmppath, &csysItem, &csysSel); if (status != PRO_TK_NO_ERROR) { continue; } status = ProArrayObjectAdd((ProArray*)&_punchCsyses, -1, 1, &csysSel); } return PRO_TK_NO_ERROR; } // // 插入UDF以創建FORM特徵 // ProError CPunchCreation::InsertPunchUdf() { ProError status; int punchNum = 0; status = ProArraySizeGet(_punchCsyses, &punchNum); if (status != PRO_TK_NO_ERROR || punchNum < 1) { return PRO_TK_USER_ABORT; } string punchName(_punchName); int startPos = punchName.find('.'); int len = punchName.size(); punchName.erase(punchName.begin() + startPos, punchName.begin() + len); for (int i = 0; i < punchNum; ++i) { UdfData udfData; ProSelectionCopy(_punchOwnerSel, &udfData.Owner); // UDF參考 UdfRef udfRef1("p_center", _punchCsyses[i]); udfData.AddRef(udfRef1); // UDF其他信息 udfData.Path = PUNCH_UDF_PATH; udfData.Name = punchName; if (udfData.Name.empty()) { continue; } // 創建UDF ProGroup udf; status = CreateUdf(udfData, &udf); if (status != PRO_TK_NO_ERROR) { AfxMessageBox("失敗"); continue; } } return PRO_TK_NO_ERROR; } // // 獲取沖頭對應的序列號 // ProError CPunchCreation::GetPunchSerialNo(string &serialNo) { // 考慮一個專案中不同的檔案會用相同的PUNCH // 考慮TEAMWORK的方面 serialNo = ""; // 暫時從文本文件中獲取序列號 // 獲取當前路徑 ProError status; ProPath currentPathW; char currentPathS[PRO_PATH_SIZE]; status = ProDirectoryCurrentGet(currentPathW); ProWstringToString(currentPathS, currentPathW); string punchSerialNoFile; punchSerialNoFile = currentPathS; punchSerialNoFile += "//"; punchSerialNoFile += PROJECT_NAME; // 讀取文本 vector fileContent; ifstream infile(punchSerialNoFile.c_str()); if (infile) { string textline; char temp[PRO_PATH_SIZE]; while ( getline( infile, textline, '/n' )) { int findPos = textline.find(_punchName + ":"); if (findPos != string::npos) { textline.erase(textline.begin(), textline.begin() + textline.find(':') + 1); serialNo = itoa(atoi(textline.c_str()) + 1, temp, 10); textline = _punchName + ":" + serialNo; } fileContent.push_back(textline); } infile.close(); } // 寫回文本 ofstream outfile(punchSerialNoFile.c_str()); if (!outfile) { serialNo = "1"; return PRO_TK_USER_ABORT; } for (int i = 0; i < fileContent.size(); ++i) { outfile << fileContent[i] << endl; } if (serialNo.empty()) { serialNo = "1"; outfile << _punchName + ":" + serialNo << endl; } outfile.close(); return PRO_TK_NO_ERROR; } // // 對沖頭進行重命名 // ProError CPunchCreation::RenamePunch() { ProError status; ProMdl* allParts; int partNum = 0; status = ProArrayAlloc(0, sizeof(ProMdl), 1, (ProArray*)&allParts); status = ProArrayObjectAdd((ProArray*)&allParts, -1, 1, &_punchMdl); status = ProSolidFeatVisit((ProSolid)_punchMdl, GetAllFeatParts, NULL, (ProAppData)&allParts); status = ProArraySizeGet((ProArray)allParts, &partNum); if (status != PRO_TK_NO_ERROR || partNum <= 0) { return PRO_TK_BAD_INPUTS; } for (int i = 0; i < partNum; ++i) { ProName punchOldNameW; char punchOldNameS[PRO_NAME_SIZE]; ProName punchNewNameW; char punchNewNameS[PRO_NAME_SIZE]; status = ProMdlNameGet(allParts[i], punchOldNameW); if (status != PRO_TK_NO_ERROR) { continue; } ProWstringToString(punchOldNameS, punchOldNameW); string punchSerialNo; GetPunchSerialNo(punchSerialNo); sprintf(punchNewNameS, "%s_%s_%s", PROJECT_NAME, punchOldNameS, punchSerialNo.c_str()); ProStringToWstring(punchNewNameW, punchNewNameS); if (strlen(punchNewNameS) > 31) { AfxMessageBox("PUNCH的新名字太長,無法重命名!", MB_OK|MB_ICONERROR); continue; } status = ProMdlRename(allParts[i], punchNewNameW); if (status != PRO_TK_NO_ERROR) { continue; } } return PRO_TK_NO_ERROR; } // // 保存檔案 // ProError CPunchCreation::SavePunch() { ProError status; ProMdl currentMdl; status = ProMdlCurrentGet(&currentMdl); if (status != PRO_TK_NO_ERROR) { return status; } status = ProMdlSave(currentMdl); status = ProMdlSave(_punchMdl); // 由於內存中的UDF會記錄參考的哪個檔案,所以需要清除內存 // 重新加載UDF status = ProMdlEraseNotDisplayed(); return PRO_TK_NO_ERROR; } //===================================================================================== // // PunchCreation.h: interface for the CPunchCreation class. // // #if !defined(AFX_PUNCHCREATION_H__A7BCD950_FE34_4EB2_9E3B_A4C1CADA4CB5__INCLUDED_) #define AFX_PUNCHCREATION_H__A7BCD950_FE34_4EB2_9E3B_A4C1CADA4CB5__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include #include #include #include #include // 屏蔽STL中嵌套STL導致聲明過長的警告 // #pragma warning(disable:4786)必須先於庫文件的包含之前 #pragma warning(disable:4786) #include #include #include using namespace std; struct PunchDim { string Name; double Value; PunchDim(const string &name, const double value) { Name = name; Value = value; } }; class CPunchCreation { public: CPunchCreation(); virtual ~CPunchCreation(); public: ProError CreatePunch(const string &punchName, vector &punchDims, ProSelection &punchOwnerSel, ProSelection *&pointSels, ProSelection &zOrientSel, ProSelection &xOrientSel); private: ProError DownloadPunchFromLibrary(); ProError CreatePunchCsys(); ProError InsertPunchUdf(); ProError RenamePunch(); ProError SavePunch(); ProError GetPunchSerialNo(string &serialNo); private: ProMdl _punchMdl; ProSelection *_punchCsyses; ProSelection _punchOwnerSel; ProSelection *_pointSels; ProSelection _zOrientSel; ProSelection _xOrientSel; string _punchName; vector _punchDims; }; #endif // !defined(AFX_PUNCHCREATION_H__A7BCD950_FE34_4EB2_9E3B_A4C1CADA4CB5__INCLUDED_)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值