Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z - Internet FAQ Archives

comp.cad.autocad AutoLISP FAQ (part 2/2) - samples, code
Section - [21] Sample Lisp Programs:

( Part1 - Part2 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Zip codes ]

Top Document: comp.cad.autocad AutoLISP FAQ (part 2/2) - samples, code
Previous Document: [20] General helper functions
Next Document: [21.8] How to write XYZ data of selected objects to a file?
See reader questions & answers on this topic! - Help others by sharing your knowledge
[21.1] Globally change texts, polylines, layer utils, datestamp

  For globally changing text attributes use CHTEXT.LSP in your
  sample directory.

  For globally changing polyline attributes, freeze layers by pick
  and other similar tasks search for free lisp tools at any AutoLISP
  site. See "[1]" and some code at "[22]","[23]","[24]"

  For putting a datestamp and others onto your plots automatically
  first check out if your plotter supports HPGL/2. Then use the
  internal HPGL/2 driver and configure the datestamp in HPCONFIG.

  DATESTAMP.LSP: Change the plot header attributes by yourself
  as in [22.2]. A profi plotstamp routine is here:


[21.2] Plot dialog from within Lisp. Using DDE or ActiveX or initdia

  (initdia)(command "_PLOT")

  Calling the PLOT dialogbox from AutoLISP *before R14* was possible
  only under Windows i.e. with LISPPLOT by Mike Dickason. This fed
  the keyboard buffer with keystrokes. files/lispd-l/
  or also:
  Otherwise create a script and call this at the end of your lisp, but
  this will not show up the dialogbox.

  Xiang Zhu: You could have used "DDELISP" under Windows. [shortened]
  ;;; [fixed for all releases]
  (defun DDECMD (str / tmp acadver ddestr)
    (if (not (boundp 'initiate))
        ((= 14 (setq acadver (atoi (getvar "ACADVER"))))
         (setq ddestr "AutoCAD.R14.DDE") (arxload "ddelisp"))
        ((= 13 acadver)
         (setq ddestr "autocad.r13.dde") (xload "ddelisp"))
        ((= 12 acadver)
         (setq ddestr "autocad.dde") (xload "ddelisp"))
        (T (princ "DDE not supported")(exit))))
      (if (not (zerop (setq tmp (initiate ddestr "system"))))
          (execute tmp (strcat "[" str "]"))
          (terminate tmp)
  (ddecmd "_plot ") ; return would be "^13"

  Beware that Acad accepts only DDE commands if the command line is
  active, that means no dialogbox must be open.

  With vlisp/ViLL ActiveX methods can be used to plot, but the
  dialog can not be called:

  ;;; vlisp syntax:
  (setq vlax:ActiveDocument (vla-get-ActiveDocument 
  (setq plt (vla-get-plot vlax:ActiveDocument))   ;=> plot object
  (vla-PlotWindow plt pt1 pt2)                    ;WCS pts
  (vla-PlotPreview plt 1)                         ;0; partial, 1: full
  (vla-PlotToDevice plt "Default System Printer") ;if it exists

  With R14 INITDIA was introduced, which can be applied to most but not
  all dialogs:
    (initdia)(command "_PLOT")

  With A2000 use OLE (VLA- methods) instead of DDE. DDE support is 
  discontinued by Microsoft. OLE provides a better object-oriented 


[21.3] (entmod) and (entmake) Layers, without (command "_LAYER"...)

  ENTMOD a layer
  I try to change a layer property without calling COMMAND function
  inside a lisp routine.

  Since R13, using the following lisp
    (setq tbl_lst (entget (tblobjname "LAYER" "ANY_LAYER"))
          clr_grp (assoc 62 tbl_lst)
    (entmod (subst (cons 62 (- (cdr clr_grp))) clr_grp tbl_lst))
  you can toggle "ANY_LAYER" On or Off, even it is the current layer.

  But AutoCAD doesn't know a table entry has been changed until you
  click the Layer Control on the toolbar or something similar.
  Besides, you can issue 'DDLMODES to see On/OFf property of
  "ANY_LAYER" changed.
  Doing the same way to freeze a layer, you will still see entities on
  that layer shown on screen, but you can not select them, until you do
  something related to layer settings, and AutoCAD will hide those

  ENTMAKE a layer
  You must get your pattern with entget, using the table object name as
  argument. This table object name can be retrieved with the TBLOBJNAME
  (entget (tblobjname "LAYER" "Any Layer Name")) ; R2000 can have spaces!

  ;;; This routine will create a layer with any name you type:
  (defun C:MLAY ()	; by Reinaldo Togores <>
    (setq laynam (getstring "\nLayer name: "))
          '(0 . "LAYER")
          '(5 . "28")
          '(100 . "AcDbSymbolTableRecord")
          '(100 . "AcDbLayerTableRecord")
          (cons 2 laynam)
          '(70 . 64)
          '(62 . 7)
          '(6 . "CONTINUOUS")


[21.4] How to select multiple files in Lisp? (as in FILES - Unlock) [new]

  * DOSLIB v4.3 from McNeel contains dos_getfilem,
  * STDLIB contains std-getfilem
  * At
    is another lisp helper routine to select multiple files with DCL.
    You will also need VLISP, DOSLIB or the STDLIB to access the
    directory functions. Another lisp version is at


[21.5] Replace multiple blocks

  A search at the lisp archives yielded those hits:
  => 97code.htm and a question for your username which can be obtained
   free and automatically
  or xarch: and search for
  also at the Cadalog: OpenSource Freeware Keyword: "Block Replace"
  => (this one is the best)


[21.6] (vports), VIEWPORT entity, pixel conversion

  VIEWPORT entity:
  The answer to "I can do an (entget) on a VIEWPORT and get its lower
  left (DXF group 10) and upper right (DXF group 11) corner.  But it
  appears that these coordinates are in the paper space system.  What
  I'm interested in finding out is what portion of the "real" drawing
  (the model space drawing) are currently shown in that viewport."
  is at has also some tricks.

  How to change viewports in AutoLISP?
  with (setvar "CVPORT" vport-id)

  With the following functions you convert pixel<->drawing units:

  ;;; Conversion pixel to drawing units
  (defun PIX2UNITS (pix)
    (* pix (/ (getvar "VIEWSIZE") (cadr (getvar "SCREENSIZE")))))

  ;;; Conversion drawing units to pixel
  (defun UNITS2PIX (units)
    (* units (/ (cadr (getvar "SCREENSIZE"))(getvar "VIEWSIZE"))))

  Note also the "Pixel Off by One" Errors in AutoCAD, written by Vibrant


[21.7] Select all visible objects: zoom coordinates

  Beware that with (ssget) you will only get visible objects, because
  all interface functions (entsel,ssget,osnap) work with pixel, only
  (ssget "X") will select not visible objects.

  ;;; returns a list of the actual viewport corners in WCS
  (defun ZOOMPTS ( / ctr h screen ratio size size_2)
    (setq ctr (xy-of (getvar "VIEWCTR")) ;3D -> 2D
          h   (getvar "VIEWSIZE")        ;real
          screen (getvar "SCREENSIZE")   ;2D: Pixel x,y
          ratio (/ (float (car screen))  ;aspect ratio
                   (cadr screen))
          size (list (* h ratio) h)      ;screensize in coords
          size_2 (mapcar '/ size '(2.0 2.0)))
      (list (mapcar '- ctr size_2)
            (mapcar '+ ctr size_2)))
  (defun XY-OF (pt) (list (car pt)(cadr pt)))   ;assure 2D coords

  Note: The points returned are in WCS but this is ok, because the
    "CP" "WP" and "P" options of ssget expect WCS points.
    "W" and "C" require UCS points - why the difference I don't know.

  ;;; one way to define this function
  (defun SSALL-VISIBLE (/ l)
    (ssget "C" (car (setq l (maptrans0-1 (zoompts)))) (cadr l)))
  ;;; or another
  (defun SSALL-VISIBLE-1 ()      ;combine "C" and (p1 p2) to one list
    (apply 'ssget (append '("C") (maptrans0-1 (zoompts)))))

  ;;; map some pts from WCS to UCS, easier with just one argument
  (defun MAPTRANS0-1 (pts) (mapcar '(lambda (pt)(trans pt 0 1)) pts))

User Contributions:

Comment about this article, ask questions, or add new information about this topic:

Top Document: comp.cad.autocad AutoLISP FAQ (part 2/2) - samples, code
Previous Document: [20] General helper functions
Next Document: [21.8] How to write XYZ data of selected objects to a file?

Part1 - Part2 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer: (Reini Urban)

Last Update March 27 2014 @ 02:11 PM