Author Topic: trying to write my first lisp and i need a bit of help  (Read 23581 times)

0 Members and 1 Guest are viewing this topic.

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #30 on: July 27, 2005, 12:08:21 PM »
could you use newlist in combonation with cdr to narrow it down?

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #31 on: July 27, 2005, 12:09:39 PM »
Hehe. I must've not copied that last paren.
So, did you try the other items?

Let's move on then.
Where are you going to put the cdr in your routine above? I don't feel that you need to create another variable for it and cdr requires a list, so set it and post it and then we'll move on, but you need to answer the first question before I do.

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #32 on: July 27, 2005, 12:36:22 PM »
Code: [Select]
(defun c:mto (/ ename elist)
     (setq ename (car (entsel "\nSelect text: ")) ;;we get the list
              elist (cdr (entget ename) ;;we have the list and want to get the dxf code we need from the list
              code10 (assoc 10 elist) ;;we get the dwf code ename
;;i am guessing the cdr goes in the third line???
     )
)

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #33 on: July 27, 2005, 12:39:38 PM »
i'd like to comment everything so i know what's going on which i'm not sure that i do?

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #34 on: July 27, 2005, 12:48:26 PM »
Quote
could you use newlist in combonation with cdr to narrow it down?

Yes, you could as you've done in your code above, but it would amount to very little. Remember me telling you about keys and values? With (assoc 10 elist) we've grabbed the list we want to manipulate. All we're trying to do now is remove the 10 from the associated list and pass it to the variable code10.

Oh, let me let you in on something: When viewing an elist, you are looking at a list containing a bunch of other lists of an entity. We don't need to manipulate the elist itself but a list within it. CDR the next line down, just the way you've placed it now and the routine will return the group 10 list without the 10 in it.

Is this all starting to make sense?
Have you tried my question from previous posts about typing in acad !ename, !elist and code10?

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #35 on: July 27, 2005, 01:03:00 PM »
Command: !ename
nil

Command: !elist
nil

Command: !code10
(10 1077.0 811.598 0.0)

Code: [Select]
(defun c:mto (/ ename elist)
     (setq ename (car (entsel "\nSelect text: ")) ;;we get the elist
              elist (entget ename) ;;we have the elist and want to get the ename we need from the list
              code10 (assoc 10 cdr elist) ;;we get the ename and break it down
;;is this placement right. when i inspect it it say complete the expression???
     )
)

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #36 on: July 27, 2005, 01:09:36 PM »
i've put the cdr in various places in that line and cannot get it to return without the 10  :cry:

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #37 on: July 27, 2005, 01:28:26 PM »
Code: [Select]
(cdr (entget ename)
Just like you did here, Dan. Wrap () the (assoc 10 elist) in the cdr like: (cdr (assoc 10 elist)).

Quote
Command: !ename
nil

Command: !elist
nil

Command: !code10
(10 1077.0 811.598 0.0)

Now answer this question, why are the first two nil and the third one has a value? Read Lesson2 at around page 8 for help. Ask, if that still doesn't help.

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #38 on: July 27, 2005, 01:53:55 PM »
sigh i don't know i see that page talks about using arguments and variables in defun. are these 2 variables and the last a value?

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #39 on: July 27, 2005, 02:16:17 PM »
Local and Global variables. I don't want to define the difference here. It's been covered to exhaustion on this forum. I will tell you this and then we can move on. !ename and !elist are defined as local variables currently and !code10 is defined as a global variable. There is no need for it to be Global.

Question: What can we do to make it Local?
Hint: Look at the line that defun is on.

Post the code with the cdr and code10 localized and we'll move on.

Don't worry if it seems overwhelming. Some of these things I'm pointing out so when we need to worry about them, the light will turn on.

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #40 on: July 27, 2005, 03:00:15 PM »
daron i was telling my boss about stigs class and told her i was trying to write something. she said why dont i ask her and jumped into the creation of this lisp, renaming some variables and performing some tests so i understand what's going on. i think i know what you mean about localizing the variable code10. i needed to add it after elist at the beginning. my boss thought i should name the variables so they make better sense and declare them once i finish. anyway here is the testing she had me do and my code in the current state:


Command: (setq txtname (car (entsel)))

Select object: <Entity name: 7ef977a0>

Command: (setq txtlist (entget txtname))
((-1 . <Entity name: 7ef977a0>) (0 . "TEXT") (330 . <Entity name: 7ef94cd0>) (5
. "2A7C") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (62 . 2) (100
. "AcDbText") (10 1077.0 811.598 0.0) (40 . 3.0) (1 . "S.C.") (50 . 0.0) (41 .
1.0) (51 . 0.0) (7 . "ROMANS") (71 . 0) (72 . 4) (11 1081.29 813.098 0.0) (210
0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))

Command: (setq pt1temp (assoc 10 txtlist))
(10 1077.0 811.598 0.0)

Command: (setq pt1 (cdr pt1temp))
(1077.0 811.598 0.0)


Code: [Select]
(defun c:mto ( / )
     (setq txtname (car (entsel "\nSelect text: ")) ;;we get the txtlist
              txtlist (entget txtname) ;;we have the txtlist and want to get the txtname we need from the list
              pt1 (cdr (assoc 10 txtlist));;we break this down to only the coordinates
     )
)

ELOQUINTET

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #41 on: July 27, 2005, 03:35:39 PM »
now that i have the coordinates of the text i want to move stored how do i get the midpoint of the dimension line. i thought i could do it the same way but i'm not sure about the format or the dxf code for the dimension line?

Code: [Select]
(defun c:mto ( / )
     (setq txtname (car (entsel "\nSelect text: ")) ;;we save the txtname
              txtlist (entget txtname) ;;we have the txtlist and want to get the txtname we need from the list
              pt1 (cdr (assoc 10 txtlist));;we break this down to only the coordinates
     (setq dimname (car (entsel "\nSelect text: ")) ;;we get the dimlist
              dimlist (entget dimname) ;;we have the dimlist and want to get the txtname we need from the list
              pt2 (cdr (assoc 1 dimlist))
     )
)

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #42 on: July 27, 2005, 03:46:37 PM »
Code: [Select]
(defun c:mto ( / )
I hesitate to get into this.
What you have there is fine, though unneccesary. (defun c:mto () will also work, however, what you've done is globalized ALL your variables. You want to Localize them or one day you'll end up with a slow processing computer. To localize them we need to ADD
(defun c:mto (/ txtname txtlist pt1)
This will clear all your variables to nil when you are done processing the lisp. I have to work with an add-on right now that has all kinds of global dependencies. It sucks and if any of them gets changed, the whole program (add-on) won't work properly until I restart autocad.

As far as changing the variable names to make more sense, I agree, but what happens when we go from selecting single text objects to multiple text objects? Honestly, it doesn't matter, since the end user will never know the difference. As long as you, the code hacker can understand it when you go in to edit it 3 months down the line, is all that matters. What you wouldn't want to do is something like:
Code: [Select]
(defun c:mto () ;<-Naming like this isn't good either.
    (setq a (car (entsel "\nSelect text: ")
           b (entget a)
           c (cdr (assoc 10 b))
    )
)

After a while the code gets a little long and you'll tend to forget what "a" means.

Let's get back on topic. Localize your variables (/ txtname txtlist pt1).
All you need is the return value. Post your code and I'll see what's next.

daron

  • Guest
trying to write my first lisp and i need a bit of help
« Reply #43 on: July 27, 2005, 03:52:30 PM »
Code: [Select]
;;pick the text using the node as the point
;;then move the text to the midpoint of the dimension line
;;then move the text down 3-1/2

Add this to the top of the code, so we remember what it is we're trying to accomplish. This is your pseudo-code. It'll help us retain focus. I just looked at it again and realized that we need to select the dimension line. Why don't you take that as your next assignment within the current function. Add the selection of a dimension and go about getting an elist of it. We'll need that.

Every time you post code make sure that the pseudo-code is also attached.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
trying to write my first lisp and i need a bit of help
« Reply #44 on: July 27, 2005, 03:56:03 PM »
Just to dovetail (by example) onto what Daron is saying consider this ---

Code: [Select]
(defun c:example ( / ename data result )
    (if (setq ename (car (entsel)))    
        (cond
            (   (eq "TEXT"
                    (cdr
                        (assoc 0
                            (setq data
                                (entget ename)
                            )
                        )
                    )
                )
                (setq result
                    (cdr
                        (assoc 10 data)
                    )
                )
            )
        )
    )    
    result    
)

Salient points: (1) Somewhat error trapped (traps an unsuccessful entity selection), (2) variables are self describing, (3) variables are declared as locals, (4) coded to work [at this time] specifically with text objects, (5) the overall structure is flexible, lending itself to future growth.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst