LISP to fully automatically create a layout from block

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hei there!
I want to write a LISP that creates a printable layout from a template. The layout template is chosen based on a paper frame block selected in Model space.
So basically it would be like this:
1. I load the app and move the paper.dwt (the paper.dwt contains layouts named A4-portrait, A4-landscape and so on) to the default template folder. (this is a one time event)
2. Lets name the application "NTL"
3. In modelspace, I have a drawing inside an A4-portrait custom block called KN, it has a parameter called "papername", the value is "A4-portrait".
4. I write NTL, autocad tells me to choose a block. I choose the block.
5. Now it should set a variable to A4-portrait, which means autocad selects a new layout template from PAPER.dwt called A4-portrait.
6. Then it should create a viewport based on the position of the select block and also set the center of the viewport to be the center of the selected block (so basically half measurements of the paper).
7. There is a titleblock "TB" on the modelspace drawing which has an attribute "SCALE" which equals 1:10, so the viewports scale should be set also to 1:10
8. So autocad creates a layout from a template file according to the selected block parameters, this includes setting the scale from the titleblock and centering the created viewport.
9.Finally autocad should rename the new layout accoring to the parameters from the Titleblock "TB". The name should be "DRAWING NUMBER"_"DRAWING NAME".
My knowledge in Autocad LISP is not sufficient so I am asking for your help and advice. I ahve written a simpler one, but it needs to much user ineteraction and I want to fully automate it.
Thanks for any responses
(defun c:NTL (/ doc p1 p2 temp mp scl SC cd:DWG_LayoutsList res a b vpp vpdoc vp Match Text ) (command "selectioncycling" "0") (initdia) (command "layout" "template" "") (vl-load-com) (setq Doc (vla-get-ActiveDocument (vlax-get-Acad-Object))) (setq p1 (getpoint "\Pick Corners:")) (setq p2 (getcorner p1)) (if (< (car (trans p2 1 0)) (car (trans p1 1 0))) (setq tmp p1 p1 p2 p2 tmp ) T ) (setq mp (list (/ (+ (car p1) (car p2)) 2) (/ (+ (cadr p1) (cadr p2)) 2) 0.0 ) ) (setq sc (cond ((getint (strcat "\Scale 1: <" (itoa (setq sc (cond (sc) (10) ) ) ) ">: " ) ) ) (sc) ) ) (setq cd:DWG_LayoutsList (vlax-for % (vla-get-layouts Doc) (setq res (cons (list (vla-get-name %) (vla-get-TabOrder %) % ) res ) ) ) ) (setvar "CTab" (caar (vl-sort cd:DWG_LayoutsList '(lambda (a b) (> (cadr a) (cadr b))) ) ) ) (setq VPDoc (vla-get-PaperSpace doc)) (setq VPp (vlax-3D-point (getpoint "\npick layout center"))) (setq VP (vla-AddPViewport VPDoc VPp (/ (- (car p2) (car p1)) sc) (/ (- (cadr p2) (cadr p1)) sc) ) ) (vla-display VP :vlax-true) (vla-put-mspace doc :vlax-true) (vla-put-activepviewport Doc VP) (vla-zoomcenter (vlax-get-acad-object) (vlax-3d-point mp) 1.0 ) (vl-cmdf "_.zoom" (strcat (RTOS (/ 1.0 SC)) "xp")) (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport ) (vla-put-mspace doc :vlax-FALSE) (VLA-PUT-DisplayLocked VP :vlax-true) ;chooses the created viewport ; (command "_.MSPACE") ;rename; (while (= Match nil) (setq Match (nentsel "\nChoose a name from the titleblock: ")) (if (= Match nil)(alert "Choose a name from the titleblock...")) ) (if (/= nil Match) (progn (setq Match (entget (car Match))) ) ) (if(setq Text(cdr(assoc 1 Match))) (command "layout" "rename" "" Text) ) (setvar "cmdecho" 1) (princ) (command "model") (command "selectioncycling" "2") ) (princ "\n write LOU") (princ)