/* Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved. */ /*------------------------------------------------------------------*\ Pro/Toolkit includes \*------------------------------------------------------------------*/ #include <ProToolkit.h> #include <ProObjects.h> #include <ProSelection.h> #include <ProAsmcomppath.h> #include <ProAnimate.h> #include <ProMessage.h> #include <ProUtil.h> #include <ProModelitem.h> /*------------------------------------------------------------------*\ Application includes \*------------------------------------------------------------------*/ #include <TestError.h> /*------------------------------------------------------------------*\ C System includes \*------------------------------------------------------------------*/ #include <math.h> /*------------------------------------------------------------------*\ Application data \*------------------------------------------------------------------*/ #define MAX_NUM_FRAMES 20 #define PI 3.1451 /*==================================================================*\ FUNCTION : UserAsmcompAnimate() PURPOSE : Animates an assembly component. \*==================================================================*/ int UserAsmcompAnimate() { ProMdl model ; /* current model */ ProModelitem anim_mdlitem ; /* Modelitem for anim. component */ ProMdl anim_model ; /* Mdl for anim. component */ ProSelection *p_sel_comp ; /* ProSelection for anim. comp. */ ProAnimObj anim_obj ; /* anim. object for anim. comp. */ ProAsmcomppath comp_path ; /* component path for anim. comp. */ int n_sel ; /* number of selections */ ProMatrix pos_mat ; /* transformation matrix of assembly component */ double angle ; /* rotation angle */ int num_frames ; /* number of frames in animation */ int range[2] ; /* allowable range for input */ ProAnimFrame frame ; /* animation frame */ ProAnimMovie anim_movie ; /* movie */ int i ; /* counter */ ProName msg_file ; /* message file */ int err ; /* return status */ ProMatrix frame_view = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }; /* view matrix for frame */ void ProUtilRotX(double, ProMatrix) ; /* rotation function */ ProStringToWstring(msg_file, "msg_ugfund.txt") ; /*------------------------------------------------------------------*\ Get the current model. \*------------------------------------------------------------------*/ err = ProMdlCurrentGet(&model); ERROR_CHECK("UserAsmcompAnimate()", "ProMdlCurrentGet()", err); if (err != PRO_TK_NO_ERROR) { ProMessageDisplay(msg_file, "USER %0s F", "Error getting current model."); return(err) ; } /*------------------------------------------------------------------*\ Get the component to be animated (only 1 allowed). \*------------------------------------------------------------------*/ ProMessageDisplay(msg_file, "USER %0s F", "Select a component to be animated.") ; err = ProSelect("prt_or_asm", 1, NULL, NULL, NULL, NULL, &p_sel_comp, &n_sel) ; ERROR_CHECK("UserAsmcompAnimate()", "ProSelect()", err); if ( err != PRO_TK_NO_ERROR ) { ProMessageDisplay(msg_file, "USER %0s F", "Error or abort during selection.") ; return(err) ; } /*------------------------------------------------------------------*\ Get the transformation matrix of the selection. \*------------------------------------------------------------------*/ err = ProSelectionAsmcomppathGet(p_sel_comp[0], &comp_path) ; ERROR_CHECK("UserAsmcompAnimate()", "ProSelectionAsmcomppathGet()", err); err = ProAsmcomppathTrfGet(&comp_path, PRO_B_TRUE, pos_mat) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAsmcomppathTrfGet()", err); /*------------------------------------------------------------------*\ Get ProModelitem and ProMdl for component. \*------------------------------------------------------------------*/ err = ProSelectionModelitemGet(p_sel_comp[0], &anim_mdlitem) ; ERROR_CHECK("UserAsmcompAnimate()", "ProSelectionModelitemGet()", err); err = ProModelitemMdlGet(&anim_mdlitem, &anim_model) ; ERROR_CHECK("UserAsmcompAnimate()", "ProModelitemMdlGet()", err); /*------------------------------------------------------------------*\ Create the animation. \*------------------------------------------------------------------*/ err = ProAnimmovieCreate(anim_model, &anim_movie) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAnimmovieCreate()", err); angle = 0.0 ; ProMessageDisplay(msg_file, "USER %0s F", "Enter the number of frames: ") ; range[0] = 2 ; range[1] = MAX_NUM_FRAMES ; ProMessageIntegerRead(range, &num_frames) ; for (i=0 ; i < num_frames ; i++) { angle += 360.0/ (double) num_frames ; ProUtilRotX(angle, pos_mat) ; err = ProAnimobjectCreate(p_sel_comp[0], pos_mat, &anim_obj) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAnimobjectCreate()", err); err = ProAnimframeCreate(frame_view, &frame) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAnimframeCreate()", err); err = ProAnimframeObjAdd(frame, anim_obj) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAnimframeObjAdd()", err); err = ProAnimmovieFrameAdd(anim_movie, frame) ; ERROR_CHECK("UserAsmcompAnimate()", "ProAnimmovieFrameAdd()", err); } err = ProBatchAnimationStart(anim_movie, NULL, NULL) ; ERROR_CHECK("UserAsmcompAnimate()", "ProBatchAnimationStart()", err); return(PRO_TK_NO_ERROR) ; } /*------------------------------------------------------------------*\ FUNCTION: ProUtilRotX() PURPOSE: Add x rotation values to the transformation matrix. RETURNS: None NOTE: Only the rotation components of the matrix are modified. \*------------------------------------------------------------------*/ void ProUtilRotX( double angle, /* (In) The rotation angle */ ProMatrix mx ) /* (Out) The transformation matrix */ { mx[0][0] = 1.0; mx[0][1] = mx[0][2] = mx[1][0] = mx[2][0] = 0.0; mx[1][1] = mx[2][2] = cos( angle * PI / 180.0); mx[1][2] = sin( angle * PI / 180.0); mx[2][1] = - mx[1][2]; }