Advertisement
tarekfarrag

AT AF

Nov 12th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.83 KB | None | 0 0
  1. ;;---------------------=={ Area Label }==---------------------;;
  2. ;; ;;
  3. ;; Allows the user to label picked areas or objects and ;;
  4. ;; either display the area in an ACAD Table (if available), ;;
  5. ;; optionally using fields to link area numbers and objects; ;;
  6. ;; or write it to file. ;;
  7. ;;------------------------------------------------------------;;
  8. ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
  9. ;;------------------------------------------------------------;;
  10. ;; Version 1.9 - 29-10-2011 ;;
  11. ;;------------------------------------------------------------;;
  12.  
  13. (defun c:AT nil (AreaLabel t)) ;; Areas to Table
  14. (defun c:AF nil (AreaLabel nil)) ;; Areas to File
  15.  
  16. ;;------------------------------------------------------------;;
  17.  
  18. (defun AreaLabel ( flag / *error* _startundo _endundo _centroid _text _open _select _getobjectid _isannotative
  19. acdoc acspc ap ar as cf cm el fd fl fo n of om p1 pf pt sf st t1 t2 tb th ts tx ucsxang ucszdir )
  20.  
  21. ;;------------------------------------------------------------;;
  22. ;; Adjustments ;;
  23. ;;------------------------------------------------------------;;
  24.  
  25. (setq h1 "Area Table" ;; Heading
  26. t1 "Number" ;; Number Title
  27. t2 "Area" ;; Area Title
  28. pf "" ;; Number Prefix (optional, "" if none)
  29. sf "" ;; Number Suffix (optional, "" if none)
  30. ap "" ;; Area Prefix (optional, "" if none)
  31. as "" ;; Area Suffix (optional, "" if none)
  32. cf 1.0 ;; Area Conversion Factor (e.g. 1e-6 = mm2->m2)
  33. fd t ;; Use fields to link numbers/objects to table (t=yes, nil=no)
  34. fo "%lu6%qf1" ;; Area field formatting
  35. )
  36.  
  37. ;;------------------------------------------------------------;;
  38.  
  39. (defun *error* ( msg )
  40. (if cm (setvar 'CMDECHO cm))
  41. (if el (progn (entdel el) (setq el nil)))
  42. (if acdoc (_EndUndo acdoc))
  43. (if (and of (eq 'FILE (type of))) (close of))
  44. (if (and Shell (not (vlax-object-released-p Shell))) (vlax-release-object Shell))
  45. (if (null (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
  46. (princ (strcat "\n--> Error: " msg))
  47. )
  48. (princ)
  49. )
  50.  
  51. ;;------------------------------------------------------------;;
  52.  
  53. (defun _StartUndo ( doc ) (_EndUndo doc)
  54. (vla-StartUndoMark doc)
  55. )
  56.  
  57. ;;------------------------------------------------------------;;
  58.  
  59. (defun _EndUndo ( doc )
  60. (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  61. (vla-EndUndoMark doc)
  62. )
  63. )
  64.  
  65. ;;------------------------------------------------------------;;
  66.  
  67. (defun _centroid ( space objs / reg cen )
  68. (setq reg (car (vlax-invoke space 'addregion objs))
  69. cen (vlax-get reg 'centroid)
  70. )
  71. (vla-delete reg) (trans cen 1 0)
  72. )
  73.  
  74. ;;------------------------------------------------------------;;
  75.  
  76. (defun _text ( space point string height rotation / text )
  77. (setq text (vla-addtext space string (vlax-3D-point point) height))
  78. (vla-put-alignment text acalignmentmiddlecenter)
  79. (vla-put-textalignmentpoint text (vlax-3D-point point))
  80. (vla-put-rotation text rotation)
  81. text
  82. )
  83.  
  84. ;;------------------------------------------------------------;;
  85.  
  86. (defun _Open ( target / Shell result )
  87. (if (setq Shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
  88. (progn
  89. (setq result
  90. (and (or (eq 'INT (type target)) (setq target (findfile target)))
  91. (not
  92. (vl-catch-all-error-p
  93. (vl-catch-all-apply 'vlax-invoke (list Shell 'Open target))
  94. )
  95. )
  96. )
  97. )
  98. (vlax-release-object Shell)
  99. )
  100. )
  101. result
  102. )
  103.  
  104. ;;------------------------------------------------------------;;
  105.  
  106. (defun _Select ( msg pred func init / e ) (setq pred (eval pred))
  107. (while
  108. (progn (setvar 'ERRNO 0) (apply 'initget init) (setq e (func msg))
  109. (cond
  110. ( (= 7 (getvar 'ERRNO))
  111. (princ "\nMissed, try again.")
  112. )
  113. ( (eq 'STR (type e))
  114. nil
  115. )
  116. ( (vl-consp e)
  117. (if (and pred (not (pred (setq e (car e)))))
  118. (princ "\nInvalid Object Selected.")
  119. )
  120. )
  121. )
  122. )
  123. )
  124. e
  125. )
  126.  
  127. ;;------------------------------------------------------------;;
  128.  
  129. (defun _GetObjectID ( doc obj )
  130. (if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
  131. (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)
  132. (itoa (vla-get-Objectid obj))
  133. )
  134. )
  135.  
  136. ;;------------------------------------------------------------;;
  137.  
  138. (defun _isAnnotative ( style / object annotx )
  139. (and
  140. (setq object (tblobjname "STYLE" style))
  141. (setq annotx (cadr (assoc -3 (entget object '("AcadAnnotative")))))
  142. (= 1 (cdr (assoc 1070 (reverse annotx))))
  143. )
  144. )
  145.  
  146. ;;------------------------------------------------------------;;
  147.  
  148. (setq acdoc (vla-get-activedocument (vlax-get-acad-object))
  149. acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
  150.  
  151. ucszdir (trans '(0. 0. 1.) 1 0 t)
  152. ucsxang (angle '(0. 0. 0.) (trans (getvar 'UCSXDIR) 0 ucszdir))
  153. )
  154. (_StartUndo acdoc)
  155. (setq cm (getvar 'CMDECHO))
  156. (setvar 'CMDECHO 0)
  157. (setq om (eq "1" (cond ((getenv "LMAC_AreaLabel")) ((setenv "LMAC_AreaLabel" "0")))))
  158.  
  159. (setq ts
  160. (/ (getvar 'TEXTSIZE)
  161. (if (_isAnnotative (getvar 'TEXTSTYLE))
  162. (cond ( (getvar 'CANNOSCALEVALUE) ) ( 1.0 )) 1.0
  163. )
  164. )
  165. )
  166.  
  167. (cond
  168. ( (not (vlax-method-applicable-p acspc 'addtable))
  169.  
  170. (princ "\n--> Table Objects not Available in this Version.")
  171. )
  172. ( (= 4 (logand 4 (cdr (assoc 70 (tblsearch "LAYER" (getvar 'CLAYER))))))
  173.  
  174. (princ "\n--> Current Layer Locked.")
  175. )
  176. ( (not
  177. (setq *al:num
  178. (cond
  179. (
  180. (getint
  181. (strcat "\nSpecify Starting Number <"
  182. (itoa (setq *al:num (1+ (cond ( *al:num ) ( 0 ))))) ">: "
  183. )
  184. )
  185. )
  186. ( *al:num )
  187. )
  188. )
  189. )
  190. )
  191. ( flag
  192.  
  193. (setq th
  194. (* 2.
  195. (if
  196. (zerop
  197. (setq th
  198. (vla-gettextheight
  199. (setq st
  200. (vla-item
  201. (vla-item
  202. (vla-get-dictionaries acdoc) "ACAD_TABLESTYLE"
  203. )
  204. (getvar 'CTABLESTYLE)
  205. )
  206. )
  207. acdatarow
  208. )
  209. )
  210. )
  211. ts
  212. (/ th
  213. (if (_isAnnotative (vla-gettextstyle st acdatarow))
  214. (cond ( (getvar 'CANNOSCALEVALUE) ) ( 1.0 )) 1.0
  215. )
  216. )
  217. )
  218. )
  219. )
  220.  
  221. (if
  222. (cond
  223. (
  224. (progn (initget "Add")
  225. (vl-consp (setq pt (getpoint "\nPick Point for Table <Add to Existing>: ")))
  226. )
  227. (setq tb
  228. (vla-addtable acspc
  229. (vlax-3D-point (trans pt 1 0)) 2 2 th (* 0.8 th (max (strlen t1) (strlen t2)))
  230. )
  231. )
  232. (vla-put-direction tb (vlax-3D-point (getvar 'UCSXDIR)))
  233. (vla-settext tb 0 0 h1)
  234. (vla-settext tb 1 0 t1)
  235. (vla-settext tb 1 1 t2)
  236.  
  237. (while
  238. (progn
  239. (if om
  240. (setq p1
  241. (_Select (strcat "\nSelect Object [Pick] <Exit>: ")
  242. '(lambda ( x )
  243. (and
  244. (vlax-property-available-p (vlax-ename->vla-object x) 'area)
  245. (not (eq "HATCH" (cdr (assoc 0 (entget x)))))
  246. (or (eq "REGION" (cdr (assoc 0 (entget x)))) (vlax-curve-isclosed x))
  247. )
  248. )
  249. entsel '("Pick")
  250. )
  251. )
  252. (progn (initget "Object") (setq p1 (getpoint "\nPick Area [Object] <Exit>: ")))
  253. )
  254. (cond
  255. ( (null p1)
  256.  
  257. (vla-delete tb)
  258. )
  259. ( (eq "Pick" p1)
  260.  
  261. (setq om nil) t
  262. )
  263. ( (eq "Object" p1)
  264.  
  265. (setq om t)
  266. )
  267. ( (eq 'ENAME (type p1))
  268.  
  269. (setq tx
  270. (cons
  271. (_text acspc
  272. (_centroid acspc (list (setq p1 (vlax-ename->vla-object p1))))
  273. (strcat pf (itoa *al:num) sf)
  274. ts
  275. ucsxang
  276. )
  277. tx
  278. )
  279. )
  280. (vla-insertrows tb (setq n 2) th 1)
  281. (vla-settext tb n 1
  282. (if fd
  283. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  284. (_GetObjectID acdoc p1) ">%).Area \\f \"" fo "\">%"
  285. )
  286. (strcat ap (rtos (* cf (vla-get-area p1)) 2) as)
  287. )
  288. )
  289. (vla-settext tb n 0
  290. (if fd
  291. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  292. (_GetObjectID acdoc (car tx)) ">%).TextString>%"
  293. )
  294. (strcat pf (itoa *al:num) sf)
  295. )
  296. )
  297. nil
  298. )
  299. ( (vl-consp p1)
  300.  
  301. (setq el (entlast))
  302. (vl-cmdf "_.-boundary" "_A" "_I" "_N" "" "_O" "_P" "" "_non" p1 "")
  303.  
  304. (if (not (equal el (setq el (entlast))))
  305. (progn
  306. (setq tx
  307. (cons
  308. (_text acspc
  309. (_centroid acspc (list (vlax-ename->vla-object el)))
  310. (strcat pf (itoa *al:num) sf)
  311. ts
  312. ucsxang
  313. )
  314. tx
  315. )
  316. )
  317. (vla-insertrows tb (setq n 2) th 1)
  318. (vla-settext tb n 1 (strcat ap (rtos (* cf (vlax-curve-getarea el)) 2) as))
  319. (vla-settext tb n 0
  320. (if fd
  321. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  322. (_GetObjectID acdoc (car tx)) ">%).TextString>%"
  323. )
  324. (strcat pf (itoa *al:num) sf)
  325. )
  326. )
  327. (redraw el 3)
  328. nil
  329. )
  330. (vla-delete tb)
  331. )
  332. )
  333. )
  334. )
  335. )
  336. (not (vlax-erased-p tb))
  337. )
  338. (
  339. (and
  340. (setq tb
  341. (_Select "\nSelect Table to Add to: "
  342. '(lambda ( x ) (eq "ACAD_TABLE" (cdr (assoc 0 (entget x))))) entsel nil
  343. )
  344. )
  345. (< 1 (vla-get-columns (setq tb (vlax-ename->vla-object tb))))
  346. )
  347. (setq n (1- (vla-get-rows tb)) *al:num (1- *al:num))
  348. )
  349. )
  350. (progn
  351. (while
  352. (if om
  353. (setq p1
  354. (_Select (strcat "\nSelect Object [" (if tx "Undo/" "") "Pick] <Exit>: ")
  355. '(lambda ( x )
  356. (and
  357. (vlax-property-available-p (vlax-ename->vla-object x) 'area)
  358. (not (eq "HATCH" (cdr (assoc 0 (entget x)))))
  359. (or (eq "REGION" (cdr (assoc 0 (entget x)))) (vlax-curve-isclosed x))
  360. )
  361. )
  362. entsel (list (if tx "Undo Pick" "Pick"))
  363. )
  364. )
  365. (progn (initget (if tx "Undo Object" "Object"))
  366. (setq p1 (getpoint (strcat "\nPick Area [" (if tx "Undo/" "") "Object] <Exit>: ")))
  367. )
  368. )
  369. (cond
  370. ( (and tx (eq "Undo" p1))
  371.  
  372. (if el (progn (entdel el) (setq el nil)))
  373. (vla-deleterows tb n 1)
  374. (vla-delete (car tx))
  375. (setq n (1- n) tx (cdr tx) *al:num (1- *al:num))
  376. )
  377. ( (eq "Undo" p1)
  378.  
  379. (princ "\n--> Nothing to Undo.")
  380. )
  381. ( (eq "Object" p1)
  382.  
  383. (if el (progn (entdel el) (setq el nil)))
  384. (setq om t)
  385. )
  386. ( (eq "Pick" p1)
  387.  
  388. (setq om nil)
  389. )
  390. ( (and om (eq 'ENAME (type p1)))
  391.  
  392. (setq tx
  393. (cons
  394. (_text acspc
  395. (_centroid acspc (list (setq p1 (vlax-ename->vla-object p1))))
  396. (strcat pf (itoa (setq *al:num (1+ *al:num))) sf)
  397. ts
  398. ucsxang
  399. )
  400. tx
  401. )
  402. )
  403. (vla-insertrows tb (setq n (1+ n)) th 1)
  404. (vla-settext tb n 1
  405. (if fd
  406. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  407. (_GetObjectID acdoc p1) ">%).Area \\f \"" fo "\">%"
  408. )
  409. (strcat ap (rtos (* cf (vla-get-area p1)) 2) as)
  410. )
  411. )
  412. (vla-settext tb n 0
  413. (if fd
  414. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  415. (_GetObjectID acdoc (car tx)) ">%).TextString>%"
  416. )
  417. (strcat pf (itoa *al:num) sf)
  418. )
  419. )
  420. )
  421. ( (vl-consp p1)
  422.  
  423. (if el (progn (entdel el) (setq el nil)))
  424. (setq el (entlast))
  425. (vl-cmdf "_.-boundary" "_A" "_I" "_N" "" "_O" "_P" "" "_non" p1 "")
  426.  
  427. (if (not (equal el (setq el (entlast))))
  428. (progn
  429. (setq tx
  430. (cons
  431. (_text acspc
  432. (_centroid acspc (list (vlax-ename->vla-object el)))
  433. (strcat pf (itoa (setq *al:num (1+ *al:num))) sf)
  434. ts
  435. ucsxang
  436. )
  437. tx
  438. )
  439. )
  440. (vla-insertrows tb (setq n (1+ n)) th 1)
  441. (vla-settext tb n 1 (strcat ap (rtos (* cf (vlax-curve-getarea el)) 2) as))
  442. (vla-settext tb n 0
  443. (if fd
  444. (strcat "%<\\AcObjProp Object(%<\\_ObjId "
  445. (_GetObjectID acdoc (car tx)) ">%).TextString>%"
  446. )
  447. (strcat pf (itoa *al:num) sf)
  448. )
  449. )
  450. (redraw el 3)
  451. )
  452. (princ "\n--> Error Retrieving Area.")
  453. )
  454. )
  455. )
  456. )
  457. (if el (progn (entdel el) (setq el nil)))
  458. )
  459. )
  460. )
  461. (
  462. (and
  463. (setq fl (getfiled "Create Output File" (cond ( *file* ) ( "" )) "txt;csv;xls" 1))
  464. (setq of (open fl "w"))
  465. )
  466. (setq *file* (vl-filename-directory fl)
  467. de (cdr (assoc (strcase (vl-filename-extension fl) t) '((".txt" . "\t") (".csv" . ",") (".xls" . "\t"))))
  468. *al:num (1- *al:num)
  469. )
  470. (write-line h1 of)
  471. (write-line (strcat t1 de t2) of)
  472.  
  473. (while
  474. (if om
  475. (setq p1
  476. (_Select (strcat "\nSelect Object [Pick] <Exit>: ")
  477. '(lambda ( x )
  478. (and
  479. (vlax-property-available-p (vlax-ename->vla-object x) 'area)
  480. (not (eq "HATCH" (cdr (assoc 0 (entget x)))))
  481. (or (eq "REGION" (cdr (assoc 0 (entget x)))) (vlax-curve-isclosed x))
  482. )
  483. )
  484. entsel '("Pick")
  485. )
  486. )
  487. (progn (initget "Object") (setq p1 (getpoint (strcat "\nPick Area [Object] <Exit>: "))))
  488. )
  489. (cond
  490. ( (eq "Object" p1)
  491.  
  492. (if el (progn (entdel el) (setq el nil)))
  493. (setq om t)
  494. )
  495. ( (eq "Pick" p1)
  496.  
  497. (setq om nil)
  498. )
  499. ( (eq 'ENAME (type p1))
  500.  
  501. (_text acspc
  502. (_centroid acspc (list (setq p1 (vlax-ename->vla-object p1))))
  503. (strcat pf (itoa (setq *al:num (1+ *al:num))) sf)
  504. ts
  505. ucsxang
  506. )
  507. (write-line (strcat pf (itoa *al:num) sf de ap (rtos (* cf (vla-get-area p1)) 2) as) of)
  508. )
  509. ( (vl-consp p1)
  510.  
  511. (if el (progn (entdel el) (setq el nil)))
  512. (setq el (entlast))
  513. (vl-cmdf "_.-boundary" "_A" "_I" "_N" "" "_O" "_P" "" "_non" p1 "")
  514.  
  515. (if (not (equal el (setq el (entlast))))
  516. (progn
  517. (_text acspc
  518. (_centroid acspc (list (vlax-ename->vla-object el)))
  519. (strcat pf (itoa (setq *al:num (1+ *al:num))) sf)
  520. ts
  521. ucsxang
  522. )
  523. (write-line (strcat pf (itoa *al:num) sf de ap (rtos (* cf (vlax-curve-getarea el)) 2) as) of)
  524. (redraw el 3)
  525. )
  526. (princ "\n--> Error Retrieving Area.")
  527. )
  528. )
  529. )
  530. )
  531. (if el (progn (entdel el) (setq el nil)))
  532. (setq of (close of))
  533. (_Open (findfile fl))
  534. )
  535. )
  536. (setenv "LMAC_AreaLabel" (if om "1" "0"))
  537. (setvar 'CMDECHO cm)
  538. (_EndUndo acdoc)
  539. (princ)
  540. )
  541.  
  542. ;;------------------------------------------------------------;;
  543.  
  544. (vl-load-com)
  545. (princ)
  546. (princ "\n:: AreaLabel.lsp | Version 1.9 | © Lee Mac 2011 www.lee-mac.com ::")
  547. (princ "\n:: Commands: \"AT\" for ACAD Table, \"AF\" for File ::")
  548. (princ)
  549.  
  550. ;;------------------------------------------------------------;;
  551. ;; End of File ;;
  552. ;;------------------------------------------------------------;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement