Delete Layouts from List.TXT with exceptions

Delete Layouts from List.TXT with exceptions

miodrag.jovanovic
Contributor Contributor
876 Views
8 Replies
Message 1 of 9

Delete Layouts from List.TXT with exceptions

miodrag.jovanovic
Contributor
Contributor

Hi good People!

I have rather ease problem, but still I cant get it done - Lisp deletes all Layouts from L.txt file(which I create from excel) but he also deletes some layouts that are not in the L.txt.. Weird, but I decided to precisely give "him" the names of Layouts that he will not delete... I would say that rather simple IF Statement will solve this, but still I dont know Lisp enough to make it work...already tried to cut some Lisps and add here, but after 3h I give up

 

-------CODE---------

 

(defun c:DeleteLayoutsNEW ( / fn fp lst l)

(setq exceptions ("Deckblatt" "Layout1" "Layout2" ....) ; the list of all Layouts that we will NOT delete

(setq fn (findfile "L.txt"))

;; Open the file and create an empty list
(setq fp (open fn "r") lst '())

;; Iterate the file, writing each line to the list
(while (setq l (read-line fp))
(setq lst (cons l lst))
)

;; Close the file.
(close fp)


(foreach x lst

(if (not ??? (strcase x) exceptions )) 
);IF
(command "-LAYOUT" "D" x)

);foreach

);if
(princ)
);defun

0 Likes
Accepted solutions (1)
877 Views
8 Replies
Replies (8)
Message 2 of 9

hak_vz
Advisor
Advisor
(foreach x lst
    (if (not (member x exceptions))
      (command "-LAYOUT" "D" x)
    )
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 9

miodrag.jovanovic
Contributor
Contributor

@hak_vz  Thank you for a quick responce!

Corrected Code is still deleting "Deckblatt" Layout(this Layout is defined in the "exceptions")....I am losing my mind 😄

0 Likes
Message 4 of 9

hak_vz
Advisor
Advisor
Accepted solution
(defun c:DeleteLayouts ( / fn fp lst l)
(setq exceptions '("Deckblatt" "Layout1" "Layout2" )) ; the list of all Layouts that we will NOT delete
(setq fn (findfile "L.txt"))
;; Open the file and create an empty list
(setq fp (open fn "r") lst '())
;; Iterate the file, writing each line to the list
(while (setq l (read-line fp))
(setq lst (cons l lst))
)

;; Close the file.
(close fp)
(foreach x lst
   (if (not (member x exceptions))
     (command "-LAYOUT" "D" x)
);If
);foreach
(princ)
);defun

There were some syntax errors in your code. Try now.

In your code "(" is missing in front of exceptions list definition).

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 9

miodrag.jovanovic
Contributor
Contributor

@hak_vz Thx a lot! Sorry for the late responce, I was trying out the code - until I figured it out, that Lisp is not reading L.txt file in the current directory(but in the Dokuments), so he was always deleting the Layouts he should not...

Any chance to kindly ask you for one more solution? I would like to get ALWAYS "L.txt" file from the current location of DWG... I have some code, but still can not get it to work...Any advice would help! Thx in advance!

 

-----CODE-------

 

(defun c:DeleteLayouts ( / fn fp lst l)
(setq exceptions '("Deckblatt" "Layout1" "Layout2" )) ; the list of all Layouts that we will NOT delete


(setq Current_Directory (findfile (strcat (getvar 'dwgprefix) "..")))
(setq fn (findfile (strcat Current_Directory "\\L.txt")))

;;;(setq fn (findfile "L.txt"))
;; Open the file and create an empty list
(setq fp (open fn "r") lst '())
;; Iterate the file, writing each line to the list
(while (setq l (read-line fp))
(setq lst (cons l lst))
)

;; Close the file.
(close fp)
(foreach x lst
(if (not (member x exceptions))
(command "-LAYOUT" "D" x)
);If
);foreach
(princ)
);defun

0 Likes
Message 6 of 9

hak_vz
Advisor
Advisor

@miodrag.jovanovic wrote:

@hak_vz

Any chance to kindly ask you for one more solution? I would like to get ALWAYS "L.txt" file from the current location of DWG... I have some code, but still can not get it to work...Any advice would help! Thx in advance!


System variable DWGPREFIX stores the drive and folder path for the current drawing.

To load file "L.txt" you have to concatenate path information with file name

 

(setq text_file (strcat (getvar 'dwgprefix) "L.txt"))

 

You don't to use function filesearch  but simply

 

 (setq f (open text_file "r"))

 

And a small digression: another system variable that can be of use is TEMPPREFIX. It stores location of system temporary folder. If you have to open a temporary file, and don't want to pollute your working folder, this is the best a place to store temporary files.

I hope that you will know how to incorporate this in your code  but if you need help just ask.

 

Lijepi pozdrav!

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 9

miodrag.jovanovic
Contributor
Contributor

Hi again!

Thx a lot for the help and effort!

My code below is displaying error "Command: DELETELAYOUTS
; error: bad argument type: FILE nil"

It should be simple enough but I still dont get it it seem... Any advice?

Hvala najlepse!!! 🙂

 

 

----CODE-----------

(defun c:DeleteLayouts ( / fn fp lst l)
(setq exceptions '("Deckblatt" "Layout1" "Layout2" )) ; the list of all Layouts that we will NOT delete

(setq fn (strcat (getvar 'dwgprefix) "L.txt"))

;; Open the file and create an empty list
(setq fp (open fn "r") lst '())

;; Itterate the file, writing each line to the list lst
(while (setq l (read-line fp))
(setq lst (cons l lst))
)

;; Close the file.
(close fp)

;;Start itterations of Layouts
(foreach x lst
(if (not (member x exceptions))
(command "-LAYOUT" "D" x)
);If
);foreach
(princ)
);defun

0 Likes
Message 8 of 9

hak_vz
Advisor
Advisor

It should work correctly if you actually have file named "l.txt" in folder where your file is placed.

 

Here is a simple generalized file loader

 

(defun txt_to_list ( file_name / lst fn fp)
(setq fn (strcat (getvar 'dwgprefix) file_name))
;; Open the file and create an empty list
(setq fp (open fn "r"))
;; Itterate the file, writing each line to the list lst
(while (setq l (read-line fp))
(setq lst (cons l lst))
)
;; Close the file.
(close fp)
(reverse lst)
);defun

If we place file "test.txt" to that directory we would have

aaaaa
bbbbb
ccccc
ddddd
Command: (setq lst (txt_to_list "test.txt"))
("aaaaa" "bbbbb" "ccccc" "ddddd")

Sample  file is in attachment.

Your code is correct, so check that you have txt file in folder and that your active drawing is saved in that folder. Don't test on unsaved drawing since.

 

Sorry for delay, I was out.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 9 of 9

miodrag.jovanovic
Contributor
Contributor

@hak_vz  Thank you for all the effort and help!

I already checked everything and  I must inspect .dwg for errors(in Layouts) because the Lisp is working great on other .dwg's, but not on this one I created...

Anyway thx a lot and God bless!

Sve najbolje!