Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Polyline areas

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
sean.keohane
1280 Views, 25 Replies

Polyline areas

Hi,

I need to segregate closed polylines in my drawing by area range. Ideally, I would input the number of ranges I need.

Then somehow find the smallest and largest pline area to determine the range values (rounding up to a sensble number)

Then with the ranges determined the polylines would be segregated and moved to new layers.

I've been trying to hobble together pieces but with no joy.

Can someone set me going with the correct approach, I'd appreciate it.

Regards

Sean

25 REPLIES 25
Message 21 of 26
pbejse
in reply to: Kent1Cooper


@Kent1Cooper wrote:

 But the same can be accomplished by having a rounding function that only rounds up.

 

But it should not round up if the starting value is an exact multiple.  If the largest Polyline is 500 square units, with a divided value of 100, that should not be increased to 105, because the un-increased size of 100 will make a top category from 400 to 500, which will include the size of that largest Polyline.


Nice explanation Kent.

 

perhaps

 

(defun roundup (num near / v)
  (if (<= (setq v (* (fix (/ (+ num (/ near 2.0)) near)) near))
	 num
      )
    (+ near v)
    v
  )
)

 

On the last code i posted . i  tested the n value multiplied by number of range if it encompass the max value , if not then add the extra range to n. The adding of the extra range will be done outside the rounding routine. Not sure how that approach fairs with straigth round-up only routine [posted snippet]

 

 

Message 22 of 26
Kent1Cooper
in reply to: pbejse


@pbejse wrote:
...

 

perhaps

 

(defun roundup (num near / v)
  (if (<= (setq v (* (fix (/ (+ num (/ near 2.0)) near)) near))
	 num
      )
    (+ near v)
    v
  )
)

 

On the last code i posted . i  tested the n value multiplied by number of range if it encompass the max value , if not then add the extra range to n. The adding of the extra range will be done outside the rounding routine. Not sure how that approach fairs with straigth round-up only routine [posted snippet]


That's probably a little more work than needed, mostly because the whole adding-half-the-nearest-multiple-number thing is there specifically to make it round either up or down.  Think of it in terms of rounding to the nearest whole number ['near' = 1], where a number with a .5 or greater decimal part should be rounded up, but with less than that should be rounded down.  It adds .5 [half of 1] to the number, and uses (fix) to strip off any decimal portion from the result.  So if the decimal component is .5 or more, adding .5 to it sends it up to or above the next whole number, and that's where (fix) leaves it.  But if the decimal component is less than .5, adding .5 leaves it still below that next whole number, so (fix) takes it downward.

 

When you want to round up only, there's no purpose in messing around with half the value of the nearest-multiple number that way.

 

Here's the way it's done [split into more lines here] in my latest attached routine, after calculating 'areainc' as the unrounded quotient from dividing the largest area by the number of categories:

 

(setq areainc ; replacing previously calculated value

  (if (= (rem areainc mult) 0); if it's an exact multiple

    (fix areainc); then -- leave it alone

    (* (1+ (fix (/ areainc mult))) mult); else -- round it up to the next-

      ; higher multiple; that is, round the number of times 'mult' goes

      ; into it down, then add one, and multiply that by 'mult'

  ); if

); setq

 

That's based on the use of integers for the nearest-multiple-of number [the 'mult' variable; in our examples, 5], and the resulting category sizes.  But the same approach could be used with non-integer values.  For instance, if you want to round something to the nearest multiple of 2.5 but upward only, you would use 2.5 for 'mult' and omit the (fix) wrapper in the 'then' argument:

 

(setq areainc

  (if (= (rem areainc mult) 0); if it's an exact multiple

    areainc; then -- leave it alone [without (fix)]

    (* (1+ (fix (/ areainc mult))) mult); else -- round it up to the next-higher multiple

  ); if

); setq

 

So I think a simpler generic round-up-only-and-only-if-not-already-an-exact-multiple function would be:

 

(defun roundup (num near)
  (if (= (rem num near) 0)
    num ; then [use (fix num) if dealing in integers only]
    (* (1+ (fix (/ num near))) near); else
  ); if
); defun

Kent Cooper, AIA
Message 23 of 26
pbejse
in reply to: Kent1Cooper


@Kent1Cooper wrote: 

When you want to round up only, there's no purpose in messing around with half the value of the nearest-multiple number that way.

 

So I think a simpler generic round-up-only-and-only-if-not-already-an-exact-multiple function would be:

 

(defun roundup (num near)
  (if (= (rem num near) 0)
    num ; then [use (fix num) if dealing in integers only]
    (* (1+ (fix (/ num near))) near); else
  ); if
); defun


 

By george I think i got it.  It took me a while to understand the reasoning behind the round-up, only after reading again your last post that it clicks.

 

Thank you for the lesson Kent. Math was never my greatest strength 🙂

 

Cheers

 

Message 24 of 26
sean.keohane
in reply to: sean.keohane

Thank you  Pbejse and Kent. I haven't used your code yet but I had to thank you both for your help. I need to copy this down now and study it to come close to understanding it 'cause I sure don't understand it at first glance.( I've got the tortoise approach to lisp!)

Regards

Sean

Message 25 of 26
Kent1Cooper
in reply to: sean.keohane


@sean.keohane wrote:

Thank you  Pbejse and Kent. I haven't used your code yet but I had to thank you both for your help. I need to copy this down now and study it to come close to understanding it 'cause I sure don't understand it at first glance.( I've got the tortoise approach to lisp!)

....


You're welcome -- use it in good health.  I threw in some further EXPLanatory notes in the attached version, to help you understand what it's doing.

Kent Cooper, AIA
Message 26 of 26
sean.keohane
in reply to: pbejse

Hi Kent,

That is seriously cool to do that for me! You are a gentleman.

Best Regards

Sean

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost