乐筑天下

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

[编程交流] 将表格文本转换为CSV(&O)

[复制链接]

2

主题

10

帖子

8

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 14:22:41 | 显示全部楼层 |阅读模式
您好,我是autocad和lisp新手。我已经在网上搜索了2周,试图找到代码来修改我正在使用的lisp,但运气不好。
 
我修改了原始的BOMtoCSV。lisp代码可以完成我需要的几乎所有工作。我试图从图形中的表中提取文本,用逗号分隔字段。然后将其他两个文本字段添加到同一行。
 
例:COL1 COL2 COL3
第1行:XXX XXX XXX
 
文本被转储为XXX,XXX,XXX,但我还想添加另外两个字段,我认为需要添加为坐标。也用逗号分隔,并为表的每一行复制。
 
例如(ssget“_W”(列表16.8 10.3)(列表18.9 5.0)((0。“TEXT”)))
 
我通常不会发帖,因为我知道新手的请求有多烦人,但我一直找不到在编写之前修改提取文本布局的方法。
 
如果可能的话,我们将不胜感激。谢谢
 
 
我在下面添加了代码
回复

使用道具 举报

2

主题

10

帖子

8

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:20:56 | 显示全部楼层
  1. ;; BOM to CSV; A program to create a csv file from a text based table in AutoCAD
  2. ;; Author: Doug Barnes
  3. ;; Company: Grantek Systems Integration
  4. (defun Cleanup_Text ()
  5. ;; find all the text with no content and erase them
  6. (setq nil_text
  7. (ssget "x"
  8. '((-4 . "<and") (0 . "TEXT") (1 . "") (-4 . "and>"))
  9. )
  10. )
  11. ;; search for text with no content
  12. (if nil_text
  13. (command "erase" nil_text "")
  14. )
  15. ;; erase text with no content
  16. )
  17. ;; end cleanup_text defun
  18. ;;==========================================================================================
  19. (defun Gather_Text (/ sel2 ct)
  20. (setq Text_SS (ssget "_W" (list 16.8 10.3)(list 18.9 5.0) '((0 . "TEXT"))))
  21. (setq sel2 (ssget "_W" (list 16.8 16.6)(list 25.8 11.0) '((0 . "TEXT"))))
  22. (setq ct 0)
  23. ;set counter to zero
  24. (repeat (sslength sel2)
  25. ;get the number of items in selection set 2
  26. ;and loop that number of times
  27. (ssadd (ssname sel2 ct) Text_SS)
  28. ;get the name of the entity from selection set 2
  29. ;by using the counter index number and add it to
  30. ;selection set 1
  31. (setq ct (1+ ct))
  32. ;increment the counter by 1
  33. );end repeat
  34. )
  35. ;; create a selection set for all text selected by the user.
  36. ;; end Gather_Text defun
  37. ;;==========================================================================================
  38. (defun MakeList_X_Y_Text (Text_SS)
  39. ;; Make a list of x-coords, y-coords and text
  40. (setq MakeList_cnt 0)
  41. (repeat (sslength Text_SS)
  42. (setq X_cord
  43. (if (= (cdr (assoc 72 (entget (ssname Text_SS MakeList_cnt))))
  44. 0
  45. )
  46. ;; check for the text's justification
  47. (cadr (assoc 10 (entget (ssname Text_SS MakeList_cnt))))
  48. ;; if left justified use first alignment point
  49. (cadr (assoc 11 (entget (ssname Text_SS MakeList_cnt))))
  50. ;; else use second alignment point
  51. )
  52. ;;end if
  53. )
  54. ;;end Setq
  55. (setq y_cord
  56. (if (= (cdr (assoc 72 (entget (ssname Text_SS MakeList_cnt))))
  57. 0
  58. )
  59. ;; check for the text's justification
  60. (caddr (assoc 10 (entget (ssname Text_SS MakeList_cnt))))
  61. ;; if left justified use first alignment point
  62. (caddr (assoc 11 (entget (ssname Text_SS MakeList_cnt))))
  63. ;; else use second alignment point
  64. )
  65. ;;end if
  66. )
  67. (setq List_X_Y_Text
  68. (append
  69. List_X_Y_Text
  70. (list
  71. (list
  72. (cons "X coord"
  73. X_cord
  74. )
  75. (cons "Y coord" y_cord)
  76. (cons "Text"
  77. (cdr (assoc 1 (entget (ssname Text_SS MakeList_cnt))))
  78. )
  79. )
  80. )
  81. )
  82. )
  83. ;;make assocation list of x, y and text dotted pairs
  84. (setq MakeList_cnt (1+ MakeList_cnt))
  85. ;; index to next entity
  86. )
  87. ;;repeat
  88. )
  89. ;;end MakeList_X_Y_Text defun
  90. ;;==========================================================================================
  91. (defun Sort_by_X (List_X_Y_Text)
  92. ;; re-order list from left to right
  93. (setq X_List
  94. (vl-sort
  95. List_X_Y_Text
  96. (FUNCTION (LAMBDA (E1 E2) (< (cdar E1) (cdar E2))))
  97. )
  98. )
  99. )
  100. ;; end Sort_by_X defun
  101. ;;==========================================================================================
  102. (defun Sort_by_Y (List_X_Y_Text_Col)
  103. ;; re-order list from top to bottom
  104. (setq Y_List
  105. (vl-sort
  106. List_X_Y_Text_Col
  107. (FUNCTION (LAMBDA (E1 E2) (> (cdadr E1) (cdadr E2))))
  108. )
  109. )
  110. )
  111. ;; end Sort_by_Y defun
  112. ;;==========================================================================================
  113. (defun Get_Columns (X_List)
  114. ;; determine the number of columns text
  115. (setq Column_Qty 1
  116. X_List_cnt 0
  117. )
  118. (repeat (length X_List)
  119. ;; add a column number to List_X_Y_Text
  120. (setq List_X_Y_Text_Col
  121. (append List_X_Y_Text_Col
  122. (list (list
  123. (assoc "X coord" (nth X_List_cnt X_List))
  124. (assoc "Y coord" (nth X_List_cnt X_List))
  125. (assoc "Text" (nth X_List_cnt X_List))
  126. (cons "Column" Column_Qty)
  127. )
  128. )
  129. )
  130. )
  131. (if (/= (nth (+ X_List_cnt 1) X_List) nil)
  132. ;; Check for end of list
  133. (progn
  134. (if
  135. (>=
  136. (- (cdr (car (nth (+ X_List_cnt 1) X_List))) Line_Tolerance)
  137. (cdr (car (nth X_List_cnt X_List)))
  138. )
  139. ;; if the X Coord of the next text is out of tolerance with the current text then increase the column count
  140. (setq Column_Qty (1+ Column_Qty))
  141. )
  142. )
  143. )
  144. (setq X_List_cnt (1+ X_List_cnt))
  145. ;; index list position
  146. )
  147. ;; end repeat
  148. )
  149. ;; end Get_Columns defun
  150. ;;==========================================================================================
  151. (defun Get_Rows (Y_List)
  152. ;; determine the number of rows of text
  153. (setq Row_Qty 1
  154. Y_List_cnt
  155. 0
  156. )
  157. (repeat (length Y_List)
  158. ;; add a row number to List_X_Y_Text_Col
  159. (setq List_All
  160. (append
  161. List_All
  162. (list (list ;; make a list of x, y, text, column and row
  163. (assoc "X coord" (nth Y_List_cnt Y_List))
  164. (assoc "Y coord" (nth Y_List_cnt Y_List))
  165. (assoc "Text" (nth Y_List_cnt Y_List))
  166. (assoc "Column" (nth Y_List_cnt Y_List))
  167. (cons "Row" Row_Qty)
  168. )
  169. )
  170. )
  171. )
  172. (if (/= (nth (+ Y_List_cnt 1) Y_List) nil)
  173. ;; Check for end of list
  174. (progn
  175. (if (<= (cdr (cadr (nth (+ Y_List_cnt 1) Y_List)))
  176. (- (cdr (cadr (nth Y_List_cnt Y_List))) Line_Tolerance)
  177. )
  178. ;; if the Y Coord of the next text is out of tolerance with the current text then increase the row count
  179. (setq Row_Qty (1+ Row_Qty))
  180. )
  181. ;; end if
  182. )
  183. ;;end progn
  184. )
  185. ;; end if
  186. (setq Y_List_cnt (1+ Y_List_cnt))
  187. ;; index list position
  188. )
  189. ;; end repeat
  190. )
  191. ;; end Get_Rows defun
  192. ;;==========================================================================================
  193. (defun Write_BOM_to_CSV ()
  194. (setq Position 0)
  195. (setq Path (getvar "dwgprefix"))
  196. ;; Obtain the file path of the drawing
  197. (setq Dwg_Name (substr (getvar "dwgname")
  198. 1
  199. (- (strlen (getvar "dwgname")) 4)
  200. )
  201. )
  202. ;; remove the .dwg from the file name
  203. (setq BOM_CSV_File (open (strcat Path Dwg_Name " WELD.txt") "a"))
  204. ;; Open a CSV file for ammending
  205. (while (/= (nth Position List_All) nil)
  206. (setq Current_Row (cddr (nth Position List_All)))
  207. ;; setup to gather all of the same row text
  208. (setq Next_Row (cddr (nth (+ Position 1) List_All)))
  209. ;; setup to check end of row
  210. (while (= (cdr (assoc "Row" Current_Row))
  211. (cdr (assoc "Row" Next_Row))
  212. )
  213. ;; loop for all the same row
  214. (setq Text_Column
  215. (append Text_Column
  216. (list (list (cdr (assoc "Text" Current_Row))
  217. (cdr (assoc "Column" Current_Row))
  218. )
  219. )
  220. )
  221. )
  222. ;; create a list of text and column only
  223. (setq Position (1+ Position))
  224. ;; index position in list
  225. (setq Current_Row (cddr (nth Position List_All)))
  226. (setq Next_Row (cddr (nth (+ Position 1) List_All)))
  227. )
  228. ;; end if
  229. (setq Text_Column
  230. (append Text_Column
  231. (list (list (cdr (assoc "Text" Current_Row))
  232. (cdr (assoc "Column" Current_Row))
  233. )
  234. )
  235. )
  236. )
  237. ;; append Text_Column with the last text in the current row
  238. (setq Text_Column
  239. (vl-sort
  240. Text_Column
  241. (FUNCTION (LAMBDA (E1 E2) (< (cadr E1) (cadr E2))))
  242. )
  243. )
  244. ;; Sort list by Column number (left to right)
  245. (setq Column_Qty_Count
  246. 1
  247. Write_Row ""
  248. Column_List_Position
  249. 0
  250. )
  251. (repeat Column_Qty
  252. ;; repeat for the maximum number of columns
  253. (if (= (cadr (nth Column_List_Position Text_Column))
  254. Column_Qty_Count
  255. )
  256. ;; check the position of the current text's postion with the column count
  257. (progn ;; if equal string together the cuurent text and a comma
  258. (setq
  259. Write_Row (strcat
  260. Write_Row
  261. (car (nth Column_List_Position Text_Column))
  262. ","
  263. )
  264. )
  265. (setq Column_List_Position (1+ Column_List_Position))
  266. ;; index position
  267. )
  268. ;; end progn
  269. ;; if not equal string together previous text and a comma
  270. (setq Write_Row (strcat Write_Row ","))
  271. )
  272. ;; end if
  273. (setq Column_Qty_Count (1+ Column_Qty_Count))
  274. ;; index the column count
  275. )
  276. ;; end repeat
  277. ;;(print Write_Row)
  278. (write-line Write_Row BOM_CSV_File)
  279. ;; write the current row to the csv file
  280. (setq Position (1+ Position))
  281. ;; index list position
  282. (setq Text_Column nil)
  283. ;; reset for next row
  284. )
  285. ;; end while
  286. (close BOM_CSV_File)
  287. ;; close the csv file
  288. )
  289. ;; end Write_BOM_to_CSV defun
  290. ;;==========================================================================================
  291. ;; Create a CSV file from a text based Bill of Materials in AutoCAD
  292. (defun c:weld (/ X_List Y_List
  293. Text_Column List_X_Y_Text List_X_Y_Text_Col
  294. List_All
  295. )
  296. (Cleanup_Text)
  297. (Gather_Text)
  298. (MakeList_X_Y_Text Text_SS)
  299. (Sort_by_X List_X_Y_Text)
  300. (setq Text_Height (cdr (assoc 40 (entget (ssname Text_SS 0)))))
  301. ;; get the text height used
  302. (setq Line_Tolerance (+ Text_Height (* Text_Height 0.1)))
  303. ;; set up a line spacing tolerance
  304. (Get_Columns X_List)
  305. (Sort_by_Y List_X_Y_Text_Col)
  306. (Get_Rows Y_List)
  307. (Write_BOM_to_CSV)
  308. )
  309. ;; end defun
回复

使用道具 举报

2

主题

10

帖子

8

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:33:08 | 显示全部楼层
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-5 03:15 , Processed in 0.679608 second(s), 69 queries .

© 2020-2025 乐筑天下

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