Jump to content

Recommended Posts

Posted (edited)

Hi Guys.

 

I'm pretty new in this forum, and a total newbie in LISP :) BUt i really hope u guys can help me out.

 

My problem is, that I need to create a lisp, that can export all text from modelspace into excel, and there should also be a "counter" function, that tells me how many times a current word is repeated. Just like data excratction.

 

I have found this code somewhere, and it is actually very good. The only thing I need to implement into this code, is a function that asks me where to save the text file?

 

Can anybody please help me :)

 

I'm not pretty sure, who the author is of this code.

 


(defun c:tsel (/ ss lst tss olst ofile)


(vl-load-com)


(if (setq ss (ssget '((0 . "*TEXT"))))


(progn


(setq lst


(mapcar


(function


(lambda (x)


(vla-get-TextString


(vlax-ename->vla-object x))))


(vl-remove-if 'listp


(mapcar 'cadr (ssnamex ss)))))


(foreach str (unique lst)


(if (setq tss


(ssget "_X"


(list '(0 . "*TEXT") (cons 1 str))))


(setq olst


(cons


(cons str (sslength tss)) olst))


(setq olst


(cons


(cons str 0.) olst))))


(setq ofile


(open


(strcat


(getvar "DWGPREFIX")


(substr


(getvar "DWGNAME") 1


(- (strlen


(getvar "DWGNAME")) 4)) "-StrCnt.txt") "a"))


(mapcar


(function


(lambda (x)


(write-line


(strcat (car x) "\t"


(vl-princ-to-string (cdr x))) ofile))) olst)


(close ofile)


(princ "\n<< Strings Written to File >>"))


(princ "\n<< Nothing Selected >>"))


(princ))





;; CAB


(defun unique (lst / result)


(reverse


(while (setq itm (car lst))


(setq lst (vl-remove itm lst)


result (cons itm result)))))

Edited by maash
Posted

Welcome to CADTutor.

 

Firstly, please read this thread regarding code formatting, and edit your post.

 

Regarding your task, try this simple code:

[color=GREEN];; Text Extraction  -  Lee Mac[/color]
[color=GREEN];; Extracts all Text & MText in Modelspace to a Text file,[/color]
[color=GREEN];; with the number of occurrences of each string.[/color]

([color=BLUE]defun[/color] c:txtext ( [color=BLUE]/[/color] a d f i l s x )
   ([color=BLUE]cond[/color]
       (   ([color=BLUE]not[/color] ([color=BLUE]setq[/color] s ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"*TEXT"[/color]) (410 . [color=MAROON]"Model"[/color])))))
           ([color=BLUE]princ[/color] [color=MAROON]"\nNo Text or MText found in Modelspace."[/color])
       )
       (   ([color=BLUE]not[/color] ([color=BLUE]setq[/color] f ([color=BLUE]getfiled[/color] [color=MAROON]""[/color] [color=MAROON]""[/color] [color=MAROON]"txt"[/color] 1)))
           ([color=BLUE]princ[/color] [color=MAROON]"\n*Cancel*"[/color])
       )
       (   ([color=BLUE]setq[/color] d ([color=BLUE]open[/color] f [color=MAROON]"w"[/color]))
           ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] s))
               ([color=BLUE]setq[/color] x ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] s ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i)))))))
               ([color=BLUE]if[/color] ([color=BLUE]setq[/color] a ([color=BLUE]assoc[/color] x l))
                   ([color=BLUE]setq[/color] l ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] x ([color=BLUE]1+[/color] ([color=BLUE]cdr[/color] a))) a l))
                   ([color=BLUE]setq[/color] l ([color=BLUE]cons[/color]  ([color=BLUE]cons[/color] x 1) l))
               )
           )
           ([color=BLUE]foreach[/color] x ([color=BLUE]vl-sort[/color] l '([color=BLUE]lambda[/color] ( a b ) ([color=BLUE]<[/color] ([color=BLUE]car[/color] a) ([color=BLUE]car[/color] b))))
               ([color=BLUE]write-line[/color] ([color=BLUE]strcat[/color] ([color=BLUE]car[/color] x) [color=MAROON]"\t"[/color] ([color=BLUE]itoa[/color] ([color=BLUE]cdr[/color] x))) d)
           )
           ([color=BLUE]close[/color] d)
       )
       (   ([color=BLUE]princ[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nUnable to write to "[/color] f))   )
   )
   ([color=BLUE]princ[/color])
)
([color=BLUE]princ[/color])

Posted

Hi Lee Mac.

 

Thank you very much. I have heard about your excellent skills in other forums as well :) I'm very thankfull.

 

I have a last question: Is it possible to when the file is loaded, to get a window to pop up with instructions? And can you tell me where, I can read and learn some more about LISP?

 

Have a pleasent day.

Posted

newer mind :) I think i found out:

 


(alert 


"here i can write the text"


)

Posted
Thank you very much. I have heard about your excellent skills in other forums as well :) I'm very thankfull.

 

You're very welcome, I'm truly flattered that my reputation precedes me :)

 

I have a last question: Is it possible to when the file is loaded, to get a window to pop up with instructions? And can you tell me where, I can read and learn some more about LISP?

 

As you have subsequently discovered, the alert function is the simplest means by which to display a simple dialog message.

 

Concerning AutoLISP learning resources, there are a multitude of sites offering tutorials and code examples, including my own. In my experience, some of the best learning material can be found in the Visual LISP IDE Help Documentation, which is provided with AutoCAD. And, should you get stuck, there are several CAD programming forums where members are only too happy to lend a helping hand.

 

Here is a small selection of sites for you to peruse:

 

http://www.afralisp.net/index.php

http://www.lee-mac.com/

http://www.jefferypsanders.com/

http://ronleigh.com/autolisp/

 

Have a pleasent day.

 

Thank you, and to you.

Posted
How to create the text file oin the same folder with the same name?

 

As a quick change, replace:

 

([color=BLUE]not[/color] ([color=BLUE]setq[/color] f ([color=BLUE]getfiled[/color] [color=MAROON]""[/color] [color=MAROON]""[/color] [color=MAROON]"txt"[/color] 1)))

 

With:

 

(not (setq f (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt")))

 

The condition is actually no longer required since the above will always return nil, but the above modification reduces the number of changes you need to make to the existing code.

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