Author Topic: Function not working  (Read 337 times)

0 Members and 1 Guest are viewing this topic.

vincent.r

  • Newt
  • Posts: 63
Function not working
« on: June 29, 2020, 06:09:18 AM »
Hi guys,

Prepared a function to copy a block from other drawing to current opened drawing. Individual statements/codes are working fine but, if I call command it gives error too few arguments. Where I mistaking ? Drawing (from where want to copy block Example block name HCR22) attached.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:isfod (path npfname filename )
  2.  (setq path "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg")
  3.  (setq filename (vl-filename-mktemp "scr" (getvar "tempprefix") ".scr"))
  4.  (setq npfname (open filename "w"))
  5.  (write-line "qsave" npfname)
  6.  (write-line "open" npfname)
  7.  (write-line (strcat "\"" path "\"") npfname)
  8.  (write-line (strcat "(command " "\"" "_.copybase" "\"" " " "\"" "148.9266,7.1814,0.0000" "\"" " " "\"" "W" "\"" " " "\"" "136.7826,17.7144,0.0000" "\"" " " "\"" "159.4299,-2.3621,0.0000" "\"" " " "\"" "\"" ")" ) npfname)
  9.  (write-line "qsave" npfname)
  10.  (write-line "close" npfname)
  11.  (close npfname)
  12.  (vl-cmdf "_.script" filename)
  13.  (command "._pasteclip" "_non" "87.2605,255.5040,0.0")
  14.  )

Regards,
Vincent


EDIT (John): Added code tags.
« Last Edit: June 29, 2020, 08:33:16 AM by John Kaul (Se7en) »

kpblc

  • Bull Frog
  • Posts: 357
Re: Function not working
« Reply #1 on: June 29, 2020, 07:13:07 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:isfod (/ path npfname filename)
  2.   (setq path "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg")
  3.   (setq filename (vl-filename-mktemp "scr" (getvar "tempprefix") ".scr"))
  4.   (setq npfname (open filename "w"))
  5.   (write-line "qsave" npfname)
  6.   (write-line "open" npfname)
  7.   (write-line (strcat "\"" path "\"") npfname)
  8.     (strcat "(command "    "\""           "_.copybase"   "\""           " "            "\""           "148.9266,7.1814,0.0000"      "\""
  9.             " "            "\""           "W"            "\""           " "            "\""           "136.7826,17.7144,0.0000"     "\""
  10.             " "            "\""           "159.4299,-2.3621,0.0000"     "\""           " "            "\""           "\""           ")"
  11.            ) ;_ end of strcat
  12.     npfname
  13.   ) ;_ end of write-line
  14.   (write-line "qsave" npfname)
  15.   (write-line "close" npfname)
  16.   (close npfname)
  17.   (vl-cmdf "_.script" filename)
  18.   (command "._pasteclip" "_non" "87.2605,255.5040,0.0")
  19. ) ;_ end of defun
:?:
Sorry for my English.

kpblc

  • Bull Frog
  • Posts: 357
Re: Function not working
« Reply #2 on: June 29, 2020, 07:44:35 AM »
Another one simple code with ObjectDbx:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun import-block-def-and-insert (dwg-name block-name ins-params / adoc acad odbx def ext_def ref)
  3.                                    ;|
  4.   ;; imports and insert blocks to current dwg
  5.    dwg-name : dwg filename with block
  6.    block-name : block name
  7.    ins-params : parameters for inserting block:
  8.      '(("ins" . <>) ; insertion point . nil -> '(0. 0. 0.)
  9.        ("rot" . <>) ; rotation angle. nil -> 0.
  10.        ("x" . <>)   ; x scale factor. nil -> 1.
  11.        ("y" . <>)   ; y scale factor. nil -> use x scale
  12.        ("z" . <>)   ; z scale factor. nil -> use y scale
  13. Call sample:
  14. (import-block-def-and-insert (getfiled "library file" "" "dwg" 4) "pik-ind-3d_section{0.7}" nil)
  15. (import-block-def-and-insert "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  16. "HCR22" (list (cons "ins" (getpoint "\nInsertion point : "))))
  17.  
  18.   |;
  19.   (if (not (tblobjname "block" block-name))
  20.     (if (and dwg-name (setq dwg-name (findfile dwg-name)))
  21.       (progn (setq odbx (vla-getinterfaceobject acad (strcat "ObjectDBX.AxDbDocument." (itoa (atoi (getvar "acadver"))))))
  22.              (vla-open odbx dwg-name :vlax-true)
  23.              (if (= (type
  24.                       (setq ext_def (vl-catch-all-apply (function (lambda () (vla-item (vla-get-blocks odbx) block-name)))))
  25.                     ) ;_ end of type
  26.                     'vla-object
  27.                  ) ;_ end of =
  28.                (progn (vla-copyobjects
  29.                         odbx
  30.                         (vlax-make-variant
  31.                           (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list ext_def))
  32.                         ) ;_ end of vlax-make-variant
  33.                         (vla-get-blocks adoc)
  34.                       ) ;_ end of vla-CopyObjects
  35.                       (setq def (vla-item (vla-get-blocks adoc) block-name))
  36.                ) ;_ end of progn
  37.              ) ;_ end of if
  38.              (vlax-release-object odbx)
  39.       ) ;_ end of progn
  40.     ) ;_ end of if
  41.     (setq def (vla-item (vla-get-blocks adoc) block-name))
  42.   ) ;_ end of if
  43.   (if def
  44.                        (vla-get-modelspace adoc)
  45.                        (vlax-3d-point
  46.                          (cond ((cdr (assoc "ins" ins-params)))
  47.                                (t '(0. 0. 0.))
  48.                          ) ;_ end of cond
  49.                        ) ;_ end of vlax-3d-point
  50.                        block-name
  51.                        (cond ((cdr (assoc "x" ins-params)))
  52.                              (t 1.)
  53.                        ) ;_ end of cond
  54.                        (cond ((cdr (assoc "y" ins-params)))
  55.                              ((cdr (assoc "x" ins-params)))
  56.                              (t 1.)
  57.                        ) ;_ end of cond
  58.                        (cond ((cdr (assoc "z" ins-params)))
  59.                              ((cdr (assoc "x" ins-params)))
  60.                              (t 1.)
  61.                        ) ;_ end of cond
  62.                        (cond ((cdr (assoc "rot" ins-params)))
  63.                              (t 0.)
  64.                        ) ;_ end of cond
  65.                      ) ;_ end of vla-insertblock
  66.            ) ;_ end of setq
  67.     ) ;_ end of progn
  68.   ) ;_ end of defun
  69.   ref
  70. ) ;_ end of defun
  71. ;|
  72.  
  73.  ;; example command
  74.  
  75. (defun c:isfod (/ doc)
  76.   (vl-load-com)
  77.   (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  78.   (import-block-def-and-insert
  79.     "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  80.     "HCR22"
  81.     (list (cons "ins" (getpoint "\nInsertion point : ")))
  82.   ) ;_ end of import-block-def-and-insert
  83.   (vla-endundomark doc)
  84.   (princ)
  85. ) ;_ end of defun
  86. |;
Sorry for my English.

vincent.r

  • Newt
  • Posts: 63
Re: Function not working
« Reply #3 on: June 29, 2020, 09:10:13 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:isfod (/ path npfname filename)
  2.   (setq path "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg")
  3.   (setq filename (vl-filename-mktemp "scr" (getvar "tempprefix") ".scr"))
  4.   (setq npfname (open filename "w"))
  5.   (write-line "qsave" npfname)
  6.   (write-line "open" npfname)
  7.   (write-line (strcat "\"" path "\"") npfname)
  8.     (strcat "(command "    "\""           "_.copybase"   "\""           " "            "\""           "148.9266,7.1814,0.0000"      "\""
  9.             " "            "\""           "W"            "\""           " "            "\""           "136.7826,17.7144,0.0000"     "\""
  10.             " "            "\""           "159.4299,-2.3621,0.0000"     "\""           " "            "\""           "\""           ")"
  11.            ) ;_ end of strcat
  12.     npfname
  13.   ) ;_ end of write-line
  14.   (write-line "qsave" npfname)
  15.   (write-line "close" npfname)
  16.   (close npfname)
  17.   (vl-cmdf "_.script" filename)
  18.   (command "._pasteclip" "_non" "87.2605,255.5040,0.0")
  19. ) ;_ end of defun
:?:

Thanks kpblc for your reply. But code given by you is not working. It gives same result as mine is.

Regards,
Vincent

vincent.r

  • Newt
  • Posts: 63
Re: Function not working
« Reply #4 on: June 29, 2020, 09:17:14 AM »
Another one simple code with ObjectDbx:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun import-block-def-and-insert (dwg-name block-name ins-params / adoc acad odbx def ext_def ref)
  3.                                    ;|
  4.   ;; imports and insert blocks to current dwg
  5.    dwg-name : dwg filename with block
  6.    block-name : block name
  7.    ins-params : parameters for inserting block:
  8.      '(("ins" . <>) ; insertion point . nil -> '(0. 0. 0.)
  9.        ("rot" . <>) ; rotation angle. nil -> 0.
  10.        ("x" . <>)   ; x scale factor. nil -> 1.
  11.        ("y" . <>)   ; y scale factor. nil -> use x scale
  12.        ("z" . <>)   ; z scale factor. nil -> use y scale
  13. Call sample:
  14. (import-block-def-and-insert (getfiled "library file" "" "dwg" 4) "pik-ind-3d_section{0.7}" nil)
  15. (import-block-def-and-insert "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  16. "HCR22" (list (cons "ins" (getpoint "\nInsertion point : "))))
  17.  
  18.   |;
  19.   (if (not (tblobjname "block" block-name))
  20.     (if (and dwg-name (setq dwg-name (findfile dwg-name)))
  21.       (progn (setq odbx (vla-getinterfaceobject acad (strcat "ObjectDBX.AxDbDocument." (itoa (atoi (getvar "acadver"))))))
  22.              (vla-open odbx dwg-name :vlax-true)
  23.              (if (= (type
  24.                       (setq ext_def (vl-catch-all-apply (function (lambda () (vla-item (vla-get-blocks odbx) block-name)))))
  25.                     ) ;_ end of type
  26.                     'vla-object
  27.                  ) ;_ end of =
  28.                (progn (vla-copyobjects
  29.                         odbx
  30.                         (vlax-make-variant
  31.                           (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list ext_def))
  32.                         ) ;_ end of vlax-make-variant
  33.                         (vla-get-blocks adoc)
  34.                       ) ;_ end of vla-CopyObjects
  35.                       (setq def (vla-item (vla-get-blocks adoc) block-name))
  36.                ) ;_ end of progn
  37.              ) ;_ end of if
  38.              (vlax-release-object odbx)
  39.       ) ;_ end of progn
  40.     ) ;_ end of if
  41.     (setq def (vla-item (vla-get-blocks adoc) block-name))
  42.   ) ;_ end of if
  43.   (if def
  44.                        (vla-get-modelspace adoc)
  45.                        (vlax-3d-point
  46.                          (cond ((cdr (assoc "ins" ins-params)))
  47.                                (t '(0. 0. 0.))
  48.                          ) ;_ end of cond
  49.                        ) ;_ end of vlax-3d-point
  50.                        block-name
  51.                        (cond ((cdr (assoc "x" ins-params)))
  52.                              (t 1.)
  53.                        ) ;_ end of cond
  54.                        (cond ((cdr (assoc "y" ins-params)))
  55.                              ((cdr (assoc "x" ins-params)))
  56.                              (t 1.)
  57.                        ) ;_ end of cond
  58.                        (cond ((cdr (assoc "z" ins-params)))
  59.                              ((cdr (assoc "x" ins-params)))
  60.                              (t 1.)
  61.                        ) ;_ end of cond
  62.                        (cond ((cdr (assoc "rot" ins-params)))
  63.                              (t 0.)
  64.                        ) ;_ end of cond
  65.                      ) ;_ end of vla-insertblock
  66.            ) ;_ end of setq
  67.     ) ;_ end of progn
  68.   ) ;_ end of defun
  69.   ref
  70. ) ;_ end of defun
  71. ;|
  72.  
  73.  ;; example command
  74.  
  75. (defun c:isfod (/ doc)
  76.   (vl-load-com)
  77.   (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  78.   (import-block-def-and-insert
  79.     "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  80.     "HCR22"
  81.     (list (cons "ins" (getpoint "\nInsertion point : ")))
  82.   ) ;_ end of import-block-def-and-insert
  83.   (vla-endundomark doc)
  84.   (princ)
  85. ) ;_ end of defun
  86. |;


Thanks kpblc for your valuable time and efforts. These codes are working great !. Happy to learn from all of you lisp masters.

Regards,
Vincent
« Last Edit: June 29, 2020, 09:19:49 PM by vincent.r »

vincent.r

  • Newt
  • Posts: 63
Re: Function not working
« Reply #5 on: June 30, 2020, 12:36:47 AM »
Another one simple code with ObjectDbx:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun import-block-def-and-insert (dwg-name block-name ins-params / adoc acad odbx def ext_def ref)
  3.                                    ;|
  4.   ;; imports and insert blocks to current dwg
  5.    dwg-name : dwg filename with block
  6.    block-name : block name
  7.    ins-params : parameters for inserting block:
  8.      '(("ins" . <>) ; insertion point . nil -> '(0. 0. 0.)
  9.        ("rot" . <>) ; rotation angle. nil -> 0.
  10.        ("x" . <>)   ; x scale factor. nil -> 1.
  11.        ("y" . <>)   ; y scale factor. nil -> use x scale
  12.        ("z" . <>)   ; z scale factor. nil -> use y scale
  13. Call sample:
  14. (import-block-def-and-insert (getfiled "library file" "" "dwg" 4) "pik-ind-3d_section{0.7}" nil)
  15. (import-block-def-and-insert "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  16. "HCR22" (list (cons "ins" (getpoint "\nInsertion point : "))))
  17.  
  18.   |;
  19.   (if (not (tblobjname "block" block-name))
  20.     (if (and dwg-name (setq dwg-name (findfile dwg-name)))
  21.       (progn (setq odbx (vla-getinterfaceobject acad (strcat "ObjectDBX.AxDbDocument." (itoa (atoi (getvar "acadver"))))))
  22.              (vla-open odbx dwg-name :vlax-true)
  23.              (if (= (type
  24.                       (setq ext_def (vl-catch-all-apply (function (lambda () (vla-item (vla-get-blocks odbx) block-name)))))
  25.                     ) ;_ end of type
  26.                     'vla-object
  27.                  ) ;_ end of =
  28.                (progn (vla-copyobjects
  29.                         odbx
  30.                         (vlax-make-variant
  31.                           (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list ext_def))
  32.                         ) ;_ end of vlax-make-variant
  33.                         (vla-get-blocks adoc)
  34.                       ) ;_ end of vla-CopyObjects
  35.                       (setq def (vla-item (vla-get-blocks adoc) block-name))
  36.                ) ;_ end of progn
  37.              ) ;_ end of if
  38.              (vlax-release-object odbx)
  39.       ) ;_ end of progn
  40.     ) ;_ end of if
  41.     (setq def (vla-item (vla-get-blocks adoc) block-name))
  42.   ) ;_ end of if
  43.   (if def
  44.                        (vla-get-modelspace adoc)
  45.                        (vlax-3d-point
  46.                          (cond ((cdr (assoc "ins" ins-params)))
  47.                                (t '(0. 0. 0.))
  48.                          ) ;_ end of cond
  49.                        ) ;_ end of vlax-3d-point
  50.                        block-name
  51.                        (cond ((cdr (assoc "x" ins-params)))
  52.                              (t 1.)
  53.                        ) ;_ end of cond
  54.                        (cond ((cdr (assoc "y" ins-params)))
  55.                              ((cdr (assoc "x" ins-params)))
  56.                              (t 1.)
  57.                        ) ;_ end of cond
  58.                        (cond ((cdr (assoc "z" ins-params)))
  59.                              ((cdr (assoc "x" ins-params)))
  60.                              (t 1.)
  61.                        ) ;_ end of cond
  62.                        (cond ((cdr (assoc "rot" ins-params)))
  63.                              (t 0.)
  64.                        ) ;_ end of cond
  65.                      ) ;_ end of vla-insertblock
  66.            ) ;_ end of setq
  67.     ) ;_ end of progn
  68.   ) ;_ end of defun
  69.   ref
  70. ) ;_ end of defun
  71. ;|
  72.  
  73.  ;; example command
  74.  
  75. (defun c:isfod (/ doc)
  76.   (vl-load-com)
  77.   (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  78.   (import-block-def-and-insert
  79.     "C:\\Users\\shrinand_bhedasgaonk\\Desktop\\stuff\\CAD Tools_1_3-18-2020\\Tools Electrical Automation\\block insert\\LegendDrawing.dwg"
  80.     "HCR22"
  81.     (list (cons "ins" (getpoint "\nInsertion point : ")))
  82.   ) ;_ end of import-block-def-and-insert
  83.   (vla-endundomark doc)
  84.   (princ)
  85. ) ;_ end of defun
  86. |;


Thanks kpblc for your valuable time and efforts. These codes are working great !. Happy to learn from all of you lisp masters.

Regards,
Vincent

sometimes function gives error

error: Automation Error. Description was not provided.

What could be the reason ?

kpblc

  • Bull Frog
  • Posts: 357
Re: Function not working
« Reply #6 on: June 30, 2020, 12:51:47 AM »
File could be opened or read-only mode. ObjectDBX requires full access to file.
Actually I check is file read-only (using vl-file-systime function and check file attributes). And if it does - I copy file to %temp% folder, proceed it and then erase it.
Also you can use vl-catch-* functions to catch any kind of errors.
Sorry for my English.

vincent.r

  • Newt
  • Posts: 63
Re: Function not working
« Reply #7 on: June 30, 2020, 08:17:52 PM »
File could be opened or read-only mode. ObjectDBX requires full access to file.
Actually I check is file read-only (using vl-file-systime function and check file attributes). And if it does - I copy file to %temp% folder, proceed it and then erase it.
Also you can use vl-catch-* functions to catch any kind of errors.

Thanks kpblc for your valuable suggestion.
Is it possible to avoid this error (screenshot attached) ? I believe its looping error.

Regards,
Vincent

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Function not working
« Reply #8 on: June 30, 2020, 08:58:44 PM »
It’s not a looping error. Re-read the error description.

Globally change the variable name acad to app.

Cheers.
Engineering Technologist • Programmer Analyst • CAD Specialist
Multi-Discipline • Design • Drafting • Document Control • Automation.
cadanalyst@gmail.com • www.linkedin.com/in/cadanalyst

kpblc

  • Bull Frog
  • Posts: 357
Re: Function not working
« Reply #9 on: July 01, 2020, 04:36:27 AM »
Ah, sorry - my mistake. I forgot that last AutoCAD versions could reserve this symbol.
Thanks for reply, MP!
Sorry for my English.

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Function not working
« Reply #10 on: July 01, 2020, 09:55:40 AM »
You’re most welcome - cheers.
Engineering Technologist • Programmer Analyst • CAD Specialist
Multi-Discipline • Design • Drafting • Document Control • Automation.
cadanalyst@gmail.com • www.linkedin.com/in/cadanalyst