Mtext Lisp help

Discussion in 'AutoCAD' started by jfranklin, Oct 15, 2004.

  1. jfranklin

    jfranklin Guest

    I am pretty new to Lisp, so hopefully someone can point out what I am missing. I am having one problem with the lisp that I pieced together from a few other routines.

    I am writing a routine to create mtext that is scaled according to the dimscale and set to the correct layer.

    When I run the routine below, it works except the text is placed on the current layer instead of the one specified in the routine. I am trying to save the current layer, set the text layer, make the text, and change the layer back to previously current, but something must be wrong with that logic.

    (defun C:SCMTEXT ()
    (setvar "cmdecho" 0)
    (setq Cl (getvar "CLAYER"))
    (setq dsc (getvar "DIMSCALE"))
    (setq osm (getvar "osmode"))
    (setvar "osmode" 0)
    (setvar "attdia" 1)
    (setvar "attreq" 1)
    (setq b (tblsearch "layer" "AN_NOTE"))
    (if (= b nil)(command "-layer" "n" "AN_NOTE" "c" "131" "AN_NOTE" ""))
    (setvar "textsize" (/ dsc 12.8))
    (command "-layer" "s" "AN_NOTE" "")
    (initdia) (command ".mtext" pause)
    (setvar "clayer" cl)
    (setvar "osmode" osm)
    (setvar "cmdecho" 1)
    (princ)
    )
     
    jfranklin, Oct 15, 2004
    #1
  2. jfranklin

    Simon Wegner Guest

    I'm no LISP wizard myself but it seems that you're missing a pause -selecting the two corners of the mtext box counts as two user inputs. As it was, you were setting the layer back before completing the mtext. Attdia and attreq are variables which control the insert command so I'm assuming they were holdovers from one of the LISPs you patched together. Lose them. On a side note, you really should get in the habit of localizing your variables.

    Simon

    [pre]
    (defun C:SCMTEXT (/ cl dsc osm) ;;;List variables as local
    (setvar "cmdecho" 0)
    (setq Cl (getvar "CLAYER"))
    (setq dsc (getvar "DIMSCALE"))
    (setq osm (getvar "osmode"))
    (setvar "osmode" 0)
    ;;;Deleted attdia and attreq
    (setq b (tblsearch "layer" "AN_NOTE"))
    (if (= b nil)(command "-layer" "n" "AN_NOTE" "c" "131" "AN_NOTE" ""))
    (setvar "textsize" (/ dsc 12.8))
    (command "-layer" "s" "AN_NOTE" "")
    (initdia) (command ".mtext" pause pause) ;;;Added pause here
    (setvar "clayer" cl)
    (setvar "osmode" osm)
    (setvar "cmdecho" 1)
    (princ)
    )
    [/pre]
     
    Simon Wegner, Oct 15, 2004
    #2
  3. jfranklin

    fengk Guest

    Try the modifed one below. It can be approved a lot.

    (defun C:SCMTEXT (/ cl dsc osm pt1 pt2)
    (setvar "CMDECHO" 0)
    (setq Cl (getvar "CLAYER"))
    (setq dsc (getvar "DIMSCALE"))
    (setq osm (getvar "OSMODE"))
    (setvar "OSMODE" 0)
    (if (not (tblsearch "layer" "AN_NOTE"))
    (command "-layer" "n" "AN_NOTE" "c" "131" "AN_NOTE" "")
    )
    (setvar "textsize" (/ dsc 12.8))
    (command "-layer" "s" "AN_NOTE" "")
    (setq pt1 (getpoint "Specify first corner: ")
    pt2 (getcorner pt1 "Specify other corner: ")
    )
    (command ".mtext" pt1 pt2)
    (while (> (logand (getvar "CMDACTIVE") 1) 1)
    (command pause)
    )
    (setvar "CLAYER" cl)
    (setvar "OSMODE" osm)
    (setvar "CMDECHO" 1)
    (princ)
    )
     
    fengk, Oct 17, 2004
    #3
  4. jfranklin

    jfranklin Guest

    thanks, that works great... and i appreciate the other tips.
     
    jfranklin, Oct 17, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.