Jump to content

Getting error while using Amscript for multiple .dwg files


amol1234

Recommended Posts

I wrote down code for my repetitive title block placement. It is working when drawing is opened manually and run that script on it. I want run that for multiple .dwg files using Amscript. Is there any way I can achieve that. I wrote down very simple code. Guide me if I have to changes in code.

(defun TitleBlockPlacement_TKSY(excel-file value)

  (OpenExcel excel-file)

  (GetTab "Sample_V1") ; Replace "Sheet1" with the name of your worksheet

  (setq found nil)

  (setq row 1)

  (while (not found)

    (setq cell-value (GetCell (strcat "A" (itoa row)))) ; Read value from column A

    (if (= cell-value value)

      (progn

        (setq found t)

        (setq b-column-value (GetCell (strcat "G" (itoa row)))) ; Read corresponding value from column B

        (if (= cell-value drawingName)

          (addTitleblock b-column-value)

          (prompt "Value found in A column but corresponding B column value is empty.")

        )

      )

    )

    (setq row (1+ row))

  )

  (if (not found)

    (prompt "Value not found in A column.")

  )

  (CloseExcel)

)



(defun OpenExcel (exfile)

  (setq myfile (findfile exfile))                                      ; double check file exists at location

  (if (/= myfile nil)                                                   ; nil = file not found

      (progn                                                            ; if file found open it

        (setq myxl (vlax-get-or-create-object "Excel.Application"))     ; find Excel application

        (vla-put-visible myxl :vlax-false)                              ; hide application from view

        (vlax-put-property myxl 'DisplayAlerts :vlax-false)             ; hide Excel alerts

        (setq mybook (vl-catch-all-apply 'vla-open (list (vlax-get-property myxl "WorkBooks") myfile)))

      )

  )

)                                                                       ; return - myfile = nil if file not found



;--- Routine to CLOSE Excel file & Session

;--- Assumes previously opened with OpenExcel function

(defun CloseExcel ()

  (vl-catch-all-apply 'vlax-invoke-method (list mybook "Close"))

  (vl-catch-all-apply 'vlax-invoke-method (list myxl "Quit"))

  (vl-catch-all-apply 'vlax-release-object mycell)

  (vl-catch-all-apply 'vlax-release-object myrange)

  (vl-catch-all-apply 'vlax-release-object mysheet)

  (vl-catch-all-apply 'vlax-release-object mybook)  

  (vl-catch-all-apply 'vlax-release-object myxl)

  (setq myfile nil myxl nil mybook nil mysheet nil myrange nil

        mytab nil mycell nil excell nil)                              ; clear variables from memory

  (gc)                                                                ; garbage cleanup

)                                                                      ; return



;--- Routine to set Worksheet Tab

;--- Call using GetTab "Tabname"  (Example:  GetTab "Sheet1")

;--- If mysheet = nil on return then requested TAB not found in Excel file or Excel file was not open

(defun GetTab (mytab)

  (if (/= myxl nil)                                                    ; ensure file is open

      (progn                                                            ; if it is then...

        (setq mysheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property mybook "Sheets") "Item" mytab)))

        (if (not (vl-catch-all-error-p mysheet))                       ; if requested tab found then...

            (vlax-invoke-method mysheet "Activate")                    ; set the desired active tab

            (setq mysheet nil))                                        ; if tab not found then nil mysheet

      )

      (setq mysheet nil))                                               ; if file wasn't open then nil mysheet

  mysheet                                                              ; return with mysheet status

)



;--- Routine to READ an Excel Cell on the current active tab

;--- Call using GetCell "Cell Name" (Example:  GetCell "A1")

;--- MyCell returns cell value (nil = empty)

(defun GetCell (excell)

  (if (/= myxl nil)                                                    ; ensure file is open

      (progn                                                            ; if it is then...

        (setq myrange (vlax-get-property (vlax-get-property mysheet 'Cells) "Range" excell))

        (setq mycell (vlax-variant-value (vlax-get-property myrange 'Value2)))

      )

    (setq mycell nil))                                                 ; nil cell value if file not open



  mycell                                                               ; return with cell value

)



(defun addTitleblock (mycell1)

  ;start(insertion point of title block according to sheet in excel)

  (setq size (cond

               ((= mycell1 "A0") "A0")

               ((= mycell1 "A1") "A1")

               ((= mycell1 "A2") "A2")

               ((= mycell1 "A3") "A3")

               ((= mycell1 "A4") "A4")

               (t "???")

             ))

  (cond

    ((= size "A0") (setq templatePath "C:\\Users\\amol1203\\Documents\\Grob\\filling\\SYMBIO_11.dwg"))

    (t (setq templatePath nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath

      (progn

        (if (findfile templatePath)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath "1169,20" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A1") (setq templatePath2 "C:\\Users\\amol1203\\Documents\\Grob\\filling\\SYMBIO_11.dwg"))

    (t (setq templatePath2 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath2

      (progn

        (if (findfile templatePath2)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath2 "821,20" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A2") (setq templatePath3 "C:\\Users\\amol1203\\Documents\\Grob\\filling\\SYMBIO_11.dwg"))

    (t (setq templatePath3 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath3

      (progn

        (if (findfile templatePath3)

            (progn

              (setq a "320.17")

              (command "_.OPEN" drawingName)

              (command "_.-insert" templatePath3 (strcat a ",15") "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

             

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A3") (setq templatePath4 "C:\\Users\\amol1203\\Documents\\Grob\\filling\\SYMBIO_11.dwg"))

    (t (setq templatePath4 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath4

      (progn

        (if (findfile templatePath4)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath4 "405,10" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )                                          

)

(setq drawingName(getvar "dwgname"))



(TitleBlockPlacement_TKSY "C:/Users/amol1203/Documents/Grob/TKSY/drawings-to_try/Sample_V1.xlsx" drawingName )



 

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • amol1234

    9

  • rlx

    7

  • BIGAL

    4

  • fuccaro

    1

Top Posters In This Topic

Posted Images

In this program I guess problem there when it is placing title block there is user enter attribute dialog box pop up, which I want to bypass. or press ok.

Link to comment
Share on other sites

@BIGAL Thanks for comment I have resolved that issue by deleting prompt from title block but still I am not achieving doing it bunch of files through Amscript

Link to comment
Share on other sites

If the problem is not with your routine it might be with your script.

Like Bigal stated , you may want to set attreq to 0 , same for filedia and even set variable expert to 5 (in case the drawing you want to save already exists). 

Thing is , you have to do this again for every drawing in your script.

Also you have to either autoload your routine or load it for every drawing you open in your script.

 

so something like :

filedia

0

expert

5

open

dwgname1

(load "TitleBlockPlacement_TKSY")

attreq

0

(TitleBlockPlacement_TKSY)

.qsave

open

dwgname2

(load "TitleBlockPlacement_TKSY")

attreq

0

(TitleBlockPlacement_TKSY)

.qsave

etc.

Edited by rlx
Link to comment
Share on other sites

@rlx thanks for reply! I think the routine that you told me is very also lengthy in my case as I have around 1000 drawings. I want to load all files in script generator of Amscript. I think this is good way to it. but somehow it's not achieving that. I thought that if my program works for one drawing it will work for bunch of drawings if I upload them in script generator and then run my script. It will automatically take files from loaded files one by one and perform its operation like it does for single drawing. If you have any clue let me know

 

Link to comment
Share on other sites

I have no experience with Amscript so not sure how that works. Also can't test it here on my work computer because company policy (you name it , we block it). But I assume in the end it should generate a script file as mentioned above. Maybe better try Lee Mac's scriptwriter? If you can run a lisp file so should you be able to generate a script file tru Lee's program. You could do all the setvars in your lisp file so the script itself would only have to open the drawing , load your routine , execute your routine and save the drawing. 

Link to comment
Share on other sites

I never used Amscript, here is how I would try to solve this task:

 

At the end of the lisp, add a line to save and close the file.

Put the Lisp in the startup suit.

In AutoCAD, go to OPEN, and select all the files (use the CTRL/SHIFT keys)

 

Doing so, AutoCAD will open the first file and immediate it will start the program. The program will complete its job, it will save and close the file and will return the control to AutoCAD. The next file will be opened and processed, and so on...

 

After the job is done, remove the lisp from the start-up suit.

Link to comment
Share on other sites

Posted (edited)

@rlx I tried to use Lee Mac's script writer, but it is not for long script as like me. it allows only one line. I might be using it wrong. I read Lee mac's article for that script writer, and it is saying it is for instant script generator for 2 to 3 command. If you have any clue how to upload and run my .lsp into it please let me know. Thanks for your input and taking my request in consideration.

Edited by amol1234
Link to comment
Share on other sites

My script creating, I use old fashioned DOS to make a dwg list, then use Word to edit the list by taking advantage of the replace end of paragraph "^p", its a bit tricky but can do like a 100 dwgs at a time producing multi line script. But I have been making scripts for like 40 years. You replace before the end of line and after the end of line with a new line of text.

 

Pretty sure there is replace end of line in Notepad++ also.

Edited by BIGAL
Link to comment
Share on other sites

I've never worked with Lee's scriptwriter myself but I assume you only need on line , the name of the script and in that (.scr) file you input your commands.

Just start notepad , write line 1 : (load "TitleBlockPlacement_TKSY") , line 2 (TitleBlockPlacement_TKSY) , line 3 : .qsave and then save the file as TitleBlockPlacement.scr

Link to comment
Share on other sites

You said a 1000 dwg's well don't do that many in one go in case somethings srews up, always make a back up copy 1st. You can put a lot on a USB these days. 

 

I've never worked with Lee's scriptwriter myself also. But had a go and got it to work. But did not test, do that on say 10 dwg's in a test directory. be careful and check the script I did not have a space between .close and the file naem but fixed with a replace. Just another suggestion use (setvar 'attreq 0) 

 

I just cut and pasted the rlx code 1 line at a time.

 

image.png.f5f790b39308228e8bc166d1925f71c2.png

 

Ok you get like this 1st line I had 67 lines.

_.open "D:\Acadtemp\3D Model.dwg" (load "TitleBlockPlacement_TKSY") (setvar 'attreq 0) (TitleBlockPlacement_TKSY) .qsave "D:\Acadtemp\3D Model.dwg"

 

You may want to use Accoreconsole much faster now you have a script idea. It opens the dwgs in a different way in that it edits the dwg but no visible opening the dwg. GOOGLE 

 

 

 

 

 

 

 

 

 

Edited by BIGAL
Link to comment
Share on other sites

Posted (edited)

@BIGAL Thanks for trying this out for me. Does this thing worked for you? I have tried on myend but its not giving me desired output.Instead it gives me this selection which is attached with this post

image.thumb.png.5685a6ff2db84cd71b3e96e866abed39.png

 

_.open *file*(load "TitleBlockPlacement_TKSY") attreq 0 (TitleBlockPlacement_TKSY) _.save  *file* _.close (I tried this line in that script writer)

Edited by amol1234
Link to comment
Share on other sites

  1 - Open notepad and make script file (whatever.scr) containing load your routine , attreq bladiebla....and qsave at the end.
  2 - Open one of the drawings you want to process
       (or 'AddSubfolder' after RlxBatch is started)
  3 - Load RlxBatch (this app is part of my startup / autoload)
  4 - Start with command Rlxbatch


 image.thumb.png.91819490a81d0ac967f180faec5cc996.png


  5 - click 'add current folder'
  6 - click 'Saveas List' and give it name
  7 - click OK to close app (that way list is loaded next time because you could also click cancel )
  8 - close drawing (all drawings in list must be closed)
  9 - start new drawing
10 - load & start RlxBatch again
11 - click on 'Script' , select script you just created and lean back

 

I assume you at least know how to make a script file.

This app doesn't save drawings by itself so you must add save command in your script.


And , sorry, no , still haven't written a manual but I hope app is intuitive enough. It's not my latest version but that version is so over the top (over 500kb) and probably half of it I don't remember any more how it works or ever having written it (lot of code was written on the fly / in the heat of the moment so a lot of dead code probably)

 

app can handle both MDI and SDI & accoreconsole also (see Setup , but I can't use that option myself anymore on my work laptop because all bat files are blocked)

 

I hope app (still) works and it helps

 

ps. you don't have to add open or close in your script , appie will do that for you.

If somethings is unclear or not working let me know and if it is working that also would be nice to know.

 

🐉

RlxBatch2018.jpg

RlxBatch.lsp

Edited by rlx
Link to comment
Share on other sites

  • 3 weeks later...

@rlx sorry for late reply. I tried your suggestion. Its loading files but after running script. First this error occurred

image.png.db0d5898e0099d0945103fc73b279166.png

Link to comment
Share on other sites

yeah , was afraid not everthing was working after all these years. Also some buttons are not generic but designed specifically for my own work environment. You could start app and click on button [edit list] to see if all files are in list or button [Check List]  to see which files are missing. It may have something to do with (mapped) network drives. I believe in [Setup] there's an option for that , not sure , would have to test this version my self. 

 

I'm still working on & off on other app in the spare free moments I have right now (big six week turn around at this moment at the plant I'm working) but that app is still unfinished. I think the script part is working. I'll add it on a as-it-is basis with absolutely no time for support at this time. Too much pressure right now to get things finished.

 

Publish part is not working yet and [Future] button also isn't working. That should enable me to press button and five seconds later work is done but I'm still thinking about how to write a Tardis sub-routine for that 🙂 

 

Also no manual for that one. It's meant as an evolution of DIP , my dynamic input routine.

Index part is also not implemented yet. 

 

but you're free to try this still experimental version.

 

image.png.5d36fe286e843720e31a0202727b5125.png

 

🐉

 

 

Fikkie2024.lsp

Link to comment
Share on other sites

Posted (edited)

@rlx I tried to run fikkie but after putting script I am getting these error:

Command: FIKKIE
there were no naughty files
No commands found in slave script : C:\Users\Amol1203\Documents\lisp\Fikkie-ScriptFolder\script.scr
Done.

There might be chance that I am not able to make script of lisp program. Cause I just put down my lisp program into notepad file and saved it as .scr

 

Edited by amol1234
Link to comment
Share on other sites

just tried it and script works for me. Fikkie tests if there are any commands in script file. If test returns an empty list it displays this error.

You could try to start script dialog , put in a name and click on create , enter some commands and don't forget to save the script file (as scr) , maybe your scriptfile is saved as txt?

Will add edit botton for scriptfile later.

Link to comment
Share on other sites

@rlx Thanks for support but still it is not working for all files. It just doing its work for one file and close my file.

;;-----------------------=={ Update Attributes }==----------------------;;

;;                                                                      ;;

;;  Reads a CSV file containing attribute data, and, should the drawing ;;

;;  name of the current drawing appear in the first column of the CSV   ;;

;;  Drawing List, the program will proceed to update block attributes.  ;;

;;                                                                      ;;

;;  The attributes updated will be those with tags corresponding to the ;;

;;  CSV column headings. These will be updated with values from the     ;;

;;  row in which the drawing file is listed.                            ;;

;;                                                                      ;;

;;  -------------------------------------                               ;;

;;  Example of CSV format:                                              ;;

;;  -------------------------------------                               ;;

;;                                                                      ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;  |    DWG     |  Layout*  |   Block*  |   TAG1   | ... |   TAGN   |  ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;  |  Drawing1  |  Layout1  |   Block1  |  Value1  | ... |  ValueN  |  ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;  |  Drawing1  |  Layout2  |   Block1  |  Value1  | ... |  ValueN  |  ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;  |  Drawing2  |  Layout1  |   Block2  |  Value1  | ... |  ValueN  |  ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;  |    ...     |    ...    |    ...    |    ...   | ... |    ...   |  ;;

;;  +------------+-----------+-----------+----------+-----+----------+  ;;

;;                                                                      ;;

;;  *Layout & Block Name columns are optional.                          ;;

;;                                                                      ;;

;;  -------------------------------------                               ;;

;;  Possible Warnings:                                                  ;;

;;  -------------------------------------                               ;;

;;  -  Without a block filter or block name column the code will        ;;

;;     update ALL attributed blocks with tags listed in the CSV         ;;

;;     headings.                                                        ;;

;;                                                                      ;;

;;  -  Currently designed to run on startup - add to either             ;;

;;     Startup-Suite or ACADDOC.lsp to update blocks when drawing is    ;;

;;     opened. To disable code running when loaded, remove (c:utb)      ;;

;;     from the bottom of the code.                                     ;;

;;                                                                      ;;

;;----------------------------------------------------------------------;;

;;  Author:  Lee Mac, Copyright © 2011  -  www.lee-mac.com              ;;

;;----------------------------------------------------------------------;;

;;  Version 1.0    -    2011-01-12                                      ;;

;;                                                                      ;;

;;  - First release.                                                    ;;

;;----------------------------------------------------------------------;;

;;  Version 1.1    -    2011-01-13                                      ;;

;;                                                                      ;;

;;  - Added optional 'Layout' column (next to DWG Column) to allow      ;;

;;    multiple titleblocks to be updated with different information     ;;

;;    within a single drawing.                                          ;;

;;----------------------------------------------------------------------;;

;;  Version 1.2    -    2011-08-28                                      ;;

;;                                                                      ;;

;;  - Removed case-sensitivity of drawing file column in CSV.           ;;

;;----------------------------------------------------------------------;;

;;  Version 1.3    -    2011-12-27                                      ;;

;;                                                                      ;;

;;  - Revised the code to correctly process CSV files generated using   ;;

;;    OpenOffice software.                                              ;;

;;----------------------------------------------------------------------;;

;;  Version 1.4    -    2012-01-16                                      ;;

;;                                                                      ;;

;;  - Updated the 'ReadCSV' local function to correctly read CSV cells  ;;

;;    containing commas and quotes.                                     ;;

;;----------------------------------------------------------------------;;

;;  Version 1.5    -    2012-09-19                                      ;;

;;                                                                      ;;

;;  - Updated CSV file parser function to account for the use of        ;;

;;    alternative cell delimiter characters in CSV file (such as a      ;;

;;    semi-colon).                                                      ;;

;;----------------------------------------------------------------------;;

;;  Version 1.6    -    2013-05-22                                      ;;

;;                                                                      ;;

;;  - Removed the need for file extension in first column of CSV file.  ;;

;;  - Updated CSV file parser function to correct bug.                  ;;

;;----------------------------------------------------------------------;;

;;  Version 1.7    -    2014-11-01                                      ;;

;;                                                                      ;;

;;  - Fixed bug causing filenames containing ASCII character 46 (point) ;;

;;    to not be found in the first column of the CSV file.              ;;

;;----------------------------------------------------------------------;;

;;  Version 1.8    -    2015-04-13                                      ;;

;;                                                                      ;;

;;  - Added support for duplicate attribute tags.                       ;;

;;  - Added support for optional 'Block Name' column in CSV file.       ;;

;;  - Added inclusion of anonymous block names to optional block name   ;;

;;    filter to enable dynamic block support.                           ;;

;;----------------------------------------------------------------------;;

;;  Version 1.9    -    2016-09-18                                      ;;

;;                                                                      ;;

;;  - Fixed implementation of block filter when processing attributed   ;;

;;    dynamic blocks.                                                   ;;

;;----------------------------------------------------------------------;;



(defun c:utb

   

    (

        /

        *error*

        ano

        bln bno

        csv

        ent

        flg fnb:fun

        inc

        lst

        sel str

        tag

        utb:blk utb:csv utb:ftr utb:lay

        val

       



    )



;;----------------------------------------------------------------------;;

;; Location of CSV Drawing List (set to nil for prompt)                 ;;

;;                                                                      ;;

;; If the CSV file resides in the same directory as the drawing, omit   ;;

;; the filepath from the location of the CSV file, and only include     ;;

;; the filename, e.g. "myfile.csv"                                      ;;

;;                                                                      ;;

;; If only a filename is specified, AutoCAD will first search the       ;;

;; working directory for this file before searching the Support Paths.  ;;

;;----------------------------------------------------------------------;;



    (setq utb:csv nil)  e.g. (setq utb:csv "C:/temp/_TKSY/CSV/Parameter_filling.csv")



;;----------------------------------------------------------------------;;

;; Block Filter (may use wildcards and may be nil)                      ;;

;;----------------------------------------------------------------------;;



    (setq utb:ftr nil)  ;; e.g. (setq utb:ftr "*BORDER")



;;----------------------------------------------------------------------;;

;; Layout Column (t or nil)                                             ;;

;;----------------------------------------------------------------------;;



    (setq utb:lay t)    ;; set to t if CSV file contains Layout Column



;;----------------------------------------------------------------------;;

;; Block Name Column (t or nil)                                         ;;

;;----------------------------------------------------------------------;;



    (setq utb:blk nil)  ;; set to t if CSV file contains Block Name Column



;;----------------------------------------------------------------------;;



    (defun *error* ( msg )

        (LM:endundo (LM:acdoc))

        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))

            (princ (strcat "\nError: " msg))

        )

        (princ)

    )



    (setq fnb:fun

        (lambda ( s )

            (if (wcmatch (strcase s t) "*.dwg,*.dxf,*.dwt,*.dws")

                (vl-filename-base s) s

            )

        )

    )

    (cond

        (   (not (setq sel (ssget "_X" (vl-list* '(0 . "INSERT") '(66 . 1) (if utb:ftr (list (cons 2 (strcat "`*U*," utb:ftr))))))))

            (princ "\nNo Attributed Blocks found in drawing.")

        )

        (   (and utb:csv (not (setq csv (findfile utb:csv))))

            (princ

                (strcat

                    "\n"

                    (vl-filename-base utb:csv)

                    (vl-filename-extension utb:csv)

                    " not found."

                )

            )

        )

        (   (and csv (/= ".CSV" (strcase (vl-filename-extension csv))))

            (princ "\nAttribute data file must be in CSV format.")

        )

        (   (not (or csv (setq csv (getfiled "Select CSV File" "" "csv" 16))))

            (princ "\n*Cancel*")

        )

        (   (not (setq lst (mapcar '(lambda ( x ) (cons (strcase (fnb:fun (car x))) (cdr x))) (LM:readcsv csv))))

            (princ

                (strcat

                    "\nNo data found in "

                    (vl-filename-base csv)

                    ".csv file."

                )

            )

        )

        (   (not

                (setq tag (mapcar 'strcase (cdar lst))

                      lst (LM:massoc (strcase (fnb:fun (getvar 'dwgname))) lst)

                )

            )

            (princ (strcat "\n" (fnb:fun (getvar 'dwgname)) " not found in first column of CSV file."))

        )

        (   t

            (setq lst (mapcar '(lambda ( x ) (mapcar 'cons tag x)) lst)

                  ano 0

                  bno 0

            )

            (LM:startundo (LM:acdoc))

            (repeat (setq inc (sslength sel))

                (setq ent (ssname sel (setq inc (1- inc)))

                      bln (strcase (LM:al-effectivename ent))

                      val lst

                      flg nil

                )

                (if (or (null utb:ftr) (wcmatch bln (strcase utb:ftr)))

                    (progn

                        (if utb:lay

                            (setq val (mapcar '(lambda ( x ) (cons (strcase (cdar x)) (cdr x))) val)

                                  val (LM:massoc (strcase (cdr (assoc 410 (entget ent)))) val)

                            )

                        )

                        (if utb:blk

                            (setq val (mapcar '(lambda ( x ) (cons (strcase (cdar x)) (cdr x))) val)

                                  val (cdr (assoc bln val))

                            )

                            (setq val (car val))

                        )

                        (if val

                            (foreach att (vlax-invoke (vlax-ename->vla-object ent) 'getattributes)

                                (if

                                    (and

                                        (setq str (assoc (strcase (vla-get-tagstring att)) val))

                                        (progn

                                            (setq val (LM:remove1st str val))

                                            (/= (vla-get-textstring att) (cdr str))

                                        )

                                    )

                                    (progn

                                        (vla-put-textstring att (cdr str))

                                        (setq flg t

                                              ano (1+ ano)

                                        )

                                    )

                                )

                            )

                        )

                        (if flg (setq bno (1+ bno)))

                    )

                )

            )

            (if (zerop ano)

                (princ "\nAll attributes are up-to-date.")

                (princ

                    (strcat

                        "\n"           (itoa ano) " attribute" (if (= 1 ano) "" "s")

                        " updated in " (itoa bno) " block"     (if (= 1 bno) "" "s") "."

                    )

                )

            )

            (LM:endundo (LM:acdoc))

        )

    )

    (princ)

  (command "_.ZOOM" "ALL" "_fit")

  (command "_.qsave")

  (command "_.close" "_yes")

)



;; Effective Block Name  -  Lee Mac

;; ent - [ent] Block Reference entity



(defun LM:al-effectivename ( ent / blk rep )

    (if (wcmatch (setq blk (cdr (assoc 2 (entget ent)))) "`**")

        (if

            (and

                (setq rep

                    (cdadr

                        (assoc -3

                            (entget

                                (cdr

                                    (assoc 330

                                        (entget

                                            (tblobjname "block" blk)

                                        )

                                    )

                                )

                               '("AcDbBlockRepBTag")

                            )

                        )

                    )

                )

                (setq rep (handent (cdr (assoc 1005 rep))))

            )

            (setq blk (cdr (assoc 2 (entget rep))))

        )

    )

    blk

)



;; Read CSV  -  Lee Mac

;; Parses a CSV file into a matrix list of cell values.

;; csv - [str] filename of CSV file to read



(defun LM:readcsv ( csv / des lst sep str )

    (if (setq des (open csv "r"))

        (progn

            (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))

            (while (setq str (read-line des))

                (setq lst (cons (LM:csv->lst str sep 0) lst))

            )

            (close des)

        )

    )

    (reverse lst)

)



;; CSV -> List  -  Lee Mac

;; Parses a line from a CSV file into a list of cell values.

;; str - [str] string read from CSV file

;; sep - [str] CSV separator token

;; pos - [int] initial position index (always zero)



(defun LM:csv->lst ( str sep pos / s )

    (cond

        (   (not (setq pos (vl-string-search sep str pos)))

            (if (wcmatch str "\"*\"")

                (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))

                (list str)

            )

        )

        (   (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")

                (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))

            )

            (LM:csv->lst str sep (+ pos 2))

        )

        (   (wcmatch s "\"*\"")

            (cons

                (LM:csv-replacequotes (substr str 2 (- pos 2)))

                (LM:csv->lst (substr str (+ pos 2)) sep 0)

            )

        )

        (   (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))

    )

)



(defun LM:csv-replacequotes ( str / pos )

    (setq pos 0)

    (while (setq pos (vl-string-search  "\"\"" str pos))

        (setq str (vl-string-subst "\"" "\"\"" str pos)

              pos (1+ pos)

        )

    )

    str

)



;; MAssoc  -  Lee Mac

;; Returns all associations of a key in an association list



(defun LM:massoc ( key lst / item )

    (if (setq item (assoc key lst))

        (cons (cdr item) (LM:massoc key (cdr (member item lst))))

    )

)



;; Remove 1st  -  Lee Mac

;; Removes the first occurrence of an item from a list



(defun LM:remove1st ( itm lst / f )

    (setq f equal)

    (vl-remove-if '(lambda ( a ) (if (f a itm) (setq f (lambda ( a b ) nil)))) lst)

)



;; Start Undo  -  Lee Mac

;; Opens an Undo Group.



(defun LM:startundo ( doc )

    (LM:endundo doc)

    (vla-startundomark doc)

)



;; End Undo  -  Lee Mac

;; Closes an Undo Group.



(defun LM:endundo ( doc )

    (while (= 8 (logand 8 (getvar 'undoctl)))

        (vla-endundomark doc)

    )

)



;; Active Document  -  Lee Mac

;; Returns the VLA Active Document Object



(defun LM:acdoc nil

    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))

    (LM:acdoc)

)



;;----------------------------------------------------------------------;;



(vl-load-com)

(princ

    (strcat

        "\n:: UpdateTitleblock.lsp | Version 1.9 | \\U+00A9 Lee Mac "

        (menucmd "m=$(edtime,0,yyyy)")

        " www.lee-mac.com ::"

        "\n:: Type \"utb\" to run manually ::\n"

    )

)

(princ)



;;----------------------------------------------------------------------;;

(defun A_Title_Block_TKSY_emotors(excel-file value)

  (OpenExcel excel-file)

  (GetTab "Sample_V1") ; Replace "Sheet1" with the name of your worksheet

  (setq found nil)

  (setq row 1)

  (while (not found)

    (setq cell-value (GetCell (strcat "A" (itoa row)))) ; Read value from column A

    (if (= cell-value value)

      (progn

        (setq found t)

        (setq b-column-value (GetCell (strcat "G" (itoa row)))) ; Read corresponding value from column B

        (if (= cell-value drawingName)

          (addTitleblock b-column-value)

          (prompt "Value found in A column but corresponding B column value is empty.")

        )

      )

    )

    (setq row (1+ row))

  )

  (if (not found)

    (prompt "Value not found in A column.")

  )

  (CloseExcel)

)



(defun OpenExcel (exfile)

  (setq myfile (findfile exfile))                                      ; double check file exists at location

  (if (/= myfile nil)                                                   ; nil = file not found

      (progn                                                            ; if file found open it

        (setq myxl (vlax-get-or-create-object "Excel.Application"))     ; find Excel application

        (vla-put-visible myxl :vlax-false)                              ; hide application from view

        (vlax-put-property myxl 'DisplayAlerts :vlax-false)             ; hide Excel alerts

        (setq mybook (vl-catch-all-apply 'vla-open (list (vlax-get-property myxl "WorkBooks") myfile)))

      )

  )

)                                                                       ; return - myfile = nil if file not found



;--- Routine to CLOSE Excel file & Session

;--- Assumes previously opened with OpenExcel function

(defun CloseExcel ()

  (vl-catch-all-apply 'vlax-invoke-method (list mybook "Close"))

  (vl-catch-all-apply 'vlax-invoke-method (list myxl "Quit"))

  (vl-catch-all-apply 'vlax-release-object mycell)

  (vl-catch-all-apply 'vlax-release-object myrange)

  (vl-catch-all-apply 'vlax-release-object mysheet)

  (vl-catch-all-apply 'vlax-release-object mybook)  

  (vl-catch-all-apply 'vlax-release-object myxl)

  (setq myfile nil myxl nil mybook nil mysheet nil myrange nil

        mytab nil mycell nil excell nil)                              ; clear variables from memory

  (gc)                                                                ; garbage cleanup

)                                                                      ; return



;--- Routine to set Worksheet Tab

;--- Call using GetTab "Tabname"  (Example:  GetTab "Sheet1")

;--- If mysheet = nil on return then requested TAB not found in Excel file or Excel file was not open

(defun GetTab (mytab)

  (if (/= myxl nil)                                                    ; ensure file is open

      (progn                                                            ; if it is then...

        (setq mysheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property mybook "Sheets") "Item" mytab)))

        (if (not (vl-catch-all-error-p mysheet))                       ; if requested tab found then...

            (vlax-invoke-method mysheet "Activate")                    ; set the desired active tab

            (setq mysheet nil))                                        ; if tab not found then nil mysheet

      )

      (setq mysheet nil))                                               ; if file wasn't open then nil mysheet

  mysheet                                                              ; return with mysheet status

)



;--- Routine to READ an Excel Cell on the current active tab

;--- Call using GetCell "Cell Name" (Example:  GetCell "A1")

;--- MyCell returns cell value (nil = empty)

(defun GetCell (excell)

  (if (/= myxl nil)                                                    ; ensure file is open

      (progn                                                            ; if it is then...

        (setq myrange (vlax-get-property (vlax-get-property mysheet 'Cells) "Range" excell))

        (setq mycell (vlax-variant-value (vlax-get-property myrange 'Value2)))

      )

    (setq mycell nil))                                                 ; nil cell value if file not open



  mycell                                                               ; return with cell value

)



(defun addTitleblock (mycell1)

  ;start(insertion point of title block according to sheet in excel)

  (setq size (cond

               ((= mycell1 "A0") "A0")

               ((= mycell1 "A1") "A1")

               ((= mycell1 "A2") "A2")

               ((= mycell1 "A3") "A3")

               ((= mycell1 "A4") "A4")

               (t "???")

             ))

  (cond

    ((= size "A0") (setq templatePath "C:\\temp\\_TKSY\\Template\\TKSY_eMOTORS_Title Block.dwg"))

    (t (setq templatePath nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath

      (progn

        (if (findfile templatePath)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath "1169,20" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

              (c:utb)

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A1") (setq templatePath2 "C:\\temp\\_TKSY\\Template\\C:\\temp\\1\\Template\\TKSJ_Title Block.dwg"))

    (t (setq templatePath2 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath2

      (progn

        (if (findfile templatePath2)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath2 "821,20" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

              (c:utb)

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A2") (setq templatePath3 "C:\\temp\\1\\Template\\TKSJ_Title Block.dwg"))

    (t (setq templatePath3 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath3

      (progn

        (if (findfile templatePath3)

            (progn

              (setq a "320.17")

              (command "_.OPEN" drawingName)

              (command "_.-insert" templatePath3 (strcat a ",15") "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

             

              (command "_.QSAVE")

              (c:utb)

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )

  (cond

    ((= size "A3") (setq templatePath4 "C:\\temp\\1\\Template\\TKSJ_Title Block.dwg"))

    (t (setq templatePath4 nil)) ; Handle "???" or any other unknown size case

  )

  (if templatePath4

      (progn

        (if (findfile templatePath4)

            (progn

              (command "_.OPEN" currentDrawing)

              (command "_.-insert" templatePath4 "405,10" "1" "1" "0") ; Insert the template at the origin

              (princ "\nTemplate added successfully.")

              (command "_CUILOAD" "Enter" "OK")

              (command "_.QSAVE")

              (c:utb)

            )

            (princ "\nTemplate file not found.")

        )

      )

      (princ "\nTemplate path not set for the detected size.")

  )                                          

)

(setq drawingName(getvar "dwgname"))



(A_Title_Block__emotors "C:/temp/1/CSV/Title_block_placement.xlsx" drawingName )







(c:utb) ;; Remove or comment this line to disable autorun



;;----------------------------------------------------------------------;;

;;                             End of File                              ;;

;;----------------------------------------------------------------------;;

After that gave this error:

AutoCAD menu utilities loaded.
Command:
Command:
Command:
Command: (load"RlxBatch")
; error: LOAD failed: "RlxBatch"
Command: (RlxBatch_CloseAllButCurrent)
; error: no function definition: RLXBATCH_CLOSEALLBUTCURRENT
Command: .open
Command:

Link to comment
Share on other sites

you should place fikkie of RlxBatch in one of your support path's.

If you type (findfile "RlxBatch.lsp") or (findfile "Fikkie2024.lsp") and this returns nil instead of a path, routines are in a bad place.

If your own script or lisp routine contains an error , this also will stopt any script.

Link to comment
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
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...