AutoLISP DWGToPDF: bad argument type: VLA-OBJECT nil

AutoLISP DWGToPDF: bad argument type: VLA-OBJECT nil

adonispaz3G2GB
Explorer Explorer
1,481 Views
11 Replies
Message 1 of 12

AutoLISP DWGToPDF: bad argument type: VLA-OBJECT nil

adonispaz3G2GB
Explorer
Explorer

Im trying to set up Design Automation for AutoCAD. After submitting the work item I get a response saying it was successful. But when I look at the logs it seems to have trouble running the script

AutoCAD menu utilities loaded.
[07/23/2024 23:02:54] Command:
[07/23/2024 23:02:54] Command: Enter BACKSPACE to interrupt script.
[07/23/2024 23:02:54] Command:
[07/23/2024 23:02:54] Command: (load "DWGToPDF.lsp")
[07/23/2024 23:02:54] C:DWGTOPDF
[07/23/2024 23:02:54] Command: (c:DWGToPDF)
[07/23/2024 23:02:54] ; error: bad argument type: VLA-OBJECT nil
[07/23/2024 23:02:55] End AutoCAD Core Engine standard output dump.
[07/23/2024 23:02:55] Error: AutoCAD Core Console output contains error(s).
[07/23/2024 23:02:55] End script phase.

Am I doing something wrong in my script?

(defun c:DWGToPDF()
  (vl-load-com)

  ; Suppress file dialog boxes
  (setq originalFileDia (getvar "FILEDIA"))
  (setvar "FILEDIA" 0)

  ; Get the active document in AutoCAD
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))

  ; Check if the document is valid
  (if (null doc)
    (progn
      (princ "\nError: No active document found.")
      (exit)
    )
  )

  ; Get the full path of the active document
  (setq docPath (vlax-get-property doc 'FullName))

  ; Extract the directory path from the full path
  (setq outputDir (vl-filename-directory docPath))

  ; Get the Layouts collection
  (setq layouts (vla-get-Layouts doc))

  ; List to store output PDF file paths
  (setq outputFiles '())

  ; Iterate through all layouts
  (vlax-for layout layouts
    (setq layoutName (vla-get-Name layout))
    
    ; Skip the "Model" layout
    (if (/= layoutName "Model")
      (progn
        ; Print the name of the current layout for debugging
        (princ (strcat "\nProcessing layout: " layoutName "\n"))

        ; Generate the output file path
        (setq sanitizedLayoutName (vl-string-translate "/\\:*?\"<>|" "_" layoutName))
        (setq outputFile (strcat outputDir "\\" sanitizedLayoutName ".pdf"))
        (princ (strcat "\nOutput file path: " outputFile "\n"))

        ; Add to output files list
        (setq outputFiles (cons outputFile outputFiles))

        ; Start the plot command
        (command
          "-plot"                                   ; Start the plot command
          "Yes"                                     ; Detailed plot configuration
          layoutName                                ; Layout name
          "DWG To PDF.pc3"                          ; Output device name
          "ARCH full bleed E1 (30.00 x 42.00 Inches)"; Paper size
          "inches"                                  ; Paper units (default)
          "Landscape"                               ; Drawing orientation
          "No"                                      ; Plot upside down?
          "Extents"                                 ; Plot area
          "Fit"                                     ; Plot scale
          "0,0"                                  ; Plot offset (centered)
          "Yes"                                     ; Plot with plot styles?
          ""                          ; Plot style table name
          "Yes"                                     ; Plot with lineweights?
          "Yes"                                     ; Scale lineweights with plot scale?
          "No"                                      ; Plot paper space last
          "No"                                      ; Hide plot
          outputFile                                ; Output file name
          "No"                                      ; Prompt for plot details
          "No"                                     ; Save changes to layout
        )
      )
    )
  )

  ; Reset FILEDIA variable to original state
  (setvar "FILEDIA" originalFileDia)

  ; Print completion message
  (princ "\nPlotting complete.\n")

  ; Print output files for Design Automation to recognize
  (foreach file outputFiles
    (princ (strcat "\nOutput file: " file "\n"))
  )

  (princ)
)

 

0 Likes
Accepted solutions (1)
1,482 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

Has the script worked in the past? 

0 Likes
Message 3 of 12

marko_ribar
Advisor
Advisor

Maybe you need to run script through ACAD session and not through CoreConsole... And BTW, can we see script file (*.scr)...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 4 of 12

paullimapa
Mentor
Mentor
Accepted solution

The vla functions most likely are not supported with accoreconsole. Even (layoutlist) function is not supported. This the workaround to cycle through layouts in accoreconsolle

(defun GetLayoutListACoreConsole ()
 (defun massoc (key alist / x nlist)
  (foreach x alist
   (if (eq key (car x))
    (setq nlist (cons (cdr x) nlist))
   )
  )
  (reverse nlist)
 )
 (massoc 3 (dictsearch(namedobjdict)"acad_layout"))
)

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 12

pbejse
Mentor
Mentor

@paullimapa wrote:

The vla functions most likely are not supported with accoreconsole. Even (layoutlist) function is not supported. 


@marko_ribar wrote:

Maybe you need to run script through ACAD session and not through CoreConsole... And BTW, can we see script file (*.scr)...


I did not even consider that Design Automation use accoreconsole

Good one @paullimapa  @marko_ribar 

 

 

0 Likes
Message 6 of 12

paullimapa
Mentor
Mentor

@pbejse Just reading the output from OP:

[07/23/2024 23:02:55] Error: AutoCAD Core Console output contains error(s).
[07/23/2024 23:02:55] End AutoCAD Core Engine standard output dump.

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 7 of 12

CLL_PBE
Contributor
Contributor

@paullimapa wrote:

@pbejse Just reading the output from OP:


You are right, I've been away too long 😀

0 Likes
Message 8 of 12

adonispaz3G2GB
Explorer
Explorer

Yes this script has always worked locally. But now im trying to get it to work on Design Automation which has yet to work.

0 Likes
Message 9 of 12

adonispaz3G2GB
Explorer
Explorer
This was it right here. I had to re-write my entire AutoLISP script to not use VLA functions and replaced layoutlist with the workaround @paullimapa provided. Thank you very much!
0 Likes
Message 10 of 12

paullimapa
Mentor
Mentor

glad to have helped...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 12

Sea-Haven
Mentor
Mentor

Just tried it is there a problem that the list includes "Model" ? It just requires a workaround checking layout name or if Model is always last, just use (repeat (- (length nlist) 1) so does not call "Model". Or just remove "Model" from nlist as part of defun.

0 Likes
Message 12 of 12

paullimapa
Mentor
Mentor

exactly


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes