How to use script with lisp ?

How to use script with lisp ?

Anonymous
Not applicable
13,599 Views
10 Replies
Message 1 of 11

How to use script with lisp ?

Anonymous
Not applicable

Hi,

 

Can someone tell me the way use lisp and scrips:

 

my idea:

 

1. open file via browse foler or file to select folder 

2. Run script run lisp 

(vl-load-com)

(foreach layout (layoutlist); conveniently returns list of all Paper-space Layouts

  (setvar 'ctab layout); get into the Layout

  (command "_.pspace"); [in case it might be in Model space through a Viewport in that Layout]

  (if (setq ss (ssget "x" (list '(0 . "INSERT") '(2 . "TITLEBLOCKA") (cons 410 (getvar "ctab")))))

    (progn

      (vla-getboundingbox (vlax-ename->vla-object (ssname ss 0)) 'minpt 'maxpt)

      (setq LL (vlax-safearray->list minpt)); Lower Left corner of block's contents
      (command

        "_.layer" "_thaw" "*" "_on" "*" "_unlock" "*" "" ; make everything movable

        "_.move"

        (ssget "x" (list (cons 410 (getvar "ctab")))) ""

        LL '(0.0 0.0 0.0)

        "_.layerp" ; return previous Layer settings

      ); end command

    ); end progn

  ); end if

); foreach

Kent Cooper
 
3. save and close.
 
Thanks for solution. ^_^
Accepted solutions (2)
13,600 Views
10 Replies
Replies (10)
Message 2 of 11

wispoxy
Advisor
Advisor

Run command APPLOAD then select your script. You're my post 2,000 by-the-way Smiley Very Happy

0 Likes
Message 3 of 11

hmsilva
Mentor
Mentor
Accepted solution

 Hi pony.chubby,
in your previous thread I did post a link to a post where you could get some ideas to start writing the code... 😞

 

One way...

 

Save Kent1Cooper's code in a 'Trusted Locations' folder, with the name you think best (e.x. "MyCode.lsp")

 

;; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/update-titleblock-insertion-point-or-move-border-frame-s-to-the/m-p/6383170#M342043
;; by Kent1Cooper
(vl-load-com)
(foreach layout (layoutlist); conveniently returns list of all Paper-space Layouts
  (setvar 'ctab layout); get into the Layout
  (command "_.pspace"); [in case it might be in Model space through a Viewport in that Layout]
  (if (setq ss (ssget "x" (list '(0 . "INSERT") '(2 . "TITLEBLOCKA") (cons 410 (getvar "ctab")))))
    (progn
      (vla-getboundingbox (vlax-ename->vla-object (ssname ss 0)) 'minpt 'maxpt)
      (setq LL (vlax-safearray->list minpt)); Lower Left corner of block's contents
      (command
        "_.layer" "_thaw" "*" "_on" "*" "_unlock" "*" "" ; make everything movable
        "_.move"
        (ssget "x" (list (cons 410 (getvar "ctab")))) ""
        LL '(0.0 0.0 0.0)
        "_.layerp" ; return previous Layer settings
      ); end command
    ); end progn
  ); end if
); foreach

 

Change the command name to the name you think best, and change "MyCode.lsp" to the correct one.
Save the file in a 'Trusted Locations' folder.
Open a new dwg, load the code and type the new command name...

 

;; Load Supporting Functions
;; Old Version of 'BrowseForFolder' by: Tony Tanzillo 
(defun BrowseForFolder (Message / sh folder parentfolder folderobject result)
   (vl-load-com)
   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 Message 0))
   (vlax-release-object sh)
   (if folder
      (progn
         (setq parentfolder (vlax-get-property folder 'ParentFolder))
         (setq FolderObject (vlax-invoke-method ParentFolder 'ParseName (vlax-get-property Folder 'Title)))
         (setq result (vlax-get-property FolderObject 'Path))
         (mapcar 'vlax-release-object
                 (list folder parentfolder folderobject)
         )
         (if (/= (substr result (strlen result)) "\\")
            (setq result (strcat result "\\"))
            result
         )
      )
   )
)


(defun c:demo (/ DirPath DwgFile DwgList my_lisp_file Ofile) ; <<< change the command name!!!
   (if (setq DirPath (BrowseForFolder "Select directory to scan drawings."))
      (progn
         (setq my_lisp_file "MyCode.lsp") ; <<< Place the correct lisp name here!!!
         (setq Scrfile (strcat DirPath "test.scr"))
         (setq ofile (open Scrfile "w"))
         (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
         (foreach Dwg DwgList
            (setq DwgFile (strcat DirPath Dwg))
            (write-line (strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile)
            (write-line (strcat "(load " (chr 34) my_lisp_file (chr 34) ")") Ofile)
            (write-line "_.qsave" Ofile)
            (write-line "_.close" Ofile)
         )
         (close Ofile)
         (command "_.script" Scrfile)
         (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(vl-file-delete Scrfile)\r")
      )
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 4 of 11

Anonymous
Not applicable

right, exactly Henrique Heart

 

and question:

 

How can select TITLEBLOCK by ssget and name of TITLEBLOCK then run lisp Kent1Cooper ? Because TITLEBLOCK maybe is TITLEBLOCK 1, TITLEBLOCK, .....

 

Thanks for your help.

0 Likes
Message 5 of 11

hmsilva
Mentor
Mentor

You're welcome!

 

Change the

(2 . "TITLEBLOCKA")

to your titleblock names

(2 . "TITLEBLOCKA,TITLEBLOCKB,TITLEBLOCKX")


Henrique

EESignature

0 Likes
Message 6 of 11

Anonymous
Not applicable

No, i mean:

(setq insert (entsel "\nSelect TITLEBLOCK in drawing"))

(setq name (cdr (assoc 2 (entget insert))))

 

how can assign to : (ssget "X" .......... name) in ssget.

 

Thanks.

0 Likes
Message 7 of 11

Anonymous
Not applicable

i am trying:

 

(setq ename (entget (car (entsel "\nSelect TitleBlock: "))))
(setq name_title (assoc 2 ename))

(if (setq ss (ssget "x" (list '(0 . "INSERT") name_title (cons 410 (getvar "ctab")))))
(progn

......

 That is clear for me.

 

Can u see it Henrique !

 

Thanks for your solution

0 Likes
Message 8 of 11

Anonymous
Not applicable

Hi,

 

i tried in my pc is ok

but path file in server error:

; error: bad argument type: streamp nil

 

how can fix it.

 

Thanks

0 Likes
Message 9 of 11

hmsilva
Mentor
Mentor
Accepted solution

@Anonymous wrote:

i am trying:

 

(setq ename (entget (car (entsel "\nSelect TitleBlock: "))))
(setq name_title (assoc 2 ename))

(if (setq ss (ssget "x" (list '(0 . "INSERT") name_title (cons 410 (getvar "ctab")))))
(progn

......

 That is clear for me.

 

Can u see it Henrique !

 

Thanks for your solution


To run the script in a folder, we can't have one of the dwg's in that folder opened... we need to have all dwg's in that folder closed...otherwise the code will error...

 

If you have a dwg in another folder with the same TitleBlock, you can modify Kent1Cooper's code, as a function with one argumente

i.e.

(defun MyCode (name_title / ll minpt ss)
   ;; http://forums.autodesk.com/t5/visual-lisp-autolisp?-and-general/update-titleblock-insertion-point-or-?move-border-frame-s-to-the/m-p/6383170#M342043
   ;; by Kent1Cooper
   (vl-load-com)
   (foreach layout (layoutlist) ; conveniently returns list of all Paper-space Layouts
      (setvar 'ctab layout) ; get into the Layout
      (command "_.pspace") ; [in case it might be in Model space through a Viewport in that Layout]
      (if (setq ss (ssget "x" (list '(0 . "INSERT") (cons 2 name_title) (cons 410 (getvar "ctab")))))
         (progn
            (vla-getboundingbox (vlax-ename->vla-object (ssname ss 0)) 'minpt 'maxpt)
            (setq LL (vlax-safearray->list minpt)) ; Lower Left corner of block's contents
            (command "_.layer" "_thaw" "*" "_on" "*" "_unlock" "*" "" ; make everything movable
               "_.move" (ssget "x" (list (cons 410 (getvar "ctab")))) "" LL '(0.0 0.0 0.0)
               "_.layerp" ; return previous Layer settings
            ) ; end command
         ) ; end progn
      ) ; end if
   ) ; foreach
)

 

and in the code to write and run the script

 

;; Load Supporting Functions
;; Old Version of 'BrowseForFolder' by: Tony Tanzillo 
(defun BrowseForFolder (Message / sh folder parentfolder folderobject result)
   (vl-load-com)
   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 Message 0))
   (vlax-release-object sh)
   (if folder
      (progn
         (setq parentfolder (vlax-get-property folder 'ParentFolder))
         (setq FolderObject (vlax-invoke-method ParentFolder 'ParseName (vlax-get-property Folder 'Title)))
         (setq result (vlax-get-property FolderObject 'Path))
         (mapcar 'vlax-release-object
                 (list folder parentfolder folderobject)
         )
         (if (/= (substr result (strlen result)) "\\")
            (setq result (strcat result "\\"))
            result
         )
      )
   )
)


(defun c:demo (/ dirpath dwgfile dwglist ent my_lisp_file name_title ofile sel) ; <<< change the command name!!!
   (if (and (setq DirPath (BrowseForFolder "Select directory to scan drawings."))
            (setq sel (car (entsel "\nSelect TitleBlock: ")))
            (= (cdr (assoc 0 (setq ent (entget sel)))) "INSERT")
            (setq name_title (cdr (assoc 2 (entget sel))))
       )
      (progn
         (setq my_lisp_file "MyCode.lsp") ; <<< Place the correct lisp name here!!!
         (setq Scrfile (strcat DirPath "test.scr"))
         (setq ofile (open Scrfile "w"))
         (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
         (foreach Dwg DwgList
            (setq DwgFile (strcat DirPath Dwg))
            (write-line (strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile)
            (write-line (strcat "(load " (chr 34) my_lisp_file (chr 34) ")") Ofile)
            (write-line "(my_lisp_file name_title)" Ofile)
            (write-line "_.qsave" Ofile)
            (write-line "_.close" Ofile)
         )
         (close Ofile)
         (command "_.script" Scrfile)
         (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(vl-file-delete Scrfile)\r")
      )
   )
   (princ)
)

 

But remember, none of the dwg's to process can be open!

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 10 of 11

Anonymous
Not applicable

Right , 

 

Thanks Henrique again.Heart

0 Likes
Message 11 of 11

hmsilva
Mentor
Mentor

You're welcome!
Glad I could help 🙂

Henrique

EESignature

0 Likes