Jump to content

Edit .LAS file through LISP


Argo

Recommended Posts

Hi All,

 

I'm currently trying to get AutoCAD to replace all instances of a string of text with another. Basically the reason for this is because I have a .las file which is designed to apply layer states to all layers including XREF's but the name and thus the prefix before each XREF will change depending on the project I am working on.

 

Before you mention I know I could simply do a Find and Replace in notepad but I want the user to be able to do it all through CAD.

 

I managed to find a code which replaces text within a file on a certain line to something new but I require the code to do a search and replace through the whole file and replace a certain portion of text (with wildcards) to text which the user will specify.

 

I.E. The .las file specifies the layer name within an XREF in a format of XREF_NAME|Layer_NAME. So an Example string would be 'Project 12345-Architectual Plan|Walls' where 'Project 12345-Architectural' Is the XREF name and 'Walls' is the layer name. I'd like to change the '12345' part as each project will have a different code.

 

I'd need the lisp to prompt the user to input the replacement text.

 

(defun c:test (/ $TEXT $FILENAME lineList newList LineNumToChange)

 (setq LineNumToChange 124)

; set filename to change
  (setq $FILENAME (findfile "SDSK.DFM"))

; new text to swap out with old
 (setq $TEXT "PROT=(G:\\cadd standards\\Surveying\\Prototypes\\,Project Prototypes)")

;;;--- Open the text file
     (if (findfile $FILENAME)
       (progn
         (if (setq fil(open (findfile $FILENAME) "r"))
           (progn
             ;;;--- Read the lines of data in the text file
             (while (setq a (read-line fil))
               ;;;--- Add the data to the list
               (setq lineList
                 (append 
                   lineList
                   (list a)
                 )
               )
             )
             ;;;--- Close the file.
             (close fil)
           )
         )
       )
     )

 
 (setq count 0)
 (foreach item lineList
   (progn  
    (if (= count (- LineNumToChange 1))
     (setq newList(append newList(list $TEXT))); if true do this
     (setq newList(append newList(list item))); if false do this
    )
    (setq count (+ count 1))
   )
 )


;;;--- write changes back to file
 (setq f (open $FILENAME "w"))

; loop to write list to file
 (foreach item newlist    
  (write-line item f)
 )

;;;--- Close the file.
 (close f)
(princ)
)

Link to comment
Share on other sites

While external reference file names may be unique, regardless of relative path or not, they can be named anything you want within the host drawing... Proposed, Existing, etc... Which makes your Layer State 'relative' to any, and all projects thereafter.

 

Consider instead streamlining the renaming of these references at attachment, so you mitigate the need to secondarily modify an .LAS, and subsequently re-import + restore.

 

Cheers

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