admin 发表于 2018-1-10 17:48:41

NX二次开发源码分享: 获取当前Reference Component的通用方法



NX二次开发源码分享: 获取当前Reference Component的通用方法





static logical attribute_exists(Component cmpnt, char *title)
{

//The traditional method of asking for all of the components recursively//does not work if you are looking for reference-only components,//because they are not retured when you ask for the children.////Also, asking for the AssembliesGeneralPropertiesBuilder.ReferenceComponent //Propertydoes not work in versions earlier than NX12.////The workaround is to cycle for components the old-fashioned way,//and then ask whether each one has the REFERENCE_COMPONENT attribute.////If you have multiple occurrences of a given component in the assembly,//each one will be reported.
   
    logical
      hasRefAttr = FALSE;

    hasRefAttr = cmpnt.HasUserAttribute(RO, NXObject::AttributeTypeAny, -1);

    if(hasRefAttr == TRUE)
    {
      return TRUE;
    }

    return FALSE;

}

static tag_t ask_next_of_type(tag_t part, int type, tag_t object)
{
    UF_CALL(UF_OBJ_cycle_objs_in_part(part, type, &object));
    return (object);
}

static void do_it(void)
{
    // Assumes that the display part is the top of the assembly

    tag_t
      dispPart = UF_PART_ask_display_part();

    if(NULL_TAG == dispPart)
    {
      ECHO("Program requires an active displayed part");
      return;
    }

    tag_t
      compTag = NULL_TAG;

    while((compTag = ask_next_of_type(dispPart, UF_component_type, compTag)) != NULL_TAG)
    {
      char
            msg = {""},
            fSpec = {""};

            tag_t
                protoPart = UF_ASSEM_ask_prototype_of_occ(compTag);

            UF_CALL(UF_PART_ask_part_name(protoPart, fSpec));

            // uncomment to see all component names:
            //sprintf(msg, "Component Part: %s\n", fSpec);
            //ECHO(msg);

            Component *theComponent = (Component *)NXObjectManager::Get(compTag);
            if (attribute_exists(*theComponent, RO))
            {
                sprintf(msg, "**** Reference-Only Component: %s\n", fSpec);
                ECHO(msg);
            }
    }
}
页: [1]
查看完整版本: NX二次开发源码分享: 获取当前Reference Component的通用方法