乐筑天下

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

[编程交流] Lisp有点像offset,但是

[复制链接]

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:16:49 | 显示全部楼层
再次讨论主题
 
嗨,李,
经过一点研究。。。因为你坚持;我要学点什么!
我想,学习阅读例程最终会给我提供足够的知识,让我自己写一些例程。所以我必须读书。
  1. (command "_.offset" "_L" ...)

通过保持可变曲线;def global,例程设置为
稍后在会话中调用时,再次使用选定的线(样条线或样条线)。
那好吧。
  1. (defun c:draw (/ *error* vl ov tmp cEnt pt dEnt dObj)
  2. ;; Define Function and Localise Variables
  3. ;; Don't localise Curv:def, as it remains Global.

Visual Lisp对我来说是全新的,所以我想“嘿,为什么不下载一些教程呢?”。
所以我这么做了,现在我有很多信息要处理。
哦。。。我真幸运!
来自afralisp。净值:
在将VLisp函数与AutoLisp一起使用之前,需要加载支持这些函数的代码。(vl load com)功能首先检查VLisp支持是否已加载;如果是这样,该函数将不执行任何操作。如果未加载函数,(vl load com)加载它们。
  1. (vl-load-com)
  2. ;; Load Visual LISP functions - for use later.
  3. ;; I find it easier to change object properties
  4. ;; using Visual LISP - it can be done in many ways,
  5. ;; but this is just my preference.

那个,我不明白。
我说的直线(或Curv:def(setq Curv:def“Spline”)是指:
这是一个or函数,当变量curv:def为NIL时,将其设置为“spline”。
如果它不是零,则保持原样,即在以前使用此例程时,给定pline。"
  1. (or Curv:def (setq Curv:def "Spline"))
  2. ;; First time default, as Curv:def will be nil otherwise.

我知道错误处理是必要的,但我觉得有点复杂。
以(defun*error*(msg)开头是标准的。
使用的变量是msg,然后。。。嗯
尤其是我以前从未看过的wcmatch。
  1. ;; Error Handler - as we are tampering with Sys Vars
  2. (defun *error* (msg)
  3.    (if ov (mapcar 'setvar vl ov)) ; Reset Sys Vars
  4.    (if (not
  5.          (wcmatch
  6.            (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
  7.      (princ (strcat "\n<< Error: " msg " >>")))
  8.    (princ))

创建一个系统变量列表,我们想在一种主变量vl中读出。
将变量ov(旧变量)设置为vl中读取的值。
它到底是怎么工作的,我不知道,但我知道发生了什么。。。我想。。。
  1. (setq vl '("CMDECHO" "OSMODE") ; Define a list of Sys Vars
  2.        ov (mapcar 'getvar vl)) ; Retrieve their Settings

让我们给用户一个选择,是选择样条线还是样条线。
请注意,变量Curv:def是在本例行程序开始时读取的。
当第一次在会话中使用例程时,它返回“样条曲线”,否则返回两者中选择的一个。
变量tmp将设置为所选的任何值,下一个曲线:def设置为tmp的值。
我唯一没有得到的是那个角色。
问题是,是否或用于强制在initget选项之间进行选择?
再说一遍,是不是用来获取正确的错误消息?
  1. (initget "Spline Pline")
  2. (or (not
  3.        (setq tmp
  4.          (getkword (strcat "\nSpline or Pline? <" Curv:def ">: "))))
  5.      (setq Curv:def tmp))
  6. ;; Either the user has hit Enter, in which case we don't need to
  7. ;; change the value of Curv:def, else, the user has typed something
  8. ;; other than that of Curv:def, so we set it as the new default.

当曲线:def等于“spline”时,则命令“_.spline”else命令“_.pline”。
正当请注意,缺少用于结束命令的“”。
  1. (command
  2.    (if (eq Curv:def "Spline") "_.Spline" "_.Pline")) ; Invoke Command

它是一个循环,只要命令处于活动状态(直到出现“”或enter),它就会进行循环。
  1. (while (> (getvar "CMDACTIVE") 0) ; Whilst the Command is active
  2.    (command pause)) ; pause for user input

将最后创建的曲线(样条线或样条线)放入变量cEnt。
  1. (setq cEnt (entlast)) ; Retrieve newly created Curve

提示要偏移的边。当pt给定时,则到达prgn部分。
如果未给出pt,即按下退出按钮,则转至下一部分,称为(mapcar’setvar vl ov)。因此,重置系统变量。
  1. (if (setq pt (getpoint "\nSelect Side of Curve to Offset: "))
  2.    ; If the user selects a point to offset

为什么选择progn?我是说,为什么要包裹?
当不使用progn时,它不也会这样做吗?
  1.    
  2.    (progn ; Wrap the following

这对我来说很有意义。
  1.      (mapcar 'setvar vl '(0 0))
  2.      ;; Turn off OSMODE, as we don't want it to affect the Offset.
  3.      ;; Turn off CMDECHO, as we don't want to see the returns of
  4.      ;; the offset at the command line.
  5.      (command "_.offset" 50 cEnt pt "")
  6.      ;; Perform Offset
  7.      (setq dEnt (entlast))
  8.      ;; Collect Offset Object

 
如果这能让事情变得更简单然而,我喜欢使用or,因为它使代码更简洁。
 
考虑这个问题的方法是,or一直对语句求值,直到语句返回T,然后停止求值。因此,如果Curv:def有一个值,那么它是非nil(记住T是任何非nil的东西),因此(setq…将不会被计算。反之亦然。
 
 
如果查看wcmatch函数上的Visual LISP编辑器帮助文件,它用于匹配字符串中的模式-因此我使用它来抑制“取消”消息。如果错误消息不包含列出的短语,则将打印,否则不打印。
 
 
vl只是一个字符串列表。在本例中,字符串是系统变量的名称。
 
mapcar将对列表中的每个项目执行提供的函数(在本例中为getvar),并返回结果列表。
 
因此,如果CMDECHO为1,OSMODE为55,ov将设置为以下值:
 
  1.      (setq dObj (vlax-ename->vla-object dEnt))
  2.      ;; Convert it to a VLA-OBJECT
  3.      (vla-put-layer dObj "0") ; <<---- Change this to your layer
  4.      ;; Set Object Layer

 
取决于所列系统变量的值。
 
 
好的,or与initget函数无关。initget函数设置为允许列出关键字,还允许用户点击enter键,这将使getkword返回nil。
 
因此,使用上述逻辑:
 
要么:
 
1) getkword返回nil,因此not getkword返回T,因此第二条语句不计算,因此在以下语句中使用Curv:def的当前值。
 
或:
 
2) getkword返回一个非nil值,因此用户键入了一些内容,因此将计算第二条语句,因此curv:def的值将设置为这个新输入。
 
 
 
progn用于“包装”表达式,以便将其视为单个表达式。
 
IF通常需要三个(或更少)表达式:
 
-测试表达式
-“then”表达式
-“else”表达式
 
因此,如果测试表达式返回t,我们使用progn包装所有我们想要计算的表达式。因此,if语句将它们视为一个表达式。
 
如果未使用progn,则只计算下一个表达式,然后会出现错误,说明参数过多。
 
 
正如我所说,这可以通过使用“offset”命令中的“layer”选项来实现。
 
 
希望这有帮助,
 
如果你还是不懂我写的东西,尽管问。
 
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

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

铜币
295
发表于 2022-7-6 15:19:51 | 显示全部楼层
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:25:19 | 显示全部楼层
 
Ok, some people are against using the or function in this way, and would rather use:
 
  1. (if (not Curv:def) (setq Curv:def "Spline"))
 
If that makes things simpler. - However I like the use of or as it makes the code more concise.
 
The way to think about it is that or keeps evaluating statements until a statement returns T, at which point it stops evaluating. Hence, if Curv:def has a value, then it is non-nil (remember that T is anything non-nil), and hence the (setq... will not be evaluated. And vice-versa.
 
 
If you look at the Visual LISP Editor help file on the wcmatch function, it is used to match a pattern within a string - hence I am using it to suppress the "cancelled" messages. If the error message does not contain the phrases listed, then it will be printed, else not.
 
 
vl is just a list of Strings. in this case the strings are the names of our System Variables.
 
mapcar will execute the provided function (in this case getvar) on every item in the list, and return a list of the results.
 
Hence, if the CMDECHO was 1, an OSMODE was 55, ov would be set to something like:
 
  1. (1 55)
 
Depending on the values of the System Variables listed.
 
 
Ok, the or has nothing to do with the initget function. The initget function is set to allow the keywords listed, and also allows the user to hit enter - which would make the getkword return nil.
 
Hence, using the logic described above:
 
Either:
 
1) the getkword returns nil, hence the not getkword returns T - and so the second statement is not evaluated, so the Current value of Curv:def is used in the following statements.
 
Or:
 
2) The getkword returns a non-nil value - and so the user has typed something, so the second statement will be evaluated, and so the value of curv:def will be set to this new input.
 
 
 
progn is used to "wrap" expressions so that they are treated as a single expression.
 
IF would normally require three (or fewer) expressions:
 
- a test expression
- "then" expression
- "else" expression
 
And so, we use progn to wrap all the expressions that we want evaluated if the test expression returns t. So that the if statement treats them all as one expression.
 
If the progn was not used, only the next expression would be evaluated, and then an error would ensue saying too many arguments.
 
 
This, as I said, could be done differently, using the "layer" option in the "offset" command.
 
 
Hope this helps,
 
If you still don't understand something that I have written, just ask.
 
Lee
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-5 03:08 , Processed in 0.314214 second(s), 56 queries .

© 2020-2025 乐筑天下

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