Cant get lisp to work properly

Cant get lisp to work properly

DGRL
Advisor Advisor
954 Views
7 Replies
Message 1 of 8

Cant get lisp to work properly

DGRL
Advisor
Advisor

Dear forum members,

 

I made an lisp that needs to open a few dwg's from a list and then do some things

Credits for owners of small lisps I used from them are not for me. They earn the credits for there work

On my system I cant get it to work so I wonder if I made an mistake.

 

Can someone please help me to get this to work?

What I need to do is following

 

1.  make an list of all dwg files in DIR1 including subfolders

2. process that list to read out specific ATT values

3. write that to an txt file

 

(defun c:dwglijst ()


(setvar "sdi" 1)

 


(defun GrabAllFiles (Path FileExt / tempPath FileList)


; [Path] is the path to search it, and all it's sub-directory
; [FileExt] is the extenstion (with the period) of the type of files to look for.
; Example (GrabAllFiles "c:\\" ".dwg")

 

 

 (setq Path
  (if (= (substr Path (strlen Path)) "\\")
   Path
   (strcat Path "\\")
  )
 )
 (foreach file (vl-directory-files Path)
  (setq tempPath (strcat Path file))
  (if
   (not
    (or
     (= file ".")
     (= file "..")
    )
   )
   (if (vl-file-directory-p tempPath)
    (setq FileList (append FileList (GrabAllFiles tempPath FileExt)))
    (if (= (strcase FileExt) (strcase (vl-filename-extension tempPath)))
     (setq FileList (cons tempPath FileList))
    )
   )
  )
 )
 FileList
)

 

 

(setq a (GrabAllFiles "P:\\Acad\\P3D\\project templates\\Empty Metric Project\\Orthos\\DWGs" ".dwg"))

 

 

(foreach dwg a
(command "fileopen" dwg)
(defun LM:getattributevalue ( blk tag / val enx )
    (while
        (and
            (null val)
            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
        )
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (setq val (cdr (assoc 1 enx)))
        )
    )
)

(setq blk (ssname (ssget "x" (list (cons 0 "INSERT") (cons 2 "Title Block")))0))
(setq strname (LM:GetAttributeValue blk "DWGNAME" ))
(setq iso_str (LM:GetAttributeValue blk "REV" ))

 

 

;write to file

(setq fn "D:\\output.txt"
      fp (open fn "a")
)
(princ strname fp)
(princ "," fp)
(princ iso_str fp)
(princ "\n" fp)
(close fp)
(setq strname nil)
(setq iso_str nil)
;(setvar "sdi" 0)
;(command "close")
);end foreach
(setvar "sdi" 0)
(command "close" "n")
)

 

 

Best regards

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
955 Views
7 Replies
Replies (7)
Message 2 of 8

DannyNL
Advisor
Advisor

As soon as another drawing is opened, the LISP will stop running. LISP code is only available in the drawing where it is loaded. Close the current drawing (like with FILEOPEN) en the routine is gone.

This could be possible with scripting and/or LISP routines that use DBX, but DBX is another level and can only be used with Visual LISP en not AutoLISP.

 

But as far as I can tell you do not need any LISP at all as what you want is already possible with Data Extraction (Insert Ribbon -> Extract Data). As output you have several option incl. CSV, XLS and TXT if I'm not mistaken.

0 Likes
Message 3 of 8

cadffm
Consultant
Consultant

DannyNL

There is a example for running AutoCAd in SingleDocumentMode(SDI=1),  it is not important whether one uses commandline-input by hand,  from a Script.scr or for example a Lisp (as here).

Thats the different between SDI and MDI(SDI=0), there is one Namespace that switched frome one to the next file, all symbols and function remain.

 

@DGRL

Acad don´t knows a command "fileopen", only a Lispfunction (fileopen ...), but you send AutoCAD-command to the commandline thru  (command ..).

The AutoCAD command to open a DWG is: OPEN

 

 

(command "fileopen" dwg) => (command "open" dwg)

 

 

The second Problem could be: In SDI-Mode, if you have unsaved changes in the current file and want to open another file AutoCAD  asking for save changes yes/no.

(that is what you want with open another file, Acad need to close the active file when SDI=1)

 

So (command "open" dwg) can "crash" again in some situations, better is this:

(foreach dwg a
(if (zerop(getvar "DBMOD")) ; no unsaved changes in current drawing?
    (command "_.open" dwg)   
    (command "_.open" "_yes" dwg)
)

<your code>

)

 

 

last comment:

You Should delete your old "output.txt" on start if you want only new values inside.

(and (findfile "D:\\output.txt") (vl-file-delete "D:\\output.txt")) ; delete output.txt if possible, place it as first /  in front of the foreach loop.

Sebastian

Message 4 of 8

devitg
Advisor
Advisor

MAybe it can be do with ODBX . please send me or upload , at least 3 dwg , with such ATT. 

 

devitg@gmail.com

0 Likes
Message 5 of 8

DGRL
Advisor
Advisor

Hi @cadffm @devitg @DannyNL

 

Thanks for all the answers

 

@cadffm

 

I got it to work properly

Only thing I changed was adding indeed sdi=1 at the start and sdi=0 at the end

And instead of ending with (command "close" "n") I end now with (command "new" "y" "")

The script works good enough ( Not perfect but good enough )

At least I am getting the results I need

 

Best regards,

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 6 of 8

DannyNL
Advisor
Advisor

Sebastian,

 

Thanks for correcting me; I'd missed the SDI part in the code.

 

However, based on the description I still think this can easily be done with Extract Data and there is no need for a custom routine unless there is a need for other actions as well.

 

@cadffm; good to see you got it to work!

But please also check the Extract Data as it is capable of extracting a lot more info from drawings than you requested and it wouldn't require you the change any LISP code if you would want to process other drawings or change the amount of information that you need from the drawing.

0 Likes
Message 7 of 8

DGRL
Advisor
Advisor

HI @DannyNL

 

The reason why I started this routine is because data extraction does not give me the results I need.

Maybe I cant work well with data extraction, I don't know but most of the times I just make an LSP.

I can customize the LSP when I want and also make it the way I want it to work.

 

Best regards,

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 8 of 8

DannyNL
Advisor
Advisor

HI @DGRL,

 

OK, clear.

 

Was hoping to save you some coding.

But as long as it gets the job done and gives you the result you need, that's the most important thing Smiley Happy