乐筑天下

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

[编程交流] Rectangle drawing LISP – help

[复制链接]

3

主题

9

帖子

6

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-6 14:31:41 | 显示全部楼层 |阅读模式
Hello everyone
 
  I tried to make a lisp that would draw a rectangle 3,-3 points from the first point i select, and -3,3 from the second.
 
  But, something weird id going on, this is my failed attempt:
 
  1. (defun c:rf (/ pt1 pt2)  (setq pt1 (getpoint "PT1:"))  (setq pt2 (getpoint "PT2:"))  (command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3") )
 
If i select points in "empty" space it works. But when i select specific points on objects (with OSNAP) it draws a rectangle without offset of 3, ie. precisely on selected points.
 
    Thanks in advance!
回复

使用道具 举报

VVA

1

主题

308

帖子

308

银币

初来乍到

Rank: 1

铜币
8
发表于 2022-7-6 14:48:30 | 显示全部楼层
  1. (defun c:rf (/ pt1 pt2)  (setq pt1 (getpoint "PT1:"))  (setq pt2 (getpoint "PT2:"))  (command "_rectangle" "_from" [color="Red"]"_non"[/color] pt1  [color="Red"]"_non" [/color]"@3,-3" "_from" [color="Red"]"_non"[/color] pt2  [color="Red"]"_non"[/color] "@-3,3") )
回复

使用道具 举报

3

主题

9

帖子

6

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-6 14:55:44 | 显示全部楼层
Thanks, works!
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

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

铜币
295
发表于 2022-7-6 15:05:39 | 显示全部楼层
_NON is like "disable snappoints" for 1 action only.
 
For commands, use "_." as prefix to suit other languages. I was told, this to use it.
 
You could use some coding to set the old variable:
 
  1. (defun c:rf (/ pt1 pt2)  (setq pt1 (getpoint "PT1:"))  (setq pt2 (getpoint "PT2:"))  (setq oldosmode (getvar "osmode"))  (setq osmode 0)  (command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")(setq osmode "oldosmode"))
 
Or this one:
 
  1. (defun c:rf (/ pt1 pt2)  (setq     pt1 (getpoint "PT1:")     pt2 (getpoint "PT2:")     oldosmode (getvar "osmode") ;;remember old osnapmode     osmode 0 ;; set the osnapmode to no osnappoints     )(command "._rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")(setq osmode "oldosmode") ;; back to the previous osnapmode(princ) ;;clean exit)
 
I'm not that great programmer but I have learned a lot in here so I am glad to say that this is one of my first contibutes to the forum.
 
Cheers.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:07:38 | 显示全部楼层
 
Marco, its great that you are learning
 
Just a few things:
 
  1. (defun c:rf (/ [color=Red][b]oldosmode[/b][/color] pt1 pt2)  (setq pt1 (getpoint "PT1:"))  (setq pt2 (getpoint "PT2:"))  (setq oldosmode (getvar "osmode"))  ([b][color=Red]setvar[/color][/b] [color=Red][b]"[/b][/color]osmode[b][color=Red]"[/color][/b] 0)  (command "_rectangle" "_from" pt1 "@3,-3" "_from" pt2 "@-3,3")([color=Red][b]setvar[/b][/color] [b][color=Red]"[/color][/b]osmode[b][color=Red]"[/color][/b] oldosmode)[b][color=Red](princ) ; Exit Cleanly[/color][/b])
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

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

铜币
295
发表于 2022-7-6 15:16:16 | 显示全部楼层
Lee shouldn't you be seriously sweating with your exam?
 
You are right, some stupid mistakes I made. Did test it but it ran... at least I thought...
 
  1. (defun c:rf (/ oldosmode pt1 pt2)
 
Yes, it should be cleared after command's ready.
 
  1. (setvar "osmode" 0)
 
We'll ehm... yes.
 
  1. (princ) ; Exit Cleanly
 
What's wrong here?
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:30:29 | 显示全部楼层
 
Yes, just had the exam this morning, could've gone better I think... but it was OK...
 
As for the "princ", you missed it in your first code... shout if I'm being too picky
回复

使用道具 举报

20

主题

344

帖子

325

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 15:39:35 | 显示全部楼层
 
Nothing "wrong" per se, but it's sort of the programming equivalent of "good manners" to null returns from a function, unless you want it to return something. A function returns the last argument it evaluated, which in your code would have been the return of (setvar "osmode" oldosmode), which means, your old osnap setting.
 
In this case, since it's a command, it doesn't really matter much. When you run the command, you'll probably see a number printed indicating your old OSMODE. Since this number isn't hurting anyone or anything, it's fine for it to be there. If you put that (princ) before the parentheses to close the defun, however, it'll return what (princ) returns instead of what (setvar) returns, that is to say, nil.
 
Just something to be aware of. It's a good habit to get into. ^^
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-5 00:12 , Processed in 0.775409 second(s), 68 queries .

© 2020-2025 乐筑天下

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