Use Lisp to test for Text Style

Discussion in 'AutoCAD' started by dcouch, Nov 11, 2004.

  1. dcouch

    dcouch Guest

    Using AutoLISP, I want to test to see if the Text style "romans" exists in the drawing, and if not, create it. I know how to create the style, I am having trouble figuring out how to test for the style and if the text fails, create it. Any help would be appreciated.
     
    dcouch, Nov 11, 2004
    #1
  2. (while (setq Style (tblnext "style" (null Style)))
    (if (= "ROMANS.SHX" (strcase (cdr (assoc 3 Style))))
    (setq Romans (cdr (assoc 2 Style)))
    );end if
    );end while
    (if (not Romans (CreateTxtStyle "Romans" "Romans.shx" 0.0 1.0))

    where:

    (defun CreateTxtStyle (Name File Height Width)
    (entmake (list '(0 . "STYLE") '(100 . "AcDbSymbolTableRecord")
    '(100 . "AcDbTextStyleTableRecord") (cons 2 Name) (cons 3
    File)
    '(70 . 0) '(71 . 0) (cons 40 Height) (cons 41 Width)))
    );end CreateTxtStyle
     
    User of Internet, Nov 11, 2004
    #2
  3. dcouch

    Jeff Mishler Guest

    (if (not (tblsearch "style" "romans"))
    (progn
    ;create it here, it doesn't exist
    )
    )
     
    Jeff Mishler, Nov 11, 2004
    #3
  4. dcouch

    ecable Guest

    I use this to check for romand text style and if it exists check the height. If the height is not set to 0.0 recreate ir, or it does not exist create it.
    Substitute romans for romand.
    Hope this will help you.

    (setq NEWTXTSTY (tblsearch "style" "romand"))
    (if
    (or
    (= NIL NEWTXTSTY)
    (/= (cdr (assoc 40 NEWTXTSTY)) 0.0)
    ) ;_ end of cond
    ;CREATE ROMAND IF NEEDED
    (command "style" "romand" "romand" 0.0 1.0 0.0 "n" "n" "n")
    ) ;_ end of if
     
    ecable, Nov 11, 2004
    #4
  5. You don't need to test for it. You can simply create it, even if it already
    exists. After you enter a style name, it says either "New style" or
    "Existing style", but there's no "Do you want to replace..." prompt to
    account for, or anything.

    The only reason I can think of that you might NOT want to do that is if the
    existing style with that name might have characteristics (like an assigned
    height) that are different from what you would assign, and if you want to
    keep that style definition for some reason (e.g. because of existing text).
     
    Kent Cooper, AIA, Nov 11, 2004
    #5
  6. dcouch

    Tom Smith Guest

    That code's excessively wordy. You could accomplish the same logic with:

    (if
    (and
    (setq NEWTXTSTY (tblsearch "style" "romand"))
    (not (zerop (cdr (assoc 40 NEWTXTSTY)))))
    (command "style" "romand" "romand" 0.0 1.0 0.0 "n" "n" "n"))

    However, as Kent noted, none of this testing is necessary at all. If you
    want to always set the style's height to 0, you can accomplish exactly the
    same thing with simply:

    (command "style" "romand" "romand" 0.0 1.0 0.0 "n" "n" "n")
     
    Tom Smith, Nov 11, 2004
    #6
  7. dcouch

    ecable Guest

    One of my first stabs at writing lisp. Still need a lot of help, and am happy to get your comments. Next time I will know a little better.
    BTW Logic is not my strong suite.

    Thanks Tom
    Ed Cable
     
    ecable, Nov 12, 2004
    #7
  8. dcouch

    Scot-65 Guest

    This is the method that is the most efficient!

    Scot-65
     
    Scot-65, Nov 12, 2004
    #8
  9. dcouch

    Tom Smith Guest

    This is the method that is the most efficient!

    Wrong, you didn't read the whole thread. Kent explained why it's unnecessary
    in this case to do any "if" test at all. Just make the style the way you
    want it, whether it already exists or not.
     
    Tom Smith, Nov 12, 2004
    #9
  10. dcouch

    Scot-65 Guest

    Tom,

    (if (desired textstyle)
    (set textstyle as current)
    (create textstyle)
    );endIF

    Scot-65
     
    Scot-65, Nov 16, 2004
    #10
  11. dcouch

    Tom Smith Guest

    That would work, and it's literally what the OP asked for, but I still think
    that the test is unnecessary, per Kent's comments.

    The OP didn't address the scenario of what if the style exists but it isn't
    defined the way it should be? In that case, I assume it should be corrected
    AND set current. If you just go ahead and create the style as desired,
    regardless of whether that name already exists, it will always be correct
    and set current.
     
    Tom Smith, Nov 16, 2004
    #11
  12. dcouch

    Scot-65 Guest

    Tom,

    We are getting close.
    The original poster' wanted hints as he himself has little expierence with LISP.
    I did not originally want to go into detail any more than necessary.
    Nobody is wrong here.

    =====================

    We have over 3000 archived documents available to our customers.
    Many of them generated in R12 and R14.
    Many of them drawn by hand.

    Before I overhauled the menu system, the standard text was set to a height of 4-1/2 inches. The new standard we now use is 4 inches (modelspace). We do not employ the variable TEXTHEIGHT. Therefore, all of our styles are declared with a fixed height greater than zero (0). Oh, the beauty of many items to customize...

    The macro has accounted for this new setting to a previous file that will now be released to the new customer. Not only does this macro test for an existing declared textstyle, it will also check the declared height. If one or the other does not pass, reload style.

    When was the last time you waited for the mouse box to appear after typing STYLE? It has not changed since R13 (TTF files).

    When was the last time your associates loaded a textstyle with the wrong height (relative to drawing scale)?


    Scot-65

    ---------------------------

    DCouch here is the code ("H1" is our regular text):

    (if (and (tblsearch "style" c)
    (= (rtos (cdr (assoc 40 (tblsearch "style" c))) 2 2) d) )
    (setvar "textstyle" c)
    (progn
    (if (findfile "H1.shx")
    (setq e "H1.shx")
    (progn
    (setq e "txt.shx")
    (alert (strcat
    "The font file called \"H1.shx\" is not in your support path."
    "\nA default font file will be used instead.")) );progn
    );if
    (setvar "cmdecho" 0)(command ".style" c e d "" "" "" "")
    (setvar "cmdecho" 1)(princ (strcat "\n" c " textstyle loaded. ")) );progn
    );if

    Where "c" and "d" is passed to this menu function as the style with desired height (chosen from a list).


    See my above post for hints of other switches to set when changing drawing scales within a file (also incorporated in this same menu function macro).

    If your environment declares text styles with a height of 0, do Kent's method (ensure a routine accounts for the desired height without the User entering a value).

    [see if ya'll can find the bug I purposely designed into this routine]

    It is only fair to give back to this fourm as much as it is to extract.


    Scot-65
     
    Scot-65, Nov 16, 2004
    #12
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.