NX 二次开发 插件的自动化部署

自己做好的插件,放到用户机上
如果还手动让用户在custom_dirs.dat文件后面追加目录,就有点不专业了
下面介绍如何自动部署插件

1、读取客户机安装的NX软件版本及其目录

NX会在如下注册表位置写入安装目录
SOFTWARE\Unigraphics Solutions\Installed Applications
在这里插入图片描述
可以看到键值与版本对应关系

2、自动将本目录追加写入custom_dirs.dat

注意要先用管理权限改变此文件,使其可读写,否则会写入失败

总的示例代码放下面了

	// IntallAdd.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//


#include "GetRegValue.h"


using namespace std;
typedef std::vector<std::string>  StringList;

string GetExePath()
{
        char szFilePath[MAX_PATH + 1] = { 0 };
        GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
        (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串//
        (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串//
        string path = szFilePath;
        return path;
}
StringList splitstr(const std::string& str, const std::string& pattern)
{
    StringList  li;
    std::string subStr;
    std::string tPattern;
    size_t      patternLen = pattern.length();
    size_t      strLen = str.length();

    for (size_t i = 0; i < str.length(); i++)
    {
        if (pattern[0] == str[i] && ((strLen - i) >= patternLen))
        {
            if (memcmp(&pattern[0], &str[i], patternLen) == 0)
            {
                i += patternLen - 1;
                if (!subStr.empty())
                {
                    li.push_back(subStr);
                    subStr.clear();
                }
            }
            else
            {
                subStr.push_back(str[i]);
            }
        }
        else
        {
            subStr.push_back(str[i]);
        }
    }

    if (!subStr.empty())
    {
        li.push_back(subStr);
    }

    return li;
}
void WriterInstallPath(string strValue) {

 
    if (strValue.empty())
    {
        std::cout << "未检测到NX安装位置,自动挂载停止\n软件安装将继续完成" << std::endl;
        std::cout << "\n输入任意键关闭窗口" << std::endl;
        std::cin.get();
        return;
    }

    StringList strlist = splitstr(strValue, "\\");
    string NXrootDirectory = "";
    for (size_t i = 0; i < strlist.size() - 2; i++)
    {
        if (i != 0)
        {
            NXrootDirectory = NXrootDirectory + "\\";
        }
        NXrootDirectory = NXrootDirectory + strlist[i];
    }
 
    string Installpath = GetExePath();
 
    string custom_dirsPath = NXrootDirectory + "\\UGII\\menus\\custom_dirs.dat";
 
    //文件属性设为一般
    SetFileAttributesA(custom_dirsPath.c_str(), FILE_ATTRIBUTE_NORMAL);
    fstream f;
    //追加写入,在原来基础上加了ios::app 
    f.open(custom_dirsPath, ios::out | ios::app);
    //输入你想写入的内容 
    if (!f.is_open())
    {
        f.close();
       

        std::cout << "尝试自动配置插件失败!请将下面文件只读属性去除" << std::endl;
        std::cout << custom_dirsPath << std::endl;
        std::cout << "" << std::endl;
        std::cin.get();
    }
    else
    {
        f << Installpath << endl;
    }

    f.close();


}

int main()
{
 

	map<string, string> NxName;
	NxName["1926"] = "Unigraphics V1926";
	NxName["1953"] = "Unigraphics V1953";
    NxName["1980"] = "Unigraphics V1980";
    NxName["12"] = "Unigraphics V30.0";


    //计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Unigraphics Solutions\Installed Applications

    std::string strValue01 = GetRegValue(2, "SOFTWARE\\Unigraphics Solutions\\Installed Applications", NxName["1980"]);

    WriterInstallPath(strValue01);

    std::string strValue02 = GetRegValue(2, "SOFTWARE\\Unigraphics Solutions\\Installed Applications", NxName["1926"]);

    WriterInstallPath(strValue02);
 
}


#include <Windows.h>
#include "GetRegValue.h"
#include <fstream>

//可移植版本 wstring => string
std::string ws2s(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, "");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = wcstombs(NULL, _Source, 0) + 1;
    char* _Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

//可移植版本 string => wstring
std::wstring s2ws(const std::string& s)
{
    std::string curLocale = setlocale(LC_ALL, "");
    const char* _Source = s.c_str();
    size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
    wchar_t* _Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest, _Source, _Dsize);
    std::wstring result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

std::string GetRegValue(int nKeyType, const std::string& strUrl, const std::string& strKey)
{
    std::string strValue("");
    HKEY hKey = NULL;
    HKEY  hKeyResult = NULL;
    DWORD dwSize = 0;
    DWORD dwDataType = 0;
    std::wstring wstrUrl = s2ws(strUrl);
    std::wstring wstrKey = s2ws(strKey);

    switch (nKeyType)
    {
    case 0:
    {
        hKey = HKEY_CLASSES_ROOT;
        break;
    }
    case 1:
    {
        hKey = HKEY_CURRENT_USER;
        break;
    }
    case 2:
    {
        hKey = HKEY_LOCAL_MACHINE;
        break;
    }
    case 3:
    {
        hKey = HKEY_USERS;
        break;
    }
    case 4:
    {
        hKey = HKEY_PERFORMANCE_DATA;
        break;
    }
    case 5:
    {
        hKey = HKEY_CURRENT_CONFIG;
        break;
    }
    case 6:
    {
        hKey = HKEY_DYN_DATA;
        break;
    }
    case 7:
    {
        hKey = HKEY_CURRENT_USER_LOCAL_SETTINGS;
        break;
    }
    case 8:
    {
        hKey = HKEY_PERFORMANCE_TEXT;
        break;
    }
    case 9:
    {
        hKey = HKEY_PERFORMANCE_NLSTEXT;
        break;
    }
    default:
    {
        return strValue;
    }
    }

    //打开注册表
    if (ERROR_SUCCESS == ::RegOpenKeyEx(hKey, wstrUrl.c_str(), 0, KEY_QUERY_VALUE||KEY_WOW64_64KEY, &hKeyResult))
    {
        // 获取缓存的长度dwSize及类型dwDataType
        ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, NULL, &dwSize);
        switch (dwDataType)
        {
        case REG_MULTI_SZ:
        {
            //分配内存大小
            BYTE* lpValue = new BYTE[dwSize];
            //获取注册表中指定的键所对应的值
            LONG lRet = ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, lpValue, &dwSize);
            delete[] lpValue;
            break;
        }
        case REG_SZ:
        {
            //分配内存大小
            wchar_t* lpValue = new wchar_t[dwSize];
            memset(lpValue, 0, dwSize * sizeof(wchar_t));
            //获取注册表中指定的键所对应的值
            if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize))
            {
                std::wstring wstrValue(lpValue);
                strValue = ws2s(wstrValue);
            }
            delete[] lpValue;
            break;
        }
        default:
            break;
        }
    }

    //关闭注册表
    ::RegCloseKey(hKeyResult);


    return strValue;
}

编译成exe后,双击执行就安装上了

END

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nx 二次开发自动装配是指通过对nx平台进行定制和开发,实现自动化装配的功能。nx平台是一种用于设计和工程的综合解决方案,能够帮助用户进行产品设计、工程分析和制造过程的管理。通过二次开发,可以将nx平台与其他软件和系统进行集成,提升其自动化装配的能力。 通过nx二次开发自动装配,用户可以实现以下功能: 1. 自动装配流程优化:通过对nx平台进行二次开发,可以定制自动装配流程,优化产品装配的效率和质量。例如,可以实现零件自动对齐、自动配对等功能,减少手动操作的工作量。 2. 工艺规划优化:通过二次开发,可以将nx平台与工艺规划系统进行集成,实现自动化装配工艺的优化。例如,根据产品设计要求和工艺要求,自动选择最佳的装配工艺方案,并进行自动化的工艺规划。 3. 数据集成与共享:通过二次开发,可以实现nx平台与企业其他系统的数据集成与共享,实现自动化装配数据的自动传递和更新。例如,可以将产品设计数据、零部件库存数据等自动传递给装配工艺规划系统,实现数据的自动更新和共享。 综上所述,nx 二次开发自动装配可以通过定制和集成的方式,实现自动化装配流程的优化和工艺规划的优化,实现数据的集成与共享,提升装配效率和质量。这对于提高企业的生产效率和竞争力具有重要意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值