That was fun.
P=
Code:
;___________________________________________________________________________________________________________|
;
; Written By: Peter Jamtgaard copyright 2023 All Rights Reserved
;___________________________________________________________________________________________________________|
;
; Abstract: This function will extract Y,X position of ALL test block plus data from attributes and
; export it to a csv data file of same name as drawing.
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command line function list
;___________________________________________________________________________________________________________|
;* C:SUR
;* Command Line Function to extract position and attribute textstrings of ALL "test" blocks
;* C:SurveyData
;* Command Line Function to extract position and attribute textstrings of ALL "test" blocks
;___________________________________________________________________________________________________________|
;
; General Function Header List
;___________________________________________________________________________________________________________|
;* (BlockData objBlock)
;* Function to convert a entity based selection set to a list.
;* (SurveyDataFileName)
;* Function to make a datafile name.
;* (ErrorTrap symFunction)
;* Function to convert a entity based selection set to a list.
;* (ListToCSVFile strFilename lstOfSublists strChar)
;* Export a list of sublists of strings to a text file
;* (ListToCSVString lstSublist strChar)
;* Function to Convert List to CSV String
;* (SelectionSetToList ssSelections)
;* Function to convert a entity based selection set to a list.
;$ End Header
;___________________________________________________________________________________________________________|
;
; Command Line Function to extract position and attribute textstrings of ALL "test" blocks
;___________________________________________________________________________________________________________|
(defun C:SUR ()(C:SurveyData))
(defun C:SurveyData (/ lstBlocks lstOfSublists ssSelections strDataFile)
(if (and (setq ssSelections (ssget "X" (list (cons 2 "TEST"))))
(setq lstBlocks (SelectionSetToList ssSelections))
(setq lstOfSublists (mapcar 'BlockData lstBlocks))
(setq strDataFile (SurveyDataFileName))
(ListToCSVFile strDataFile lstOfSublists ",")
(getstring "\nPress Enter to view data file: ")
)
(command "notepad" strDataFile)
)
(princ)
)
;___________________________________________________________________________________________________________|
;
; Function to convert a entity based selection set to a list.
;___________________________________________________________________________________________________________|
(defun BlockData (objBlock / lstAttributeObjects lstTextStrings lstInsertion )
(if (and (setq lstInsertion (vlax-get objBlock "insertionpoint"))
(setq lstAttributeObjects (vlax-invoke objBlock "getattributes"))
(setq lstTextStrings (mapcar '(lambda (X)(vlax-get X "textstring")) lstAttributeObjects))
)
(list (nth 1 lstInsertion)
(nth 0 lstInsertion)
(nth 0 lstTextStrings)
(nth 1 lstTextStrings)
(nth 2 lstTextStrings)
)
)
)
;___________________________________________________________________________________________________________|
;
; Function to make a datafile name.
;___________________________________________________________________________________________________________|
(defun SurveyDataFileName (/ strFileName strFullName)
(if (and (setq strFilename (getvar "dwgname"))
(/= strFileName "")
(setq strFullName (findfile strFileName))
(setq strFullName (strcase strFullName T))
)
(vl-string-subst ".csv" ".dwg" strFullName 0)
)
)
;___________________________________________________________________________________________________________|
;
; Function to convert a entity based selection set to a list.
;___________________________________________________________________________________________________________|
(defun ErrorTrap (symFunction / objError result)
(if (vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result))))
nil
(if result result 'T)
)
)
;___________________________________________________________________________________________________________|
;
; Export a list of sublists of strings to a text file
;___________________________________________________________________________________________________________|
(defun ListToCSVFile (strFilename lstOfSublists strChar / filData lstSublist)
(and
(errortrap (quote (setq filData (open strFileName "w"))))
(errortrap (quote (close filData)))
(errortrap (quote (setq filData (open strFileName "w"))))
(mapcar '(lambda (X)(and
(setq strText (ListtoCSVString X strChar))
(errortrap (quote (write-line strText filData)))
)
)
lstOfSublists
)
)
(and
filData
(errortrap (quote (close filData)))
)
)
;___________________________________________________________________________________________________________|
;
; Function to Convert List to CSV String
;___________________________________________________________________________________________________________|
(defun ListToCSVString (lstSublist strChar / lstOfSublists)
(if (and
(setq lstSublist (mapcar 'vl-princ-to-string lstSublist))
(setq lstSublist (mapcar '(lambda (X)(list strChar X)) lstSublist))
(setq lstSublist (apply 'append lstSublist))
)
(apply 'strcat (cdr lstSublist))
)
)
;___________________________________________________________________________________________________________|
;
; Function to convert a entity based selection set to a list.
;___________________________________________________________________________________________________________|
(defun SelectionSetToList (ssSelections / entSelection intCount lstObjects objSelection )
(repeat (setq intCount (sslength ssSelections))
(setq intCount (1- intCount))
(setq entSelection (ssname ssSelections intCount))
(setq objSelection (vlax-ename->vla-object entSelection))
(setq lstObjects (cons objSelection lstObjects))
)
lstObjects
)
(princ "!")
(vl-load-com)