+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
  1. #1
    Junior Member
    Computer Details
    RocketBott's Computer Details
    Operating System:
    Windows XP
    Using
    AutoCAD 2008
    Join Date
    Mar 2009
    Location
    Aberdeen, Scotland
    Posts
    18

    Default Output only PART of text string to .txt file

    Registered forum members do not see this ad.

    At the moment I have a string of text to represent a wire TB[25] - K201/1.0mm/GY or K201/1.0mm/GY - TB[25] dependant on which side of equipment it's attached to. I need to extract the K201 (wire No.) part to a text file.
    All text in the string can change dependant on No.,Size,Colour,Destination. The green parts will be constant.
    I have a few lisp's that extract the text string but need to know if there is a way to filter out the wire No.
    Idealy the lisp would select all single line text in the dwg, filter out the wire No. then write to a txt file one line per No. in a specified folder with the same file name as the dwg.
    I have tried using blocks with attributes but they do not expand with different string lengths & dynamic block have too many problems.
    Sample dwg attached.
    Thanks
    Attached Files

  2. #2
    Senior Member
    Computer Details
    VVA's Computer Details
    Operating System:
    Windows 7
    CPU:
    Intel Core i5-2400
    RAM:
    8 Gb
    Graphics:
    Nvidia Quadro 600
    Primary Storage:
    Seagate 500 GB + WD 750 GB
    Monitor:
    Philips 27"
    Using
    AutoCAD 2013
    Join Date
    Dec 2006
    Location
    Minsk, Belarus
    Posts
    401

    Default

    Try this function. It will be return a list of part text string if you want (I hope).
    *** ADD Add command GPT
    Code:
    ;;Get Part Text
    (defun C:GPT ( / file ret count)
      (if (and (setq ret (get-part-text))
    	   (setq file (open (strcat "F:\\ENGINEER\\GENERIC\\Label Schedule\\Extracted Labels\\Ferrule_"
                (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname"))4)) ".txt") "a"))
    	   )
        (progn
          (mapcar '(lambda(x)(write-line x file)) ret)
          (close file)
          (princ "\nWritting ")
          (princ (length ret))
          (princ " text string to file")
          )
        )
      (princ)
      )
    (defun get-part-text ( / ss lst item lst1 lst2 ret)
    (vl-load-com)
      ;;;Usage (get-part-text)
      (defun str-str-lst (str pat / i)
      (cond ((= str "") nil)
            ((setq i (vl-string-search pat str))
             (cons (substr str 1 i)
                   (str-str-lst (substr str (+ (strlen pat) 1 i)) pat)
             ) ;_  cons
            )
            (t (list str))
      ) ;_  cond
    )
     (if  (setq ss (ssget "_X" (list(cons 0 "TEXT")(cons 410 (getvar "CTAB")))))
       (progn
         (repeat (setq item (sslength ss)) ;_ end setq 
             (setq lst (cons (cdr(assoc 1(entget(ssname ss (setq item (1- item)))))) lst)) 
             )
         (foreach txt lst
           (setq lst1 (str-str-lst txt " "))
           (foreach part-txt lst1
    	 (setq lst2 (str-str-lst part-txt "/"))
    	 (if (and (> (length lst2) 1)
    		  (wcmatch (nth 1 lst2) "*mm")
    		  )
    	   (setq ret (cons (nth 0 lst2) ret))
    	   )
    	 )
           )
         )
       )
      ret
      )
    Last edited by VVA; 27th Mar 2009 at 03:42 pm. Reason: Add command

  3. #3
    Junior Member
    Computer Details
    RocketBott's Computer Details
    Operating System:
    Windows XP
    Using
    AutoCAD 2008
    Join Date
    Mar 2009
    Location
    Aberdeen, Scotland
    Posts
    18

    Default

    That works great, thanks a lot.
    I'm not getting on very well writng that list to a file. I have created a txt file with
    Code:
          (setq file (open (strcat "F:\\ENGINEER\\GENERIC\\Label Schedule\\Extracted Labels\\Ferrule_"
                (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname"))4)) ".txt") "a"))
    But can't write the string to the file (write-line ret file) ?
    I also need each No. on a new line.
    Sorry but I'm very new to this and have a lot to learn.

    Thanks again

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,708

    Default

    Try

    Code:
    (mapcar '(lambda (x) (write-line x file)) ret)
    Also remember to use:

    Code:
    (close file)
    at the end, otherwise the file will remain read-only and the data won't be written.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  5. #5
    Junior Member
    Computer Details
    RocketBott's Computer Details
    Operating System:
    Windows XP
    Using
    AutoCAD 2008
    Join Date
    Mar 2009
    Location
    Aberdeen, Scotland
    Posts
    18

    Default

    Thanks Lee that's it.
    I have a long way to go in understand all these different functions.
    Thank you both again.

  6. #6
    Senior Member
    Computer Details
    VVA's Computer Details
    Operating System:
    Windows 7
    CPU:
    Intel Core i5-2400
    RAM:
    8 Gb
    Graphics:
    Nvidia Quadro 600
    Primary Storage:
    Seagate 500 GB + WD 750 GB
    Monitor:
    Philips 27"
    Using
    AutoCAD 2013
    Join Date
    Dec 2006
    Location
    Minsk, Belarus
    Posts
    401

    Default

    I have a long way to go in understand all these different functions
    Add command to #2

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,708

    Default

    Quote Originally Posted by RocketBott View Post
    Thanks Lee that's it.
    I have a long way to go in understand all these different functions.
    Thank you both again.
    Most people don't quite understand the mapcar and lambda functions too well - just think of it as doing something iteratively to each member of a list and outputting this as all the results in a list.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  8. #8
    Junior Member
    Computer Details
    RocketBott's Computer Details
    Operating System:
    Windows XP
    Using
    AutoCAD 2008
    Join Date
    Mar 2009
    Location
    Aberdeen, Scotland
    Posts
    18

    Default

    Nice one Vladimir I like the "Writing xxx text string to file" that will be a good check for the printer. I have sorted the list in alphabetical order with acad_strlsort and it's working great.
    Cheers Guys.

  9. #9
    Junior Member
    Computer Details
    RocketBott's Computer Details
    Operating System:
    Windows XP
    Using
    AutoCAD 2008
    Join Date
    Mar 2009
    Location
    Aberdeen, Scotland
    Posts
    18

    Default

    I have hit a problem.
    There are some drawings with Wire No,s with a / in them i.e.
    TB[25] - K201/S18/1.0mm/GY need the K201/S18 part.
    I have made some progress with a solution but have got stuck & I'm sure there will be a better way of solving this.
    Got the following which gives what I want on the screen but I can't get it to write to a file. This is only for the "double" Wire No's as above but would be need to combine this with the previous "single" Wire No Lisp.
    This one has the user select the text but I will need both options of selection (User & whole drawing) in seperate Lisp files.

    Code:
    (defun c:get-part-text ( / ss lst item lst1 lst2 ret alp)
    (vl-load-com)
      ;;;Usage (get-part-text)
      (defun str-str-lst (str pat / i)
      (cond ((= str "") nil)
            ((setq i (vl-string-search pat str))
             (cons (substr str 1 i)
                   (str-str-lst (substr str (+ (strlen pat) 1 i)) pat)
             ) ;_  cons
            )
            (t (list str))
      ) ;_  cond
    )
     (if  (setq ss (ssget '((-4 . "<OR")(0 . "TEXT")(-4 . "OR>"))))
       (progn
         (repeat (setq item (sslength ss)) ;_ end setq 
             (setq lst (cons (cdr(assoc 1(entget(ssname ss (setq item (1- item)))))) lst)) 
             )
         (foreach txt lst
           (setq lst1 (str-str-lst txt " "))
           (foreach part-txt lst1
      (setq lst2 (str-str-lst part-txt "/"))
      (if (and (> (length lst2) 1)
        (wcmatch (nth 2 lst2) "*mm")
        )
        (setq ret (cons (strcat (nth 0 lst2) "/" (nth 1 lst2)) ret))
        )
      )
           )
         )
       ) (setq alp (acad_strlsort ret))
      alp
      )

  10. #10
    Senior Member
    Computer Details
    VVA's Computer Details
    Operating System:
    Windows 7
    CPU:
    Intel Core i5-2400
    RAM:
    8 Gb
    Graphics:
    Nvidia Quadro 600
    Primary Storage:
    Seagate 500 GB + WD 750 GB
    Monitor:
    Philips 27"
    Using
    AutoCAD 2013
    Join Date
    Dec 2006
    Location
    Minsk, Belarus
    Posts
    401

    Default

    Registered forum members do not see this ad.

    try it
    Code:
    (defun c:get-part-text (/ ss lst item lst1 lst2 ret alp i tmp cnt)
      (vl-load-com)
    ;;;Usage (get-part-text)
      (defun str-str-lst (str pat / i)
        (cond ((= str "") nil)
    	  ((setq i (vl-string-search pat str))
    	   (cons (substr str 1 i)
    		 (str-str-lst (substr str (+ (strlen pat) 1 i)) pat)
    	   ) ;_  cons
    	  )
    	  (t (list str))
        ) ;_  cond
      )
      (if (setq ss (ssget '((-4 . "<OR") (0 . "TEXT") (-4 . "OR>"))))
        (progn
          (repeat (setq item (sslength ss)) ;_ end setq 
    	(setq lst
    	       (cons
    		 (cdr (assoc 1 (entget (ssname ss (setq item (1- item)))))
    		 )
    		 lst
    	       )
    	)
          )
          (foreach txt lst
    	(setq lst1 (str-str-lst txt " "))
    	(foreach part-txt lst1
    	  (setq lst2 (str-str-lst part-txt "/"))
    	  (if (and (> (length lst2) 1)
    		   (setq cnt
    			  (vl-member-if '(lambda (x) (wcmatch x "*mm")) lst2)
    		   )
    	      )
    	    (progn
    	      (setq cnt (- (length lst2) (length cnt)))
    	      (setq i	'-1
    		    tmp	nil
    	      )
    	      (while (< (setq i (1+ i)) cnt)
    		(setq tmp (cons (nth i lst2) tmp))
    	      )
    	      (setq tmp (reverse tmp))
    	      (setq
    		tmp (strcat
    		      (car tmp)
    		      (if (cdr tmp)
    			(apply '(lambda (x) (strcat "/" x)) (cdr tmp))
    			""
    		      )
    		    )
    	      )
    	      (setq ret (cons tmp ret))
    	    )
    	  )
    	)
          )
        )
      )
      (setq alp (acad_strlsort ret))
      alp
    )

Similar Threads

  1. Extract Part Of String ?
    By ZORANCRO in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 4th Nov 2008, 01:50 pm
  2. Weird output when rendering a part
    By 5mall5nail5 in forum SolidWorks
    Replies: 3
    Last Post: 6th Aug 2008, 01:44 pm
  3. Conver text string into file.txt
    By HelloWorld in forum AutoLISP, Visual LISP & DCL
    Replies: 19
    Last Post: 4th Feb 2008, 09:03 am
  4. Inventor 12 - Part file - cannot see text
    By Doug in forum Autodesk Inventor
    Replies: 4
    Last Post: 28th Sep 2007, 07:59 pm
  5. Creating a file with the output of BCOUNT
    By JimWebster in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 16th Mar 2005, 09:19 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts