NX二次开发:字体的调用

一、概述

        刻字功能时大家常常用到的功能,如何自动识别字体库中的字体类型呢?这里用到两种方法,但是有点遗憾,读取系统字体时不知为何明明是汉字,到最后显示的是汉字,不过万幸的是NX可以识别汉字进行创建文字

textBuilder1->SelectFont("黑体", NXOpen::Features::TextBuilder::ScriptOptionsWestern);

可以将常用的汉字字体复制到nx的字体目录,将文件名重新命名为中文,或者做一个中英文对照表,用户看到的是中文,系统调用的是英文。

1、读取指定目录下的文件(mode:1:全路径,2:单独文件名,3:去除后缀的文件名,type:"*"全部类型)

C++方式读取

//头文件
#include <io.h>

int GetFolderAllFileNames(string path, vector<string> &vecNameStrs, string type, int mode)
{
	//if ((char*)path[path.size()-1] != "\\")  path=path+"\\";
	if (path.at(path.length() - 1) != '\\')
		path += '\\';

	//文件句柄  
	long   hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = (long)_findfirst(p.assign(path).append(type.c_str()).c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之  
			//如果不是,加入列表  
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					GetFolderAllFileNames(p.assign(path).append("\\").append(fileinfo.name), vecNameStrs, type, mode);
			}
			else
			{
				if (mode == 1) //1-单独文件名
				{
					vecNameStrs.push_back(fileinfo.name);
				}
				else if (mode == 2) //2-去除后缀的名字
				{
					char * fn = fileinfo.name;
					int i = 0;
					i = (int)strlen(fn);
					char* fn1 = fn + i;
					while (fn1>fn) {
						if (*fn1 == '.')
						{
							*fn1 = 0;
							break;
						}
						fn1--;
					}
					vecNameStrs.push_back(fn);
				}
				else
				{
					vecNameStrs.push_back(p.assign(path).append(fileinfo.name)); //0-取全路径名
				}

			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
	return 0;
}

NX方式读取

#include<vector>
#include <uf_cfi.h>

//获取目录所有的文件名( type为文件的类型例如:*.ttf,注意后缀前加*;mode:1:全路径,2:单独文件名,3:去除后缀的文件名)
vector<string> GetFolderAllFileNames(string Filepath, const char *type,int mode)
{
	vector<string> ReadFontsName;
	int Exists = uc4560(Filepath.c_str(), 100);
	if (Exists!=0)
	{
		uc1601("温馨提示:指定的目录不存在", 1);
	}
	else
	{

		uc4508(Filepath.c_str(), (1 << 11), 0, type);
		int Readfonts = uc4518();
		if (Readfonts != 0)
		{
			uc1601("温馨提示:指定的目录不存在字体文件", 1);

		}
		else
		{
			//遍历当前文件夹下的.prt文件
			char thefile[MAX_FSPEC_BUFSIZE];

			while (Readfonts == 0)
			{
				uc4519(thefile);
				string strfilePath = thefile;
				//分割字符串
				if (mode == 1)
				{
					ReadFontsName.push_back(strfilePath.substr((strfilePath.find_last_of("\\")) + 1));
				}
				else if(mode == 2)
				{
					string tName = strfilePath.substr((strfilePath.find_last_of("\\")) + 1);
					char * fn = (char*)tName.c_str();
					int i = 0;
					i = (int)strlen(fn);
					char* fn1 = fn + i;
					while (fn1>fn) {
						if (*fn1 == '.')
						{
							*fn1 = 0;
							break;
						}
						fn1--;
					}
					ReadFontsName.push_back(string(fn));
				}
				else if(mode == 0)
				{
					ReadFontsName.push_back(thefile);
				}
				Readfonts = uc4518();
			}
		}
	}
	return ReadFontsName;
}

读取NX字体文件和计算机系统文件的字体:

主函数:

void MyClass::do_it()
{

	// TODO: add your code here
	string SystemPath = "C:\\Windows\\Fonts";//系统字体库
	string UGIIfontsPath = "D:\\ug\\UGII\\fonts";//NX字体库
	string UGIIugfontsPath = "D:\\ug\\UGII\\ugfonts";//NX字体库

    //C++f方式读取
	vector<string>vec1, vec2, vec3, FontsName;
	GetFolderAllFileNames(SystemPath, vec1, "*", 2);
	GetFolderAllFileNames(UGIIfontsPath, vec2, "*", 2);
	GetFolderAllFileNames(UGIIugfontsPath, vec3, "*", 2);
	FontsName.insert(FontsName.end(), vec1.begin(), vec1.end());
	FontsName.insert(FontsName.end(), vec2.begin(), vec2.end());
	FontsName.insert(FontsName.end(), vec3.begin(), vec3.end());
	for (int i = 0; i < (int)FontsName.size(); i++)
	{
		print(FontsName[i]);
	}

    //NX方式读取
    vector<string>vec1, vec2, vec3, FontsName;
	vec1 = GetFolderAllFileNames(SystemPath, "*.ttf",2);
	vec2 = GetFolderAllFileNames(UGIIfontsPath, "*.ttf",2);
	vec3 = GetFolderAllFileNames(UGIIugfontsPath, "*.fnx",2);

	FontsName.insert(FontsName.end(), vec1.begin(), vec1.end());
	FontsName.insert(FontsName.end(), vec2.begin(), vec2.end());
	FontsName.insert(FontsName.end(), vec3.begin(), vec3.end());

	for (int i = 0; i < (int)FontsName.size(); i++)
	{
		print(FontsName[i]);
	}

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白雪公主的后妈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值