乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 26|回复: 5

[编程交流] optional parameters - user inp

[复制链接]

15

主题

315

帖子

361

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 17:22:20 | 显示全部楼层 |阅读模式
Is there a standard (good practice) way of handling optional parameters for functions?
 
 
---
 
 
The idea is: the user either
- selects an entity (entsel, ssget, ...) , fills in a value (getint, getstring, ...)
OR
- presses enter, then an other procedure takes over, or a default value gets set.
 
 
One concrete example,  is a post I just answered, to insert (Lamp) blocks evenly spread in a room.
http://www.cadtutor.net/forum/showthread.php?88364-block-spacing-lisp&p=667270#post667270
 
 
Let's say this function is used 5 times in 1 dwg, and mostly with the same blockname, and the same number of rows and columns.  It would be useful for the user if the function inserts the previous value if the user presses Enter
 
 
---
Here is an example
 
  1. ;; global variables with default values(setq  global_blockname "LAMP" global_rows 2 global_cols 2);; notice, this function doesn't work; this is the problem(defun testfunction (blockname cols rows / ) (if (= nil blockname)   (setq blockname global_blockname) )  (if (= nil cols)   (setq cols global_cols) )  (if (= nil rows)   (setq rows global_rows) )   (princ blockname) (princ cols) (princ rows))(defun c:test ( / p1 p2) (testfunction    ;; user specifies a block   (setq global_blockname (getstring "\nBlock name: "))      ;; user gives 2 numbers x & y    (setq global_cols (getint "\nnumber of colums "))   (setq global_rows (getint "\nnumber of rows "))    ) (princ))
 
 
- (= nil ...) is not the answer.  If I get this to work for different types of input, I think I can handle the rest.
 
 
- Overwriting a parameter, is that a wrong way doing it? Better declare extra variables instead?
 
 
- Setting global variables, to store default values; to store the previous values, ... in this a good idea?
回复

使用道具 举报

18

主题

1529

帖子

973

银币

中流砥柱

Rank: 25

铜币
649
发表于 2022-7-5 17:43:08 | 显示全部楼层
Some remarks:
1.
The (getstring) function never returns nil.
2.
In your c:test function you are overwriting global variables making your testfunction useless.
3.
There is nothing wrong with global variables. Most programmers will use them in this situation. But there can be name conflicts.
 
Maybe this is helpful:
  1. (defun MyGetInt (msg def) ; Msg without "\n" and ": ". (cond   ((getint (strcat "\n" msg ": " (if def (strcat ": ") ""))))   (def) )); Note: function can return nil.(defun MyGetString (msg def / ret) ; Msg without "\n" and ": ". (cond   ((/= "" (setq ret (getstring T (strcat "\n" msg ": " (if def (strcat ": ") "")))))     ret   )   (def) ))(defun c:MyCommand ( / ) ... (if (not *myDefaultInt*) (setq *myDefaultInt* 2)) (setq *myDefaultInt* (MyGetInt "Default integer" *myDefaultInt*)) (if (not *myDefaultString*) (setq *myDefaultString* "Lamp")) (setq *myDefaultString* (MyGetString "Default string" *myDefaultString*)) ... ...)
回复

使用道具 举报

15

主题

315

帖子

361

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 17:53:45 | 显示全部楼层
Thanks.  
 
 
I'll see how best to use these elements, I'll try it out on that link I posted.
I suppose that use of (cond) is the basis of a solution.
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
475
发表于 2022-7-5 18:04:51 | 显示全部楼层
I think that Lee Mac's tutorial is the best out there for how to prompt with a default option http://www.lee-mac.com/promptwithdefault.html. How I often do it when I am doing something like an entsel is
  1. (initget "Settings")   (setq sel   (entsel     "\\nSelect an object to do something [settings]: "   )   )
I then use a conditional that is based on what the user does.
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 18:15:38 | 显示全部楼层
If you make your variable names very unique then you can keep them as global to end of a session. eg EM-val1 EM-val2
 
use a simple if this is a bit long winded but make it a defun and then its short 1 line call.
 
  1. (if (= horiz nil)   (progn (setq horiz 100)   (prompt "\nEnter Horizontal scale  ::")   (setq newhoriz (getint))                           (if (= newhoriz nil)  (PRINC "\N")  (setq horiz newhoriz)         )   ); progn horiz
回复

使用道具 举报

rlx

21

主题

1505

帖子

1551

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
81
发表于 2022-7-5 18:31:07 | 显示全部楼层
Just one more little example I'm using right now. I have a paper copy of a layout that has to be redrawn in AutoCad. I measure a known distance with a known dimension to calculate a scale factor and from there on the routine returns the actual distance for every distance I measure until I cancel or enter 0 to recalibrate the scale factor. It stores this factor in a variable (rescale-factor) that is valid during the current drawing session.
 
 
  1. ;rescale - rescale-factor value will be remembered during drawing session(defun c:rescale ( / a b c) (if (not rescale-factor)   (cond     ;enter known distance in millimeters , trap cancel     ((= (vl-catch-all-error-p (setq a (vl-catch-all-apply 'getreal (list "\nReference distance as noted (mm) " )))) t)      (princ "\nInvalid input or user abort for noted reference distance"))     ((= (vl-catch-all-error-p (setq b (vl-catch-all-apply 'getreal (list "\nReference distance as measured (mm) " )))) t)      (princ "\nInvalid input or user abort for measured reference distance"))     (t (setq rescale-factor (vl-catch-all-apply '/ (list a b)))))) (if rescale-factor   (progn     (princ (strcat "\nCurrent rescale-factor = " (rtos rescale-factor)))     (princ "\nEnter new distances or 0 to reset")     (while (not (= (vl-catch-all-error-p (setq c (vl-catch-all-apply 'getreal (list "\nEnter new distance (mm) : " )))) t))(if (member c '(0 nil));reset rescale-factor  (progn (setq rescale-factor nil)(c:rescale))  (princ (strcat "\nRe-scaled distance = " (rtos (* rescale-factor c)))))))   (princ "\nUnable to find or set rescale-factor") ) (princ "\nRescale function ended") (princ))
 
 
gr.Rlx
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 04:15 , Processed in 1.144127 second(s), 64 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表