Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
richie.hodgson
Posts: 223
Registered: ‎11-05-2008
Message 1 of 10 (185 Views)

Help with reading a file needed

185 Views, 9 Replies
06-07-2012 03:21 PM

Hi

 

I have a file list which contains code x y z ina a space deliminated format. there are six different codes in the file and I want to end up with a list for each code containing just the x y z coordinates so I can create a seperate polyline for each. I have tried a number of methods and not getting very far. The codes are bi,bp,sq,tr,bc,sh.

Richie
Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 2 of 10 (176 Views)

Re: Help with reading a file needed

06-07-2012 06:31 PM in reply to: richie.hodgson

Hi, 

 

Please post an example file.

 

Gaston Nunez

Mentor
richie.hodgson
Posts: 223
Registered: ‎11-05-2008
Message 3 of 10 (174 Views)

Re: Help with reading a file needed

06-07-2012 06:36 PM in reply to: richie.hodgson

example is as follows

 

sq 3.0 100 0

bp 3.1 80 0

sq 3.1 100 0

tr 3.1 40 0

bc 3.2 40 0

sh 3.47 60 0

bp 6.67 80 0

tr 6.67 40 0

sq 6.67 140 0

 

Not a complete list but enough to be going on with if you have the six diferent codes, the 0 at the end could be left off.

I would like to be able to controll the poliline perameters like line type and color so once the seperate lists are compiled I just set the layer,color,linetype then command a polyline for each of the six lists if they exist.

Richie
*Expert Elite*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 4 of 10 (161 Views)

Re: Help with reading a file needed

06-07-2012 08:46 PM in reply to: richie.hodgson

richie.hodgson wrote:

example is as follows

 

sq 3.0 100 0

bp 3.1 80 0

sq 3.1 100 0

tr 3.1 40 0

bc 3.2 40 0

sh 3.47 60 0

bp 6.67 80 0

tr 6.67 40 0

sq 6.67 140 0

 

Not a complete list but enough to be going on with if you have the six diferent codes, the 0 at the end could be left off.

I would like to be able to controll the poliline perameters like line type and color so once the seperate lists are compiled I just set the layer,color,linetype then command a polyline for each of the six lists if they exist.


(defun c:REbuild (/ _delFinder fl a b pl_ RebList)
(defun _delFinder  (str md / d l str)
      (while (setq d (vl-string-position md str nil T))
            (setq l (cons (substr str (+ 2 d)) l)
                  str (substr str 1 d)))
      (cons str l)
      )     
      (if (and (setq fl (GetFiled "Select point file" "" "txt" 4))
               (setq RebList nil fl  (open fl "r")))
          (progn
           (while (setq a (read-line fl))
        (setq pointlist (cons (_delFinderc a 32) pointlist))
                  )
                (close fl)                         
  (while (setq a (car pointlist))
                   (while (setq b (assoc (car a) pointlist ))
   (setq pl_ (cons b pl_) pointlist (vl-remove b pointlist)))
                (setq RebList (cons (list (caar pl_)(mapcar 'cdr pl_))
                                                   RebList) pl_ nil))
                (foreach p RebList (print p)(princ));<test purposed
                )
          )
      (princ)
      )

 

("bc" ((3.2 40 0)))
("sh" ((3.47 60 0)))
("bp" ((3.1 80 0) (6.67 80 0)))
("tr" ((3.1 40 0) (6.67 40 0)))
("sq" ((3.0 100 0) (3.1 100 0) (6.67 140 0)))


HTH

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 5 of 10 (157 Views)

Re: Help with reading a file needed

06-07-2012 08:51 PM in reply to: richie.hodgson

Hi,

 

I'm not sure what you want, but this lisp  take a file like the example and return a list of lists with the following format: ((code1 point1)(code2 point2)...)

 

(defun readF(MyFile / fp rl slist p pldata pldatalist)
  (setq fp (open Myfile "r"))
  (if fp
    (progn
      (while (setq rl (read-line fp))
	(setq slist (split rl 32))
	(setq p (list (read (nth 1 slist)) (read (nth 2 slist))))
	(setq pldata (list (car slist)  p))
	(setq pldataList (cons pldata pldataList))
      )
    )
    (alert "File not found!")
  )
 (reverse pldatalist)
)  


(defun split(str char / sl i n e word slist)
  (setq sl (vl-string->list str))
  (setq n (length sl))
  (setq i 0)
  (setq word "")
  (while (< i n)
    (setq e (nth i sl))
    (if	(/= e char)
      (setq word (strcat word (chr e)))
      (progn
	(setq slist (cons word slist))
	(setq word "")
      )
    )
    (setq i (+ i 1))
  )
  (reverse (cons word slist))
)

 Gaston Nunez

Mentor
richie.hodgson
Posts: 223
Registered: ‎11-05-2008
Message 6 of 10 (151 Views)

Re: Help with reading a file needed

06-07-2012 09:05 PM in reply to: richie.hodgson

Hi thanks for that, I get ; error: no function definition: _DELFINDERC, it looks like the outputs on your message are what I want, I assume I just need to write a car  and cadr statement to separate the code and points so I can use an if statement to write the points to a polyline with colour and linetype, please let me know if I am wrong I am still relatively new to this lisp thing.

Richie
*Expert Elite*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 7 of 10 (144 Views)

Re: Help with reading a file needed

06-07-2012 10:00 PM in reply to: richie.hodgson

richie.hodgson wrote:

Hi thanks for that, I get ; error: no function definition: _DELFINDERC, it looks like the outputs on your message are what I want, I assume I just need to write a car  and cadr statement to separate the code and points so I can use an if statement to write the points to a polyline with colour and linetype, please let me know if I am wrong I am still relatively new to this lisp thing.



oops my bad. i posted the wrong sub. replace  the code in the previous post with this one

This converts the values as it compiels the list

 

(defun c:REbuild (/ _delFinder fl a b pl_ RebList)
(defun _delFinderC  (str md / d l str)
(setq conv (lambda (k / num word)
                 (if (numberp (setq num  (read k)))
                                  num
                                  k)))
      (while (setq d (vl-string-position md str nil T))
            (setq l   (cons
                            (conv (substr str (+ 2 d)))
                            l)
                  str (substr str 1 d)))
      (cons (conv str) l)
      )     
      (if (and (setq fl (GetFiled "Select point file" "" "txt" 4))
               (setq RebList nil fl  (open fl "r")))
          (progn
           (while (setq a (read-line fl))
        (setq pointlist (cons (_delFinderc a 32) pointlist))
                  )
                (close fl)                         
  (while (setq a (car pointlist))
                   (while (setq b (assoc (car a) pointlist ))
   (setq pl_ (cons b pl_) pointlist (vl-remove b pointlist)))
                (setq RebList (cons (list (caar pl_)(mapcar 'cdr pl_))
                                                   RebList) pl_ nil))
                (foreach p RebList (print p)(princ));<test purposed
                )
          )
      (princ)
      )

 

Try it and tell me waht you think

*Expert Elite*
Kent1Cooper
Posts: 5,879
Registered: ‎09-13-2004
Message 8 of 10 (134 Views)

Re: Help with reading a file needed

06-08-2012 07:00 AM in reply to: richie.hodgson

richie.hodgson wrote:

example is as follows

 

sq 3.0 100 0

bp 3.1 80 0

sq 3.1 100 0

tr 3.1 40 0

bc 3.2 40 0

sh 3.47 60 0

bp 6.67 80 0

tr 6.67 40 0

sq 6.67 140 0

....


It's not necessary to break the lines up into pieces around the spaces -- point lists in AutoLISP are also space-delimited, so you can keep that aspect of the lines in the file.  If the "categories" are always two letters, try this:

 

(setq fil (open "Y:/our/filepath/and/file.txt" "r"))
(while
  (setq lin (read-line fil)); get next line in file
  (set (read (substr lin 1 2)); variable name for list of points, from first two characters
    (append
      (eval (read (substr lin 1 2))); existing list of points for that category, if present
      (list (read (strcat "(" (substr lin 3) ")"))); add point for current line
    ); append
  ); set
); while
(close fil)

 

On your example file content, that returns a list of point(s) for each category, stored in a variable named for that category, as follows:

 

Command: !sq
((3.0 100 0) (3.1 100 0) (6.67 140 0))

Command: !bp
((3.1 80 0) (6.67 80 0))

Command: !tr
((3.1 40 0) (6.67 40 0))

Command: !bc
((3.2 40 0))

Command: !sh
((3.47 60 0))

Kent Cooper
Mentor
richie.hodgson
Posts: 223
Registered: ‎11-05-2008
Message 9 of 10 (98 Views)

Re: Help with reading a file needed

06-10-2012 02:25 PM in reply to: richie.hodgson

Thanks all for the help I can hit the ground runing now

Richie
Active Contributor
rakesh_rao
Posts: 30
Registered: ‎01-14-2005
Message 10 of 10 (87 Views)

Re: Help with reading a file needed

06-10-2012 08:36 PM in reply to: richie.hodgson

 

Hi Richie,

 

You may also want to take a look at our FREE Lisp resourceson the TechCenter web-site.

 

Here is the URL:

 

http://www.4d-technologies.com/techcenter

 

Over there, you can find Lisp routines for a vast array of common CAD and geo-data processing functions.

 

In fact, our commercial software add-ons, GeoTools and CADPower are built using the FREE libraries you see in TechCenter.

 

Let me know if you need any further assistance.

 

Regards

Rakesh

 

http://rakeshrao.typepad.com

http://www.coordsys.com

http://www.4d-technologies.com

 

- rakesh.rao@4d-technologies.com
- www.4d-technologies.com / www.coordsys.com
- Blog:EN: http://rakeshrao.typepad.com
- Blog: ES: http://kiranabhat.typepad.com
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.