乐筑天下

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

[编程交流] Carriage return in lisp-create

[复制链接]

12

主题

25

帖子

13

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 19:11:43 | 显示全部楼层
That works perfectly, thanks for your help and explanations guys.
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:14:13 | 显示全部楼层
 
I'm looking to create a similar lisp to the above which inserts a pre-defined mtext string at a given coordinates to use within a separate lisp that I a developing. The mtext will be zero width, text height 2, justified ml & a rotation of zero. This is what I have so far, but I'm stuck :-(
 
  1. (defun c:insmtx ( / a b pt width words )(Setq width 0) (setq pt (getpoint " 0.0 ")) (setq a "Bacon &") (setq b "Eggs")(setq words (strcat "A \n" "B")) (command "mtext" pt "h" "2" "j" "ml" "R" "0" "S" "STANDARD" "W" width pause "" "" words ""))
 
Btw I don't really want to insert 'bacon & eggs' into my drawing
- Humble Harry
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:17:54 | 显示全部楼层
A couple of points to note:
 


  • Use a conditional expression (if / cond) to test whether the getpoint expression returns a valid point, else, if the user presses Enter or right-clicks at the prompt, getpoint will return nil but the program will still continue (and will likely error).

 


  • The expression (strcat "A \n" "B") is concatenating two literal strings, and will simply return the string "A \nB". If you are looking to concatenate the two variables defined earlier in the code, these will need to appear outside of any quotation marks, i.e. (strcat a " \n" b)

 


  • Some AutoCAD commands can be reliably called from AutoLISP (and sometimes there is no other option), however, certain commands can have unpredictable command prompts (such as the MLEADER command), and so I would suggest using entmake/entmakex or a Visual LISP method where possible.

 
Here are my suggestions for your code:
  1. ([color=BLUE]defun[/color] c:insmtx ( [color=BLUE]/[/color] ins )   [color=GREEN];; Define function, declare local variables[/color]   ([color=BLUE]if[/color] [color=GREEN];; If the following expression returns a non-nil value[/color]       ([color=BLUE]setq[/color] ins [color=GREEN];; Bound the value returned by the following expression to the 'ins' symbol and return this value[/color]           ([color=BLUE]getpoint[/color] [color=MAROON]"\nSpecify insertion point: "[/color]) [color=GREEN];; Prompt the user to specify an insertion point for the MText[/color]       ) [color=GREEN];; end setq[/color]       ([color=BLUE]entmake[/color] [color=GREEN];; Append the following DXF data to the drawing database[/color]           ([color=BLUE]list[/color] [color=GREEN];; Construct a list containing the following DXF data[/color]              '(000 . [color=MAROON]"MTEXT"[/color])          [color=GREEN];; Entity type[/color]              '(100 . [color=MAROON]"AcDbEntity"[/color])     [color=GREEN];; Subclass marker[/color]              '(100 . [color=MAROON]"AcDbMText"[/color])      [color=GREEN];; Subclass marker[/color]               ([color=BLUE]cons[/color] 10 ins)            [color=GREEN];; Insertion point[/color]              '(001 . [color=MAROON]"Bacon & \nEggs"[/color]) [color=GREEN];; Content[/color]              '(040 . 2.0)              [color=GREEN];; Height[/color]              '(071 . 4)                [color=GREEN];; Justification[/color]           ) [color=GREEN];; end list[/color]       ) [color=GREEN];; end entmake[/color]       [color=GREEN];; Else the user did not provide a valid point[/color]       ([color=BLUE]princ[/color] [color=MAROON]"\nNo insertion point given, goodbye."[/color])   ) [color=GREEN];; end if[/color]   ([color=BLUE]princ[/color]) [color=GREEN];; Suppress the output of the value returned by the last evaluated expression[/color]) [color=GREEN];; end defun[/color]
Here is a DXF reference to help you to understand the meaning of each DXF group; and here is the reference for the MTEXT entity type.
 
There are other improvements which could be implemented to account for changes to the UCS, the above should at least provide a good starting point for a beginner.
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:22:24 | 显示全部楼层
Wow, I never thought it would have to be so complicated. Thank you very much Lee.
 
Please can you tell me how the resultant of a polar function – within the main lisp I am writing – could be incorporated into this code?
 
Say the resultant is 5,5 My guesstimate is that it would go in the getpoint statement.
Obviously the coordinate would be in a variable
Variable = myvar = 5,5
 
i.e
  1. (getpoint myvar)
 
By the way, is your name actually an acronym for ‘Lisps Excellent Examples Made Always Cleverly’? :-)
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:23:44 | 显示全部楼层
 
It doesn't have to be, but then it also doesn't have to be robust... the above may initially look daunting, but is relatively simple when studied.
 
 
You're most welcome.
 
 
The getpoint function is used to prompt the user to specify a point; this function will then return the given point, or nil if the user presses enter or right-clicks at the prompt.
 
If you already have the insertion point (i.e. a list of two or three numerical values), you needn't use the getpoint function (as there is no need to prompt the user), but instead you can simply supply the point as the DXF group 10 value within the list supplied to entmake.
 
The optional point argument for getpoint will cause a 'rubber-band' to be displayed between the cursor and the given point (as a visual aid for the user when specifying a point which relates to another point).
 
 
Haha! Very good
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:27:39 | 显示全部楼层
Thank you very much indeed Lee, you've helped me learn at lot
 
I've sussed it I think. I created a polar function to vaguely mimic the one(s) in the main code
 
  1. (setq ins (polar '(1 1) 0.785398 1.414214))
 
Which gives 2.0 2.0
 
Then put in:
 
  1. (cons 10 ins)
 
voila! The text is at 2,2
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:29:52 | 显示全部楼层
You're welcome Harry.
 
Of course, if the insertion point is a fixed coordinate, the DXF group 10 can be represented as a literal expression, e.g.:
  1. '(10 2.0 2.0 0.0)
(More information about this here)
 
Alternatively, you can calculate the new position more cleanly using mapcar, e.g.:
  1. (cons 10 (mapcar '+ '(1.0 1.0) '(1.0 1.0)))
Of course, given that all data is known in the above example, the above expression is redundant, but I assume that you are obtaining the initial point through other means.
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:34:49 | 显示全部楼层
Sorry about the delay in replying, I've been away for a few days.
 
Unfortunately the coordinates aren’t fixed but vary according to where the entity being draw is placed.
 
I’ve got around that by referencing a part of the new entity (a) then offsetting the mtext with polar:
 
  1. (setq a-ins (polar a pi 1))
 
Then using ‘a-ins’ for the insertion point for the mtext
 
It works although it’s a little scruffy
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:38:21 | 显示全部楼层
 
There are many ways to achieve the same result in AutoLISP, and that's not too scruffy at all!
 
If you are using the a-ins variable only once, you could omit it entirely and pass the result of the polar expression directly to the cons function, e.g.:
  1.         (entmake ;; Append the following DXF data to the drawing database           (list ;; Construct a list containing the following DXF data              '(000 . "MTEXT")          ;; Entity type              '(100 . "AcDbEntity")     ;; Subclass marker              '(100 . "AcDbMText")      ;; Subclass marker               (cons 10 (polar a pi 1)) ;; Insertion point              '(001 . "Bacon & \nEggs") ;; Content              '(040 . 2.0)              ;; Height              '(071 . 4)                ;; Justification           ) ;; end list       ) ;; end entmake
 
Other ways to calculate the new position could be:
  1. (cons (1- (car a)) (cdr a))
  1. (mapcar '- a '(1 0 0))
But polar is likely to be the cleanest in this case.
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:40:57 | 显示全部楼层
I like the following:
 
  1. (cons 10 (polar a pi 1)) ;; Insertion point
 
I must admit I hadn't thought of that
 
It is only used once, so I'll try that.
 
Although I'm intrigued by the other two options, I'll have to look them up to understand them.
 
car = beginning of list (what list?)
 
cdr = end of list (what list?)
 
As for mapcar, forget it
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-12 13:41 , Processed in 0.481797 second(s), 70 queries .

© 2020-2025 乐筑天下

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