乐筑天下

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

[编程交流] 插入带a的多个块

[复制链接]

5

主题

183

帖子

178

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 03:55:08 | 显示全部楼层 |阅读模式
我刚刚重新激发了我对编写Lisp例程的兴趣,因为我需要自动化一些任务。其中之一是根据坐标列表将多个块插入到图形中。我现在唯一的问题是,我似乎无法让它工作,也没有成功地在网上搜索到一个。我发现了几个对lisp很有帮助的网站
 
我的目标是创建一个defun命令,该命令将自身循环,并仅从我使用LDD点导出创建的文本文件中检索坐标。但是由于我的经验不足,而且我刚刚重新开始学习,所以我无法创建这种类型的lisp。从理论上讲,这似乎是一个简单而直接的Lisp,但由于我缺乏知识,到目前为止,它完全失败了。
 
简而言之,我有一个包含200多个坐标的文本文件,我想在其中插入块,我想创建一个lisp例程,为每个插入点引用这个坐标列表。
 
这是我到目前为止的资料(不多)。
 
 
(定义C:ITREE()
 
(命令“Insert”“Tree”“1”“1”“0”)
 
我应该在哪里为这个创建循环,我应该在哪里放置循环
(setq文件(打开“testfile.txt”“r”))部分?看起来像这样吗?
 
(定义C:ITREE()
 
(命令“Insert”“Tree”“1”“1”“0”(setq文件(打开“testfile.txt”“r”))“”)
 
还是我完全在太空的某个地方?
 
斯克里布
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 04:00:58 | 显示全部楼层
不知道这样行不行?
 
  1. (defun c:ITREE (/ *error* file1)
  2.    (defun *error* (msg)
  3.    (setvar "cmdecho" 1)
  4.    (if (= msg "")
  5.        (princ "\nFunction Complete.")
  6.        (princ "\nError or Esc Pressed.")
  7.    ) ;_  end if
  8.    ) ;_  end defun
  9.    (selfile)
  10.    (setvar "cmdecho" 0)
  11.    (setq file1 (open file "r"))
  12.    (while
  13.    (/= (read-line file1) nil)
  14.       (command "-insert"
  15.            "C:\" ;<<< --- INSERT BLOCK FILEPATH HERE
  16.            (read-line file1)
  17.            ""
  18.            ""
  19.            "0"
  20.       ) ;_  end command
  21.    ) ;_  end while
  22.    (*error* "")
  23.    (princ)
  24. ) ;_  end defun
  25. (defun selfile ()
  26.    (setq file (getfiled "Select a Text File"
  27.             "C:/"
  28.             "csv"
  29.             9
  30.           ) ;_  end getfiled
  31.    ) ;_  end setq
  32. ) ;_  end defun

 
第二个例子是逗号删除
 
  1. (defun c:ITREE (/ file1)
  2.    (defun *error* (msg) ; define error handler
  3.    (setvar "cmdecho" 1) ; if an error set the command echo back to 1
  4.    (if (= msg "") ; if the msg argument with the error is ""
  5.        (princ "\nFunction Complete.") ; then print Function Complete
  6.        (princ "\nError or Esc Pressed.") ; otherwise print Error...
  7.    ) ;_  end if
  8.    ) ;_  end defun
  9.    (selfile) ; invoke selfile
  10.    (setvar "cmdecho" 0) ; turn off the command echo (for when we use the insert)
  11.    (setq file1 (open file "r")) ; open the selected file for reading
  12.    (while ; as it says on the tin
  13.    (/= (read-line file1) nil) ; up until the readline cannot read any more lines
  14.       (command "-insert" ; invoke insert
  15.            "C:\" ;<<< --- INSERT BLOCK FILEPATH HERE
  16.            (read-line file1) ; read a line of the file selected
  17.            "" ; x scale factor 1
  18.            "" ; y scale factor 1
  19.            "0" ; no rotation
  20.       ) ;_  end command
  21.    ) ;_  end while
  22.    (*error* "") ; < call error handler with msg = "", to reset cmdecho
  23.    (princ) ; exit cleanly
  24. ) ;_  end defun
  25. (defun selfile ()
  26.    (setq file (getfiled "Select a Text File" ; <<-- Title of Dialog Box
  27.             "C:/" ; Default Path to display files
  28.             "txt" ; file extension to look for (was on csv - sorry)
  29.             9 ; bit argument --> 8 + 1 --> look in ACAD help for meanings, it'll explain better
  30.           ) ;_  end getfiled
  31.    ) ;_  end setq
  32. ) ;_  end defun
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 04:03:36 | 显示全部楼层
回复

使用道具 举报

5

主题

183

帖子

178

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 04:05:32 | 显示全部楼层
Wow! This is way more than I would have imagined it would be to such (seemly) simple process.
 
Even though I have trouble creating lisp from scratch I can read them and understand what they can do. Most of the time. This one looks like it will work Lee. I appreciate your time. I will try this out as soon as I get to work in the morning. I could VPN but I'm too damn tired at the moment.
 
I really look forward to learning how to get to this point in writing lisp myself.
 
Thanks again Lee. BIGAL what you said makes perfect since to me after reading what Lee Mac added.
 
BTW how do you put the code in a neat frame like that? I do understand HTML but I have not gotten this complex with it.
 
Thanks
Scribs
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 04:08:09 | 显示全部楼层
Hi Scribble,
 
Thanks for your message, just wrap your code in [ code] [/ code] tags (without the spaces), to put it in the frame. Or, alternatively, select all your code and click on the # button on the post text editor.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 04:14:03 | 显示全部楼层
 
It could probably be made simpler by using a default text file (iradicating the need for the separate (defun selfile) program. Also, I have added an error handler which makes it look more complicated than it is.
 
In writing it, I wasn't sure if you were after a LISP to insert a single block numerous times, or lots of different blocks at lots of different coordinates - if it was the latter, obviously the LISP will need to be altered somehwhat.
 
Also, one more thing, I have left the filepath for the block blank, but remember (and you probably know this already) to use a double backslash \\ to indicate the single backslash \.
 
i.e.
 
C:\\Users\\Lee Mac\\Documents\\block.dwg
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 04:14:30 | 显示全部楼层
EDIT: posted LISP updated - forgot to localise the *error*
回复

使用道具 举报

5

主题

183

帖子

178

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 04:18:59 | 显示全部楼层
 
Yeah I noticed that. I'm glad you added that because now I know how the syntax looks.
 
The former is correct. I just wanted to be able to place one tree block on each coordinate so we can show which trees would need to be cut down. For instance a block of a tree with an X through it to denote demolition. 
Actually I did not know this. Thanks for the heads-up. 
Does the format of the file with the coordinates matter? From what I can tell it will except the file format of the coordinates as the command expects them to see them. (i.e. x,y or y,x) Does this read the entire set of coordinates all at once and store them in memory or does it read them one line at a time? If the latter how does it know to move onto the next line after it places the block?
 
If you don't mind would you comment on each line of code starting at the (selfile) so that I can wrap my head around it and turn this into a greater learning process? Some of it I understand but a few lines I do not because I am not familiar with some of the operators and arguments.
 
Thanks
Scribs
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 04:21:08 | 显示全部楼层
Ok, first I will admit - I have never used this method for inserting a block into a drawing and haven't actually tested my posted LISP yet either.
 
But I read something about the read-line command earlier that day and thought I'd try and use it.
 
But I'll help as much as I can.
 
The read-line syntax will read the file line by line until it returns nil - as I say I have not tried to use it before, so I am not sure if I have even used it correctly. But I would say that the co-ordinates need to be in the form that the command would expect them.
 

[code](defun c:ITREE (/ file1)   (defun *error* (msg) ; define error handler   (setvar "cmdecho" 1) ; if an error set the command echo back to 1   (if (= msg "") ; if the msg argument with the error is ""       (princ "\nFunction Complete.") ; then print Function Complete       (princ "\nError or Esc Pressed.") ; otherwise print Error...   ) ;_  end if   ) ;_  end defun   (selfile) ; invoke selfile   (setvar "cmdecho" 0) ; turn off the command echo (for when we use the insert)   (setq file1 (open file "r")) ; open the selected file for reading   (while ; as it says on the tin   (/= (read-line file1) nil) ; up until the readline cannot read any more lines      (command "-insert" ; invoke insert           "C:\\" ;
回复

使用道具 举报

5

主题

183

帖子

178

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 04:26:13 | 显示全部楼层
Thanks Lee. I am going to give it a test run at lunch time. I will post the results here for feedback.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-10 18:27 , Processed in 1.234366 second(s), 72 queries .

© 2020-2025 乐筑天下

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