首页 > 分享 > NX二次开发:字体的调用

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 (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)

{

vecNameStrs.push_back(fileinfo.name);

}

else if (mode == 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));

}

}

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

_findclose(hFile);

}

return 0;

}

NX方式读取

#include<vector>

#include <uf_cfi.h>

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

{

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()

{

string SystemPath = "C:\Windows\Fonts";

string UGIIfontsPath = "D:\ug\UGII\fonts";

string UGIIugfontsPath = "D:\ug\UGII\ugfonts";

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]);

}

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]);

}

}

相关知识

[Python学习]调用X
5320的花字体怎么弄?
田横岛二次开发全面启动 花卉艺术节精彩呈现
【花案图形字体和CAD字体库大全哪个好用】花案图形字体和CAD字体库大全对比-ZOL下载
网页字体设置:默认字体、font
男子注册商标遭遇“二次开发”潜规则
将字体添加为 XML 资源
使用Python调用mysql
一次性设定Java程序所有组件字体
后期处理:让花儿更红、草地更绿——NX“彩色控制点”的运用

网址: NX二次开发:字体的调用 https://m.huajiangbk.com/newsview742083.html

所属分类:花卉
上一篇: 【嵌入式】利用freetype字
下一篇: Android进阶之路