Jump to content

Learn from my mistakes....


WHM

Recommended Posts

Good day everyone!

 

Have you ever been completely dumbfounded by your own ingenuity and stupidity at the same time?

 

Mine involves data types.... I am very well aware that autolisp shortens floating point data to the 4th or 5th decimal (for display purposes), but still retains the full decimal number.

 

So I've been trying to write a program that gets the geojson data from a server via an API call and then goes and draws the data in cad for you (which is now finished and working). After I translate the data to autolisp I use the read function to convert the string into lists.... this is where all the magic happened.

 

While looking at the results after using the read function, I noticed that my Longitude and Latitude was shortened to 4 decimals. So I thought to myself I had to add \" to all the coordinates to force the read function to see it as a string, so while the data is still in string I had to add the \".

 

I don't know if it is just me or if anyone else experienced this, but manipulating string data (that is actually in list shape already) is pretty difficult. So I converted the string into an ASCII list which I could then use to look for a pattern to find the coordinates and add the " to them. Program below..

 

I was very happy with the result and I decided to compare it with the just the read function and to my horror, the results were exactly the same and my method was far slower.

 

So to any of you wondering - read will store your floating number! Don't be stupid like me!

;Example data
;(_t2 "type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006))")

(defun _t2 (datastr / datalist numlist pos prevpos gateop check prevchar gate firstchar gatecl)
    (defun _insertatpos ( ins pos lst / ctr )
        (setq ctr -1)
        (apply 'append (mapcar '(lambda ( x ) (if (= pos (setq ctr (1+ ctr))) (list ins x) (list x))) lst))
    )
    (setq datalist (vl-string->list datastr)
          numlist (list 45 46 48 49 50 51 52 53 54 55 56 57)
          gateop (list 40)
          gatecl (list 41)
          pos 0)
    (foreach x datalist
        (if (member x gateop)
            (setq gate t)
            (if (and (member x gatecl) gate) 
                (progn
                    (if check 
                        (setq datalist (_insertatpos 34 pos datalist)
                              pos (1+ pos)))
                    (setq gate nil 
                          firstchar nil 
                          check nil)
                )
            )
        )  
        (if gate
            (progn
                (if (and (member x numlist) (member prevchar gateop))(setq firstchar t))
                (if (and (member x numlist) firstchar)
                    (progn
                        (if (not (= prevpos (- pos 1)))
                            (progn
                                (setq datalist (_insertatpos 34 pos datalist)
                                      pos (1+ pos)
                                      prevpos pos
                                      check t)
                            )
                            (setq prevpos pos)
                        )
                    )
                    (if check 
                        (progn
                            (setq datalist (_insertatpos 34 pos datalist)
                                  pos (1+ pos)
                                  check nil)
                        )
                    )
                )
            )
        )
        (setq prevchar x
            pos (1+ pos))
    )
(vl-list->string datalist)
)

 

  • Funny 1
Link to comment
Share on other sites

Are all you wanting to do is output your cords to string?

 

;(setq lst '((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006)))

(defun _foo (lst / strlst str)
  (foreach itm lst
    (setq strlst (cons (rtos (car itm) 2 9) strlst))
    (setq strlst (cons (rtos (cadr itm) 2 9) strlst))
  )
  (setq strlst (reverse strlst))
  (setq str "type LineString coordinates (")
  (while (> (length strlst) 2)
    (setq str (strcat str "(" (car strlst) " " (cadr strlst) ") "))
    (setq strlst (cddr strlst))
  )
  (setq str (strcat str "(" (car strlst) " " (cadr strlst) "))"))
  (princ)
)

 

 

(_foo lst) outputs

"type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.084074900 -33.316215820) (26.084660183 -33.316272006))"

  • Like 1
Link to comment
Share on other sites

I wanted to make sure that the Latitude and Longitude will accurately translated to X and Y points for AutoCAD. When evaluating the variables after using the read function it only shows to the 4th decimal, but in reality it does store the correct information. I just spent all this time for nothing trying to reinvent the wheel! This post is nothing more than a warning or a reminder to other people doing the same

21 minutes ago, mhupp said:

Are all you wanting to do is output your cords to string?

 

;(setq lst '((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006)))

(defun _foo (lst / strlst str)
  (foreach itm lst
    (setq strlst (cons (rtos (car itm) 2 9) strlst))
    (setq strlst (cons (rtos (cadr itm) 2 9) strlst))
  )
  (setq strlst (reverse strlst))
  (setq str "type LineString coordinates (")
  (while (> (length strlst) 2)
    (setq str (strcat str "(" (car strlst) " " (cadr strlst) ") "))
    (setq strlst (cddr strlst))
  )
  (setq str (strcat str "(" (car strlst) " " (cadr strlst) "))"))
  (princ)
)

 

 

(_foo lst) outputs

"type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.084074900 -33.316215820) (26.084660183 -33.316272006))"

 

  • Like 2
Link to comment
Share on other sites

9 hours ago, WHM said:

Have you ever been completely dumbfounded by your own ingenuity and stupidity at the same time?

🤣 Many times!

  • Like 1
Link to comment
Share on other sites

The amount of times solved a big problem at 20 minutes, then a rewrite now 6 minutes, then a dumfounded why did I not do this, now 50 seconds. We are talking processing thousands of objects.

  • Like 1
Link to comment
Share on other sites

Ingenuity and Stupidity are flipsides of a single coin...AKA life.  :beard:   :beer:

The more you know, the more you realize you don't know.

Perfection is an unattainable aspiration.

A commendable goal?  Hell yes!

Live and learn.

Enjoy the ride.

  • Like 2
  • Agree 1
Link to comment
Share on other sites

I guess my most recent brain fart was trying to make a lisp that would extend an both sides of the arc to the closest quadrant point. was about 80-100 lines of figuring out were the start and end angles where and if they needed to be extended or not. Scrapped it all to 6 lines of code that pull the center point and radius then delete the arc and create a circle in its place.

  • Like 1
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...