Author Topic: rtosta & statof function  (Read 3666 times)

0 Members and 1 Guest are viewing this topic.

ymg

  • Guest
rtosta & statof function
« on: September 15, 2013, 08:35:40 PM »
Guys,

Having to play with station numbers on alignments, I could not find anything to my liking in the forum or on the internet.

So I wrote the two following functions.  However would like your help on how to handle improper formats.

What would be the best way to throw an error?

Any better way to do it  ?  Maybe a Challenge ?

ymg

Code - Auto/Visual Lisp: [Select]
  1. ;; rtosta                     by ymg  September 2013                          ;
  2. ;;                                                                            ;
  3. ;; Arguments:   sta Real number to format as a Station                        ;
  4. ;;             unit 1 for Imperials,                                          ;
  5. ;;                  2 for Metrics.                                            ;
  6. ;;             prec Integer for number of decimals                            ;
  7. ;;                                                                            ;
  8. ;; Examples: (rtosta 0 1 0)  -> "0+00"   (rtosta 1328.325 1 2) -> "13+28.33"  ;
  9. ;;           (rtosta 0 2 0)  -> "0+000"  (rtosta 1328.325 2 2) -> "1+328.33"  ;
  10. ;;                                                                            ;
  11. ;; If sta is negative, format is as follow:                                   ;
  12. ;;                                       (rtosta -1328.325 1 2) -> "13-28.33" ;
  13. ;;                                       (rtosta -1328.325 2 2) -> "1-328.33" ;
  14. ;;                                                                            ;
  15.  
  16. (defun rtosta (sta unit prec / str a b )
  17.     (setq str (rtos (abs sta) 2 prec))
  18.     (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
  19.         (setq str (strcat "0" str))
  20.     )
  21.     (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
  22.           b (substr str 1 (- a 1))
  23.           a (substr str a)
  24.     )
  25.     (strcat b (if (minusp sta) "-" "+") a)
  26. )
  27.  
  28. ;; statof                     by ymg  September 2013                          ;
  29. ;;                                                                            ;
  30. ;; Argument: String in format of a Station.                                   ;
  31. ;;                                                                            ;
  32. ;; Examples: (statof "0+00") -> 0.0      (statof "13+28.33") -> 1328.33       ;
  33. ;;                                       (statof "1+328.33") -> 1328.33       ;
  34. ;;                                                                            ;
  35. ;; If sta is negative, format is as follow:                                   ;
  36. ;;                                       (statof "13-28.33") -> -1328.33      ;
  37. ;;                                       (statof "1-328.33") -> -1328.33      ;
  38. ;;                                                                            ;
  39.  
  40. (defun statof (sta / p )
  41.    (cond
  42.       ((setq p (vl-string-position (ascii "+") sta))(atof (strcat (substr sta 1 p) (substr sta (+ p 2)))))  
  43.       ((setq p (vl-string-position (ascii "-") sta))(* (atof (strcat (substr sta 1 p) (substr sta (+ p 2)))) -1))
  44.    )   
  45. )
  46.  

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: rtosta & statof function
« Reply #1 on: September 16, 2013, 12:46:34 AM »
I've always thought this would be :

(rtosta -1328.325 1 2) -> "-13-28.33"

instead of :

(rtosta -1328.325 1 2) -> "13-28.33"
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ymg

  • Guest
Re: rtosta & statof function
« Reply #2 on: September 16, 2013, 11:22:08 AM »
Quote
I've always thought this would be :

(rtosta -1328.325 1 2) -> "-13-28.33"

Marko,

I certainly am not the Authority when it comes to Station Marking Conventions.  However from my days as a surveyor,  this is how we would use it.

Stationing is always from an origin, and  usually runs either from  West to East or South to North depending on the road direction.

The very rare case where we would use negative marking was when you needed to extend an alignment South or West of the origin and didn't want to affect the Original Stationing.

ymg
« Last Edit: September 16, 2013, 11:36:10 AM by ymg »

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: rtosta & statof function
« Reply #3 on: September 16, 2013, 11:28:48 AM »
I have seen negative stationing done as 13-28.33 and -13+28.33, but was told long ago to never use the double negative version.

ymg

  • Guest
Re: rtosta & statof function
« Reply #4 on: September 16, 2013, 11:47:12 AM »


Quote
Posted by: Jeff_M « on: Today at 10:28:48 am »
 
I have seen negative stationing done as 13-28.33 and -13+28.33

Jeff_M,

How does C3D handles the negative situation ?

Any idea on how to throw error for bad format ?

Thanks,

ymg

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: rtosta & statof function
« Reply #5 on: September 16, 2013, 12:52:55 PM »
C3D normally uses the "-3+28.33" format, which is good for me because this is what I prefer. However, you can force the styles to use just about any method you like with a bit of work.

No, not sure of a good way to check for bad format...not something I've considered.

ymg

  • Guest
Re: rtosta & statof function
« Reply #6 on: September 16, 2013, 01:49:08 PM »
Quote
Posted by: Jeff_M  « on: Today at 11:52:55 am »
C3D normally uses the "-3+28.33" format, which is good for me because this is what I prefer.

Jeff_M,

Thanks, for the reply.  I think I will stick to simply changing the "+" for a "-" indicating that the direction is reversed.

Then again, it would be a simple matter to modify the function to accommodates C3D's way.

ymg

ymg

  • Guest
Re: rtosta & statof function
« Reply #7 on: September 16, 2013, 03:45:19 PM »
Coming back from a stroll on Lee's page Consistent rtos modified rtosta to make it consistent.  :-o

ymg

Code - Auto/Visual Lisp: [Select]
  1. ;; rtosta                     by ymg  September 2013                          ;
  2. ;;                                                                            ;
  3. ;; Arguments:   sta Real number to format as a Station                        ;
  4. ;;             unit 1 for Imperials,                                          ;
  5. ;;                  2 for Metrics.                                            ;
  6. ;;             prec Integer for number of decimals                            ;
  7. ;;                                                                            ;
  8. ;; Examples: (rtosta 0 1 0)  -> "0+00"   (rtosta 1328.325 1 2) -> "13+28.33"  ;
  9. ;;           (rtosta 0 2 0)  -> "0+000"  (rtosta 1328.325 2 2) -> "1+328.33"  ;
  10. ;;                                                                            ;
  11. ;; If sta is negative, format is as follow:                                   ;
  12. ;;                                       (rtosta -1328.325 1 2) -> "13-28.33" ;
  13. ;;                                       (rtosta -1328.325 2 2) -> "1-328.33" ;
  14. ;;                                                                            ;
  15.  
  16. (defun rtosta (sta unit prec / str a b dz)
  17.      
  18.     (setq dz (getvar 'dimzin))
  19.     (setvar 'dimzin 0)
  20.     (setq str (rtos (abs sta) 2 prec))
  21.     (setvar 'dimzin dz)
  22.     (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
  23.         (setq str (strcat "0" str))
  24.     )
  25.     (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
  26.           b (substr str 1 (- a 1))
  27.           a (substr str a)
  28.     )
  29.     (strcat b (if (minusp sta) "-" "+") a)
  30. )
  31.  
  32. ;; statof                     by ymg  September 2013                          ;
  33. ;;                                                                            ;
  34. ;; Argument: String in format of a Station.                                   ;
  35. ;;                                                                            ;
  36. ;; Examples: (statof "0+00") -> 0.0      (statof "13+28.33") -> 1328.33       ;
  37. ;;                                       (statof "1+328.33") -> 1328.33       ;
  38. ;;                                                                            ;
  39. ;; If sta is negative, format is as follow:                                   ;
  40. ;;                                       (statof "13-28.33") -> -1328.33      ;
  41. ;;                                       (statof "1-328.33") -> -1328.33      ;
  42. ;;                                                                            ;
  43.  
  44. (defun statof (sta / p )
  45.    (cond
  46.       ((setq p (vl-string-position (ascii "+") sta))(atof (strcat (substr sta 1 p) (substr sta (+ p 2)))))  
  47.       ((setq p (vl-string-position (ascii "-") sta))(* (atof (strcat (substr sta 1 p) (substr sta (+ p 2)))) -1))
  48.    )   
  49. )
  50.