首页/文章/ 详情

HyperWork 2024 -二次开发-Tcl语言-proc(9/12)

2月前浏览313

Procedures organize a repetitive group of commands into a logical block and allow for arguments to be parameterized. They improve performance and memory because a procedure’s commands and variables only exist within the scope of the procedure.


This means that variables created inside a procedure only exist while the procedure is executed and are destroyed when the procedure returns.

The proc command is used to define a procedure. There are optional arguments that can be defined for a procedure as values that will be passed to the procedure when the procedure is called. Each parameter becomes a variable in the procedure. The commands contained within the curly braces are run when the procedure is called. The format of a procedure is:

proc name {?arg1? ?arg2? etc…} {body}

After a procedure is defined, it is used just like any other Tcl command. The name of the procedure becomes a new Tcl command name and is called just like any standard Tcl command. In this way, it is also possible to redefine (or undefine) the function of existing Tcl commands.

An example procedure named curve_info that takes curve_reference and color as its arguments:

proc curve_info {curve_reference color} {
  puts "Curve reference = $curve_reference";
  puts "Curve color = $color";
}
curve_info p1w1c1 5;
Curve reference = p1w1c1
Curve color = 5

The name args is a special procedure parameter when you don't know how many arguments will be used. This is useful when creating a context menu where multiple items may be passed.

proc node_info {args} {
  foreach node $args {
     puts "Node = $node";
  }
}
node_info node1 node2 node3;
Node reference = node1
Node reference = node2
Node reference = node3

Variables are treated as one argument and do not require the args parameter.

proc node_info {node_list} {
  foreach node $node_list {
     puts "Node = $node";
  }
}
set node_list "node1 node2 node3";
node_info $node_list;
Node reference = node1
Node reference = node2
Node reference = node3

The return command is used to exit a procedure. This is handy for error control.

proc node_info {args} {
  if {[llength $args] == 0} {
     puts "No nodes are selected";
     return;
  }
  foreach node $args {
     puts "Node = $node";
  }
}
curve_info;
No curves are selected

curve_info node1 node2 node3;
Node = node1
Node = node2
Node = node3

The return command can also be used to assign the results of a procedure to a variable.

proc add_three_numbers {num1 num2 num3} {
  set sum [expr $num1 + $num2 + $num3];
  return $expression;
}
set sum [add_three_numbers 2 4 5];
puts $sum;
11

Procedure names, by default, are defined in the global scope (top level) of a Tcl script. Any variables that are defined outside of a procedure are also part of the global scope and are called global variables. Different namespaces exist for procedure names and global variables so it is possible to have procedures and variables with the same name without issue. It is also possible, and recommended, to use the Tcl namespace facility to manage the scope of procedures and variables.

As mentioned above, all variables defined within a procedure are local to that procedure. This means that, unless otherwise defined, a variable definition only exists, and is only accessible, from within the procedure it is created in.

set element_list "1 2 3 4 5";
proc element_info {} {
  puts $element_list;
  set node_list "10 20 30 40";
  puts $node_list;
}
element_info;
can't read "element_list": no such variable
10 20 30 40

puts $element_list;
1 2 3 4 5

puts $node_list;
can't read "node_list": no such variable

This also allows the programmer to define the same local variable within different procedures without any conflict.

proc node_info_1 {} {
  puts $node_list;
  set node_list "10 20 30 40";
  puts $node_list;
}

proc node_info_2 {} {
  puts $node_list;
  set node_list "10 20 30 40";
  puts $node_list;
}
node_info_1;
can't read "node_list": no such variable

10 20 30 40

node_info_2;
can't read "node_list": no such variable
10 20 30 40

As a general rule, it is good practice to create focused procedures that have specific functions which are modular and reusable. This allows the procedure to perform a specific operation that allows other developers to easily see what function the procedure performs. It also allows procedures to be reused in other scripts that may require similar functions.



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

HyperMesh二次开发-运动调整机构的快速创建

在当初写手动机构调整这篇文章的时候就讲到,可以通过二次开发来完成机构的创建。HyperMesh的运动机构调整功能JinTian,公众号:TodayCAEerHyperMesh的运动机构调整功能还有一点就是,源自于手动创建运动机构的文章写了这么久了,阅读量依然保持上涨的态势,所以我来填上这个坑。下面将介绍如何在HyperMesh完成机构的快速创建。走的是通用化路线,所以运行是半自动状态,旨在简化传统的创建逻辑,让小伙伴高效地构建复杂的运动机构,从而减少重复性的工作。在手动操作的流程中,需要先创建完所有主体(Body),然后再为其定义相应的连接(Joint),当名称不具有标识性的时候,在一堆Body中找到需要的Body就很麻烦。所以在开发的时候,就摒弃了这种逻辑,采取了更加灵活的创建方式,能够在创建连接的同时定义Body。可以进一步减少用户的操作步骤,界面如图所示。同时支持的连接类型与Mechanism浏览器上创建也是一致的,包含Ball:用于创建球形连接,允许三个自由度的运动。Cylinder:用于创建圆柱连接,提供两个自由度的运动。Revolute:用于创建转动副,只有一个自由度。Slider:允许沿一条特定线的直线移动。DoubleSlider:结合两个滑动副的特点,提供更复杂的连接方式。Cam:实现凸轮机构的连接。当然程序并没有将所有参数都放在界面上,像自由度的设置就需要在Mechanism机构中修改了。在创建Joint的时候,我们可以通过多种选择方式来定义Joint的原点坐标和矢量,矢量就通过选择的两点自动计算填充,选择方式包含了:圆弧线选择:直接选择模型中已有的线,程序将自动计算线的圆心作为硬点位置,这也是新增的类型之一。点选择:允许用户选择网格点,与界面上一致。在创建Body时,用户可以灵活选择不同的类型,可以更方便创建所需的Body,适应不同的模型,选择方式包含了:comps:选择Compsset_elem:选择单元创建Setset:选择Setbodies:选择Bodypart:选择Part在某些复杂结构的创建中,程序可能无法自动适应所有的设计需求。用户可以通过Mechanism浏览器对机构的信息,进行进一步修改。同时对于机构的检查,手动调整机构的参数等,以确保机构的正确性。来源:TodayCAEer

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