编辑并打印部件族信息

UFUN在获取部件族信息时,不太方便通过行和列名获取某个属性的值,经过研究,发现必须通过UF_FAM_ask_attribute_data变相获取,经过测试,封装了两个函数UF_FAM_print_table和UF_FAM_edit_value,分别用来打印部件族信息和编辑属性值,代码如下:

bool UF_FAM_edit_value(tag_t part, int row, const char* title, const char* value)
{
    if ((NULL_TAG == part) || (NULL == title) || (0 > row))
    {
        return false;
    }

    int family_count;
    tag_p_t families = NULL_TAG;
    UF_PART_ask_families(part, &family_count, &families);

    for (int i = 0; i < family_count; ++i)
    {
        tag_t family = *(families+i);
        UF_FAM_family_data_t family_data = {0};
        UF_FAM_ask_family_data(family, &family_data);

        int attribute_index = -1;
        for (int j = 0; j < family_data.attribute_count; j++)
        {
            tag_t attr = *(family_data.attributes + j);
            UF_FAM_attribute_data_t data = {0};

            UF_FAM_ask_attribute_data(attr, &data);
            if (0 == strcmp(data.name, title))
            {
                UF_FAM_free_attribute_data(&data);
                attribute_index = j;
                break;
            }
            UF_FAM_free_attribute_data(&data);
        }

        if (row < family_data.member_count)
        {
            UF_FAM_member_data_t member_data = {0};
            UF_FAM_ask_member_row_data(family, row, &member_data);

            if (attribute_index >= 0)
            {
                UF_free(*(member_data.values + attribute_index));
                int error_code;
                *(member_data.values + attribute_index) = (char*)UF_allocate_memory(((unsigned int)strlen(value) + 1) *sizeof(char), &error_code);
                strcpy(*(member_data.values + attribute_index), value);
                UF_FAM_edit_member(family, row, &member_data);
                UF_FAM_free_member_data(&member_data);
            }
        }

        UF_FAM_free_family_data(&family_data);
    }
    
    UF_MODL_update();
    return true;
}


void UF_FAM_print_table(tag_t part)
{
    if (NULL_TAG == part)
    {
        return ;
    }

    int count;
    tag_p_t familiesTag = NULL_TAG;
    UF_PART_ask_families(part, &count, &familiesTag);
    UF_FAM_family_data_t data = {0};

    for (int i = 0; i < count; ++i)
    {
        tag_t familyTag = *(familiesTag+i);
        UF_FAM_ask_family_data(familyTag, &data);
        for (int j = 0; j < data.member_count; j++)
        {
            UF_FAM_member_data_t member_data = {0};
            UF_FAM_ask_member_row_data(familyTag, j, &member_data);

            for (int k = 0; k < data.attribute_count; k++)
            {
                tag_t attrTag = *(data.attributes + k);
                UF_FAM_attribute_data_t attrValue = {0};
                UF_FAM_ask_attribute_data(attrTag, &attrValue);
                printf("%s:%s\t",attrValue.name, *(member_data.values + k));
                
                UF_FAM_free_attribute_data(&attrValue);
            }

            putchar('\n');
            UF_FAM_free_member_data(&member_data);
        }

        UF_FAM_free_family_data(&data);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    /* Initialize the API environment */
    if( UF_CALL(UF_initialize()) ) 
    {
        /* Failed to initialize */
        return 0;
    }
    UF_PART_load_status_t status = {0};
    tag_t prt = NULL_TAG;
    UF_PART_open("C:\\TEMP\\gb.prt", &prt, &status);
    UF_FAM_print_table(prt);
    UF_FAM_edit_value(prt, 3, "备注", "abcd");
    cout << "***********************" << endl;
    UF_FAM_print_table(prt);
    
    /* Terminate the API environment */
    UF_CALL(UF_terminate());
    return 0;
}

 

测试结果:

成功将第三行,“备注”属性的值改过来了

 

posted @ 2020-09-23 16:25  快雪  阅读(326)  评论(0编辑  收藏  举报