Author Topic: Foreach works not so I will  (Read 1540 times)

0 Members and 1 Guest are viewing this topic.

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Foreach works not so I will
« on: October 15, 2012, 10:33:48 AM »
Hi all!

I have two lists and would like calculate within but Iīm not sure that itīs right foreach construction
The problem is if I change direction in Mid_list I get another result as I donīt change direction. That is wrong, It must be the same.

I see the first result calculates is right but not the second ...

But Itīs not false in calculation. It must to do something with foreach that works not correctly.

Can see anybody what I did wrong
Code: [Select]
(defun c:test ()
  (setq section_list '(("S" 152786.0 6.40866e+006 71.9021) ("S" 152774.0 6.40863e+006 72.1143)) )
  (setq Mid_list '((152786.0 6.40866e+006 70.0) (152774.0 6.40863e+006 73.0)) )
;;;  (setq Mid_list (reverse Mid_list))
  (setq obb 1 oby 1)
  (foreach X section_list
    (foreach Y Mid_list
          (if (minusp (- (cadddr X) (caddr Y)))
            (progn
              (setq C (- (cadddr X) (caddr Y)))
              (setq Tdis (+ obb (* 2 oby)))
              (setq Mdis (+ (* (* C 0.25) 2) Tdis))
              (setq area (* (/ (+ Tdis Mdis) 2) C))
              )
            (progn
              (setq F (- (cadddr X) (caddr Y)))
              (setq Tdis (+ obb (* 2 oby)))
              (setq Mdis (+ (* F 2) Tdis))
              (setq area (* (/ (+ Tdis Mdis) 2) F))
              )
            )
          )
        (princ area)
        )
  )


CADDOG

  • Newt
  • Posts: 82
  • wishbonesr
Re: Foreach works not so I will
« Reply #1 on: October 16, 2012, 11:01:05 AM »
It sounds as though you wish to calculate the area to result in a positive value no matter what???

I read this several times, and I'm not understanding what your looking to do; then intent of the code?; and if there is a problem.

Judging from the line of code that performs a 'reverse' on list Mid_list, I think this isn't a qualified test if you've done things right or not.
Perhaps you are trying to compensate for different coordinate systems??  If yes, then post back as there are ways to handle UCS to WCS.

Can you ellaborate more, adding some comments inline of your code for each step?

Cathy

  • Guest
Re: Foreach works not so I will
« Reply #2 on: October 16, 2012, 02:21:31 PM »
You are actually doing 4 calculations.  For the first item in the section list, you do a calculation with both of the items in the mid-list.  Then for the second item in the section list, you again do 2 calculations.

I'm guessing that what you are trying to do is only 2 calculations.  Matching the first items in each list, and then matching the second items in each list?  Is that right? 

I think you need to look at using mapcar instead of foreach. 

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Foreach works not so I will
« Reply #3 on: October 16, 2012, 02:45:20 PM »
No need for mapcar... Only change position of (princ area) to most inner foreach loop...

But still I don't know what should you get - foreach is correct (you have to figure out what do you want to accomplish with the code):

Code: [Select]
(defun c:test ()
  (setq section_list '(("S" 152786.0 6.40866e+006 71.9021) ("S" 152774.0 6.40863e+006 72.1143)) )
  (setq Mid_list '((152786.0 6.40866e+006 70.0) (152774.0 6.40863e+006 73.0)) )
;;;  (setq Mid_list (reverse Mid_list))
  (setq obb 1 oby 1)
  (foreach X section_list
    (foreach Y Mid_list
          (if (minusp (- (cadddr X) (caddr Y)))
            (progn
              (setq C (- (cadddr X) (caddr Y)))
              (setq Tdis (+ obb (* 2 oby)))
              (setq Mdis (+ (* (* C 0.25) 2) Tdis))
              (setq area (* (/ (+ Tdis Mdis) 2) C))
            )
            (progn
              (setq F (- (cadddr X) (caddr Y)))
              (setq Tdis (+ obb (* 2 oby)))
              (setq Mdis (+ (* F 2) Tdis))
              (setq area (* (/ (+ Tdis Mdis) 2) F))
            )
          )
          (princ area)
    )
  )
  (princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube