Jump to content

can anyone write me a lisp file for opening a latest drawing


Recommended Posts

Posted

As i said in the above title, I am looking for a lisp file which will open the latest dxf file from the pre-defined directory given in the lisp file.

 

Is it possible to do so?

Posted

When you say "Latest" do you mean the last one to be modified in a directory?

Posted

Yes, you are right. Need to open latest modified drawing in the directory.

Posted

This should open the lastest dwg file in a directory, I think you need to modify it to using vla-import to open the lastest dxf file.

 

(defun c:open_last (/ path lst dwg)
 (vl-load-com)
 (if (setq path (Directory-Dia "Select Directory: "))
   (progn
     (foreach dwg (mapcar
                    (function
                      (lambda (file)
                        (strcat path file)))
                    (vl-directory-files path "*.dwg" 1))
       (setq lst
              (cons
                (list dwg (remove_nth (vl-file-systime dwg) 2)) lst)))
     (if (vl-catch-all-error-p
           (vl-catch-all-apply
             (function
               (lambda ( )
                 (vla-open
                   (vla-get-Documents
                     (vlax-get-acad-object))
                   (setq dwg (dSort lst)) :vlax-false)))))
       (princ (strcat "\n** " dwg " Failed to Open **"))))
   (princ "\n*Cancel*"))
 (princ))

(defun Directory-Dia  (Message / sh folder folderobject result)
 ;; By Tony Tanzillo
 (vl-load-com)
 (setq sh (vla-getInterfaceObject
            (vlax-get-acad-object) "Shell.Application"))
 (setq folder (vlax-invoke-method sh 'BrowseForFolder
                (vla-get-HWND
                  (vlax-get-Acad-Object)) Message 0))
 (vlax-release-object sh)
 (if folder
   (progn
     (setq folderobject (vlax-get-property folder 'Self)
           result (vlax-get-property FolderObject 'Path))
     (vlax-release-object folder)
     (vlax-release-object FolderObject)
     (if (/= (substr result (strlen result)) "\\")
       (setq result (strcat result "\\"))
       result))))

; Stig
(defun remove_nth (lst i / a)
 (setq a -1)
 (vl-remove-if
   (function
     (lambda (n) (= (setq a (1+ a)) i))) lst))

(defun dSort (lst / lst)
 (while
   (progn
     (cond ((not
              (vl-remove-if 'null
                (mapcar 'cadr lst)))
            (setq lst nil))
           ((apply '=
              (vl-sort
                (mapcar 'caadr lst)
                  (function
                    (lambda (a b) (< a b)))))
            (setq lst (mapcar
                        (function
                          (lambda (x)
                            (list (car x) (cdadr x)))) lst)))
           (t (setq lst (caar lst)) nil))))
 lst)

Posted

Hi Lee mac,

 

Thanks for your reply

 

But as i said earlier cant we include pre-defined directory path in the code?

and can you modify it to open the dxf file

Posted

I thought you might like my ObjectDBX Directory window... :(

Posted

Try this:

 

(defun c:open_last (/ path lst dwg)
 (vl-load-com)

 (setq Dir "C:\\")  ;; <<--- Your "Pre-Defined Directory" goes here
 
 (if (vl-file-directory-p Dir)
   (progn
     (foreach dwg (mapcar
                    (function
                      (lambda (file)
                        (strcat path file)))
                    (vl-directory-files path "*.dxf" 1))
       (setq lst
              (cons
                (list dwg (remove_nth (vl-file-systime dwg) 2)) lst)))
     (if (vl-catch-all-error-p
           (vl-catch-all-apply
             (function
               (lambda ( )
                 (vla-import
                   (vla-get-ActiveDocument
                     (vlax-get-acad-object))
                   (setq dwg (dSort lst))
                     (vlax-3D-point '(0 0 0)) 1.)))))
       (princ (strcat "\n** " dwg " Failed to Open **"))))
   (princ "\n*Cancel*"))
 (princ))

; Stig
(defun remove_nth (lst i / a)
 (setq a -1)
 (vl-remove-if
   (function
     (lambda (n) (= (setq a (1+ a)) i))) lst))

(defun dSort (lst / lst)
 (while
   (progn
     (cond ((not
              (vl-remove-if 'null
                (mapcar 'cadr lst)))
            (setq lst nil))
           ((apply '=
              (vl-sort
                (mapcar 'caadr lst)
                  (function
                    (lambda (a b) (< a b)))))
            (setq lst (mapcar
                        (function
                          (lambda (x)
                            (list (car x) (cdadr x)))) lst)))
           (t (setq lst (caar lst)) nil))))
 lst)

Posted

How about just opening up your Explorer Window, browse to the specific directory and then click on the "Date Modified" tab.

 

You can do the same thing within Autocad using the "Design Center".

 

You can also use the sidebar in the Autocad "Open" dialog to store folders that you need to access on a regular basis. You can add job folders containing drawings you are currently working on, and remove ones that you are finished with. This is a real time saver for me and it's something that I don't think too many people are aware of. :unsure:

Posted

I thought this could be done another way, but i took it as a challenge :twisted:

Posted

That's fine, but I think we should be offering practical answers using standard Autocad utilities or Windows functionality first, before diving into lisp. Lisp is great, and it can be a real lifesaver in some instances, but there are plenty of ways to do a lot of the things people are asking, that are already built into Autocad. I think people are relying a bit too much on lisp, and not learning the standard commands. If these guys ever go to work for a company that doesn't allow cutom routines, or if they have to work for a company that uses Autocad LT, they are going to be lost.

 

Sorry, but that circle scaling lisp just got me going. I just don't understand why someone would request a lisp routine to scale a circle? :roll:

Posted

I understand completely Rod.

 

I am not too good at offering an alternative solution - many times I will miss the obvious answer. Basically because I don't use CAD from day to day, I just write the LISP, as I enjoy it. :)

Posted

 
(defun c:open_last (/ path lst dwg)
(vl-load-com)
(setq Dir "C:\Acad_dwg\00109")

(if (vl-file-directory-p Dir)
(progn
(foreach dwg (mapcar
(function
(lambda (file)
(strcat path file)))
(vl-directory-files path "*.dxf" 1))
(setq lst
(cons
(list dwg (remove_nth (vl-file-systime dwg) 2)) lst)))
(if (vl-catch-all-error-p
(vl-catch-all-apply
(function
(lambda ( )
(vla-import
(vla-get-ActiveDocument
(vlax-get-acad-object))
(setq dwg (dSort lst))
(vlax-3D-point '(0 0 0)) 1.)))))
(princ (strcat "\n** " dwg " Failed to Open **"))))
(princ "\n*Cancel*"))
(princ))
; Stig
(defun remove_nth (lst i / a)
(setq a -1)
(vl-remove-if
(function
(lambda (n) (= (setq a (1+ a)) i))) lst))
(defun dSort (lst / lst)
(while
(progn
(cond ((not
(vl-remove-if 'null
(mapcar 'cadr lst)))
(setq lst nil))
((apply '=
(vl-sort
(mapcar 'caadr lst)
(function
(lambda (a b) (< a b)))))
(setq lst (mapcar
(function
(lambda (x)
(list (car x) (cdadr x)))) lst)))
(t (setq lst (caar lst)) nil))))
lst)

 

I had modified directory path to this C:\Acad_dwg\00109

When I am using this lisp command it was showing *cancel* in command window.

 

@Cad64

I am having 5 years of experience on cad. I know all the ways mentioned in your post and I am using that side bar method also for a long time.

Actually my problem is I have to access multiple latest drawings from multiple folders for atleast 20-30 times a day. It was frustrating me by using normal file accessing methods.

Posted

No problem in the LISP file.

 

You need to specify a valid directory,

 

for example:

 

C:\\Acad_dwg\\00109\\

 

Using double backslashes :)

 

 

Lee

Posted

Sorry to bother you again.

 

I tried with double backslashes also.

 

Now it was showing the following error in command line

 

; error: bad argument type: stringp nil

 

I am using acad2004, does the version has anything to do with this problem?

Posted

Ok, that one was my fault :oops:

 

There could be further errors as I haven't at all tested this LISP :)

 

(defun c:open_last (/ Dir lst dwg)
 (vl-load-com)

 (setq Dir "C:\\")  ;; <<--- Your "Pre-Defined Directory" goes here
 
 (if (vl-file-directory-p Dir)
   (progn
     (foreach dwg (mapcar
                    (function
                      (lambda (file)
                        (strcat Dir file)))
                    (vl-directory-files Dir "*.dxf" 1))
       (setq lst
              (cons
                (list dwg (remove_nth (vl-file-systime dwg) 2)) lst)))
     (if (vl-catch-all-error-p
           (vl-catch-all-apply
             (function
               (lambda ( )
                 (vla-import
                   (vla-get-ActiveDocument
                     (vlax-get-acad-object))
                   (setq dwg (dSort lst))
                     (vlax-3D-point '(0 0 0)) 1.)))))
       (princ (strcat "\n** " dwg " Failed to Open **"))))
   (princ "\n*Cancel*"))
 (princ))

; Stig
(defun remove_nth (lst i / a)
 (setq a -1)
 (vl-remove-if
   (function
     (lambda (n) (= (setq a (1+ a)) i))) lst))

(defun dSort (lst / lst)
 (while
   (progn
     (cond ((not
              (vl-remove-if 'null
                (mapcar 'cadr lst)))
            (setq lst nil))
           ((apply '=
              (vl-sort
                (mapcar 'caadr lst)
                  (function
                    (lambda (a b) (< a b)))))
            (setq lst (mapcar
                        (function
                          (lambda (x)
                            (list (car x) (cdadr x)))) lst)))
           (t (setq lst (caar lst)) nil))))
 lst)

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...