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

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge (for the newbies)
« on: June 15, 2004, 10:28:55 AM »
Write a program that will set the layer and text style current of the selected text entity.

<pseudocode>
  prompt user to select an entity

  if entity = text
    collect layer and text style
 
   set current layer to text entity
   set current style to that of text entity
</pseudocode>
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #1 on: June 15, 2004, 01:56:38 PM »
...Im on it!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
Challenge (for the newbies)
« Reply #2 on: June 15, 2004, 02:11:16 PM »
lol
Se7en and CAB should be exposed to Dent's gasses  ..  newbies the man said, newbies!

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #3 on: June 15, 2004, 02:13:09 PM »
When can we submit our programs?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #4 on: June 15, 2004, 02:13:43 PM »
lol, well it was fun anyways
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge (for the newbies)
« Reply #5 on: June 15, 2004, 02:17:03 PM »
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
TheSwamp.org  (serving the CAD community since 2003)

M-dub

  • Guest
Challenge (for the newbies)
« Reply #6 on: June 15, 2004, 02:18:02 PM »
Damn, I'd love to get into this, but I couldn't start it yet...We're starting a VB course on Friday in the office.  (One of the young gurus is going to teach us).  Good thing is that we're going to get paid for it and we're going to learn something, but still....I have a feeling that it's going to be pretty 'Mickey Mouse'.
It makes sense to me...how programs should work, but the language is what I've yet to learn.  I think I'll be good at it once I HAVE to learn it and devote some time to learning it.  Right now, I'm just too busy to dedicate time to it.  Anyway, I'm looking forward to getting there.  Maybe the VB will get me more into learning LISP too.

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #7 on: June 15, 2004, 02:22:23 PM »
Quote from: Mark Thomas
Yes but for you it has to be all in ActiveX and completely bomb proof :D
WHAT?!  ...But i did it all with AutoLisp already?!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #8 on: June 15, 2004, 02:34:53 PM »
Oh by the way; because of "circumstances", I'm selling plain AutoLisp "crib notes" for this assignment.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge (for the newbies)
« Reply #9 on: June 15, 2004, 02:35:20 PM »
Quote from: Se7en
Quote from: Mark Thomas
Yes but for you it has to be all in ActiveX and completely bomb proof :D
WHAT?!  ...But i did it all with AutoLisp already?!

well ok then......... go ahead and post it.
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #10 on: June 15, 2004, 02:47:31 PM »
Quote from: Mark Thomas
well ok then......... go ahead and post it.
HELOOOOoo?! ..Im selling it?!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Challenge (for the newbies)
« Reply #11 on: June 15, 2004, 02:57:34 PM »
Code: [Select]

(defun c:setstl(/ oldcmdecho en ent clr stl)
     (setq oldcmdecho (getvar "cmdecho"))
     (setvar "cmdecho" 0)
     (setq en (entsel "\nSelect Style/Layer By Entity:"))
     (if en
          (progn
               (setq ent (cdr (assoc 8 (entget (car en)))))
               (command "layer" "s" ent "")
               (setq stl (cdr (assoc 7 (entget (car en)))))
               (command "style" stl "" "" "" "" "" "" "")
          )
          (prompt "\n***Entity Not Selected...***")
     )
     (setvar "cmdecho" oldcmdecho)
     (princ)
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Challenge (for the newbies)
« Reply #12 on: June 15, 2004, 03:23:56 PM »
Here was my version. Not alot of error checking but that can be added with little to no problems.
 
Code: [Select]
;;;*******************************************************************;
;;; Get and set layer and text style current of selected text ent.

;;; Safer entsel
(defun entse7 (/ x)
  (while (not (setq x (car (entsel))))
    (princ "\nYou missed, try again.")) x)
;;; Retrun the text style of an entity
(defun GetTxtStyle (ent) (cdr (assoc 7 (entget ent))))
;;; Return the layer of an entity
(defun GetLayer (ent) (cdr (assoc 8 (entget ent))))
;;; *** No need for the following cause the layer and
;;;     text style will already exist in the dwg ...dummy!?
;;; (defun Exist? (tpe ent) (and (tblsearch tpe ent)))
;;; ***
;;; Set the layer curent
(defun PutLayerCur (lay) (setvar "clayer" lay))
;;; set the text style current
(defun PutTxtStyleCur (sty) (setvar "textstyle" sty))
;;; Main
(defun c:PutStuffCur ( / ent)
  (setq ent (entse7))
  (PutTxtStyleCur (getTxtStyle ent))
  (PutLayerCur (getLayer ent))
 (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Challenge (for the newbies)
« Reply #13 on: June 15, 2004, 03:29:32 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

Just thought I'd give Se7en a hand since he's busy peddling his wares..... :twisted:
Code: [Select]

(defun c:set-lay-styl (/ doc ent)
  (vl-load-com)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (if (and (setq ent (car (entsel "\nSelect Text object for style & layer to set current: ")))
  (setq ent (vlax-ename->vla-object ent))
  (vl-string-search "TEXT" (strcase (vla-get-objectname ent)))
  )
    (progn
      (vla-put-activelayer doc (vla-item (vla-get-layers doc) (vla-get-layer ent)))
      (vla-put-activetextstyle doc (vla-item (vla-get-textstyles doc) (vla-get-stylename ent)))
      )
    (princ "\nInvalid selection, exiting......")
    )
  (princ)
  )


Jeff
* darn, Se7en posted before I could get off the phone.......

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge (for the newbies)
« Reply #14 on: June 15, 2004, 06:06:50 PM »
Nice one Jeff..
No time today, but had this one I use a lot.
Gimmee some of that gas anyway. :)
Code: [Select]
;;            SetCurrent.lsp        
;;      Created by C. Alan Butler  2003
;;
;;  Routine to set current Text or Dim Style by
;;  picking an existing object in the drawing  
;;  sets the layer of selected ent current as well
;;  
;;  Enter tds from the command line to run
;;  or set up a menu button with ^C^Ctds
;;;
;;;
;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
;;;   WARRANTY.  ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
;;;   PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.
;;;
;;;
(defun c:tds (/ ent entbl styold stynew usercmd)
  (if (setq ent
             (car (entsel "\nSelect Dimension or Text to make current: "))
      )
    (progn
      (setq entbl (entget ent)) ; Get entity definition list
      (setq usercmd (getvar "CMDECHO"))
      (setvar "CMDECHO" 0)
      (command "undo" "begin")
      (cond
        ((or (= "MTEXT" (cdr (assoc 0 entbl)))
             (= "TEXT" (cdr (assoc 0 entbl)))
         )
         (progn
           (setq stynew (cdr (assoc 7 entbl))
                 styold (getvar "textstyle")
           )
           (if (/= stynew nil)
             (setvar "TextStyle" stynew)
             (progn
               (setvar "TextStyle" "Standard")
               (setq stynew "STANDARD")
             )
           )
           (command "'Layer" "M" (cdr (assoc 8 entbl)) "")
           (prompt
             (strcat "\nText style changed from "
                     styold              " to "
                     stynew              "."
                    )
           )
         )
        ) ; end cond 1

        ((= "DIMENSION" (cdr (assoc 0 entbl)))
         (setq styold (getvar "dimstyle"))
         (command "dimstyle" "restore" "" ent)
         (setq stynew (getvar "dimstyle"))
         (command "'Layer" "M" (cdr (assoc 8 entbl)) "")
         (prompt
           (strcat "\nDimension style changed from "
                   styold                 " to "
                   stynew                 "."
                  )
         )
        ) ; end cond 2

        (t (prompt "\nSelection was not Text or Dimension.")
         ) ; end cond (T)
       
      ) ; end Cond stmt
      (command "undo" "end")
      (setvar "CMDECHO" usercmd)
      (princ)
    ) ; end progn
  ) ; endif
) ;End of Defun
(prompt "\nText / Dimension Style Changer Loaded, Type TDS to run")
(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.

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: 10625
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: 28762
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: 28762
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: 28762
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.