Lisp to extract attribute value and print to PDF

Lisp to extract attribute value and print to PDF

diogo.cardoso
Participant Participant
2,487 Views
10 Replies
Message 1 of 11

Lisp to extract attribute value and print to PDF

diogo.cardoso
Participant
Participant

Hi all,

 

I'm trying to create a lisp that has the following path:

 

1. Get value from specific tag attribute in selected block (in model) and then store it to a variable

2. Print to PDF with filename being the value of previous point and the printing window the extremes of the block

 

Any help would be appreciated.

 

Thanks in advance!

 

 

0 Likes
Accepted solutions (1)
2,488 Views
10 Replies
Replies (10)
Message 2 of 11

DannyNL
Advisor
Advisor

Check code below.

 

Change the block name and attribute tag to what you need. If no block is found with the specified attribute tag or it is empty, the drawing will not be plotted. If multiple blocks and/or attribute tags with the same name are found, only the first one will be used to determine the plot file name.

Also change the plotting variables to whatever you need.

 

(defun c:PlotTest (/ PT_Selection PT_Attributes PT_Filename)
   (if
      (setq PT_Selection (ssget "_X" '((0 . "INSERT")(2 . "BLOCK_NAME")(66 . 1))))
      (progn         
         (setq PT_Attributes (vla-GetAttributes (vlax-ename->vla-object (ssname PT_Selection 0))))
         (foreach PT_Object (vlax-safearray->list (vlax-variant-value PT_Attributes))
            (if
               (and
                  (not PT_Filename)
                  (= (vla-get-TagString PT_Object) "ATT_TAG")
               )
               (setq PT_Filename (vla-get-TextString PT_Object))
            )
         )
         (if
(and PT_FileName
(/= PT_FileName "")
) (progn (mapcar 'setvar '("CMDECHO" "FILEDIA") '(0 0))
(command "_.ZOOM" "E") (command "_.PLOT" "Y" ; detailed plot "Model" ; layout name "DWG To PDF.pc3" ; plot device "ISO A3 (420.00 x 297.00 MM)" ; paper size "M" ; units (if ;--- (apply '> (reverse (cdr ; (reverse (mapcar '- ; (getvar "EXTMAX") ; (getvar "EXTMIN") ; paper orientation depending on extents )) ; ))) ; "L" ; landscape "P" ; portrait ) ;--- "N" ; plot upside down "EXTENTS" ; what to plot "F" ; scale of plot "0,0" ; plot offset "Y" ; plot with plot styles "monochrome.ctb" ; plot style table "Y" ; plot with lineweights "As Displayed" ; shading PT_FileName ; file name "N" ; save changes to page setup "Y" ; proceed ) (mapcar 'setvar '("CMDECHO" "FILEDIA") '(1 1)) ) ) ) ) (princ) )
Message 3 of 11

diogo.cardoso
Participant
Participant

Hi DannyNL,

 

first of all thank you very much for the help. I've ran you code and it's almost there 🙂

 

In fact I have multiple blocks with the same name in the same file (basicly it's a sheet with a header), so printing only the first one is a problem.

 

Is it any way of gettin the attributes / print using the selected block (instead of search all drawing for blocks with that name).

 

Thank you again

0 Likes
Message 4 of 11

DannyNL
Advisor
Advisor

Hi,

 

Sure, just remove the "_X" from code below and it will enable you to select the block yourself.

But if you have multiple blocks in one drawing (different layouts or in multiple 'drawings' in model?), the code for plotting will also need some modifications.

 

(setq PT_Selection (ssget "_X" '((0 . "INSERT")(2 . "BLOCK_NAME")(66 . 1))))

 

0 Likes
Message 5 of 11

diogo.cardoso
Participant
Participant

Yes, I have multiple "drawings / blocks" in same drawings. The idea is to get the filename from the selected block and print only the selected block.

0 Likes
Message 6 of 11

DannyNL
Advisor
Advisor

Does the title block also includes the drawing border or are these two separate blocks/items?

If the border is part of the the title block it should be possible to modify the plot code to get the bounding box of the border as plotting extents. However, if they are separate objects the user would nee to specify the lower left and upper right corners.

 

Can you post an empty example drawing with only a couple of the drawing blocks as mentioned? It would make it easier to see exactly what your needs are and modify the code accordingly.

0 Likes
Message 7 of 11

diogo.cardoso
Participant
Participant

Yes, the title block is by itself the boundary of the printing area.

 

Example attached.

0 Likes
Message 8 of 11

DannyNL
Advisor
Advisor
Accepted solution

OK, changed code based on your example drawing.

 

(defun c:PlotTest (/  PT_Selection PT_Block PT_Attributes PT_Filename PT_LowerLeft PT_UpperRight PT_VarList PT_OldVars)
   (if
      (setq PT_Selection (ssget "_:S+." '((0 . "INSERT")(2 . "A4 LEG")(66 . 1))))
      (progn         
         (setq PT_Attributes (vla-GetAttributes (setq PT_Block (vlax-ename->vla-object (ssname PT_Selection 0)))))
         (foreach PT_Object (vlax-safearray->list (vlax-variant-value PT_Attributes))
            (if
               (and
                  (not PT_Filename)
                  (= (vla-get-TagString PT_Object) "DESENHONº")
               )
               (setq PT_Filename (vla-get-TextString PT_Object))
            )
         )
         (if
            (and
               PT_FileName
               (/= PT_FileName "")
            )
            (progn
               (vla-GetBoundingBox PT_Block 'PT_LowerLeft 'PT_UpperRight)
               (setq PT_LowerLeft  (vlax-safearray->list PT_LowerLeft))
               (setq PT_UpperRight (vlax-safearray->list PT_UpperRight))
               (setq PT_FileName   (strcat (getvar "DWGPREFIX") PT_FileName))
               (setq PT_VarList '("CMDECHO" "FILEDIA" "OSMODE"))
               (setq PT_OldVars (mapcar 'getvar PT_VarList))
               (mapcar 'setvar PT_VarList '(0 0 0))
               (command "_.ZOOM" "E")
               (command
                  "_.PLOT"
                  "Y"                              ; detailed plot
                  "Model"                          ; layout name
                  "DWG To PDF.pc3"                 ; plot device
                  "ISO A3 (420.00 x 297.00 MM)"    ; paper size
                  "M"                              ; units
                  (if                              ;---
                     (apply '> (reverse (cdr       ;
                        (reverse (mapcar '-        ;
                           PT_UpperRight           ;
                           PT_LowerLeft            ; paper orientation depending on extents
                         ))                        ;
                     )))                           ;
                     "L"                           ; landscape
                     "P"                           ; portrait
                  )                                ;---
                  "N"                              ; plot upside down
                  "WINDOW"                         ; what to plot
                  PT_LowerLeft                     ; Lower left of window
                  PT_UpperRight                    ; Upper right of window
                  "F"                              ; scale of plot
                  "0,0"                            ; plot offset
                  "Y"                              ; plot with plot styles
                  "monochrome.ctb"                 ; plot style table
                  "Y"                              ; plot with lineweights
                  "As Displayed"                   ; shading
                  PT_FileName                      ; file name
                  "N"                              ; save changes to page setup
                  "Y"                              ; proceed                  
               )
               (mapcar 'setvar PT_VarList PT_OldVars)
            )
         )
      )
   )
   (princ)
)
Message 9 of 11

diogo.cardoso
Participant
Participant

DannyNL,

 

It's perfect! Thank you very much!

0 Likes
Message 10 of 11

DannyNL
Advisor
Advisor

You're welcome & glad I could help Smiley Happy

0 Likes
Message 11 of 11

Anonymous
Not applicable

hello DannyNL,

It's perfect! Thank you very much!

Please Some Help command PLOT TEST add Attribute value "Drawing No. & Revision" tag

Link this

xxxxxxxxxxxxx-Rev01


@DannyNL wrote:

OK, changed code based on your example drawing.

 

(defun c:PlotTest (/  PT_Selection PT_Block PT_Attributes PT_Filename PT_LowerLeft PT_UpperRight PT_VarList PT_OldVars)
   (if
      (setq PT_Selection (ssget "_:S+." '((0 . "INSERT")(2 . "A4 LEG")(66 . 1))))
      (progn         
         (setq PT_Attributes (vla-GetAttributes (setq PT_Block (vlax-ename->vla-object (ssname PT_Selection 0)))))
         (foreach PT_Object (vlax-safearray->list (vlax-variant-value PT_Attributes))
            (if
               (and
                  (not PT_Filename)
                  (= (vla-get-TagString PT_Object) "DESENHONº")
               )
               (setq PT_Filename (vla-get-TextString PT_Object))
            )
         )
         (if
            (and
               PT_FileName
               (/= PT_FileName "")
            )
            (progn
               (vla-GetBoundingBox PT_Block 'PT_LowerLeft 'PT_UpperRight)
               (setq PT_LowerLeft  (vlax-safearray->list PT_LowerLeft))
               (setq PT_UpperRight (vlax-safearray->list PT_UpperRight))
               (setq PT_FileName   (strcat (getvar "DWGPREFIX") PT_FileName))
               (setq PT_VarList '("CMDECHO" "FILEDIA" "OSMODE"))
               (setq PT_OldVars (mapcar 'getvar PT_VarList))
               (mapcar 'setvar PT_VarList '(0 0 0))
               (command "_.ZOOM" "E")
               (command
                  "_.PLOT"
                  "Y"                              ; detailed plot
                  "Model"                          ; layout name
                  "DWG To PDF.pc3"                 ; plot device
                  "ISO A3 (420.00 x 297.00 MM)"    ; paper size
                  "M"                              ; units
                  (if                              ;---
                     (apply '> (reverse (cdr       ;
                        (reverse (mapcar '-        ;
                           PT_UpperRight           ;
                           PT_LowerLeft            ; paper orientation depending on extents
                         ))                        ;
                     )))                           ;
                     "L"                           ; landscape
                     "P"                           ; portrait
                  )                                ;---
                  "N"                              ; plot upside down
                  "WINDOW"                         ; what to plot
                  PT_LowerLeft                     ; Lower left of window
                  PT_UpperRight                    ; Upper right of window
                  "F"                              ; scale of plot
                  "0,0"                            ; plot offset
                  "Y"                              ; plot with plot styles
                  "monochrome.ctb"                 ; plot style table
                  "Y"                              ; plot with lineweights
                  "As Displayed"                   ; shading
                  PT_FileName                      ; file name
                  "N"                              ; save changes to page setup
                  "Y"                              ; proceed                  
               )
               (mapcar 'setvar PT_VarList PT_OldVars)
            )
         )
      )
   )
   (princ)
)

 

0 Likes