TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: rlxozzang on August 08, 2012, 07:50:22 PM

Title: while statement returns an error to wonder why?
Post by: rlxozzang on August 08, 2012, 07:50:22 PM
Code: [Select]
(defun c:aa (/ e)

(setq e (entnext (tblobjname "BLOCK" (cdr (assoc 2 (entget (car (entsel "\n블럭을 선택하세요!! :"))))))))

(while
(not (eq (cdr (assoc 2 (entget e))) "SEQEND"))
(if (eq (cdr (assoc 0 (entget e))) "TEXT")
(vla-put-Alignment (vlax-ename->vla-object e) acAlignmentMiddle)
)
(setq e (entnext e))
)

(vla-regen (vla-get-activedocument (vlax-get-acad-object)) acAllViewports);The syntax does not return after the end regen while statement.
)
Title: Re: while statement returns an error to wonder why?
Post by: Keith™ on August 08, 2012, 09:20:07 PM
Are you sure that this doesn't product nil?

Code: [Select]
(setq e (entnext e))
Title: Re: while statement returns an error to wonder why?
Post by: BlackBox on August 08, 2012, 10:20:09 PM
If you're only performing this task on a single block, then why not simply RefEdit?
Title: Re: while statement returns an error to wonder why?
Post by: rlxozzang on August 08, 2012, 10:36:56 PM
every one thanks.... Success...........
Code: [Select]
(defun c:aa (/ e)

(setq e (tblobjname "BLOCK" (cdr (assoc 2 (entget (car (entsel "\n블럭을 선택하세요!! :")))))))

(while (and (setq e (entnext e)) (not (eq (cdr (assoc 2 (entget e))) "SEQEND")))
(if (eq (cdr (assoc 0 (entget e))) "TEXT")
(vla-put-Alignment (vlax-ename->vla-object e) acAlignmentMiddle)
)
)

(vla-regen (vla-get-activedocument (vlax-get-acad-object)) acAllViewports)

)
Lisp still rookies....^^
Title: Re: while statement returns an error to wonder why?
Post by: BlackBox on August 09, 2012, 01:29:50 AM
FWIW -

Given that you're converting each entity in your selection set to a Vla-Object, consider iterating the ActiveSelectionSet Collection of the ActiveDocument Object using vlax-for.
Title: Re: while statement returns an error to wonder why?
Post by: rlxozzang on August 09, 2012, 03:03:12 AM
FWIW -

Given that you're converting each entity in your selection set to a Vla-Object, consider iterating the ActiveSelectionSet Collection of the ActiveDocument Object using vlax-for.

For example???,
Code: [Select]
(defun c:aa ()

(ssget)

(vlax-for o (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object)))

(if (eq (vla-get-objectname o) "AcDbText")
(vla-put-Alignment (vlax-ename->vla-object o) acAlignmentMiddle)
)
)
)
Title: Re: while statement returns an error to wonder why?
Post by: irneb on August 09, 2012, 03:44:08 AM
Actually in your case a selection set won't cut it. You're editing the objects inside a block so simply iterate through the block's collection using vlax-for:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:aa (/ blk)
  2.   (or *blocks* (setq *blocks* (vla-get-Blocks *doc*)))
  3.   (if (not (vl-catch-all-error-p (setq blk (vl-catch-all-apply 'vla-Item (list *blocks* "BLOCKNAME")))))
  4.     (vlax-for obj blk
  5.       (if (eq (vla-get-ObjectName obj) "AcDbText")
  6.         (vla-put-Alignment obj acAlignmentMiddle))))
  7.   (vla-regen *doc* acAllViewports)
  8.   (princ))
BTW, what editor do you use for your source-code. If I copy-paste your code into VLIDE that UniCode block-name becomes a string of question marks.

Edit: sorry ... that's the message for entsel  :-[ ... stupid of me.
Title: Re: while statement returns an error to wonder why?
Post by: rlxozzang on August 09, 2012, 04:10:43 AM
Actually in your case a selection set won't cut it. You're editing the objects inside a block so simply iterate through the block's collection using vlax-for:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:aa (/ blk)
  2.   (or *blocks* (setq *blocks* (vla-get-Blocks *doc*)))
  3.   (if (not (vl-catch-all-error-p (setq blk (vl-catch-all-apply 'vla-Item (list *blocks* "BLOCKNAME")))))
  4.     (vlax-for obj blk
  5.       (if (eq (vla-get-ObjectName obj) "AcDbText")
  6.         (vla-put-Alignment obj acAlignmentMiddle))))
  7.   (vla-regen *doc* acAllViewports)
  8.   (princ))
BTW, what editor do you use for your source-code. If I copy-paste your code into VLIDE that UniCode block-name becomes a string of question marks.

Edit: sorry ... that's the message for entsel  :-[ ... stupid of me.

Wow Good~~~~~
I write notepad + +.
Syntax is hard for me .... Still or, vl-catch-all-error-p, these functions because the concept ...
Title: Re: while statement returns an error to wonder why?
Post by: irneb on August 09, 2012, 04:45:58 AM
Oh yes, N+ does allow UniCode (or more probably UTF8). I wish VLIDE had that capability (amoungst many other things I wish for  :pissed: )!

About the vl-catch-* functions:

vl-catch-all-apply
This simply runs as an apply (see later for description), with the added feature that if an error occurs it doesn't stop the lisp. If an error occurs it returns a <%catch-all-apply-error%> object instead. If no error occurs it returns the evaluated result as normal.

vl-catch-all-error-p
 This checks if the argument is a <%catch-all-apply-error%> object. If it is returns T, else returns nil.

apply
This is something only found in lisp-like languages. Most of lisp's built-in functions accept multiple arguments, these include even functions which are binary operators in other languages. E.g. the add function (+ ...) is a binary operator in C. Say you'd want to add 4 numbers together. In C that would be:
Code - C: [Select]
  1. total = 1 + 2 + 3 + 4;
In lisp it's
Code - Auto/Visual Lisp: [Select]
  1. (setq total (+ 1 2 3 4))
Now say you don't know how many numbers there are to add together. Rather you've got a list/array of numbers (which could be any length). In C you'd have to loop through the list keeping a running total. Not to mention, you'd need to know somehow the length of the array, or calculate it by figuring out from the size of the RAM used. E.g. :
Code - C: [Select]
  1. int numbers[] = {1 , 2, 3, 4, 5, 6};
  2. int len = sizeof(numbers) / sizeof(int);
  3. int total = 0;
  4. int index;
  5. for (index = 0; index < len; index++)
  6. {
  7.   total += numbers[index];
  8. }
  9. return total;
In lisp you could do it the same way (though I'm using lisp's foreach instead of an index and trying to figure out how long the list/array is):
Code - Auto/Visual Lisp: [Select]
  1. (setq numbers '(1 2 3 4 5 6)
  2.       total 0)
  3. (foreach num numbers
  4.   (setq total (+ total num)))
  5. total
Or you could be smart and use the idea that + takes more than 2 arguments using the apply function
Code - Auto/Visual Lisp: [Select]
  1. (setq numbers '(1 2 3 4 5 6))
  2. (apply '+ numbers)
Title: Re: while statement returns an error to wonder why?
Post by: rlxozzang on August 09, 2012, 10:28:27 PM
irneb thanks...
Lisp still just a beginner should not be difficult to understand the function! Lisp for the good of studying ~!