Jump to content

While and foreach functions with VL- codes


Recommended Posts

Posted

Hello,

 

Why the function while is no working with VL- or is there any specific procedure

with VL- codes with while ?

 

But if I replaced while with function cond, it would run normally. What's wrong?

 

Besides that, function foreach I couldn't included to the codes. Any suggestions ?

 

(vl-load-com)
(if (setq something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
  (progn
   (setq i -1 ss (sslength something))
 ([b][color="red"]while[/color][/b]
   (< i ss)
 (setq loops (ssname something (setq i (1+ i))))
 (setq obj (vlax-ename->vla-object loops))
 (setq vol (vla-get-area obj))
    	 ) )
 (princ)
 )

 

Many Thanks,

 

Tharwat

Posted
Hello,

 

Why the function while is no working with VL- or is there any specific procedure

with VL- codes with while ?

 

But if I replaced while with function cond, it would run normally. What's wrong?

 

Besides that, function foreach I couldn't included to the codes. Any suggestions ?

 

(vl-load-com)
(if (setq something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
(progn
(setq i -1 ss (sslength something))
([b][color=red]while[/color][/b]
(< i ss)
(setq loops (ssname something (setq i (1+ i))))
(setq obj (vlax-ename->vla-object loops))
(setq vol (vla-get-area obj))
) )
(princ)
)

 

Many Thanks,

 

Tharwat

Why not?

(vl-load-com)
(if (setq something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
  (progn
(setq pf (vla-get-activeselectionset
        (setq adoc
           (vla-get-activedocument
          (vlax-get-acad-object))))
   )
 (vlax-for obj pf
(princ (strcat "\n" (vl-princ-to-string 
(setq vol (vla-get-area obj))))........

 

~'J'~

Posted
Why not?

(vl-load-com)
(if (setq something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
  (progn
(setq pf (vla-get-activeselectionset
        (setq adoc
           (vla-get-activedocument
          (vlax-get-acad-object))))
   )
 (vlax-for obj pf
(princ (strcat "\n" (vl-princ-to-string 
(setq vol (vla-get-area obj))))........

 

~'J'~

 

All right, That's really great.

 

And the list looks upside down.

0.913223
4.24149
2.13487
[b][color="red"]"\n[/color][/b]2.13487" 

Which I thing "\n must be at the begining. mustn't be?

 

So what was wrong with my previous functions ?

 

many Thanks fixo.

 

Tharwat

Posted

I've tried not to modify your code too much so you can learn from it:

 

(vl-load-com)
(if (setq something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
 (progn
   (setq i -1 ss (1- (sslength something)))
   
   (while (< i ss)
     (setq loops (ssname something (setq i (1+ i))))
     (setq obj (vlax-ename->vla-object loops))
     (setq vol (vla-get-area obj))
   )
 )
 (princ)
)

 

Because you are incrementing inside the loop, when the variable 'i' is set to the last item in the selection, i.e. (1- (sslength )) the loop will continue, however, 'i' is then incremented when using ssname, hence returning nil, causing your error.

 

I would perhaps rewrite it as:

 

(if (setq i -1 something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
 (while (setq loops (ssname something (setq i (1+ i))))
   (setq obj (vlax-ename->vla-object loops))
   (setq vol (vla-get-area obj))
 )
)

Posted

Thanks a lot Lee.

 

All right , so I did duplicate the increment two time which causing the error. :thumbsup:

 

And what about the use of function foreach to print the outcome in a list at the end ?

 

Best Regards.

Posted
Thanks a lot Lee.

 

All right , so I did duplicate the increment two time which causing the error. :thumbsup:

 

And what about the use of function foreach to print the outcome in a list at the end ?

 

Best Regards.

 

It wasn't so much that you duplicated the increment, but rather you increment after the test expression of the WHILE loop - think it through in your mind what values each variable will have at each stage of the loop.

 

As for the foreach, I would print the values in the WHILE loop - why loop through the same information twice... :)

Posted

As for the foreach, I would print the values in the WHILE loop - why loop through the same information twice... :)

 

May be due to being in a harry, the function (print vol) ran away from my mind. :lol:

 

Print function gave me all values in a list greatly.

 

Thank you so much.

 

My best regards.

 

Tharwat

Posted
All right, That's really great.

 

And the list looks upside down.

0.913223
4.24149
2.13487
[b][color=red]"\n[/color][/b]2.13487" 

Which I thing "\n must be at the begining. mustn't be?

 

So what was wrong with my previous functions ?

 

many Thanks fixo.

 

Tharwat

 

In addition

"\n" is means the new line

"\t" is means the tab

and also you need to build other filter to select closed polylines:

(vl-load-com)
(defun C:try(/ adoc ar ar_list pf something)
(if (setq something (ssget (list (cons -4 "<or")
    (cons -4 "<and")
    (cons 0 "LWPOLYLINE")
    (cons 70 1);<--closed polyline
    (cons -4 "and>")
    (cons 0 "CIRCLE,ELLIPSE")
    (cons -4 "or>"))))
(progn
(setq pf (vla-get-activeselectionset
        (setq adoc
           (vla-get-activedocument
          (vlax-get-acad-object))))
   )
(vlax-for obj pf

(princ (strcat "\n" (vl-princ-to-string 
(setq ar (vla-get-area obj)))))
(setq ar_list (cons ar ar_list))
)))
 (princ (strcat "\nTotal area: " (rtos (apply '+ ar_list)) " cubic drawing units"))
(princ)
)

 

~'J'~

Posted

The only reason you might want to create and step through the list is sort or apply to something else.

 

eg. (sorted)

(defun c:Foo (/ i something loops obj lst)
 (if (setq i         -1
           something (ssget '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE")))
     )
   (progn
     (while (setq loops (ssname something (setq i (1+ i))))
       (setq obj (vlax-ename->vla-object loops))
       (setq lst (cons (vla-get-area obj) lst))
     )
     (mapcar 'print (vl-sort lst '<))
   )
 )
 (princ)
)

Posted

Certainly , a very great routine fixo with your last adds.

 

Mr.alanjt also great as well, your way of sorting it, is wonderful.

 

I have learned lots of things instead of foreach function tonight . :)

 

Thank you so much.

 

Tharwat

Posted

Foreach, mapcar and mapcar+lambda all have their place.

Posted
And the list looks upside down.

0.913223
4.24149
2.13487
[b][color="red"]"\n[/color][/b]2.13487" 

Which I thing "\n must be at the begining. mustn't be?

Actually the "\n2.13487" line at the end is a duplicate of the last line. Since all lisp calls return something (the last thing calculated) this is returned to the command line at the end of the routine. That's why you usually have the (princ) as the very last thing happening in any defun which can be called from the command line - just so you don't end up with unintended output.
Posted

Thanks a lot. irneb

 

I am so happy to hear from you in here.

 

And regarding to (princ) function I guess it took that way of appearing due to the use of it after the (princ) itself .

 

And if we use it the end after the (print) function it would behave as expected.

 

Regards,

 

Tharwat

Posted

Thanks Tharwat. I saw some very nice things here and decided I'm widening my base from Augi.

 

I think you've got the idea about that princ thing. 8)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...