Jump to content

how to write 2 foreach loops in a program


gvgbabu

Recommended Posts

hi all

 

how to write a lisp program for drawing which is attached to this thread

i am thinking like this

a. using getpoint for getting the lower left corner coordinates

b. 2 foreach loops will come for length and depth

 

but i could not get how to write the 2 foreach loops in the program

 

can somebody help me to find the solution

 

thanks

Link to comment
Share on other sites

hi all

 

how to write the lisp program for drawing which is attached to this thread

i am thinking like this

a. using getpoint for getting the lower left corner coordinates

b. 2 foreach loops will come for length and depth

 

but i could not get how to write the 2 foreach loops in the program

 

can somebody help me to find the solution

 

thanks

sample.dwg

Link to comment
Share on other sites

an embedded foreach statement is written the same way as a single foreach statement

(foreach c someList
 (foreach d someOtherList

 )
)

Just make sure you use different variable names for list elements, in this case "c" and "d"

Link to comment
Share on other sites

thanks soliver

 

see my drawing (sample.dwg)

 

i will set the 2 lists as below

 

(setq length (list 1.5 2.5 2.0 3.0))

(setq depth (list 0.23 0.38 0.45 0.6))

 

my intension is that top horizontal linea are at same elevation but lower horizontal lines are differenet with respect to depths

 

1st pick the lower left corner from the getpoint

and draw vertical line with depth 0.23 and draw top and bottom horizontal lines

with a lenth 1.5

and then start second rectangle with depth 0.38 and lenth 2.5 again it should start from the left corner

ans so on

 

i am asking how to write this lisp

can any body help me

thanks

Link to comment
Share on other sites

You don't need two FOREACH cycles to achive that (by doing this will get 25 rectangles instead of 5). Please check if this is what you are looking for:

 

(setq OldOsmode (getvar "OSMODE"))
(setq theCorner (getpoint "\nPick the corner: "))
(setq theLength (list 1.5 2.5 2.0 3.0))
(setq theDepth (list 0.23 0.38 0.45 0.6))

(setq CrIndex 0)
(setvar "OSMODE" 0)
(repeat (length theLength)
(command "_RECTANGLE" theCorner
                  (list (+ (car theCorner)  (nth CrIndex theLength))
                (- (cadr theCorner) (nth CrIndex theDepth))))
(setq CrIndex (1+ CrIndex))
)
(setvar "OSMODE" OldOsmode)

Please take care that isn't recomended to use "length" or any other AutoLISP protected symbol to name your variables.

 

Regards,

Mircea

Link to comment
Share on other sites

So, please find below the fixed version:

 

(setq OldOsmode (getvar "OSMODE"))
(setq theCorner (getpoint "\nPick the corner: "))
(setq theLength (list 1.5 2.5 2.0 3.0))
(setq theDepth (list 0.23 0.38 0.45 0.6))

(setq CrIndex 0)
(setvar "OSMODE" 0)
(repeat (length theLength)
(command "_RECTANGLE" theCorner
                      (list (+ (car theCorner)  (nth CrIndex theLength))
                            (- (cadr theCorner) (nth CrIndex theDepth))))
(setq theCorner (polar theCorner 0.0 (nth CrIndex theLength)))
(setq CrIndex (1+ CrIndex))
)
(setvar "OSMODE" OldOsmode)

Regards,

Mircea

Link to comment
Share on other sites

thanks Mircea,

i got the same

but you are taking the pick corner as upper left corner

if the selected pick corner is lower left corner

what would be the program

can u modify it

 

thanks

Link to comment
Share on other sites

Sorry, the reference point isn't marked on your sketch... so, please check the code below to see if is the right one:

 

(setq OldOsmode (getvar "OSMODE"))
(setq theCorner (getpoint "\nPick the corner: "))
(setq theLength (list 1.5 2.5 2.0 3.0))
(setq theDepth (list 0.23 0.38 0.45 0.6))
(setq theCorner (polar theCorner (* 0.5 pi) (car theDepth)))

(setq CrIndex 0)
(setvar "OSMODE" 0)
(repeat (length theLength)
(command "_RECTANGLE" theCorner
                      (list (+ (car theCorner)  (nth CrIndex theLength))
                            (- (cadr theCorner) (nth CrIndex theDepth))))
(setq theCorner (polar theCorner 0.0 (nth CrIndex theLength)))
(setq CrIndex (1+ CrIndex))
)
(setvar "OSMODE" OldOsmode)

 

Regards,

Mircea

Link to comment
Share on other sites

thanks Mircea,

i got it

but one more thing is there

u are using rectangle

in my case, lines are needed

if we use line is there any change in the program

can u modify it again

 

thank u very much

Link to comment
Share on other sites

Here you go:

 

[font=monospace](setq OldOsmode (getvar "OSMODE"))
(setq theCorner (getpoint "\nPick the corner: "))
(setq theLength (list 1.5 2.5 2.0 3.0))
(setq theDepth (list 0.23 0.38 0.45 0.6))
(setq theCorner (polar theCorner (* 0.5 pi) (car theDepth)))

(setq CrIndex 0)
(setvar "OSMODE" 0)
(repeat (length theLength)
(command "_RECTANGLE" theCorner
                      (list (+ (car theCorner)  (nth CrIndex theLength))
                            (- (cadr theCorner) (nth CrIndex theDepth)))
         "_EXPLODE" (entlast))
(setq theCorner (polar theCorner 0.0 (nth CrIndex theLength)))
(setq CrIndex (1+ CrIndex))
)
(setvar "OSMODE" OldOsmode)[/font]

Regards,

Mircea

Link to comment
Share on other sites

thanks Mircea,

 

but in the program only first rectangle only picking the lower left corner

for other rectangles again it goes to upper left corner

in some cases, there must be some gapes in between rectangles (ie gaps 0.23 0.6 0.45 etc)

if that is the case every time it should go to lower left corner while drawing rectangles

can u modify it again with gaps in between the rectangles

 

thank u very much

Link to comment
Share on other sites

Sorry, but really don't understand your explanations - so, why don't you try to edit my code to suit your needs? Just post your attempt and I will provide you support. Also, please post an image to help me understand the desired result. Thank you.

 

Regards,

Mircea

Link to comment
Share on other sites

I have commented my last "release" to give you a clue:

 

(setq OldOsmode (getvar "OSMODE"))                                          ;retain user's OSMODE
(setq theCorner (getpoint "\nPick the corner: "))                           ;ask user to input base point
(setq theLength (list 1.5 2.5 2.0 3.0))                                     ;lists of lengths / depths (size should match)
(setq theDepth (list 0.23 0.38 0.45 0.6))
(setq theCorner (polar theCorner (* 0.5 pi) (car theDepth)))                ;translate the base point on vertical, positive

(setq CrIndex 0)                                                            ;start counting items
(setvar "OSMODE" 0)                                                         ;disable OSMODE to don't interfere with drawing process
(repeat (length theLength)                                                  ;repeat procesing for all items
(command "_RECTANGLE" theCorner                                            ;start rectangle from base point
                  ;calculate the coordinates of second point
                      (list (+ (car theCorner)  (nth CrIndex theLength))   ;displacement on horizontal from X of first point
                            (- (cadr theCorner) (nth CrIndex theDepth)))   ;displacement on vertical from Y of first point
         "_EXPLODE" (entlast))                                             ;explode to convert the PLINE to LINEs
;translate on horizontal the base point with length of current item
(setq theCorner (polar theCorner 0.0 (nth CrIndex theLength)))
(setq CrIndex (1+ CrIndex))                                                ;indexate the counter to draw next item
)
(setvar "OSMODE" OldOsmode)                                                 ;restore user's OSMODE

Regards,

Mircea

Link to comment
Share on other sites

hi Mircea

yeah u have lot of patience

i tried ur code which suit my requirement

finally i got what i want with ur guidance

 

thank u very much

Link to comment
Share on other sites

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...