Jump to content
JackD

Changing the save path of a CSV created from LISP

Recommended Posts

JackD

Hi, I found the below lisp routine online which creates a CSV file containing the insertion point, handle, and name for all objects within a dwg. It currently saves the CSV to the same folder that the dwg is saved in, however when working within a CDE such as ProjectWise the folder can be a bit troublesome to locate on the 😄 drive. I'm not familiar with lisp routines, can someone show me how i would edit it to have the save path set to the desktop? 

 

Thanks in advance

(defun c:BlockExtract (/ BE_ActiveSpace BE_DataList BE_CSVFileID BE_ActiveDoc BE_WriteLine)
   (setq BE_ActiveSpace (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object)))))
   (vlax-for BE_Object BE_ActiveSpace
      (if
         (= (vla-get-ObjectName BE_Object) "AcDbBlockReference")
         (setq BE_DataList
            (append
               BE_DataList
               (list
                  (list
                     (vla-get-EffectiveName BE_Object)
                     (vla-get-Handle BE_Object)
                     (vlax-safearray->list (vlax-variant-value (vla-get-InsertionPoint BE_Object)))
                     (* (vla-get-Rotation BE_Object) (/ 180 pi))                     
                  )
               )
            )
         )
      )
   )
   (if
      BE_DataList
      (progn
         (setq BE_CSVFileID (open (strcat (vla-get-Path (setq BE_ActiveDoc (vla-get-ActiveDocument (vlax-get-acad-object)))) "\\BlockExtract.csv") "a"))
         (foreach BE_Item BE_DataList
            (setq BE_WriteLine "")
            (foreach BE_SubItem BE_Item
               (setq BE_WriteLine (strcat BE_WriteLine (vl-princ-to-string BE_SubItem) ","))
            )
            (setq BE_WriteLine (strcat BE_WriteLine (vla-get-FullName BE_ActiveDoc)))
            (write-line BE_WriteLine BE_CSVFileID)
         )
         (close BE_CSVFileID)
      )
   )
   (mapcar '(lambda ( att ) (cons (vla-get-tagstring att) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))
   (mapcar 'vlax-release-object (list BE_ActiveSpace BE_ActiveDoc))
   (princ)
)

 

Share this post


Link to post
Share on other sites
Lee Mac

Change:

(strcat (vla-get-Path (setq BE_ActiveDoc (vla-get-ActiveDocument (vlax-get-acad-object)))) "\\BlockExtract.csv")

To:

"C:\\YourPathHere\\BlockExtract.csv"

Remember to use either double-backslashes or single-forward-slashes as path delimiters.

 

If you always want the path to be the user's desktop, you can use a function such as the following to obtain the desktop path:

;; Special Folder  -  Lee Mac
;; Queries the WshSpecialFolders collection for the specified folder
;; Ref: http://msdn.microsoft.com/en-us/library/9x9e7edx%28v=vs.85%29.aspx

(defun LM:SpecialFolder ( folder / res spf wsh )
    (setq res
        (vl-catch-all-apply
            (function
                (lambda ( )
                    (setq wsh (vlax-get-or-create-object "wscript.shell")
                          spf (vlax-get-property wsh 'specialfolders)
                    )
                    (vlax-invoke-method spf 'item folder)
                )
            )
        )
    )
    (if spf  (vlax-release-object  spf))
    (if wsh  (vlax-release-object  wsh))
    (if (not (vl-catch-all-error-p res))
        res
    )
)

In this case, you would copy the above code to a new line at the end of the code for your current program, and then change:

(strcat (vla-get-Path (setq BE_ActiveDoc (vla-get-ActiveDocument (vlax-get-acad-object)))) "\\BlockExtract.csv")

To:

(strcat (LM:SpecialFolder "Desktop") "\\BlockExtract.csv")

 

Share this post


Link to post
Share on other sites
Lee Mac
Posted (edited)

FWIW, the following will likely be significantly faster for larger drawings:

(defun c:blockex ( / csv dir dwg enx fnm idx sel )
    (setq dir (LM:specialfolder "desktop")
          fnm (strcat dir "\\BlockExtract.csv")
          dwg (strcat (getvar 'dwgprefix) (getvar 'dwgname))
    )
    (if (and (setq sel (ssget "_X" (list '(0 . "INSERT") (if (= 1 (getvar 'cvport)) (cons 410 (getvar 'ctab)) '(410 . "Model")))))
             (setq csv (open fnm "a"))
        )
        (progn
            (repeat (setq idx (sslength sel))
                (setq idx (1- idx)
                      enx (entget (ssname sel idx))
                )
                (write-line
                    (strcat
                        (LM:name->effectivename (cdr (assoc 2 enx))) ","
                        (cdr (assoc 5 enx)) ","
                        (vl-princ-to-string (mapcar 'rtos (trans (cdr (assoc 10 enx)) (cdr (assoc 210 enx)) 0))) ","
                        (angtos (cdr (assoc 50 enx))) ","
                        dwg
                    )
                    csv
                )
            )
            (close csv)
        )
    )
    (princ)
)

;; Block Name -> Effective Block Name  -  Lee Mac
;; blk - [str] Block name

(defun LM:name->effectivename ( blk / rep )
    (if
        (and (wcmatch blk "`**")
            (setq rep
                (cdadr
                    (assoc -3
                        (entget
                            (cdr (assoc 330 (entget (tblobjname "block" blk))))
                           '("acdbblockrepbtag")
                        )
                    )
                )
            )
            (setq rep (handent (cdr (assoc 1005 rep))))
        )
        (cdr (assoc 2 (entget rep)))
        blk
    )
)

;; Special Folder  -  Lee Mac
;; Queries the WshSpecialFolders collection for the specified folder
;; Ref: http://msdn.microsoft.com/en-us/library/9x9e7edx%28v=vs.85%29.aspx

(defun LM:SpecialFolder ( folder / res spf wsh )
    (setq res
        (vl-catch-all-apply
            (function
                (lambda ( )
                    (setq wsh (vlax-get-or-create-object "wscript.shell")
                          spf (vlax-get-property wsh 'specialfolders)
                    )
                    (vlax-invoke-method spf 'item folder)
                )
            )
        )
    )
    (if spf  (vlax-release-object  spf))
    (if wsh  (vlax-release-object  wsh))
    (if (not (vl-catch-all-error-p res))
        res
    )
)

(princ)

The formatting of the insertion point in the output is an odd choice in your program - I would have thought it would make more sense to separate the X, Y & Z coordinates into separate columns.

Edited by Lee Mac

Share this post


Link to post
Share on other sites
JackD

Thank you Lee! I agree with your comment regarding separating the x,y,z values, but considering i didn't write the original function i wasn't sure how to change it. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...