Okay so now ive been using a script, but i still want to change it since it prompts you with another selection after it selects everything from a layer, and with the undo function at the end, that can cause an error that can delete a measurement.
; This code was made by Youri.W (date: June 6 2023)
; Explanation: This code is to extract data from your autocad drawing and put it into excel, it will first get to the layer you want
; add any revisions above here as to make it easier to troubleshoot the piece of code, because it may or may not be hard to find why this code may have an error in the future
;
;add any further comments here:
;
(defun c:Selex () ; Here is the name of my Visual Lisp, This is changeable to whatever you would want the name of the command to be.
; Here it will select all measurements on the layer 'Maatvoering_Export' the orange layer, it does this by first making the layer 'Maatvoering_Export' as the current layer and then it selects everything on the current layer, after it has everything on the layer selected it will explode the things that are selected, the reason for this is that it cant get rotated and angular dimensions. So by exploding the dimensions it will turn them into something called MTexts, and Mtexts can be exported to excel.
(setq layerName "Maatvoering_Export") ; This can be any layer you want but it has to exist in the drawing just change the name between the "..."
(command "-LAYER" "M" layerName "") ; This command makes the name you put in the current layer, M is for make current layer
(sssetfirst nil (ssget "X" (list (cons 8 (getvar "clayer"))))) ; selects everything
(command "_.EXPLODE") ; This explodes everything on the curent layer so it can be brought into excel
(sssetfirst nil (ssget "X" (list (cons 8 (getvar "clayer")))))
(princ)
; Here it will extract your selected text and mtexts and it will put it into an excel file
(defun _sortsel (sel cl)
(mapcar 'cdr
(vl-sort
(mapcar '(lambda (e)
(redraw e 3)
(list (_dxf 10 e) (_dxf 1 e) e))
(vl-remove-if 'listp (mapcar 'cadr (ssnamex sel))))
'(lambda (a b) (< (caar a) (caar b))))))
(defun _dxf (d e)
(cdr (assoc d (entget e))))
(defun _get_text_strings (ent)
(if (eq (cdr (assoc 0 (entget ent))) "MTEXT")
(vla-get-textstring (vlax-ename->vla-object ent))
(_dxf 1 ent)))
(setq collection nil
inc 0
_comma (lambda (s) (strcat s))) ; i want to change this
(while (progn
(princ (strcat "\nSelect text or mtext for column " (itoa (setq inc (1+ inc))) " "))
(setq fc (ssget '((0 . "TEXT,MTEXT")))))
(setq collection (cons (_sortsel fc inc) collection)))
; This is the excel part
(if (setq collection (reverse collection))
(progn
(setq csvfile (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".csv")) ; makes the excel.csv file, it uses the same name as your drawing and places it in the same map
(setq opf (open csvfile "w"))
(while (and (car collection)
(setq data (mapcar 'car collection)))
(setq row (apply 'strcat (mapcar '_comma (mapcar '_get_text_strings (mapcar 'cadr data)))))
(write-line row opf)
(foreach itm (mapcar 'cadr data) (redraw itm 4))
(setq collection (mapcar 'cdr collection)))
(close opf) ; this can give an error if you used the code once and have to excel file open in the background
(startapp "explorer" csvfile)
(my-undo 3) ; this repeats the undo function 3 times
(princ "\nUndo operation performed.")
(princ)
)
)
)
; Here is the undo function, the reason i added an undo function is because i expoded all the dimensions on the layer, and you dont want them to be exploded for multiple reasons.
; Also it repeats the undo 3 times as stated above here
(defun my-undo (count)
(repeat count
(command "_UNDO") ; the undo can cause errors like deleting a measurement so id advise to first look at the excel for a second and then get back to the drawing and press enter
)
) ; end of the code