Author Topic: Challenge (for the newbies)  (Read 9499 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
Challenge (for the newbies)
« Reply #15 on: June 15, 2004, 06:11:43 PM »
Quote from: Mark Thomas
Quote from: Se7en
When can we submit our programs?

Yes but for you it has to be all in ActiveX and completely bomb proof :D

When Se7en submits code to a newbie challenge, I'd at least expect it to work for both AutoCAD and MicroStation

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Challenge (for the newbies)
« Reply #16 on: June 15, 2004, 06:24:02 PM »
ummm, i,  dont ...err, like that idea.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

rude dog

  • Guest
Challenge (for the newbies)
« Reply #17 on: June 15, 2004, 09:28:30 PM »
Sorry I can post anything  MT
I'm working long hours <6-10's> out in the field <Tool hookup Motorola> I  barely have the energy to see whats happin on my favorite forum....
But thanks for thinking of us noobz...

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Challenge (for the newbies)
« Reply #18 on: June 16, 2004, 07:57:44 AM »
Okay, well here's my version, with some explanation(I hope).
Code: [Select]

;;; this program prompts the user to select either dtext or mtext
;;; then extracts the layer name and text style name from the
;;; selected entity it then sets the current layer (clayer) and
;;; text style (textstyle) variables to reflect that of the selected
;;; entity
;;; version 1.0 Tue Jun 15, 2004 15:42:40
;;; Mark S. Thomas (mark_at_theswamp.org)
;;;
(defun c:slt (/ ent entlst lay sty)

  ;; the first thing we do is make sure the user selects something
  ;; the following line 1 returns T if something was selected
  ;; and nil if not. So line 1 is our test and line 2 is our
  ;; action(expression), or what to do if the test is T. As you can
  ;; see there is no action if the test is nil. We really don't care,
  ;; do we?
  (if (setq ent (car (entsel "\nSelect (M)TEXT: "))); [1]
    (setq entlst (entget ent)); [2]
    )

  ;; line 3 is a test, it wants to know if 'entlst' is T or nil
  ;; if it's T we move down into another test 4. Here we make sure
  ;; what the user selected was in fact (M)Text. Our test has two
  ;; arguments this time, one is "is the entity TEXT?" and two is
  ;; "is the entity MTEXT?". 'or' will return T if either test is T,
  ;; otherwise 'or' returns nil. Because 'if' can only do two things
  ;; we add a 'progn' (line 5) in order to do all the actions we want
  ;; if our test (4) is T. Lines 6 and 7 we extract layer and text
  ;; style from the selected entity, 8 and 9 we set the acad variables
  ;; to reflect that of our entity. Note we do nothing if either of
  ;; our tests are nil. We could add (and probably should) a prompt
  ;; to our second 'if' statement to let the user know they selected
  ;; something that was *not* (M)TEXT.
  (if entlst ;[3]
    (if         ;[4]
      (or (= (cdr (assoc 0 entlst)) "TEXT"); [one]
          (= (cdr (assoc 0 entlst)) "MTEXT"); [two]
          )
      (progn    ;[5]
        (setq lay (cdr (assoc 8 entlst)); [6]
              sty (cdr (assoc 7 entlst)); [7]
              )
        (setvar 'clayer lay); [8]
        (setvar 'textstyle sty); [9]
        ); progn
      ;; lets add an *else* clause to the second 'if' this
      ;; will be the action(expression) if our tests (one, two)
      ;; are nil. Lets use 'alert' to really get in the users
      ;; face if they select say, an attribute.
      (alert "The entity you select was **NOT** (M)TEXT, please re-run the program")
      ); 2nd if
    ); 1st if
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Challenge (for the newbies)
« Reply #19 on: June 16, 2004, 08:06:11 AM »
And just to show how many ways there are to get the same result here are a couple more!
Code: [Select]

;;; this program prompts the user to select either dtext or mtext
;;; then extracts the layer name and text style name from the
;;; selected entity it then sets the current layer (clayer) and
;;; text style (textstyle) variables to reflect that of the selected
;;; entity
;;; version 1.0 Tue Jun 15, 2004 15:42:40
;;; Mark S. Thomas (mark_at_theswamp.org)
;;;
(defun c:slt2 (/ ent entlst lay sty)

  (if (setq ent (car (entsel "\nSelect (M)TEXT: ")))
    (setq entlst (entget ent))
    )

  (if entlst
    (if
      (or (= (cdr (assoc 0 entlst)) "TEXT")
          (= (cdr (assoc 0 entlst)) "MTEXT")
          )
      (progn
        (setq lay (cdr (assoc 8 entlst))
              sty (cdr (assoc 7 entlst))
              )
        (mapcar 'setvar
                (list 'clayer 'textstyle)
                (list lay sty)
                )
        ); progn
      (alert "The entity you select was **NOT** (M)TEXT, please re-run the program")
      ); 2nd if
    ); 1st if
  (princ)
  )


I like this one :D
Code: [Select]

;;; this program prompts the user to select either dtext or mtext
;;; then extracts the layer name and text style name from the
;;; selected entity it then sets the current layer (clayer) and
;;; text style (textstyle) variables to reflect that of the selected
;;; entity
;;; version 1.0 Tue Jun 15, 2004 15:42:40
;;; Mark S. Thomas (mark_at_theswamp.org)
;;;

;;; returns T if entlst contains either (M)TEXT
(defun type-test (entlst)
  (or (= (cdr (assoc 0 entlst)) "TEXT")
      (= (cdr (assoc 0 entlst)) "MTEXT")
      )
  )

(defun do-it (layname stylename)
  (mapcar
    'setvar
    (list 'clayer 'textstyle)
    (list layname stylename)
    )
  )

(defun get-ent (/ ent entlst)
  (if (setq ent (car (entsel "\nSelect (M)TEXT: ")))
    (setq entlst (entget ent))
    )
  )

(defun c:slt3 (/ entlst)

  (if (setq entlst (get-ent))
    (if (type-test entlst)
      (do-it (cdr (assoc 8 entlst)) (cdr (assoc 7 entlst)))
      (alert "The entity you select was **NOT** (M)TEXT, please re-run the program")
      ); 2nd if
    ); 1st if

  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge (for the newbies)
« Reply #20 on: June 16, 2004, 08:26:03 AM »
Good examples Mark.
You can also make use of the AND function to process a string of code.
Just make sure that every item returns True.
Some functions return nil no matter what. For those just wrap
them in a ( null  ( function)) to force a True return value.

The AND will quit at the first nil return.

Code: [Select]
(defun c:slt_and (/ ent entlst lay sty)
  (and (setq ent (car (entsel "\nSelect (M)TEXT: ")))
       (setq entlst (entget ent))
       (or (= (cdr (assoc 0 entlst)) "TEXT")
           (= (cdr (assoc 0 entlst)) "MTEXT")
       )
       (setq lay (cdr (assoc 8 entlst)))
       (setq sty (cdr (assoc 7 entlst)))
       (setvar 'clayer lay)
       (setvar 'textstyle sty)
  ) ; end and
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Challenge (for the newbies)
« Reply #21 on: June 16, 2004, 12:11:55 PM »
I have updated mine to inclued the 'textsize' variable.
Code: [Select]

;;; this program prompts the user to select either dtext or mtext
;;; then extracts the layer name and text style name from the
;;; selected entity it then sets the current layer (clayer) and
;;; text style (textstyle) variables to reflect that of the selected
;;; entity
;;; version 1.0 Tue Jun 15, 2004 15:42:40
;;; Mark S. Thomas (mark_at_theswamp.org)
;;;
;;; version 1.1 Wed Jun 16, 2004 11:54:13
;;; added the 'textsize' variable
;;; this way if you select text and it's style has a 0 height the
;;; program will set 'textsize' to that of the selected text

;;; returns T if entlst contains either (M)TEXT
(defun type-test (entlst)
  (or (= (cdr (assoc 0 entlst)) "TEXT")
      (= (cdr (assoc 0 entlst)) "MTEXT")
      )
  )

(defun do-it (layname stylename txtsize)
  (mapcar
    'setvar
    (list 'clayer 'textstyle 'textsize)
    (list layname stylename txtsize)
    )
  )

(defun get-ent (/ ent entlst)
  (if (setq ent (car (entsel "\nSelect (M)TEXT: ")))
    (setq entlst (entget ent))
    )
  )

(defun c:slt (/ entlst)

  (if (setq entlst (get-ent))
    (if (type-test entlst)
      (do-it (cdr (assoc 8 entlst)) (cdr (assoc 7 entlst))(cdr (assoc 40 entlst)))
      (alert "The entity you select was **NOT** (M)TEXT, please re-run the program")
      ); 2nd if
    ); 1st if

  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge (for the newbies)
« Reply #22 on: June 16, 2004, 01:41:29 PM »
Here is a tidbit:
 
 Using the following code to test for text or mtext
 
Code: [Select]
(or (= "MTEXT" (cdr (assoc 0 entbl)))
    (= "TEXT" (cdr (assoc 0 entbl)))
)

 It could also be written as :
 
Code: [Select]
(member  (cdr (assoc 0 entbl))'("MTEXT" "TEXT"))
 
 and if you wand to detect RTEXT as well try:
 
Code: [Select]
(wcmatch (cdr (assoc 0 entbl)) "*TEXT*")
 Not sure if there are any others with TEXT in the name?

 One note, reading the group code info for (M)TEXT in ACAD2000 it indicates that
 code 7 is optional. Not sure if that is true or I misread it.
 But if true you will have to test for nil from (cdr (assoc 7 entbl))
 
 CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
Challenge (for the newbies)
« Reply #23 on: June 16, 2004, 01:51:10 PM »
Quote from: CAB
One note, reading the group code info for (M)TEXT in ACAD2000 it indicates that code 7 is optional. Not sure if that is true or I misread it.

It's optional in the sense that entity creation procedures (DXF import, ENTMAKE or the like) uses the standard style if a style isn't provided. For existing entities, code 7 is always present.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge (for the newbies)
« Reply #24 on: June 16, 2004, 02:08:00 PM »
Thanks Stig
I misintereted the the statement then, thanks for clearing that up.
I had thought that if it was missing that STANDARD style was used.
I can go back and revise my routine. :)

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.