NX二次开发 获取组件实体

1.获取装配现在组件上的所有实体:

NXObject* ToNXObj(tag_t obj)
{
    if (obj == NULL_TAG)
    {
        return NULL;
    }
    return dynamic_cast(ToTagObj(obj));
}



//单个装配组件下所有的实体
vector GetComponentBodies(Assemblies::Component* component)
{    
    vector bodies;

    tag_t compTag = component->Tag();
    tag_t member = NULL_TAG;
    UF_ASSEM_cycle_objs_in_comp(compTag, &member);

    while (member != NULL_TAG)
    {
        int type = 0, subType = 0;
        UF_OBJ_ask_type_and_subtype(member, &type, &subType);
        if (type == 70 && subType == 0)
        {
            bodies.push_back(member);
        }
        UF_ASSEM_cycle_objs_in_comp(compTag, &member);
    }

    return bodies;

}

vector GetAssemBodies()
{
    vector bodies;

    Part* workPart = Session::GetSession()->Parts()->Work();
    tag_t workTag = workPart->Tag();
    tag_t root = UF_ASSEM_ask_root_part_occ(workTag);

    tag_t* child_part_occs;
    int part_num = UF_ASSEM_ask_part_occ_children(root, &child_part_occs);
    for (int i = 0; i < part_num; i++)
    {
        Assemblies::Component* component = dynamic_cast(ToNXObj(child_part_occs[i]));
        vectortmpBodies = GetComponentBodies(component);

        for (auto item : tmpBodies)
        {
            bodies.push_back(item);
        }
    }

    return bodies;
}

 

  2.获取装配原先零件上的所有实体:

vector GetComponentOriginPartBodies()
{
    vector allBodies;

    Part* workPart = Session::GetSession()->Parts()->Work();
    tag_t workTag = workPart->Tag();

    tag_t root = UF_ASSEM_ask_root_part_occ(workTag);
    tag_t *child_components = NULL;
    int component_count = UF_ASSEM_ask_part_occ_children(root, &child_components);
    for (int i = 0; i < component_count; i++)
    {
        tag_t prototype = UF_ASSEM_ask_prototype_of_occ(child_components[i]);
        tag_t objTag = NULL_TAG;
        while (true)
        {
            UF_OBJ_cycle_objs_in_part(prototype, UF_solid_type, &objTag);
            if (objTag == NULL_TAG)
            {
                break;
            }
            else
            {
                int type = 0, subType = 0;
                UF_OBJ_ask_type_and_subtype(objTag, &type, &subType);
                if (type == 70 && subType == 0)
                {
                    allBodies.push_back(objTag);
                }

            }
        }
    }

    return allBodies;
}

 

 

 

你可能感兴趣的:(NX)