Lisp to create pdf and send mail

Lisp to create pdf and send mail

larsr2866
Enthusiast Enthusiast
2,899 Views
30 Replies
Message 1 of 31

Lisp to create pdf and send mail

larsr2866
Enthusiast
Enthusiast

Good day everyone,

 

I was wondering if it's even possible to create a lisp for printing a specific layout (user imput to specify the layout) to pdf (user imput to name the pdf) + create a mail with the pdf as attachement?

 

Thanks for your reactions.

Lars

0 Likes
2,900 Views
30 Replies
Replies (30)
Message 2 of 31

cadffm
Consultant
Consultant

If a mail program is available with a valid mail account access and activeX API (Outlook for example) , no problem.

Sebastian

0 Likes
Message 3 of 31

larsr2866
Enthusiast
Enthusiast

Thanks for your reaction. I do use outlook for my mailing, so that's already good.

Could you (or anyone) help me getting started with this? Or do you know where i could find some explanation for these steps? Because this kind of lisp writing would be totally new for me.

 

Thanks.

0 Likes
Message 4 of 31

ronjonp
Mentor
Mentor

Code HERE to create  an outlook message and attach files to send. Not sure if it still works since it's 10 years old!

 

0 Likes
Message 5 of 31

larsr2866
Enthusiast
Enthusiast

I have been looking to create this step by step, so first i want to print my layout to pdf (and save it automatic to the location of the dwg). And after i will try to create my mail. This i what i got so far, but it doesn't work (its says: malformed list on input?) Any help?

 

(defun c:PRINTTOPDF (/)

(command "tilemode" "0")

(command "-plot" "yes" "plan_A_1" "Dwg To PDF.pc3" "Previous paper size (841.00 x 594.00 mm)" "Milimeters"
"Landscape" "No" "layout" "1:1" "0.00,0.00" "Yes" "LEGPLAN.ctb" "Yes" "No" "No" "No"

(princ)

 

Thanks!

0 Likes
Message 6 of 31

cadffm
Consultant
Consultant

This has notrhing to do with programming and 'send a mail', so next time please:

Different problems, different topics (seperates threads).

 

Without your file and enviroment we have to guess, that need more time as reading facts (your DWG).

Help for help yourself:

Set CMDDIA to 0 and run your PRINTTOPDF

You can see where the problem is in your log [F2]

Copy the part from Command:PRINTTOPDF to the end in your next answer and we are also able to see the result,

perhaps we can see and help to solve the problem then.

 

But i will try it with guessing:

 

 

(command "-plot" "yes" "plan_A_1" "Dwg To PDF.pc3" "Previous paper size (841.00 x 594.00 mm)" "Millimeters"
"Landscape" "No" "layout" "1:1" "0.00,0.00" "Yes" "LEGPLAN.ctb" "Yes" "No" "No" "No" )

 

1. Where is you closing bracket of the command statement?

2. Is "Milimeters" the right word?

3. Is "Previous paper size (841.00 x 594.00 mm)" really a valid input for your pagesetup at time you try to start the plot?

Sebastian

0 Likes
Message 7 of 31

Sea-Haven
Mentor
Mentor

Do a search here there is email a file from Autocad.

0 Likes
Message 8 of 31

larsr2866
Enthusiast
Enthusiast

Thanks for your reply. (ok, i will separate these different topics in the future)

I tried to run it with a correction of (millimeters => my mistake indeed), and even tried to change my paper size to ISO full bleed A1 (841.00 x 594.00 mm) but it keeps giving the same error. (malformed list on input)?

 

I'm not sure what i am doing wrong?

i've uploaded my dwg and lisp file. I hope someone can help me fix this?

 

Thanks!

0 Likes
Message 9 of 31

cadffm
Consultant
Consultant

@cadffm  schrieb:

(command "-plot" "yes" "plan_A_1" "Dwg To PDF.pc3" "Previous paper size (841.00 x 594.00 mm)" "Millimeters"
"Landscape" "No" "layout" "1:1" "0.00,0.00" "Yes" "LEGPLAN.ctb" "Yes" "No" "No" "No" )

 

1. Where is you closing bracket of the command statement?


 

Where is the closing bracket for your DEFUN statement?

Add one ) at the end..

 

(defun ; open ONE

c:PRINTTOPDF (/) ; open a second and close the second bracket, so you have still one bracket from defun open

(command "-plot" "yes" "plan_A_1" "Dwg To PDF.pc3" "ISO full bleed A1 ; open a second

(841.00 x 594.00 mm)" "Millimeters" ; open and close a third
"Landscape" "No" "layout" "1:1" "0.00,0.00" "Yes" "legplan.ctb" "Yes" "No" "No" "No" ) ; close the second from command statement.

(princ) ; open and close a second

 

 

...

there is still missing the closing bracket of your DEFUN statement.

 

 

 

Sebastian

0 Likes
Message 10 of 31

larsr2866
Enthusiast
Enthusiast

Thanks, it now uploads without any problems!

Only, the function doesn't work : "invalid option keyword"

However, i followed the steps (with keywords) manually in autocad first, before i created the lisp. So i'm not seeing the troubles?

 

I've copied the problems in my log (F2)

 

Command: CMDDIA
Enter new value for CMDDIA <0>: 1
**** No System Variable Changed ****
Command: Specify opposite corner or [Fence/WPolygon/CPolygon]:
Command: PRINTTOPDF
-plot Detailed plot configuration? [Yes/No] <No>: yes
Enter a layout name or [?] <plan_A_1>: plan_A_1 Enter an output device name or [?] <HP Designjet T7200ps HPGL2>: Dwg To PDF.pc3 Enter paper size or [?] <ISO A1 (594.00 x 841.00 MM)>: ISO full bleed A1 ; open a second
(841.00 x 594.00 mm)
Command: Millimeters Unknown command "MILLIMETERS".  Press F1 for help.
Command: Landscape Unknown command "LANDSCAPE".  Press F1 for help.
Command: No Unknown command "NO".  Press F1 for help.
Command: layout
Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] <set>: 1:1
Invalid option keyword.
; error: Function cancelled

0 Likes
Message 11 of 31

cadffm
Consultant
Consultant

@larsr2866  schrieb:
...
So i'm not seeing the troubles?

 

...

Enter paper size or [?] <ISO A1 (594.00 x 841.00 MM)>: ISO full bleed A1 ; open a second
(841.00 x 594.00 mm)
Command: Millimeters Unknown command "MILLIMETERS".  Press F1 for help.  <----


After your input for the papersize the PLOT command is terminated and no command is active.

 

The message "Only, the function doesn't work : "invalid option keyword"" is not the problem, it is just a subsequent error because you started the LAYOUT command..

 

See twice, take enough time and you will reach the goal.

In in LOG i can see some of my hints/comments..i thought you should start new.

Use a new DWG with simple content/layout,

check your lisp and because you write the command in this way because you can add comments easier and it is more readable.

 

(command "_.-PLOT" ; plot sequence of a layout

"_yes" ; detailed version
"plan_A_1"; layoutname
"Dwg To PDF.pc3" ; plotter
"ISO full bleed A1 (841.00 x 594.00 mm)" ; papersize
"_millimeters" ; and so on..
"_landscape"
"_no"
"_layout"
"1:1"
"0.00,0.00"
"_yes"
"legplan.ctb"
"_yes"
"_no"
"_no"
"_no"
);_command

 

Id didn't check this 1:1, if you are stil in trouble i will do it the next time.

Sebastian

0 Likes
Message 12 of 31

larsr2866
Enthusiast
Enthusiast

Thanks it works!

Except for the saving location. I doesn't save it on the dwg location, but the last history location.

Is this possible?

0 Likes
Message 13 of 31

cadffm
Consultant
Consultant

@larsr2866  schrieb:

Thanks it works!

Except for the saving location. I doesn't save it on the dwg location, but the last history location.

Is this possible?


Sorry, i don't understand.  You you like the current DWG location, or not?

 

Your lisp above do not create a PDF, no where!

If you like to create the pdf you have to add the filename, if saving the plotsettings as current pageseup and if you really want to plot 🙂

 

So you have to add 3 arguments more at the end of command

 

(if (zerop(getvar"DWGTITLED")) "~" (strcat "C:\\Temp\\" (vl-filename-base(getvar "DWGNAME"))))
"_no"
"_yes"

 

or for the current DWG folder

 

(if (zerop(getvar"DWGTITLED")) "~" "")
"_no"
"_yes"

 

 

Sebastian

0 Likes
Message 14 of 31

larsr2866
Enthusiast
Enthusiast

Wow thanks, i'm learning 🙂

It is indeed the intension to save the pdf at the current dwg-location so i used this:

 

(if (zerop(getvar"DWGTITLED")) "~" "")
"_no"
"_yes"

 

Is it possible to create a user input for typing my pdf-name? I guess i have to replace "DWGTITLED" to "userinput"?

0 Likes
Message 15 of 31

cadffm
Consultant
Consultant

(if (zerop(getvar"DWGTITLED")) "~" "")

I guess i have to replace "DWGTITLED" to "userinput"?

 

 

Please open your help[F1] and read about if (AutoLISP)

and DWGTITLED (System Variable) and ZEROP

 

Replace

(if (zerop(getvar"DWGTITLED")) "~" "")

with

"~"

 

or you have to write more code.. and you are searching for getstring (AutoLisp)

this is just a (too) simple testcode:

 

(if (zerop(getvar"DWGTITLED"))

     "~"

     (strcat (getvar "DWGPREFIX")(progn(while (= "" (setq FN (vl-filename-base (getstring T "PDF Filename: "))))) FN) ".pdf")

)

 

but you have to add security-check for valid filenames and a smarter (= "" test.

 

 

Sebastian

0 Likes
Message 16 of 31

larsr2866
Enthusiast
Enthusiast

Thank you so much! you're great!

Good to know that "~" is for user input!

It works like a charm!

 

I'm will now try to find out how to create a mail within autocad 🙂

 

Greets,

Lars

0 Likes
Message 17 of 31

cadffm
Consultant
Consultant

that "~" is for user input!

 

 

This input at this position is NOT for USERINPUT.

At this time the plot command is asking for filename..

Plot command started by hand will open the 'select file dialog'as dialogwindow or as commandlineversion, depending on FileDia setting.

Plot dialog started by (command) will use the commandlineversion by default.

 

"~" is the input to change from commandline to dialogwindow version.

 

You can also replace it with "c://temp//funnyname.pdf" if you like to have this location and name for the pdf 🙂

 

Or just 

PAUSE

Pause stand for Userinput.

Sebastian

0 Likes
Message 18 of 31

larsr2866
Enthusiast
Enthusiast

For sending a mail into autocad,  i have tried the link from rperez, but i think i doesn't work anymore.

It says  "error: no function definition: RJP-Outlookmessage". Does anyone know something else? Because this is a bit out of my knowledge 🙂

 

(defun c:mail (/)

;;Usage
(rjp-OutlookMessage
  ;;email address (multiple separated by semicolon)
  "test@nowhere.com;test2@nowhere.com"
  ;;Subject
  "Test Email"
  ;;Attachments as a list of strings
  '("C:\\Users\\Desktop\\001.jpg" )
  ;;Text in body of email
  "Nothing to read here :)"
  ;;nil will open email to edit...T will send email in the background
  nil
)

(defun rjp-OutlookMessage
       (To Subject AttachmentList Body Send / objMail objOL)
  (if (and (setq objOL (vlax-get-or-create-object "Outlook.Application"))
    (setq objMail (vlax-invoke-method objOL 'CreateItem 0))
      )
    (progn
      (vlax-put objMail 'To To)
      (vlax-put objMail 'Subject Subject)
      (vlax-put objMail 'Body Body)
      (foreach file AttachmentList
 (vl-catch-all-apply
   'vlax-invoke
   (list (vlax-get objMail 'Attachments)
  'Add
  file
   )
 )
      )
      (if send
 (vlax-invoke objMail 'Send)
 (vlax-invoke objMail 'Display :vlax-true)
      )
      (vlax-release-object objOL)
      (vlax-release-object objMail)
    )
  )
  (princ)
)
)

0 Likes
Message 19 of 31

cadffm
Consultant
Consultant

As the message said: Where is the RJP-Outlookmessage function defined? it isn't loaded - your fail.

This function is available at the same place - follow rperez link again.

 

(defun RJP-Outlookmessage

Sebastian

0 Likes
Message 20 of 31

larsr2866
Enthusiast
Enthusiast

Thanks, i got this working. Only problem, it worked once (i have to restart my computer to get this working another time) How is this possible?

 

I also noticed that it has some issues with the tekst formatting (color, size, mail-signature with logo,...)

I was wondering if this could be fixed with a standard mail template, that opens with autocadlisp?

So i made a template for my standard mail in outlook called  "standardmail.oft"

Could it be possible to open this in autocad?

 

Only poblem i see is that it should automatically attach my created pdf (and subject name of the mail should be the same as my pdf name) hmmm, i think i'm stuck with this.

 

;; Usage:
; (rjp-OutlookMessage
;  "test@nowhere.com;test2@nowhere.com"                         ;; email address (multiple separated by semicolon)
;  "Test Email"                                                          ;; Subject
;  '("C:\\test\\file1.txt" "C:\\test\\file2.txt" "C:\\test\\file3.txt")  ;; Attachments as a list of strings
;  "Nothing to read here :)"                                             ;; Text in body of email
;  nil                                                                   ;; nil will open email to edit...T will send email in the background
; )
(defun rjp-OutlookMessage (To Subject AttachmentList Body Send / objMail objOL)
 (and
   (setq objOL (vlax-get-or-create-object "Outlook.Application"))
   (setq objMail (vlax-invoke-method objOL 'CreateItem 0))
   (progn
     (vlax-put objMail 'To To)
     (vlax-put objMail 'Subject Subject)
     (vlax-put objMail 'Body Body)
     (foreach file AttachmentList (vl-catch-all-apply 'vlax-invoke (list (vlax-get objMail 'Attachments) 'Add file)) )
     (if send (vlax-invoke objMail 'Send) (vlax-invoke objMail 'Display :vlax-true) )
     (vlax-release-object objOL)
     (vlax-release-object objMail)
   ); progn
 ); and
 (princ)
); defun rjp-OutlookMessage

 

0 Likes