Author Topic: (Challange - Newbie) Adopt text stlye  (Read 3734 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« on: January 26, 2005, 03:46:37 PM »
This challange will be to adopt the current text style to a selected text's text stlye.

I want to be able to change the current text style very easily and fast. So give me a way to select a peice of text, extract its text style and set that text style current.

Criteria:
1.) Must have some level of error trapping. (e.g. Account for a miss pick by the end user.)
2.) Programer can NOT use "command" to make the switch of text styles.

Time:
-- Code can be submitted tomorow morning.

NOTE:
-- Ask any questions that may arise. Do NOT feel any question is stupid.

//-- NOTE --//
If you have this procedure coded I your head by the time you are finished reading this post then I sudgest you hold on to your code for a bit.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #1 on: January 27, 2005, 08:28:08 AM »
Alright, morning is here. Let's see what cha got.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #2 on: January 27, 2005, 10:40:39 AM »
No takers huh?!
Okay, im gonna post my code and then i want to open this up to anyone.

Code: [Select]

(defun c:ss ( / )
    ;; Issue a loop;
  (while
    ;; Loop untill...
    (not
    ;; The user hasnt selected an entity
      (setq ent (entsel))
      )
    ;; NOTE: A while loop will return nil because the while loop
    ;; loops until the test condition returns nil. This loop will
    ;; make sure that the end user has selected something and didnt
    ;; misspick. However this loop will only ensure that the user
    ;; has selected something, it will NOT check to see if the user
    ;; selected a valid object (I.e. something with a txt style.)
    )
    ;; Create a variable and fill it with the text stlye information of an ent
  (setq styl
        (cdr
          (assoc
            7
            (entget
              (car ent)
              )
            )
          )
        )
    ;; set the entities text style current;
  (setvar
    ;; instead of using "command" we can issue a setvar to change
    ;; the text style varaible
    "textstyle"
    ;; Check to see if a style was retrieved from the entity
    ;; if the user selected a line (which dosent have a text style)
    ;; the "styl" variable will contain "nil" and we cannot set the
    ;; text style to nil.  To bypass this, we can issue a small test
    (if
      (not styl)  ; <-- if the styl variable contains "nil"
      (getvar "textstyle") ; <-- then use the curent txt style instead.
                           ; The reason this works is because at the time
                           ; the program reaches this point the text style
                           ; has not been set so the users orig. txt style
                           ; is has its always been before the program ran.
      styl        ; <-- otherwise if the var actualy contains a txt style
                  ;     then use it.
      )
    )
    ;; Let's propmt the user.
  (if
    ;; Since we tested before we changed anything we should use the same
    ;; test to prompt the user as to what happned. (This is the same test
    ;; as the one we just used.)
     (not sty)
       (princ "\n> *error* -- entity selected does not have style. Textstyle remains unchanged.")
       (princ "\n> New text style adopted. Program complete. ")
    )
 (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
(Challange - Newbie) Adopt text stlye
« Reply #3 on: January 27, 2005, 12:38:27 PM »
are there certain steps to creating such a routine like this? are the steps all the same. how do you know what code to write first?

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #4 on: January 27, 2005, 12:48:11 PM »
Sure there are. Let me get something started and ill ... Tell ya what?! Wanna step thru a procedure together?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
(Challange - Newbie) Adopt text stlye
« Reply #5 on: January 27, 2005, 01:03:36 PM »
well, since i just learned that there is a course on the "teach me" forum, should i wait?...i have aim, my sn is "zzidubizz"

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #6 on: January 27, 2005, 01:19:20 PM »
No, right here, right now. (I have a site visit today after lunch but ive already written up a good protion of it already.)

Ill post it right after i post this post.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #7 on: January 27, 2005, 01:21:51 PM »
Let's step thru the challenge whdjr posted. (He wanted to take a list and separate it into separate lists of two.) It sounds harder then it really is. Its actually a real simple task.

So lets take a list of ten numbers. (It can be any number, but lets just stick to round numbers for now.) Now imagine that this list is a collection of real world objects. (Let's use coins. --pennys) Now if you had ten pennies and you wanted to separate that pile into piles of two how would you do it? Well, you would just pull off two pennies off the pile and line them up until you didnt have a pile right?! (Simple) Well the same thing goes for a list in AutoLisp. You just start pulling off two items of the list at a time till you dont have a list anymore.

Now that you have the basic concept of what you want to accomplish down lets put into code. (PseudoCode to be exact.) Pseudo code is nothing more then what we just did in our head, its just putting our procedure down in English in separate statments.


Pseudo code:

Objective: Take a list and split it into smaller separate lists of two.
Arguments: A list to split into pieces.
Date: 01.27.05
Code:

o  Iterate thru a list until there are no more members
o  Pop off two items and create a list out of them
o  Construct / append a new list of these smaller lists
o  Return the new list of smaller lists.


Now that we have our ideas worked out in English we examine that code for any potential problems we can see.

Can you think of any problems with our code sofar? hummm... Well right away I see that if our list is an odd number we are going to have a "remainder" list left over. Now we ask ourselves what application is this for and how are we intending to use it? If we know that we are always going to have an even amount of data I wouldnt worry about appending our design. Now if we are to get an unknown amount of items I would want to append our code to either toss out the remainder or just not run at all, but for this example it is to be assumed that we will always have an even amount of data.

I want you to find at least one AutoLisp functions for each statement in our PseudoCode. (e.g. "iterate thru a list..." We can use either a "while", "mapcar" or a "foreach" function.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
(Challange - Newbie) Adopt text stlye
« Reply #8 on: January 27, 2005, 02:01:22 PM »
OK, LETS see. brb...ive got some work to do...but, its kinda hard for me to understand what the lists is...am i gonnna use code functions to do this?

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #9 on: January 27, 2005, 02:13:24 PM »
what do you mean? Picture a pile of pennies instead of lists. (Dont make it harder then it is. Keep it stupid simple.)  Dont get to indepth, I wouldnt give you anything to hard to figure out at all ma man. (Im here to help you visualize this stuff. Its all really simple once you can do that.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
(Challange - Newbie) Adopt text stlye
« Reply #10 on: January 27, 2005, 02:56:50 PM »
o i c....so in the pile of pennies does each penny have its own identity,...like (p1, p2,..p10)?

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
(Challange - Newbie) Adopt text stlye
« Reply #11 on: January 27, 2005, 08:15:43 PM »
Is that how you would seperate a pile? (i.e. you would go thru and lable them before you start to seperate them.) ...No!

Right now, its just a pile of pennies. That is all we care about. (Just a pile!) All we care about is that it is a PILE of pennies. Thats it. We dont care how many there are (for the most part) and/or what year they were made.

Later, if you feel up to it, we can start "filering" out stuff in the pile. (e.g. fiter out any dimes that might be mixed in. Or even pennies of a certian year.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org