Jump to content

lisp for for sum text number before a specific alphabet character


anjan008

Recommended Posts

Could any body help me to create a lisp for for sum text number before a specific alphabet character

Example,

 

5*M16*35

2*M16*40

4*M16*45

 

4*M16*35

3*M16*40

6*M16*45

 

M16*35

M16*40

M16*45

 

 

result

 

M16*35 =10

M16*40=6

M16*45=11

 

First number indicates number of pieces,So like that texts have so many in a drawing.

I need a lisp to calculate each type how many have in that drawings and give me in csv file or in a table in the same drawing.

 

If there is no number in front of M16..it should take it as 1 piece

Please help me any body.

 

 

 

Thanks

Edited by anjan008
i want add extra information
Link to comment
Share on other sites

  • Replies 36
  • Created
  • Last Reply

Top Posters In This Topic

  • anjan008

    17

  • Snownut

    13

  • pBe

    4

  • BIGAL

    2

Top Posters In This Topic

Thanks for reply

No when number of pieces are 1 there will not be *

 

if that number are more than 1 only it will indicate as

2*M16*35

 

So when there is no * In front of M16*35 it should take it as that is 1

 

Thanks

Link to comment
Share on other sites

For the VERY restricted format that you proposed ( contained in an ascii text file ) tsv.txt :

 

[b][color=BLACK]([/color][/b]defun c:tsv [b][color=FUCHSIA]([/color][/b]/ file rf nl q v c fl[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq file [b][color=NAVY]([/color][/b]if dat_file
              [b][color=MAROON]([/color][/b]getfiled [color=#2f4f4f]"DATA File"[/color] dat_file [color=#2f4f4f]"txt"[/color] 2[b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b]getfiled [color=#2f4f4f]"DATA File"[/color] [color=#2f4f4f]"/acad/"[/color] [color=#2f4f4f]"txt"[/color] 2[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq dat_file file[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq rf [b][color=NAVY]([/color][/b]open [b][color=MAROON]([/color][/b]findfile file[b][color=MAROON])[/color][/b] [color=#2f4f4f]"r"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq nl [b][color=MAROON]([/color][/b]read-line rf[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= [color=#2f4f4f]""[/color] nl[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= [color=#2f4f4f]"M"[/color] [b][color=BLUE]([/color][/b]strcase [b][color=RED]([/color][/b]substr nl 1 1[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq q 1
                     v nl
                    fl [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]assoc v fl[b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons v [b][color=TEAL]([/color][/b]+ q [b][color=OLIVE]([/color][/b]cdr [b][color=GRAY]([/color][/b]assoc v fl[b][color=GRAY])[/color][/b][b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b]
                                  [b][color=PURPLE]([/color][/b]assoc v fl[b][color=PURPLE])[/color][/b] fl[b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]cons [b][color=PURPLE]([/color][/b]cons v q[b][color=PURPLE])[/color][/b] fl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]setq c 1[b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]while [b][color=BLUE]([/color][/b]/= [color=#2f4f4f]"*"[/color] [b][color=RED]([/color][/b]substr nl c 1[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                      [b][color=BLUE]([/color][/b]setq c [b][color=RED]([/color][/b]1+ c[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq q [b][color=BLUE]([/color][/b]atoi [b][color=RED]([/color][/b]substr nl 1 [b][color=PURPLE]([/color][/b]1- c[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                     v [b][color=BLUE]([/color][/b]substr nl [b][color=RED]([/color][/b]1+ c[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                    fl [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]assoc v fl[b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons v [b][color=TEAL]([/color][/b]+ q [b][color=OLIVE]([/color][/b]cdr [b][color=GRAY]([/color][/b]assoc v fl[b][color=GRAY])[/color][/b][b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b]
                                  [b][color=PURPLE]([/color][/b]assoc v fl[b][color=PURPLE])[/color][/b] fl[b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]cons [b][color=PURPLE]([/color][/b]cons v q[b][color=PURPLE])[/color][/b] fl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]close rf[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1 fl[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

-David

TSV.TXT

Link to comment
Share on other sites

How is the text in the drawing ie: block names, plain text, mtext you need to gather the relevant text into a list somehow.

 

Please be a lot more specific on just what you are starting with !

Link to comment
Share on other sites

It appears in the drawing that all bolt information is on the "25_Bolt designation" layer and there is no other information on that layer, will this hold true. You need a method to build the list and it seems the layer is a good place to start.

Link to comment
Share on other sites

Then try this, the heavy lifting from David's code above is applied to the list of bolts from the drawing.

 

 (defun c:tsv (/ file rf nl q v c fl)
   (defun Mk_Bolt_Lst ( / ent lth sel txt boltl)
      (setq sel (ssget "_X" (list (cons 0   "TEXT")
                   (cons 8    "25_Bolt designation")))
         )
   (repeat (setq lth (sslength sel))
     (setq ent (ssname sel (setq lth (1- lth)))
              txt (list(cdr(assoc 1 (entget ent))))
              boltL (append boltL txt)
       )
     )
   );defun
     
     (foreach nl (Mk_Bolt_Lst) ; David Bethal modified code
            (cond ((= "" nl))
                  ((= "M" (strcase (substr nl 1 1)))
                   (setq q 1
                         v nl
                        fl (if (assoc v fl)
                               (subst (cons v (+ q (cdr (assoc v fl))))
                                      (assoc v fl) fl)
                               (cons (cons v q) fl))))
                  ((setq c 1)
                   (while (/= "*" (substr nl c 1))
                          (setq c (1+ c)))
                   (setq q (atoi (substr nl 1 (1- c)))
                         v (substr nl (1+ c))
                        fl (if (assoc v fl)
                               (subst (cons v (+ q (cdr (assoc v fl))))
                                      (assoc v fl) fl)
                               (cons (cons v q) fl))))))
     (prin1 fl)
     (prin1)
   );defun

The above worked fine on the drawing you posted.

 

This is for Plain Text Only.

Link to comment
Share on other sites

Thanks for your effort.But

 

it is working only in that drawing, In another with same type not working and also when it is having MTEXT also not working.

 

thanks

22.dwg

Link to comment
Share on other sites

A couple of extra suggestions start with pick text or mtext return layer name for assoc 8 ssget this way its layer independant

 

Use the text,mtext filter to make list obviously need 2 defuns do a if so use correct one for make list.

 

Why not vlsort list do 1st pass if a-z or A-Z then add 1* to text this way one routine to add up text.

 

Re to table if you make a list then I posted code example about a week ago on how to make a table from a list. Search for table example. Will try to find.

Link to comment
Share on other sites

>

>

 

(defun c:addme (/  trunc data ss i e match var opt fr sy)
;;;	pBe04Mar2014		;;;

(defun trunc (s m [b]v w[/b])
 (atoi	(if m
  (substr s (+ 2 (vl-string-position [b]v[/b] s)))
  (substr s (+ 2 (vl-string-position [b] w[/b] s nil t)))
)
 )
)  
[b](initget "Pack Bolt")
(setq opt (getkword "\nChoose Option [Pack Washer/Bolt Information]"))
(setq var
      (if (eq opt "Bolt")
 '(77 42  "*M##`**")
 '(80 45  "*PW-*mm")
      )
fr (car var)
     sy (cadr var))[/b]
 
 (if  (setq data nil
    ss	 (ssget	"_X"
		[b](list '(0 . "TEXT")
		      (cons 1 (last var))
		  '(8 . "25_Bolt designation,7_Graphics")
		 )[/b]
	 )
     )
   (progn
     (repeat (setq i (sslength ss))
(setq e (ssname ss (setq i (1- i))))
(setq data (cons (cdr (assoc 1 (entget e))) data))
     )
(setq
  data (vl-sort	data
		'(lambda (b c)
		   (cond
		     ((< (setq d[b] (trunc b t fr sy )[/b]) [b](setq f (trunc c t fr sy)))[/b] t)
		     ((= d f) (< [b](trunc b nil fr sy)[/b] [b](trunc c nil fr sy)[/b]))
		   )
		 )
       )
)
     (While (setq a (car data))
(setq ref (substr a (1+ (vl-string-position[b] (car var)[/b] a))))
(setq match (vl-remove-if-not
	      '(lambda (s) (wcmatch s (strcat "*" ref)))
	      data
	    )
)
(princ (strcat "\n"
	       ref
	       " = "
	       (itoa (apply '+
			    (mapcar '(lambda (k)
				       (if (zerop (atoi k))
					 1
					 (atoi k)
				       )
				     )
				    match
			    )
		     )
	       )
       )
)
(setq data (vl-remove-if '(lambda (y) (member y match)) data))
     )
   )
 )
 (princ)
)
(vl-load-com)

Edited by pBe
CODE UPDATE/Bolt and Washers
Link to comment
Share on other sites

Thanks for suggestion, But i don't know how to do lisp program. Iam new to this.

 

Could you please help me.

 

One of our friend help me some extent.

 

Thanks

Link to comment
Share on other sites

The example make a table for anyone interested as you will need to add this to make a table http://www.cadtutor.net/forum/showthread.php?84472-Entmake-Table-with-AutoLISP&highlight=Table.

 

The code posted by Pbe creates a list called "Data" you can look at it by (nth 2 data) 3rd variable starts at 0 see code in example need to change name of list.

 

If your lucky Pbe will add a table to his code.

Link to comment
Share on other sites

its working but to improve some more better. is it possible to give result like in order

 

No need have this text in table format. like below result in command prompt is suitable for me

 

M16*35 = 1

M16*45 = 1

M16*50 = 16

M16*55 = 6

M16*60 = 4

M16*65 = 126

M16*75 = 8

M16*70 = 2

M16*80 = 2

M24*75 = 32

M24*95 = 36

M24*100 = 12

M24*110 = 44

 

Sorry if i ask this is not possiable

 

Thanks

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...