Is it possible for a lisp to have two separate *.txt files open?

Is it possible for a lisp to have two separate *.txt files open?

Anonymous
Not applicable
970 Views
9 Replies
Message 1 of 10

Is it possible for a lisp to have two separate *.txt files open?

Anonymous
Not applicable

Guys, please remind me because it has been a really long time.  I can't seem to pull this off.

 

I essentially have a list of layers from current drawings I've extracted from over 100 files (resulting in 12,000 uniques layers, OldLayerNames).  I have another file that has all of the AutoCAD Layer Standards in there along with linetypes, colors and line weights (2,000+ unique layers StandardLayerNames).  I have a team of interns working on manually mapping OldLayerNames to the StandardLayerNames, they will finish sometime tomorrow.

 

I want a way to read each OldLayerName and determine its StandardLayerName (once the interns finish that tomorrow, I'll have a CSV or TXT file that has OldLayerName,StandardLayerName.  That is already done.

 

I already have another TXT file that has StandardLayerName,Color,LineType,LineWeight.

 

I can't seem to read-line on my second file and I don't know if its my problem with how I wrote this or if there is a limitation on how many TXT files a lisp can open Line #19 in the Secondary While statement seems to be the problem when I'm in Microsoft Visual Studio Code.   (setq NAline (read-line f2))

2021-02-15_22-49-06.jpg

 

(defun C:GLMVLtrans()																					;	Define Funcition - typing GLMVLtrans starts the command - Layer Translator
 (princ)																								;	Dead return
  (command "Undo" "BE")																					;	Start the Undo Begining point
   (setq skip 0)																						;	save variable called skip to 0
   (setq f1 (open "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/deleteme.txt" "R"))					;	save variable called f1 using a specific file
   (setq f2 (open "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/Net App Layer Standards.txt" "R"))	;	save variable called f2 using a specific file

;;;	Start the primary while statement.

  (while												;	Start while statement
		 (setq line (read-line f1))						;	Reads line in F1 and saves it as variable line so that while the line is not nil then repeat below until it is nil
		 (setq cPos (vl-string-search "," line))		;	Find the digit position of the comma in the line and save the number
		 (setq OldLayName (substr line 1 cpos))			;	Save the Old Layer name, everything before the comma digit position
		 (setq NewLayName (substr line (+ cPos 2)))		;	Save the New Layer name, everything after  the comma digit position
		 
;;;	Start the seconday while statement.  This will keep looping on the read-line of the second file f2 until it matches the NewLayName with the NALayName
		 
	(while												;	While
		(setq NAline (read-line f2))					;	Reads line in f2 and saves it as variable NAline
		(setq NAcPos1 (vl-string-search "," NAline))	;	Find the digit position of the first comma in the line and save the number
		(setq NALayName (substr NAline 1 NAcPos1))		;	Extract the NetApp Layer name

;;;	Start Main If statement to either merge or create new NetApp layer

	(if (= NewLayName NALayName)
	 (progn
	 	(setq NAcPos2 (vl-string-search "," NAline (+ 1 NACPos1)))				;	Find the digit position of the second comma in the line and save the number
		(setq NAcPos3 (vl-string-search "," NAline (+ 1 NACPos2)))				;	Find the digit position of the third  comma in the line and save the number
		(setq NALayColor (substr NAline (+ 2 NAcPos1) (- NACPos2 1 NACPos1)))	;	Extract the NetApp Color
		(setq NALayLtype (substr NAline (+ 2 NAcPos2) (- NACPos3 1 NACPos2)))	;	Extract the NetApp Line Type
		(setq NALayLweight (/ (atoi (substr NAline (+ 2 NAcPos3))) 100.00))		;	Extract the NetApp Line Weight
		(setq DoesLayerExist (TblSearch "Layer" NALayName))						;	Determines if Layer currently exists in the file
		(setq skip 0)															;	Setq variable Skip to 0
	 );progn
		(setq skip 1)
	);if
	
;;;	Start Main If statement to either merge or create new NetApp layer

    (if (= skip 1)																					;	If Skip = 1
	 (princ)																						;	Dead Return
	  (progn																						;	Progn, Else do the following
		(If (/= nil DoesLayerExist)																;	If the NALayName already esists
		 (command "-laymrg" "n" OldLayName "" "n" NALayName "y")											;	Merge and delete the old layer to the new NetApp Layer, If it Layer doesn't exist
		  (progn																					;	progn, Do the following
		   (If (= NALayLweight -0.03)																;	Start nested if statement, If the detected NetApp linewieght is default
		    (command "-layer" "n" NALayName "c" NALayColor NALayName "l" NALayLtype NALayName "")						;	Make new layer with color, line type, If Not
			 (progn																					;	progn, do the following
		      (command "-layer" "n" NALayName "c" NALayColor NALayName "l" NALayLtype NALayName "lw" NALayLweight NALayName "")	;	Make new layer with color, line type and Line weight
			  (command "-laymrg" "n" OldLayName "" "n" NALayName "y")										;	Merge the old layer to the NetApp Layer, If it doesn't exist then
			 );progn																				;	progn, stop doing the following from line 48
		   );If																						;	End Nested If line 46
	      );progn																					;	progn, stop doing the following from line 45
		);If																						;	End If line 43
	  );progn																						;	progn, stop doing the following from line 42
	);If																							;	End secondary while satement

	);while							;	End secondary while satement
	 (close f2)						;	close the file f2
 
 );while							;	End while statement
  (close f1)						;	close the file f1


;;;	Wrap up	  
     
     (command "undo" "end")			;	Establishes the Undo End point
	   (princ)						;	dead return
);eof

 

0 Likes
Accepted solutions (1)
971 Views
9 Replies
Replies (9)
Message 2 of 10

Moshe-A
Mentor
Mentor

@Anonymous ,

 

Do not have the time to look into the code now but:-

 

a. As far as i know, there should be no problem to open more that 1 file simultaneously.

b. before continue with this, take a real look at Layer Translate (LAYTRANS) command.

 

Moshe

 

Message 3 of 10

hak_vz
Advisor
Advisor

You can have more than one files open simultaneously.

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.
Message 4 of 10

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Guys, please remind me because it has been a really long time.  I can't seem to pull this off.

the problem when I'm in Microsoft Visual Studio Code.   (setq NAline (read-line f2))

There is no issue with access to multiple txt file, the problem is your sequence.

 

(close f2)

 

 You close f2 after processing the first item from f1, but you dont open it again, hence the error

[hint] You have to place it where and when the program is finish reading f1. or open the file again [ not advisable]

 

A better approach is read ALL the data and save it to a list for BOTH of them, that way you dont open and close f2.

Try it and let us know if you need assistance

 

HTH

 

**off-topic**  what is it with this formatting 

 

0 Likes
Message 5 of 10

CodeDing
Advisor
Advisor

@pbejse wrote:

**off-topic**  what is it with this formatting 


... Well, while OPs notes are a bit excessively laden with tabs, I think we can all agree that the WORD WRAP associated with the forums Code Editor needs to be removed.

 

This, among other issues, has been addressed several times HERE. (And they have YET to hint at a fix for it since then)

 

(Edit)

Oh, and I can't even IMAGINE what OPs code looks like on mobile... The word wrap is absolutely UNBEARABLE. Removing the word wrap is just purely beneficial all-around.

(/Edit)

 

Here's what OPs code MIGHT look like without the stinking word wrap. (This uses the <pre></pre> tag in the HTML editor)

(defun C:GLMVLtrans()											;	Define Funcition - typing GLMVLtrans starts the command - Layer Translator
 (princ)												;	Dead return
  (command "Undo" "BE")											;	Start the Undo Begining point
   (setq skip 0)											;	save variable called skip to 0
   (setq f1 (open "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/deleteme.txt" "R"))			;	save variable called f1 using a specific file
   (setq f2 (open "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/Net App Layer Standards.txt" "R"))	;	save variable called f2 using a specific file

;;;	Start the primary while statement.

  (while							;	Start while statement
		 (setq line (read-line f1))			;	Reads line in F1 and saves it as variable line so that while the line is not nil then repeat below until it is nil
		 (setq cPos (vl-string-search "," line))	;	Find the digit position of the comma in the line and save the number
		 (setq OldLayName (substr line 1 cpos))		;	Save the Old Layer name, everything before the comma digit position
		 (setq NewLayName (substr line (+ cPos 2)))	;	Save the New Layer name, everything after  the comma digit position
		 
;;;	Start the seconday while statement.  This will keep looping on the read-line of the second file f2 until it matches the NewLayName with the NALayName
		 
	(while							;	While
		(setq NAline (read-line f2))			;	Reads line in f2 and saves it as variable NAline
		(setq NAcPos1 (vl-string-search "," NAline))	;	Find the digit position of the first comma in the line and save the number
		(setq NALayName (substr NAline 1 NAcPos1))	;	Extract the NetApp Layer name

;;;	Start Main If statement to either merge or create new NetApp layer

	(if (= NewLayName NALayName)
	 (progn
	 	(setq NAcPos2 (vl-string-search "," NAline (+ 1 NACPos1)))		;	Find the digit position of the second comma in the line and save the number
		(setq NAcPos3 (vl-string-search "," NAline (+ 1 NACPos2)))		;	Find the digit position of the third  comma in the line and save the number
		(setq NALayColor (substr NAline (+ 2 NAcPos1) (- NACPos2 1 NACPos1)))	;	Extract the NetApp Color
		(setq NALayLtype (substr NAline (+ 2 NAcPos2) (- NACPos3 1 NACPos2)))	;	Extract the NetApp Line Type
		(setq NALayLweight (/ (atoi (substr NAline (+ 2 NAcPos3))) 100.00))	;	Extract the NetApp Line Weight
		(setq DoesLayerExist (TblSearch "Layer" NALayName))			;	Determines if Layer currently exists in the file
		(setq skip 0)								;	Setq variable Skip to 0
	 );progn
		(setq skip 1)
	);if
	
;;;	Start Main If statement to either merge or create new NetApp layer

    (if (= skip 1)															;	If Skip = 1
	 (princ)															;	Dead Return
	  (progn															;	Progn, Else do the following
		(If (/= nil DoesLayerExist)												;	If the NALayName already esists
		 (command "-laymrg" "n" OldLayName "" "n" NALayName "y")								;	Merge and delete the old layer to the new NetApp Layer, If it Layer doesn't exist
		  (progn														;	progn, Do the following
		   (If (= NALayLweight -0.03)												;	Start nested if statement, If the detected NetApp linewieght is default
		    (command "-layer" "n" NALayName "c" NALayColor NALayName "l" NALayLtype NALayName "")				;	Make new layer with color, line type, If Not
			 (progn														;	progn, do the following
		      (command "-layer" "n" NALayName "c" NALayColor NALayName "l" NALayLtype NALayName "lw" NALayLweight NALayName "")	;	Make new layer with color, line type and Line weight
			  (command "-laymrg" "n" OldLayName "" "n" NALayName "y")							;	Merge the old layer to the NetApp Layer, If it doesn't exist then
			 );progn													;	progn, stop doing the following from line 48
		   );If															;	End Nested If line 46
	      );progn															;	progn, stop doing the following from line 45
		);If															;	End If line 43
	  );progn															;	progn, stop doing the following from line 42
	);If																;	End secondary while satement
	);while							;	End secondary while satement
	 (close f2)						;	close the file f2
 );while							;	End while statement
  (close f1)							;	close the file f1

;;;	Wrap up
 
     (command "undo" "end")					;	Establishes the Undo End point
	   (princ)						;	dead return
);eof

Best,

~DD

Message 6 of 10

pbejse
Mentor
Mentor

Its getting late here, here's a modified version of your code, I kept most of coding and took out some, but i rearrange it as to show what i meant from my previous post.

 

(defun C:GLMVLtrans ( / deleteitemCollection f1 f2 cPos OldLayName NewLayName NAline NAcPos1 NALayName NAcPos2 NAcPos3
		     	processThis NALayColor NALayLtype NALayLweight StandardsCollection DoesThisExistOnTheSecondList
		     	DoesLayerExist)	
 (princ)					
  (command "Undo" "BE")				
      
(if (and
      (setq f1 (findfile "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/deleteme.txt" "R"))
      (setq f2 (findfile "C:/Users/jayala/Documents/ATG/Clients/GLMV/Lisp/Net App Layer Standards.txt" "R"))
      )
;;;	Start the primary while statement.
  (progn
	(setq f1 (open f1 "R"))
	(setq f2 (open f2 "R"))
    
	  (while (setq line (read-line f1))				
			 (setq cPos (vl-string-search "," line))		
			 (setq OldLayName (substr line 1 cpos))			
			 (setq NewLayName (substr line (+ cPos 2)))		
			 (setq deleteitemCollection (cons (list OldLayName NewLayName) deleteitemCollection)))
	  	(close f1)
	(while								
		(setq NAline (read-line f2))				
		(setq NAcPos1 (vl-string-search "," NAline))		
		(setq NALayName (substr NAline 1 NAcPos1))
	  	(setq NAcPos2 (vl-string-search "," NAline (+ 1 NACPos1)))		
		(setq NAcPos3 (vl-string-search "," NAline (+ 1 NACPos2)))		
		(setq NALayColor (substr NAline (+ 2 NAcPos1) (- NACPos2 1 NACPos1)))	
		(setq NALayLtype (substr NAline (+ 2 NAcPos2) (- NACPos3 1 NACPos2)))	
		(setq NALayLweight (/ (atoi (substr NAline (+ 2 NAcPos3))) 100.00))
	  	(setq StandardsCollection (cons
					    (list  NALayName NALayColor NALayLtype NALayLweight) StandardsCollection))
	  )

    (close f2)
   
                      
    (while (setq processThis (car deleteitemCollection)); first item of the list [ in this case the last of the deleteme.txt content )
      (setq OldLayName (car processThis))		; first item of the list | the OldLayName from f1
      (setq NALayName  (cadr processThis))		; second item of the list | the NewLayName from f1
      	(if (and
	      (tblsearch "Layer" OldLayName)		; Does this layer exist in the drawing
	      (setq DoesThisExistOnTheSecondList	; Test if the newname is on the list from Net App Layer Standards.txt | f2
		   (assoc					
		     NALayName		 		
		  	StandardsCollection		; the items from the Net App Layer Standards.txt
		     			)		; close assoc
		  		)			; close setq DoesThisExistOnTheSecondList
		)
	  
;;;	Start Main If statement to either merge or create new NetApp layer	  
	  (progn
	    (setq NALayColor (cadr DoesThisExistOnTheSecondList))	
            (setq NALayLtype (caddr DoesThisExistOnTheSecondList))
            (setq NALayLweight	(cadddr DoesThisExistOnTheSecondList))	   
	    (setq DoesLayerExist (TblSearch "Layer" NALayName))
	    	(If (/= nil DoesLayerExist)
		 (command "-laymrg" "n" OldLayName "" "n" NALayName "y")
		  (progn
		   (If (= NALayLweight -0.03)
		    (command "-layer" "n" NALayName "c" NALayColor NALayName "l" NALayLtype NALayName "")
			 (progn										
		      (command "-layer" "n" NALayName "c" NALayColor NALayName "l" 
			NALayLtype NALayName "lw" NALayLweight NALayName "")	
			  (command "-laymrg" "n" OldLayName "" "n" NALayName "y")
			 )
		   )
	      )
		)
	  )
	)
      (setq deleteitemCollection (Cdr deleteitemCollection))	; the rest of the list except the first one
      )
	);progn
  );if findfile
     (command "undo" "end")
	   (princ)	
);eof

 HTH

 

0 Likes
Message 7 of 10

Anonymous
Not applicable

@Moshe-A 

Thanks for the tip on the LayTrans.  I actually dug into that.  Unless you know of a way to use that feature that I don't, it is more trouble than it's worth at the moment.

 

I'll go back and double check to see if there are any options that speed things up and be more efficient than completing this lisp, but my trial run (100 layers) with it took a lot longer than anticipated.

0 Likes
Message 8 of 10

Anonymous
Not applicable

@pbejse,

Ahh!!!  How did I not see that I closed f2!!!

Perhaps it was forest through the trees and all that.  GOOD CATCH!!!

0 Likes
Message 9 of 10

Sea-Haven
Mentor
Mentor

One option for speed 

(command "-laymrg" "n" OldLayName "" "n" NALayName "y")

Using entmake can be way faster for making a layer and that is used a lot in the code. So that is your homework, plenty of examples can be found.

0 Likes
Message 10 of 10

Anonymous
Not applicable

@Sea-Haven ,

Good point.  I'll have to go back and look at that again.

0 Likes