UG NX 二次开发 插入UDF

注: 此例子源于UGOpen帮助文档

#include <stdio.h>

#include <string.h>

#include <uf.h>

#include <uf_ui.h>

#include <uf_object_types.h>

#include <uf_part.h>

#include <uf_modl.h>

#include <uf_disp.h>

#include <uf_obj.h>

#include <uf_assem.h>

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)

{

	if (irc)
    {
        char
            err[133],
            messg[300],
			*msg = messg;
		int response = 0;


        UF_get_fail_message(irc, err);
        sprintf(messg, "\n%s\nerror %d at line %d in %s\n%s",
            err, irc, line, file, call);


		 /* Display the error Dialog if allowed */
		if( UF_UI_lock_ug_access( UF_UI_FROM_CUSTOM ) == UF_UI_LOCK_SET )
		  {
		  UF_UI_message_dialog( "", UF_UI_MESSAGE_ERROR, &msg, 1, TRUE, NULL, &response );
		  UF_UI_unlock_ug_access(UF_UI_FROM_CUSTOM);
		  }

		/* Write the error to Syslog, always. */
		UF_print_syslog( messg, FALSE );
		UF_print_syslog( "\n", FALSE );
	}
	return irc;

}

static void report_load_status(UF_PART_load_status_t *status)
{
    char
        msg[133];
    int
        ii;

    for (ii=0; ii<status->n_parts; ii++)
    {
        UF_get_fail_message(status->statuses[ii], msg);
        printf("    %s - %s\n", status->file_names[ii], msg);
    }

    if (status->n_parts > 0)
    {
        UF_free(status->statuses);
        UF_free_string_array(status->n_parts, status->file_names);
    }
}

static int mask_for_planar_face(UF_UI_selection_p_t select, void *type)
{
    UF_UI_mask_t
        mask = { UF_solid_type, 0, UF_UI_SEL_FEATURE_PLANAR_FACE };

    if (!UF_CALL(UF_UI_set_sel_mask(select,
            UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, 1, &mask)))
        return (UF_UI_SEL_SUCCESS);
    else
        return (UF_UI_SEL_FAILURE);
}

static tag_t select_planar_face(char *prompt)
{
    int
        resp;
    double
        cp[3];
    tag_t
        object,
        view;

    UF_CALL(UF_UI_select_with_single_dialog(prompt, "",
        UF_UI_SEL_SCOPE_WORK_PART,
        mask_for_planar_face, NULL, &resp, &object, cp, &view));

    if (resp == UF_UI_OBJECT_SELECTED || resp == UF_UI_OBJECT_SELECTED_BY_NAME)
    {
        UF_CALL(UF_DISP_set_highlight(object, FALSE));
        return object;
    }

    return NULL_TAG;

}

static void do_it(void)
{
    int
        error,
        ii,
        num_parents,
        num_expression;
    tag_t
        *expression = NULL,
        feature = NULL_TAG,
        *new_parents = NULL,
        new_udf_feature,
        *parents = NULL,
        part,
        prev_work_part;
    char
        *exp_rhs[2] = {"2", "3"},
        **expression_prompt = NULL,
        **parents_prompt = NULL,
        **new_exp_rhs;
    UF_PART_load_status_t
        error_status;

    UF_CALL(UF_PART_open_quiet("boss.prt", &part, &error_status));
    if (error_status.failed == TRUE) report_load_status(&error_status);

    UF_CALL(UF_ASSEM_set_work_part_quietly(part, &prev_work_part));

    UF_CALL(UF_OBJ_cycle_by_name("Boss", &feature));

    UF_CALL(UF_MODL_ask_udf_definition(feature, &parents, &parents_prompt,
        &num_parents, &expression, &expression_prompt, &num_expression));

    /* This array will be free'd by the UF_MODL_create_instantiated_udf()
       so it must be allocated this way - See PR 4074614  */

    new_exp_rhs = (char ** )(UF_allocate_memory(sizeof(char *) * num_expression, &error));
    for (ii = 0; ii < num_expression; ii++)
    {
        new_exp_rhs[ii] = (char *)(UF_allocate_memory(sizeof(char) * 10, &error));
        strcpy(new_exp_rhs[ii], exp_rhs[ii]);
    }

    UF_CALL(UF_ASSEM_set_work_part_quietly(prev_work_part, &prev_work_part));

    new_parents = (tag_t *)(UF_allocate_memory(sizeof(tag_t) * num_parents, &error));
    for (ii = 0; ii < num_parents; ii++)
        new_parents[ii] = select_planar_face(parents_prompt[ii]);

    UF_CALL(UF_MODL_register_rpo_routine(UF_MODL_default_rpo_menu));

    UF_CALL(UF_MODL_create_instantiated_udf(feature, "boss.cgm", parents,
        new_parents, num_parents, expression, new_exp_rhs, num_expression,
        &new_udf_feature));

    UF_free(parents);
    UF_free(new_parents);
    UF_free_string_array(num_parents, parents_prompt) ;
    UF_free(expression);
    UF_free_string_array(num_expression, expression_prompt);

}

void ufusr(char *param, int *retcode, int paramLen)
{
    if (UF_CALL(UF_initialize())) return;
    do_it();
    UF_terminate();
}

int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

 

专注MCAD开发

QQ: prodevelop@qq.com

www.mcadex.com

posted @ 2015-10-10 14:51  MCADEx  阅读(2171)  评论(0编辑  收藏  举报