首页/文章/ 详情

HyperWork必备知识-templex语言2-函数定义

3月前浏览1086

通过关键字define 定义函数,通过关键字include载入文件,而后可以直接调用。当然所定义的函数和调用的代码可以写在一起。


外部函数文件,需要保存为eulerXYZ.tpl








































{' This templex macro calculates the Euler params based on three sequential rotations.}{' Rotation sequence Rx -> Ry -> Rz.}{define eulerXYZ(rx, ry, rz)}{   t1 = dtor(rx)}{   t2 = dtor(ry)}{   t3 = dtor(rz)}{' Cosine matrix:}{   a11=cos(t2)*cos(t3)}{   a12=-cos(t2)*sin(t3)}{   a13=sin(t2)}{   a21=sin(t1)*sin(t2)*cos(t3)+sin(t3)*cos(t1)}{   a22=cos(t1)*cos(t3)-sin(t1)*sin(t2)*sin(t3)}{   a23=-sin(t1)*cos(t2)}{   a31=sin(t1)*sin(t3)-cos(t1)*sin(t2)*cos(t3)}{   a32=sin(t1)*cos(t3)+cos(t1)*sin(t2)*sin(t3)}{   a33=cos(t1)*cos(t2)}{   trace = a11+a22+a33 }{' Euler parameters:}{   e0 = 0.5*sqrt(trace+1.0) }{   e1 = 0.5*sqrt(1.0 + 2.0*a11 - trace) }{   e2 = 0.5*sqrt(1.0 + 2.0*a22 - trace) }{   e3 = 0.5*sqrt(1.0 + 2.0*a33 - trace) }{ if e0 != 0.0 }{   e0 = sqrt(e0*e0) }{   e1 = (a32-a23)/(4.0*e0) }{   e2 = (a13-a31)/(4.0*e0) }{   e3 = (a21-a12)/(4.0*e0) }{ elseif e1 != 0.0 }{   e1 = sqrt(e1*e1) }{   e2 = (a13+a31)/(4.0*e1) }{   e3 = (a21+a12)/(4.0*e1) }{ elseif e2 != 0.0 }{   e2 = sqrt(e2*e2) }{   e3 = (a32+a23)/(4.0*e2) }{ else }{   e3 = 1.0 }{ endif }{enddefine}


调用外部所定义的函数












{include "eulerXYZ.tpl"}SYSTEM 1 JOINT DOF{eulerXYZ( 90, 0, 0)} 1  FREE  {e0}  {e1}  {e2}  {e3}    0.5  0.5  0.5    0.0  0.0  0.0    50.0  0.0  0.0END JOINT DOFEND SYSSYSTEM 2 JOINT DOF{eulerXYZ( 0, 90, 0)} 1  FREE  {e0}  {e1}  {e2}  {e3}    0.5  0.5  0.5    0.0  0.0  0.0    50.0  0.0  0.0END JOINT DOFEND SYS


最终显示









SYSTEM 1 JOINT DOF  1  FREE  0.707107  0.707107  0  0    0.5  0.5  0.5    0.0  0.0  0.0    50.0  0.0  0.0END JOINT DOFEND SYSSYSTEM 2 JOINT DOF  1  FREE  0.707107  0  0.707107  0    0.5  0.5  0.5    0.0  0.0  0.0    50.0  0.0  0.0END JOINT DOFEND SYS


函数与调用代码写在一起





{define greeting(first, last)}Dear {first} {last},{enddefine}{greeting("John", "Doe")}


我们来写一个类似于python的range函数







{define range(start, end, step)}  {for (i = start; i < end; i += step)}    {i}  {endloop}{enddefine}{range(0, 20, 2)}


来源:TodayCAEer
Systempython
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-14
最近编辑:3月前
TodayCAEer
本科 签名征集中
获赞 18粉丝 28文章 249课程 0
点赞
收藏
作者推荐

HyperWork必备知识-templex语言5-逻辑判断

关键字if,elseif,else,endif输入{numer=5}{denom=4}{if(numer==0)}Thenumeratoris0,sotheresultis0.{elseif(denom==0)}Thedenominatoris0,soaresultisimpossible.{else}Theresultis:{numer/denom}.{endif}输出Output:Theresultis:1.25.输入{x=5;y=4}{if(x%y)}Theremainderis{x%y}.{else}{x}isamultipleof{y}.{endif}输出Output:Theremainderis1.通过判断传入的运算符采用不同的计算方式并返回结果{num1=10}{num2=5}{operation=&quot;divide&quot;}{if(operation==&quot;add&quot;)}Resultofaddition:{num1+num2}{elseif(operation==&quot;subtract&quot;)}Resultofsubtraction:{num1-num2}{elseif(operation==&quot;multiply&quot;)}Resultofmultiplication:{num1*num2}{elseif(operation==&quot;divide&quot;)}{if(num2==0)}Divisionbyzeroisnotpossible.{else}Resultofdivision:{num1/num2}{endif}{else}Unknownoperation.{endif}我们可以把相同的代码写成函数的形式{defineuser_calculate(num1,num2,operation)}{if(operation==&quot;add&quot;)}Resultofaddition:{num1+num2}{elseif(operation==&quot;subtract&quot;)}Resultofsubtraction:{num1-num2}{elseif(operation==&quot;multiply&quot;)}Resultofmultiplication:{num1*num2}{elseif(operation==&quot;divide&quot;)}{if(num2==0)}Divisionbyzeroisnotpossible.{else}Resultofdivision:{num1/num2}{endif}{else}Unknownoperation.{endif}{enddefine}{num1=10086}{num2=5}{operation=&quot;divide&quot;}{user_calculate(num1,num2,operation)}来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈