write LISP, but it doesn't work

write LISP, but it doesn't work

J1Z9
Participant Participant
1,570 Views
17 Replies
Message 1 of 18

write LISP, but it doesn't work

J1Z9
Participant
Participant

Thanks for the help, code works as described

 

0 Likes
Accepted solutions (4)
1,571 Views
17 Replies
Replies (17)
Message 2 of 18

ronjonp
Advisor
Advisor

Maybe these pointers will help:

;; Change this
(setq ss
       (ssget "X" '((-4 . "<OR") (0 . "LWPOLYLINE") (0 . "POLYLINE") (-4 . "OR>") (8 . "C-FDS-PLOT")))
)
;; To this note "_X"
(setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "C-FDS-PLOT"))))


;; Also change this (not the way to get a bounding box)
(setq minpt (cdr (assoc 10 (entget ent))))
(setq maxpt (cdr (assoc 11 (entget ent))))
;; To this
(vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
(mapcar 'set '(minpt maxpt) (mapcar 'vlax-safearray->list (list minpt maxpt)))
0 Likes
Message 3 of 18

Sea-Haven
Mentor
Mentor

More ideas when you use ssget "X" it gets object via creation order, so no need for 

 

 

 

 

(setq entlist (vl-sort (mapcar 'ssname (vl-list->obj ss)) '<))

 

 

NOT TESTED no CAD.

 

 

(defun c:FDSPLOT ( / ss ent minPt maxPt entlist)
  ;; Select either LWPOLYLINE or POLYLINE entities on the C-FDS-PLOT layer
  (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "C-FDS-PLOT"))))

  ;; Check if any entities were selected
  (if ss
  (alert "No objects found on layer C-FDS-PLOT \n\n will skip ")
    (progn
      ;; Loop through each entity 
      (repeat (setq x (sslength ss))
        ;; Get the bounding box (extents) of the entity
		(setq ent (car (ssname ss (setq x (1- x)))))
        (vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
        (mapcar 'set '(minpt maxpt) (mapcar 'vlax-safearray->list (list minpt maxpt)))

        ;; Call the plot command with ANSI B Full Bleed settings and object lineweights
        (command "._PLOT" "Y" "" "Model" "Window" minPt maxPt "Fit" "Y"
                 "None" "AutoCAD PDF (General Documentation).pc3" "ANSI_B_(11.00_x_17.00_Inches)_Full_Bleed"
                 "Inches" "Landscape"
                 "Yes" "monochrome.ctb" "Yes" "No" "Yes" "No" "")
      )
    )
  )
  ;; Output completion message
  (princ "\nAuto plot to PDF with ANSI B Full Bleed using AutoCAD PDF (General Documentation) complete.")
  (princ)
)​

 

 

0 Likes
Message 4 of 18

ronjonp
Advisor
Advisor
Accepted solution

@Sea-Haven This breaks the whole thing:

  ;; Check if any entities were selected
  (if ss
  (alert "No objects found on layer C-FDS-PLOT \n\n will skip ")

 

And the "complete" prompt should be pulled within the progn all like so:

(defun c:fdsplot (/ ss ent minpt maxpt entlist)
  ;; Check if any entities were selected
  (if ;; Select either LWPOLYLINE or POLYLINE entities on the C-FDS-PLOT layer
      (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "C-FDS-PLOT"))))
    (progn
      ;; Loop through each entity 
      (repeat (setq x (sslength ss))
	;; Get the bounding box (extents) of the entity
	(setq ent (car (ssname ss (setq x (1- x)))))
	(vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
	(mapcar 'set '(minpt maxpt) (mapcar 'vlax-safearray->list (list minpt maxpt)))
	;; Call the plot command with ANSI B Full Bleed settings and object lineweights
	(command "._PLOT"	       "Y"		     ""
		 "Model"	       "Window"		     minpt
		 maxpt		       "Fit"		     "Y"
		 "None"		       "AutoCAD PDF (General Documentation).pc3"
		 "ANSI_B_(11.00_x_17.00_Inches)_Full_Bleed"  "Inches"
		 "Landscape"	       "Yes"		     "monochrome.ctb"
		 "Yes"		       "No"		     "Yes"
		 "No"		       ""
		)
      )
      (princ
	"\nAuto plot to PDF with ANSI B Full Bleed using AutoCAD PDF (General Documentation) complete."
      )
    )
    (alert "No objects found on layer C-FDS-PLOT \n\n will skip ")
    ;; Output completion message
  )
  (princ)
)

 

0 Likes
Message 5 of 18

Sea-Haven
Mentor
Mentor

@ronjonp  yes my fault I normally do no CAD did not test.

 

(if (= ss nil)
(alert "no selection set will skip "))
(progn

 

 

0 Likes
Message 6 of 18

J1Z9
Participant
Participant

Thanks for the help, after I run the scrip you modified, I got following message:

 

bad argument type: consp <Entity name: 2d92b603dd0>

0 Likes
Message 7 of 18

J1Z9
Participant
Participant

code works as described


0 Likes
Message 8 of 18

Sea-Haven
Mentor
Mentor
Accepted solution

For a window need 2 points not 4 values. Not tested.

(list (car minpt) (cadr minpt))
(list (car maxpt) (cadr maxpt))

 

 

 

Message 9 of 18

paullimapa
Mentor
Mentor

First of all ChatGPT is not going to give you the proper coding when it comes to AutoLISP.

So the best thing to do is to have others who are familiar with AutoLISP to do the coding for you.

Otherwise you'll end up with code that is just wrong.

There are a number of problems I can pick up right away:

1. Because you're plotting multiple pdfs you have to generate a unique file name so AutoCAD will know what to call each pdf at time of the plot file name request.

2. There's a particular plot sequence you need to conform to otherwise the plot command will fail.

3. Paper size name entry like the other responses during the plot command responses need to match 100% and no underscores are accepted.

4. The Plotting sequence is different when it comes to plotting Model vs Layout.

The following is a screen shot of plain AutoCAD on the plot sequence.  You can do the same on your version of AutoCAD Civil 3D to see if it matches:

 

Command: FILEDIA
Enter new value for FILEDIA <1>: 0
Command: -PLOT
Detailed plot configuration? [Yes/No] <No>: Yes
Enter a layout name or [?] <Model>: Model
Enter an output device name or [?] <None>: AutoCAD PDF (General Documentation).pc3
Enter paper size or [?] <ANSI full bleed B (11.00 x 17.00 Inches)>: ANSI full bleed B (11.00 x 17.00 Inches)
Enter paper units [Inches/Millimeters] <Inches>: Inches
Enter drawing orientation [Portrait/Landscape] <Landscape>: Landscape
Plot upside down? [Yes/No] <No>: No
Enter plot area [Display/Extents/Limits/View/Window] <Display>: Window
Enter lower left corner of window <40343.955610,11524.472214>: Enter upper right corner of window <40343.955610,11524.472214>: Enter plot scale (Plotted Inches=Drawing Units) or [Fit] <Fit>:
Enter plot offset (x,y) or [Center] <0.00,0.00>: center
Plot with plot styles? [Yes/No] <Yes>: Yes
Enter plot style table name or [?] (enter . for none) <>: monochrome.ctb
Plot with lineweights? [Yes/No] <Yes>: Yes
Enter shade plot setting [As displayed/legacy Wireframe/legacy Hidden/Visual styles/Rendered] <As displayed>: As
Enter file name <C:\Users\HP-Spector\Downloads\H6 svendeprøve AutoCAD Christina-Model.pdf>:
Save changes to page setup [Yes/No]? <N>
Proceed with plot [Yes/No] <Y>:
Command: FILEDIA
Enter new value for FILEDIA <0>: 1

 

Notice I started with setting FILEDIA to 0 so you can see the command line prompting for the plot file name.

Just remember to set FILEDIA back to 1 when you've finish testing.

Others who have already responded to you can chime in on getting a unique filename during the while loop

 


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

J1Z9
Participant
Participant


 

 

Thanks for the tips and after I update my code with correct print order, this code currently prints to a single PDF file, but it overwrites the file on each loop iteration, so it won't combine all the plots into a single PDF, so I am wondering if the LISP could print all defined drawings in model space and combine them into a single PDF?

 

 

(defun c:fdsplot (/ ss ent minpt maxpt plottername papersize filepath)
  ;; Set plotter name, paper size, and file path
  (setq plottername "AutoCAD PDF (General Documentation).pc3")
  (setq papersize "ANSI full bleed B (17.00 x 11.00 Inches)")
  (setq filepath "C:\\FDSPLOT\\YourDrawing.pdf") ;; Specify the desired file path

  ;; Select entities on layer C-FDS-PLOT
  (if (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "C-FDS-PLOT"))))
    (progn
      ;; Loop through each entity
      (setq x (sslength ss)) ;; Set x to total number of entities
      (while (> x 0)
        (setq x (1- x)) ;; Decrement the count
        (setq ent (ssname ss x)) ;; Get the entity at the current index

        ;; Get the bounding box (extents) of the entity
        (vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
        (setq minpt (vlax-safearray->list minpt))  ;; Convert minpt to list
        (setq maxpt (vlax-safearray->list maxpt))  ;; Convert maxpt to list

        ;; PLOT command with corrected two-point input for window
        (command "._PLOT" "Y"             ;; Start plot, detailed plot configuration "Yes"
                 "Model"                 ;; Plot from model space
                 plottername             ;; Plotter name
                 papersize               ;; Paper size
                 "Inches"                ;; Paper units
                 "Landscape"             ;; Orientation
                 "No"                    ;; Don't plot upside down
                 "Window"                ;; Define plot area
                 (list (car minpt) (cadr minpt))  ;; Lower-left corner
                 (list (car maxpt) (cadr maxpt))  ;; Upper-right corner
                 "Fit"                   ;; Fit the plot to paper
                 "Center"                ;; Center the plot
                 "Yes"                   ;; Plot with plot styles
                 "monochrome.ctb"        ;; Plot style table
                 "Yes"                   ;; Plot with lineweights
                 "As displayed"          ;; Shade plot as displayed
                 filepath                ;; File path and name for PDF output
                 "No"                    ;; Don't save changes to page setup
                 "Y"                     ;; Proceed with plot
        )
        ;; Feedback for each entity plotted
        (princ (strcat "\nPlotted entity: " (itoa x))) 
      ) ;; End of while loop

      ;; Completion message
      (princ "\nAuto plot to PDF with ANSI full bleed B using AutoCAD PDF complete.")
    )
    ;; Alert if no objects found
    (alert "No objects found on layer C-FDS-PLOT.")
  ) ;; End of if
  (princ)
) ;; End of defun

 

 

0 Likes
Message 11 of 18

J1Z9
Participant
Participant

Hi Paul

you are totally right on those points you mentioned, and I have update the code accordinly. your 1st point really hit my issue now because when i do the plot, it will overwrite each file instead fo combine them together, so I am wondering if AUTOLISP could combine all the prints in to a single PDF. 

 

 

 

(defun c:fdsplot (/ ss ent minpt maxpt plottername papersize filepath)
  ;; Set plotter name, paper size, and file path
  (setq plottername "AutoCAD PDF (General Documentation).pc3")
  (setq papersize "ANSI full bleed B (17.00 x 11.00 Inches)")
  (setq filepath "C:\\FDSPLOT\\YourDrawing.pdf") ;; Specify the desired file path

  ;; Select entities on layer C-FDS-PLOT
  (if (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "C-FDS-PLOT"))))
    (progn
      ;; Loop through each entity
      (setq x (sslength ss)) ;; Set x to total number of entities
      (while (> x 0)
        (setq x (1- x)) ;; Decrement the count
        (setq ent (ssname ss x)) ;; Get the entity at the current index

        ;; Get the bounding box (extents) of the entity
        (vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
        (setq minpt (vlax-safearray->list minpt))  ;; Convert minpt to list
        (setq maxpt (vlax-safearray->list maxpt))  ;; Convert maxpt to list

        ;; PLOT command with corrected two-point input for window
        (command "._PLOT" "Y"             ;; Start plot, detailed plot configuration "Yes"
                 "Model"                 ;; Plot from model space
                 plottername             ;; Plotter name
                 papersize               ;; Paper size
                 "Inches"                ;; Paper units
                 "Landscape"             ;; Orientation
                 "No"                    ;; Don't plot upside down
                 "Window"                ;; Define plot area
                 (list (car minpt) (cadr minpt))  ;; Lower-left corner
                 (list (car maxpt) (cadr maxpt))  ;; Upper-right corner
                 "Fit"                   ;; Fit the plot to paper
                 "Center"                ;; Center the plot
                 "Yes"                   ;; Plot with plot styles
                 "monochrome.ctb"        ;; Plot style table
                 "Yes"                   ;; Plot with lineweights
                 "As displayed"          ;; Shade plot as displayed
                 filepath                ;; File path and name for PDF output
                 "No"                    ;; Don't save changes to page setup
                 "Y"                     ;; Proceed with plot
        )
        ;; Feedback for each entity plotted
        (princ (strcat "\nPlotted entity: " (itoa x))) 
      ) ;; End of while loop

      ;; Completion message
      (princ "\nAuto plot to PDF with ANSI full bleed B using AutoCAD PDF complete.")
    )
    ;; Alert if no objects found
    (alert "No objects found on layer C-FDS-PLOT.")
  ) ;; End of if
  (princ)
) ;; End of defun

 

 

0 Likes
Message 12 of 18

paullimapa
Mentor
Mentor

Take a look at the example in this thread:

Help with plotting lisp - Ghostscript or? - Autodesk Community - AutoCAD

@Sea-Haven provided the solution already back then 


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

J1Z9
Participant
Participant
Thank for the link, it looks like I need to use Ghostscript to run the batch plot in LISP ( or use Revers software)? unfortunately that my company cybersecurity won't approve these software or it may take years to review/test and approve it.
0 Likes
Message 14 of 18

paullimapa
Mentor
Mentor
Accepted solution

FYI: If your company's bought into Adobe Acrobat, that software can combine separate pdfs but can only be done manually.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 15 of 18

paullimapa
Mentor
Mentor
Accepted solution

You can still create separate pdfs by just modifying your code a bit like this.

1. Replace the following line of code:

(setq filepath "C:\\FDSPLOT\\YourDrawing.pdf") ;; Specify the desired file path

With this line:

(setq filepath (strcat (getvar"dwgprefix") (vl-filename-base (getvar"dwgname")))) ; current dwg path & filename

2. Since you're cycling through the selection set as part of your while loop with the following lines of code:

      (while (> x 0)
        (setq x (1- x)) ;; Decrement the count

you can use this number to create a unique suffix to your pdf file name as the response to the plot file name like this:

(strcat filepath "-" (itoa x) ".pdf")                ;; File path and name for PDF output

FYI: Since you're decrementing the number, the separate pdfs will be created in reverse order with the largest suffix # first and the smallest last.

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 16 of 18

J1Z9
Participant
Participant

thanks for the suggestion, the code works as described now. it prints indivudially, then I can use Bluebeam to combine them to a single file



0 Likes
Message 17 of 18

paullimapa
Mentor
Mentor

glad to have helped...cheers!!!


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

Sea-Haven
Mentor
Mentor

You can via lisp use Ghostscript to combine pdfs back into one pdf so just happens. Have a look at this code. It has not been edited to match this task.

 

Change this line to suit location of ghostscript, yes its free.

 

(setq gsexe "D:\\gs\\gs10.02.1\\bin\\gswin64c.exe")

 

 

 

0 Likes