Grrr 发表于 2022-7-5 17:38:45

在每个项目前添加“\P”

大家好,
今天,我想出了一个想法,在将多行文字插入绘图之前“创建”多行文字。
我对单行多行文字使用类似的lisp(仅键入并拾取插入点),因为我讨厌先定义多行文字框,然后键入的标准命令。
 
然而,这一点不同,因为它允许用户键入每一行,并在完成后插入它。构建了一个列表,其中每个项目代表一个新行,但我对每个项目前面的“\P”添加有问题,我无法生成“多行”多行文字:
; Attempt to type MTEXT:
; Construct list of strings, when done add "\P" between items, then entmake mtext with content of that list
; In user-speaking language: type rows for the mtext until done, then insert the mtext

(defun c:test ( / ans mtxt-list pt1)

(setq cont T)
(setq mtxt-list (list))
(initget "Finish")

; how to loop to type mtexts and pick points? (loop the loop)
(while cont ; loop to type rows
        (setq ans (getstring t "\nType a row for mtext or : ")) ; ask for content of the row
        (cond
                ( (or (and (= ans "Finish") mtxt-list ) (and (= ans "FINISH") mtxt-list ) (and (= ans "F") mtxt-list ) (and (= ans "f") mtxt-list ) ) ; check if the list is not nil
                        (setq cont F) ; stop loop for typing rows
                        (setq pt1 (getpoint "\nSpecify insertion point for the mtext:"))
                        (M-Text pt1 (substr (vl-princ-to-string (reverse mtxt-list)) 2 (- (strlen (vl-princ-to-string mtxt-list)) 2)) ) ; removed first and last syntax
                )
                (t
                        (setq mtxt-list (cons (strcat (substr "x\P" 2) ans) mtxt-list)) ; how to add "\P" infornt each item?
                        (print (reverse mtxt-list))(princ (strcat ", " (itoa (length mtxt-list)) " rows."))
                ); t
        );cond
);while

(princ)
);defun       

; LM
(defun M-Text (pt str)
(entmakex
        (list
                (cons 0 "MTEXT")         
                (cons 100 "AcDbEntity")
                (cons 100 "AcDbMText")
                (cons 10 pt)
                (cons 1 str)
                (cons 71 5)
        )
)
)
我遇到的下一个(不是那么大)问题是,我正在尝试循环整个过程,因此在输入多行文字后,用户会提示输入一行新的多行文字。(以此类推..键入多行文字行、拾取点..以此类推)。
 
我已经对部分代码进行了注释,以便您更容易理解我在那里做的事情。提前感谢!

Tharwat 发表于 2022-7-5 17:50:12

有一些想法需要思考。
 

(defun c:test (/ go lst ans p)
(setq go t lst "")
(while (and go (setq ans (getstring t "\nType a row for mtext or :")))
   (cond ((and (/= ans "") (eq (type ans) 'str))
          (setq lst (strcat lst ans "\\P"))
          )
         ((and (eq ans "")
               (/= lst "")
               (setq p (getpoint "\nSpecify insertion point for the mtext:"))
               )
          (entmakex (list '(0 . "MTEXT")
                        '(100 . "AcDbEntity")
                        '(100 . "AcDbMText")
                  (cons 10 (trans p 1 0))
                  (cons 1 lst)
                  '(71 . 5)
                  ))
          (setq go nil))
         ((= ans "") (setq go nil))
         )
   )
(princ)
)

Grrr 发表于 2022-7-5 17:52:06

我做了一个快速的尝试,似乎奏效了,稍后我会更仔细地分析它,并尝试找出如何循环这个循环。
乍一看,我看到您将substr和vl princ跳过为字符串函数,因此将为我提供新信息。
再次感谢你,塔瓦!

Tharwat 发表于 2022-7-5 18:02:30

祝你好运

Grrr 发表于 2022-7-5 18:05:19

就这么做了,把整个事情循环起来:
 
(defun c:test (/ go2 go lst ans p)

(setq go2 T)

(while go2
        (setq go T)
        (setq lst "")
        (while (and go (setq ans (getstring t "\nType a row for mtext or :")))
                (cond
                        ( (and (/= ans "") (eq (type ans) 'str))
                                (setq lst (strcat lst ans "\\P"))
                        )
                       
                        ( (and (eq ans "") (/= lst "") (setq p (getpoint "\nSpecify insertion point for the mtext:")))
                                (if (and
                                        (entmakex
                                                (list
                                                        '(0 . "MTEXT")
                                                        '(100 . "AcDbEntity")
                                                        '(100 . "AcDbMText")
                                                        (cons 10 (trans p 1 0))
                                                        (cons 1 lst)
                                                        '(71 . 5)
                                                )
                                        )
                                        (setq go nil)
                                )
                                (setq go2 T)
                                )
                        )
                        ( (= ans "")
                                (setq go nil)
                                (setq go2 F)
                        )
                ); cond
        ); sub-while
); main while

(princ)
);defun


可能存在一些不合逻辑的情况,但一切正常。
令我印象深刻的是,你竟然想到使用假列表,在用“\\P”分隔输入的同时构造一个很长的字符串!

BIGAL 发表于 2022-7-5 18:11:09

Grr有一个帖子,添加颜色,字体风格等李和我张贴可能会有用的未来。
 
找到了http://www.cadtutor.net/forum/showthread.php?92585-帮助仅更改多行文字中数字的颜色&高亮显示=多行文字

Grrr 发表于 2022-7-5 18:20:39

这是一条有趣的线索,比格尔
我通常不会格式化我的文本/多行文字对象,但我会看看是否可以从这些代码中学习到新的东西。
发布在那里的解决方案(代码)似乎变得越来越复杂,并且总是有这些额外的修改请求(供个人使用)。

Tharwat 发表于 2022-7-5 18:24:39

由于其中一个表达式等于nil,上述and函数将不会返回true。
 
 
我认为将变量设置为nil比将其设置为null符号更好。

Grrr 发表于 2022-7-5 18:30:39

根据Tharwats的评论,对代码进行了更正:

(defun c:test (/ go2 go lst ans p)

(setq go2 T)

(while go2
        (setq go T)
        (setq lst "")
        (while (and go (setq ans (getstring t "\nType a row for mtext or :")))
                (cond
                        ( (and (/= ans "") (eq (type ans) 'str))
                                (setq lst (strcat lst ans "\\P"))
                        )
                       
                        ( (and (eq ans "") (/= lst "") (setq p (getpoint "\nSpecify insertion point for the mtext:")))
                                (if
                                        (entmakex
                                                (list
                                                        '(0 . "MTEXT")
                                                        '(100 . "AcDbEntity")
                                                        '(100 . "AcDbMText")
                                                        (cons 10 (trans p 1 0))
                                                        (cons 1 lst)
                                                        '(71 . 5)
                                                )
                                        )
                                        (progn
                                                (setq go nil)
                                                (setq go2 T)
                                        )
                                )
                        )
                        ( (= ans "")
                                (setq go nil)
                                (setq go2 nil)
                        )
                ); cond
        ); sub-while
); main while

(princ)
);defun

Grrr 发表于 2022-7-5 18:41:18

 
很好的发现!
我不习惯这种编写代码的方式(像progn一样编写setq函数),不知道为什么会这样-也许我自己更容易跟踪(如果代码有问题)。
页: [1]
查看完整版本: 在每个项目前添加“\P”