- 积分
- 10
- 注册时间
- 2007-10-5
- 仿真币
-
- 最后登录
- 1970-1-1
|
楼主 |
发表于 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);
} |
|