Author Topic: Looking for help to get the code working the way I need it.  (Read 1197 times)

0 Members and 1 Guest are viewing this topic.

cadd4la

  • Newt
  • Posts: 27
Hello,

I'm looking to have the code use the _PDFATTACH command to attach a PDF file(s) scaled the PDF files(s) by .8 with specify on-screen insertion point turned on, on layer SCANS, using the node osnap.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:specs ()
  2.   ; Set the desired settings
  3.   (setq scaleFactor 0.8)
  4.   (setq layerName "SCANS")
  5.  
  6.   ; Get the current osnap node value
  7.   (setq originalOsnapMode (getvar "osmode"))
  8.  
  9.   ; Turn on osnap node
  10.   (setvar "osmode" 8)
  11.  
  12.   ; Prompt the user to select the PDF file
  13.   (setq pdfFilePath (getfiled "Select PDF file" "" "pdf" 1))
  14.  
  15.   ; Attach the PDF file if a valid file path is selected
  16.   (if pdfFilePath
  17.     (progn
  18.       ; Prompt the user to select the insertion point
  19.       (setq insertPoint (getpoint "\nEnter insertion point: "))
  20.  
  21.       ; Attach the PDF file
  22.       (command "_PDFATTACH" pdfFilePath insertPoint "")
  23.  
  24.       ; Select the attached PDF
  25.       (command "_SELECT" (entlast))
  26.  
  27.       ; Scale the attached PDF
  28.       (command "_SCALE" pause "L" scaleFactor insertPoint "")
  29.  
  30.       (princ "\nPDF file attached and scaled successfully.")
  31.     )
  32.     (princ "\nInvalid PDF file selected.")
  33.   )
  34.  
  35.   ; Reset osnap mode
  36.   (setvar "osmode" originalOsnapMode)
  37.  
  38.   (princ)
  39. )

The code will not scale the PDF file(s) by .8 and will not reset the osnap mode to the original after it finishes inserting the PDF file(s), also I want to have the users select the file(s) before the insert point.

Thanks,

Cadd4la
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #1 on: May 15, 2023, 04:41:43 PM »
Would be great if you replied to the other topic you started asking for help.

a small tip from me, LISP doesn't handle GUI/dialog actions well. maybe if you tried the command line version of PDFATTACH you would have better luck.

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #2 on: May 15, 2023, 06:29:59 PM »
EWCAD,

Sorry, I upset you by not replying to the other topic you helped me with before I posted this one. unfortunately, I don't do coding on a full-time bases because of my other job that pays my bills and is my main focus, plus I didn't have the time to test each person's codes to see if they work. I write this code over the weekend when I have more time to do coding and don't notice that people had helped me out because I only had time to jump on the site to post this new code and then get back to work.

Again please forgive me for my actions.

Cadd4la
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #3 on: May 16, 2023, 07:56:10 AM »
I imagine most of the people here (me included) don't do coding full time and have jobs or other obligations as well. If your going to need custom code, it would probably be wise to spend some time learning how to write it. As I said before, you should be using the command line version of PDFATTACH in this case. for all commands, if a command line version exists all you do is put "-" in front of it, so _PDFATTACH becomes _-PDFATTACH instead.

(command "_-PDFATTACH" pdfFilePath "1" insertPoint scaleFactor "")

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #4 on: May 16, 2023, 11:14:02 AM »
EWCAD,

Thank you for your help on this code, it will scale the file by .8 but unfortunately will only insert one at a time now.

Cadd4la
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #5 on: May 16, 2023, 11:25:52 AM »
Are you trying to insert multiple individual PDF Files, or is it one PDF with multiple pages?

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #6 on: May 16, 2023, 04:45:47 PM »
EWCAD,

It's one PDF file with multiple pages.

Thanks,

Cadd4la
« Last Edit: May 16, 2023, 04:55:31 PM by cadd4la »
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #7 on: May 16, 2023, 07:11:34 PM »
Okay, this should (?) work for you. I have no idea how you would want to handle inserting tons of pages at once or what size they would normally be, 6.8 worked for me to just fan them out evenly. Change the 6.8 in the "(mapcar "+ ipt (list 6.8 0 0)))" to whatever your page width typically is if you want.

Code: [Select]
(defun c:specs (/ sf ln ol osm fp ipt mp pages i )
(setq sf 0.8)
(setq ln "SCANS")
(setq ol (getvar 'clayer))
(setq osm (getvar "osmode"))
(setvar "osmode" 8)
(setq fp (getfiled "Select PDF file" "" "pdf" 1))
(setq ipt (getpoint "\nEnter insertion point: "))
(initget "Yes No")
(setq mp (getkword "\nAttach Multiple Pages?: [Yes/No]"))
(if (tblsearch "layer" ln)
(setvar 'clayer ln)
(alert(strcat "\n** Layer \"" ln "\" not found ** "))
)

(cond
(
(= mp "Yes")
(setq pages (getint "\nEnter Number of pages to print:"))
(setq i 1)
(repeat pages
(command "_-PDFATTACH" fp i ipt sf "")
(setq i (+ i 1))
(setq ipt (mapcar '+ ipt (list 6.8 0 0)))
)
)
(
(= mp "No")
(setq pages (getint "\nEnter the Page number you wish to print:"))
(command "_-PDFATTACH" fp pages ipt sf "")
)
)
(setvar 'clayer ol)
(setvar "osmode" osm)
(princ)
)

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #8 on: May 16, 2023, 08:21:46 PM »
EWCAD,

First of all, thank you very much for all the time you have spent on this code.

The code you gave me scales the pdf files to .8 without me changing your code and it resets the osmode to the original when done, that mine didn't do but the thing that it is doing that I don't want is it asks me to Enter Number of pages to print, I don't know how many sheets are in the PDF file without opening it first (I may have 2, I may have 14 or more), the way my code does it, it opens the PDF attach underlay dialog box (see the image from my last post) then I highlight all the files then click ok and pick the node points (the purple crosses) I have on my file (see attached image). Also, the strange thing is your code places the other files .25 inches apart to the right of each other which is the same that I have my node points away from the edge of the PDF file when it's scaled by .8 but will not let me pick the placement points.

If you can change your code to either let me highlight all the pages from the PDF attach underlay dialog box or insert all the pages from the PDF file without asking for the number, and let me pick the points to place each page.

Hopefully, this has cleared up the confusion.

Cadd4la
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #9 on: May 16, 2023, 09:17:29 PM »
Yeah that clears some things up. Didn't really understand what the end result was supposed to be, try this version. Borrowed a function from Lee Mac that counts the pages of the PDF (if there are a lot of pages it might hang up for a second, just let it finish.) so that now you don't need to enter how many pages, it will just dump them all. Also it will ask you to pick the insertion point for each page instead of just fanning them out. Lastly I added an error handler so that in the event you cancel the routine before it finished your snaps will be restored.

Did very limited testing, but it seemed to work with a 50+ page PDF on my end.

Code: [Select]
(defun c:specs (/ *error* sf ln ol osm fp mp pages i _PDFPageCount)
(defun *error* (msg)
(if osm (setvar 'osmode osm))
(and msg(not (wcmatch (strcase msg)"*CANCEL*,*QUIT*,*BREAK*,*EXIT*"))
    (princ (strcat "\nError: " msg))
)
(princ)
)
(defun _PDFPageCount ( filename / fob fso mat reg res str )

;; Translation by Lee Mac of the VBScript code by Chanh Ong
;; found at http://docs.ongetc.com/?q=content/pdf-pages-counting-using-vb-script
;;
;; Call with fully qualified filename of PDF file:
;; (_PDFPageCount "C:\\Folder\\Filename.pdf")
;;
;; Returns integer describing number of pages in specified PDF file

(if
(and
(setq filename (findfile filename))
(eq ".PDF" (strcase (vl-filename-extension filename)))
)
(vl-catch-all-apply
(function
(lambda ( / _ReadAsTextFile _CountPage )
(defun _ReadAsTextFile ( fso fn / fob str res )
(setq fob (vlax-invoke fso 'getfile fn)
str (vlax-invoke fso 'opentextfile fn 1 0)
res (vlax-invoke str 'read (vlax-get fob 'size))
)
(vlax-invoke str 'close)
(vlax-release-object str)
(vlax-release-object fob)
res
)
(defun _CountPage ( rgx str / mat pag )
(vlax-put-property rgx 'pattern "/Type\\s*/Page[^s]")
(vlax-put-property rgx 'ignorecase actrue)
(vlax-put-property rgx 'global actrue)
(setq mat (vlax-invoke rgx 'execute str)
pag (vlax-get mat 'count)
)
(vlax-release-object mat)
(if (zerop pag) 1 pag)             
)
(setq fso (vlax-create-object "Scripting.FileSystemObject")
reg (vlax-create-object "VBScript.RegExp")
str (_ReadAsTextFile fso filename)
res (_CountPage reg str)
)
)
)
)
)
(foreach obj (list str fob mat fso reg)
(vl-catch-all-apply 'vlax-release-object (list obj))
)
res
)
(setq sf 0.8)
(setq ln "SCANS")
(setq ol (getvar 'clayer))
(setq osm (getvar "osmode"))
(setvar "osmode" 8)
(setq fp (getfiled "Select PDF file" "" "pdf" 1))
(if (tblsearch "layer" ln)
(setvar 'clayer ln)
(alert(strcat "\n** Layer \"" ln "\" not found ** "))
)
(setq pages (_PDFPageCount fp))
(setq i 1)
(repeat pages
(vl-cmdf "_-PDFATTACH" fp i (getpoint "\nEnter insertion point: ") sf "")
(setq i (+ i 1))
)
(setvar 'clayer ol)
(setvar "osmode" osm)
(princ)
(vl-load-com)
)

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #10 on: May 16, 2023, 09:49:58 PM »
EWCAD,

Sorry, but it's not working for me. it's only inserting the first page. I tried it on two different PDF files and three different cad files.

This is what happens
Quote
Command: SPECS
Enter insertion point: _-PDFATTACH Path to PDF file to attach: C:\01 - P R O J E C T S\2023 PROJECTS\23.002 Villas at Towngate\Drawing Files\03-C A D\03.03-CD's\00-Scans\23.002 PlantingSpecs.pdf Enter page number or [?] <1>: 1
Specify insertion point:
Base image size: Width: 8.5000, Height: 14.0000, Inches
Specify scale factor or [Unit] <1">: 0.8
Specify rotation <0d0'0">:

FYI, The PDF files I used, one had 7 pages and one had 9 pages.

Thanks again,

Cadd4la
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #11 on: May 16, 2023, 10:46:51 PM »
Hmm, Strange. I can't see any issues on my end when running the code. Here's a clip of it working as intended.. I tried a few different PDF's but never had an issue.

*EDIT*

So a thought just crossed my mind, are you expecting it it place the pages on the points automatically? Because the way I have it coded is to do 1 page at a time so you can specify which point to put each page on. I could always make it insert on existing points without the need to select each one but then the same issue may arise where you don't have the number of pages readily available and end up with to few points.
« Last Edit: May 16, 2023, 11:03:48 PM by EWCAD »

cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #12 on: May 17, 2023, 12:41:02 AM »
EWCAD,

Sorry, this is what happens when I run it (see attached file).

I do want it to insert 1 page at a time just like your video.

Thanks,

Cadd4la
« Last Edit: May 17, 2023, 12:50:53 AM by cadd4la »
Windows 10 x64 - AutoCAD 2023

EWCAD

  • Mosquito
  • Posts: 14
Re: Looking for help to get the code working the way I need it.
« Reply #13 on: May 17, 2023, 10:00:52 AM »
Alright, I'm going to have to throw in the towel on this one. I've tested the previous code on 2 different machines in AutoCAD and Bricscad with multiple PDF's and have had zero issue with the _PDFPageCount function getting the pages from the PDF. Frankly I don't have the slightest clue why its not working for you and I don't have the patience to dig any deeper.

I can provide you with a worse version that "works" but has some quirks. The big thing here is that without knowing how many pages there are its going to loop until it tries to inset a page that doesn't exist, and if you cancel or exit the command without placing a duplicate page at the end its not going to reset your snaps/layer. This is not ideal but the ideal code just doesn't work for you for some reason so here is a band aid.

Code: [Select]
(defun c:specs (/ *error* sf ln ol osm fp i )
(defun *error* ( msg )
        (if osm (setvar 'osmode osm))
        (if (not (member msg '("Function cancelled" "quit / exit abort" "*Cancel*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
(setq sf 0.8)
(setq ln "SCANS")
(setq ol (getvar 'clayer))
(setq osm (getvar "osmode"))
(setvar "osmode" 8)
(setq fp (getfiled "Select PDF file" "" "pdf" 1))
(if (tblsearch "layer" ln)
(setvar 'clayer ln)
(alert(strcat "\n** Layer \"" ln "\" not found ** "))
)
(setq i 1)
(setq LastEntity T)
(while (not (equal LastEntity ThisEntity))
    (setq LastEntity (entlast))
(vl-cmdf "_-PDFATTACH" fp i (getpoint "\nEnter insertion point: ") sf 0)
(setq ThisEntity (entlast))
(setq i (+ i 1))
)
(setvar 'clayer ol)
(setvar "osmode" osm)
(princ)
)


cadd4la

  • Newt
  • Posts: 27
Re: Looking for help to get the code working the way I need it.
« Reply #14 on: May 17, 2023, 10:59:57 AM »
EWCAD,

Again thank you for all the time you have spent trying to get this to work the way I need it.

Your band-aid code does add all the pages one after another but like you stated it keeps going after all the pages are placed and doesn't reset the layer.

Maybe someone else could see why it's not working for me.

Regards,

Cadd4la
Windows 10 x64 - AutoCAD 2023