找回密码
 注册
Simdroid-非首页
查看: 85|回复: 3

[二次开发] wave link all bodies in new component

[复制链接]
发表于 2009-10-16 23:39:36 | 显示全部楼层 |阅读模式 来自 河北廊坊
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_object_types.h>
#include <uf_obj.h>
#include <uf_disp.h>
#include <uf_wave.h>
#include <uf_so.h>
#include <uf_assem.h>
#include <uf_csys.h>
#include <uf_part.h>
#include <uf_modl.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],
             msg[133];

        sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
            irc, line, file);
        UF_get_fail_message(irc, err);

    /*  NOTE:  UF_print_syslog is new in V18 */

        UF_print_syslog(msg, FALSE);
        UF_print_syslog(err, FALSE);
        UF_print_syslog("\n", FALSE);
        UF_print_syslog(call, FALSE);
        UF_print_syslog(";\n", FALSE);

        if (!UF_UI_open_listing_window())
        {
            UF_UI_write_listing_window(msg);
            UF_UI_write_listing_window(err);
            UF_UI_write_listing_window("\n");
            UF_UI_write_listing_window(call);
            UF_UI_write_listing_window(";\n");
        }
    }

    return(irc);
}

static void ask_wcs_info(double origin[3], double axes[9])
{
    tag_t
        wcs,
        wcs_mx;

    UF_CALL(UF_CSYS_ask_wcs(&wcs));
    UF_CALL(UF_CSYS_ask_csys_info(wcs, &wcs_mx, origin));
    UF_CALL(UF_CSYS_ask_matrix_values(wcs_mx, axes));
}

static logical pick_one_of_two_choices(char *prompt, char *option_one,
    char *option_two, int *choice)
{
    int
        resp;
    char
        options[2][38];

    strncpy(&options[0][0], option_one, 37);
    strncpy(&options[1][0], option_two, 37);
    options[0][37] = '\0';
    options[1][37] = '\0';

    resp = uc1603(prompt, *choice, options, 2);
    if ((resp > 4) && (resp < 19))
    {
        *choice = resp - 4;  /* returns 1 or 2 */
        return TRUE;
    }
    else return FALSE;
}

static void combine_directory_and_wildcard(char *dir, char *fltr, char *spec)
{
    if (!strcmp(dir, ""))
        strcpy(spec, fltr);
    else
    {
        UF_CALL(uc4575(dir, 0, "junk.xxx", spec));
        strcpy(strstr(spec, "junk.xxx"), fltr);
    }
}

static logical prompt_for_part_name(char *prompt, char *fspec)
{
    int
        resp;
    char
        filter[MAX_FSPEC_SIZE+1],
        *p_dir,
        *p_ext;

    UF_CALL(UF_UI_ask_dialog_directory(UF_UI_PART_DIR, &p_dir));
    UF_CALL(UF_UI_ask_dialog_filter(UF_UI_PART_OPEN_FLTR, &p_ext));

    combine_directory_and_wildcard(p_dir, p_ext, filter);

    UF_free(p_dir);
    UF_free(p_ext);

    if (!UF_CALL(UF_UI_create_filebox(prompt, "Part Name", filter, "", fspec,
        &resp)) && (resp != UF_UI_CANCEL)) return TRUE;
    else return FALSE;
}

static logical prompt_for_existing_part_name(char *fspec)
{
    logical
        nxman,
        unused;
    int
        mode = 1,
        resp;

    UF_CALL(UF_is_ugmanager_active(&nxman));

    if (nxman)
    {
        if (pick_one_of_two_choices("template part in?", "Native", "TcEng",
            &mode))
        {
            if (mode == 1)
                return (prompt_for_part_name("Select Native part", fspec));
            else
            {
                if (UF_CALL(UF_UI_ask_open_part_filename(fspec, &unused, &resp))
                    || (resp == UF_UI_CANCEL)) return FALSE;
                return TRUE;
            }
        }
        else return FALSE;
    }
    else
    {
        if (UF_CALL(UF_UI_ask_open_part_filename(fspec, &unused, &resp))
            || (resp == UF_UI_CANCEL)) return FALSE;
        return TRUE;
    }
}

static tag_t ask_next_body(tag_t part, tag_t body)
{
    int
        subtype,
        type;

    do
    {
        UF_CALL(UF_OBJ_cycle_objs_in_part(part, UF_solid_type, &body));
        if (body)
        {
            UF_CALL(UF_OBJ_ask_type_and_subtype(body, &type, &subtype));
            if (!subtype) return body;
        }
    } while (body);

    return NULL_TAG;
}

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 tag_t ask_part_occ_of_prom_occ(tag_t prom_object)
{
    tag_t
        owning_part,
        part_occ = prom_object,
        proto;

    proto = UF_ASSEM_ask_prototype_of_occ(prom_object);

    UF_CALL(UF_OBJ_ask_owning_part(proto, &owning_part));

    while (!UF_CALL(UF_ASSEM_ask_parent_component(part_occ, &part_occ))
        && (UF_ASSEM_ask_prototype_of_occ(part_occ) != owning_part));

    return part_occ;
}

static void create_wave_linked_body(tag_t body)
{
    tag_t
        feat,
        part_occ,
        proto,
        xform;

    if (UF_ASSEM_is_occurrence(body))
        proto = UF_ASSEM_ask_prototype_of_occ(body);
    else
        proto = body;

    if (UF_OBJ_is_object_a_promotion(proto))
        part_occ = ask_part_occ_of_prom_occ(body);
    else
        part_occ = UF_ASSEM_ask_part_occurrence(body);

    if (part_occ == NULL_TAG)
        UF_CALL(UF_WAVE_create_linked_body(proto,NULL_TAG,body,FALSE,&feat));
    else
    {
        UF_CALL(UF_SO_create_xform_assy_ctxt(body, part_occ, NULL_TAG, &xform));
        UF_CALL(UF_WAVE_create_linked_body(proto, xform, body, FALSE, &feat));
    }

    UF_CALL(UF_MODL_update());
}

static tag_t ask_top_instance(tag_t occurrence)
{
    tag_t
        instance,
        last;

    do
    {
        last = occurrence;
        occurrence = UF_ASSEM_ask_part_occurrence(occurrence);
        instance = UF_ASSEM_ask_inst_of_part_occ(occurrence);
    } while (instance != NULL_TAG);

    return UF_ASSEM_ask_inst_of_part_occ(last);
}

static void do_it(void)
{
    int
        resp;
    tag_t
        body = NULL_TAG,
        disp_part = UF_PART_ask_display_part(),
        instance,
        work_part = UF_ASSEM_ask_work_part();
    char
        filter[MAX_FSPEC_SIZE+1],
        fspec[MAX_FSPEC_SIZE+1];
    double
        dest_csys[9],
        dest_pnt[3];
    UF_PART_load_status_t
        error_status;

    ask_wcs_info(dest_pnt, dest_csys);

    if (work_part != disp_part)
        UF_CALL(UF_CSYS_map_point(UF_CSYS_ROOT_COORDS, dest_pnt,
        UF_CSYS_WORK_COORDS, dest_pnt));

    if (prompt_for_existing_part_name(fspec))
    {
        UF_CALL(UF_ASSEM_add_part_to_assembly(disp_part, fspec, NULL,
            NULL, dest_pnt, dest_csys, 0, &instance, &error_status));
        if (error_status.n_parts > 0) report_load_status(&error_status);
        if (!error_status.failed)
            while ((body = ask_next_body(disp_part, body)) != NULL_TAG)
                if (UF_ASSEM_is_occurrence(body))
                    if (ask_top_instance(body) == instance)
                        create_wave_linked_body(body);
    }
}

/*ARGSUSED*/
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);
}
 楼主| 发表于 2009-10-16 23:40:52 | 显示全部楼层 来自 河北廊坊
Simdroid开发平台
这个程序有一定的技术含量,不是程序本身,而是对UG相关概念的理解。
回复 不支持

使用道具 举报

 楼主| 发表于 2009-10-16 23:40:56 | 显示全部楼层 来自 河北廊坊
本帖最后由 lirui0726 于 2009-10-16 23:44 编辑

下面还有一个例子
回复 不支持

使用道具 举报

 楼主| 发表于 2009-10-16 23:43:57 | 显示全部楼层 来自 河北廊坊
place component aligning selected component csys to selected assembly csys


#include <stdio.h>
#include <string.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_undo.h>
#include <uf_part.h>
#include <uf_assem.h>
#include <uf_trns.h>
#include <uf_csys.h>
#include <uf_obj.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],
             msg[133];

        sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
            irc, line, file);
        UF_get_fail_message(irc, err);

    /*  NOTE:  UF_print_syslog is new in V18 */

        UF_print_syslog(msg, FALSE);
        UF_print_syslog(err, FALSE);
        UF_print_syslog("\n", FALSE);
        UF_print_syslog(call, FALSE);
        UF_print_syslog(";\n", FALSE);

        if (!UF_UI_open_listing_window())
        {
            UF_UI_write_listing_window(msg);
            UF_UI_write_listing_window(err);
            UF_UI_write_listing_window("\n");
            UF_UI_write_listing_window(call);
            UF_UI_write_listing_window(";\n");
        }
    }

    return(irc);
}

static void set_undo_mark(char *label)
{
    UF_UNDO_mark_id_t
        mark_id;

    UF_CALL(UF_UNDO_set_mark(UF_UNDO_visible, label, &mark_id));
}

static logical pick_one_of_two_choices(char *prompt, char *option_one,
    char *option_two, int *choice)
{
    int
        resp;
    char
        options[2][38];

    strncpy(&options[0][0], option_one, 37);
    strncpy(&options[1][0], option_two, 37);
    options[0][37] = '\0';
    options[1][37] = '\0';

    resp = uc1603(prompt, *choice, options, 2);
    if ((resp > 4) && (resp < 19))
    {
        *choice = resp - 4;  /* returns 1 or 2 */
        return TRUE;
    }
    else return FALSE;
}

static void combine_directory_and_wildcard(char *dir, char *fltr, char *spec)
{
    if (!strcmp(dir, ""))
        strcpy(spec, fltr);
    else
    {
        UF_CALL(uc4575(dir, 0, "junk.xxx", spec));
        strcpy(strstr(spec, "junk.xxx"), fltr);
    }
}

static logical prompt_for_part_name(char *prompt, char *fspec)
{
    int
        resp;
    char
        filter[MAX_FSPEC_SIZE+1],
        *p_dir,
        *p_ext;

    UF_CALL(UF_UI_ask_dialog_directory(UF_UI_PART_DIR, &p_dir));
    UF_CALL(UF_UI_ask_dialog_filter(UF_UI_PART_OPEN_FLTR, &p_ext));

    combine_directory_and_wildcard(p_dir, p_ext, filter);

    UF_free(p_dir);
    UF_free(p_ext);

    if (!UF_CALL(UF_UI_create_filebox(prompt, "Part Name", filter, "", fspec,
        &resp)) && (resp != UF_UI_CANCEL)) return TRUE;
    else return FALSE;
}

static logical prompt_for_existing_part_name(char *fspec)
{
    logical
        nxman,
        unused;
    int
        mode = 1,
        resp;

    UF_CALL(UF_is_ugmanager_active(&nxman));

    if (nxman)
    {
        if (pick_one_of_two_choices("template part in?", "Native", "TcEng",
            &mode))
        {
            if (mode == 1)
                return (prompt_for_part_name("Select Native part", fspec));
            else
            {
                if (UF_CALL(UF_UI_ask_open_part_filename(fspec, &unused, &resp))
                    || (resp == UF_UI_CANCEL)) return FALSE;
                return TRUE;
            }
        }
        else return FALSE;
    }
    else
    {
        if (UF_CALL(UF_UI_ask_open_part_filename(fspec, &unused, &resp))
            || (resp == UF_UI_CANCEL)) return FALSE;
        return TRUE;
    }
}

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

    if (status->n_parts > 0)
    {
        UF_UI_open_listing_window();
        UF_UI_write_listing_window("Load notes:\n");

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

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

static logical open_part(char *fspec, tag_t *part)
{
    UF_PART_load_status_t
        status;

    if (!UF_PART_is_loaded(fspec))
    {
        UF_CALL(UF_PART_open(fspec, part, &status));
        if (status.n_parts > 0) report_load_status(&status);
        return (!status.failed);
    }
    else
    {
        *part = UF_PART_ask_part_tag(fspec);
        if (!UF_CALL(UF_PART_set_display_part(*part)))
            return TRUE;
        else
            return FALSE;
    }
}

static tag_t specify_csys(char *title)
{
    int
        method = 4, // inferred !
        irc;
    tag_t
        csys = NULL_TAG;
    double
        matrix[9],
        origin[3];

    irc = UF_UI_specify_csys(title, &method, matrix, origin, &csys);
    return csys;
}

static tag_t create_duplicate_alive_normal_csys(tag_t csys)
{
    tag_t
        matrix,
        new_csys;
    double
        origin[3];

    UF_CALL(UF_CSYS_ask_csys_info(csys, &matrix, origin));
    UF_CALL(UF_CSYS_create_csys(origin, matrix, &new_csys));

    return new_csys;
}

static void ask_wcs_info(double origin[3], double axes[9])
{
    tag_t
        wcs,
        wcs_mx;

    UF_CALL(UF_CSYS_ask_wcs(&wcs));
    UF_CALL(UF_CSYS_ask_csys_info(wcs, &wcs_mx, origin));
    UF_CALL(UF_CSYS_ask_matrix_values(wcs_mx, axes));
}

static tag_t add_component(char *fspec)
{
    tag_t
        instance = NULL_TAG,
        part = UF_PART_ask_display_part();
    double
        origin[3],
        matrix[9];
    UF_PART_load_status_t
        error_status;

    ask_wcs_info(origin, matrix);

    UF_CALL(UF_ASSEM_add_part_to_assembly(part, fspec, NULL, NULL,
        origin, matrix, 0, &instance, &error_status));

    if (error_status.n_parts > 0) report_load_status(&error_status);

    return (UF_ASSEM_ask_part_occ_of_inst(UF_ASSEM_ask_root_part_occ(part),
        instance));

}

static tag_t create_component_csys(tag_t component)
{
    tag_t
        csys,
        matrix;
    double
        csys_matrix[9],
        origin[3],
        transform[4][4],
        zero[3] = { 0,0,0 };
    char
        instance_name[UF_CFI_MAX_FILE_NAME_SIZE],
        partname[UF_CFI_MAX_PATH_NAME_SIZE],
        refset_name[MAX_ENTITY_NAME_SIZE+1];

    UF_CALL(UF_ASSEM_ask_component_data(component, partname, refset_name,
        instance_name, origin, csys_matrix, transform));

    UF_CALL(UF_CSYS_create_matrix(csys_matrix, &matrix));
    UF_CALL(UF_CSYS_create_temp_csys(zero, matrix, &csys));

    return csys;
}

static void transform_csys2csys(tag_t ref_csys, tag_t dest_csys, double mx[16])
{
    double
        csys[9],
        orig[3],
        tx1[16],
        tx2[16],
        tx3[16],
        tx4[16],
        v[3];
    int
        ii;
    tag_t
        csys_mx;

    UF_CALL(UF_CSYS_ask_csys_info(ref_csys, &csys_mx, orig));
    UF_CALL(UF_CSYS_ask_matrix_values(csys_mx, csys));

    tx1[0] = csys[0];
    tx1[1] = csys[1];
    tx1[2] = csys[2];
    tx1[3] = 0;
    tx1[4] = csys[3];
    tx1[5] = csys[4];
    tx1[6] = csys[5];
    tx1[7] = 0;
    tx1[8] = csys[6];
    tx1[9] = csys[7];
    tx1[10] = csys[8];
    tx1[11] = 0;

    /* set up to translate from reference csys back to absolute */
    for (ii=0; ii<3; ii++) v[ii] = -orig[ii];
    FTN(uf5943)(v, tx2);

    /* combine this with the rotation matrix from the reference csys */
    FTN(uf5942)(tx2, tx1, tx3);

    UF_CALL(UF_CSYS_ask_csys_info(dest_csys, &csys_mx, orig));
    UF_CALL(UF_CSYS_ask_matrix_values(csys_mx, csys));

    /* Invert the rotation from the destination matrix */
    tx2[0] = csys[0];
    tx2[1] = csys[3];
    tx2[2] = csys[6];
    tx2[3] = 0;
    tx2[4] = csys[1];
    tx2[5] = csys[4];
    tx2[6] = csys[7];
    tx2[7] = 0;
    tx2[8] = csys[2];
    tx2[9] = csys[5];
    tx2[10] = csys[8];
    tx2[11] = 0;

    /* set up to translate from abs to the destination csys */
    FTN(uf5943)(orig, tx1);

    /* combine this with the inverted rotation csys above */
    FTN(uf5942)(tx2, tx1, tx4);

    /* and the one from the reference csys */
    FTN(uf5942)(tx3, tx4, mx);

}

static void transform_csys_to_csys(tag_t from_csys, tag_t to_csys, int n,
    tag_t *objects)
{
    int
        move = 1,
        work = -1,
        off = 2,
        resp;
    double
        mx[16];

    transform_csys2csys(from_csys, to_csys, mx);
    FTN(uf5947)(mx, objects, &n, &move, &work, &off, NULL, NULL, &resp);
}

static void ask_csys_info(tag_t csys, double origin[3], double axes[9])
{
    tag_t
        matrix;

    UF_CALL(UF_CSYS_ask_csys_info(csys, &matrix, origin));
    UF_CALL(UF_CSYS_ask_matrix_values(matrix, axes));
}

static void do_it(void)
{
    tag_t
        assy_part = UF_PART_ask_display_part(),
        assy_csys,
        comp,
        comp_csys,
        comp_csys_occ,
        comp_part,
        from_csys,
        temp_csys,
        to_csys;
    double
        new_origin[3],
        new_csys_matrix[9];  // need 9 for ask_csys_info
    char
        comp_spec[MAX_FSPEC_SIZE+1] = { "" };

    while (prompt_for_existing_part_name(comp_spec))
    {
        if ((!open_part(comp_spec, &comp_part)) ||
            (from_csys = specify_csys("From CSYS")) == NULL_TAG) continue;
        comp_csys = create_duplicate_alive_normal_csys(from_csys);

        UF_CALL(UF_PART_set_display_part(assy_part));
        if ((to_csys = specify_csys("To CSYS")) == NULL_TAG) continue;
        assy_csys = create_duplicate_alive_normal_csys(to_csys);

        set_undo_mark("Place component");

        comp = add_component(comp_spec);
        comp_csys_occ = UF_ASSEM_find_occurrence(comp, comp_csys);

        temp_csys = create_component_csys(comp);
        transform_csys_to_csys(comp_csys_occ, assy_csys, 1, &temp_csys);
        ask_csys_info(temp_csys, new_origin, new_csys_matrix);

        UF_CALL(UF_ASSEM_reposition_instance(
            UF_ASSEM_ask_inst_of_part_occ(comp), new_origin,
            new_csys_matrix));

        UF_CALL(UF_OBJ_delete_object(comp_csys));
        UF_CALL(UF_OBJ_delete_object(assy_csys));

        UF_CALL(UF_DISP_regenerate_display());
    }
}

/*ARGSUSED*/
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);
}
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-9-28 03:00 , Processed in 0.035570 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表