LISP to fill custom drawing properties from textfile

LISP to fill custom drawing properties from textfile

geraldbossink
Enthusiast Enthusiast
2,629 Views
11 Replies
Message 1 of 12

LISP to fill custom drawing properties from textfile

geraldbossink
Enthusiast
Enthusiast

I'm looking for a LISP that reads a txt-file in the same directory as the drawing. In the txt-file are two lines with text. First line is the client name, second line is the project name. In the drawing properties I want them filled in in the Custom Tab (as shown in the attached image). Is there some kind of LISP available for this?

0 Likes
Accepted solutions (1)
2,630 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

@geraldbossink wrote:

I'm looking for a LISP that reads a txt-file in the same directory as the drawing. In the txt-file are two lines with text. First line is the client name, second line is the project name. In the drawing properties I want them filled in in the Custom Tab ..


The name of the text file is the same as the drawing name with a .TXT extension?

The Custom Opdrachtgever and Project already exists or sometimes not?

 

 

(defun c:demo ( / aDoc DwgProps availableData source inc opf)
  
(setq aDoc     (vla-Get-ActiveDocument (vlax-Get-Acad-Object))
      DwgProps (vla-Get-SummaryInfo aDoc)
)
  (if (and
	(findfile
	  (setq Source (Strcat (getvar 'dwgprefix) "projectgegevens.txt")))
	  (repeat (setq inc (vla-numcustominfo DwgProps))
		  (vla-GetCustomByIndex DwgProps (setq inc (1- inc)) 'key 'value)
		  (setq availableData (cons (list key value inc) availableData))
		  )
          (Vl-every '(lambda (cn)
		       (assoc cn availableData))
		    (setq CustomNames '("Opdrachtgever" "Project")))
	)
    (progn
      (setq opf (open Source "r"))
      (foreach itm CustomNames
       (setq a (read-line opf))
		(vla-SetCustomByKey DwgProps itm a)
	)
      (close opf)
      )
    )(princ)
  )

 

HTH

 

Message 3 of 12

geraldbossink
Enthusiast
Enthusiast

@pbejse: The dir will contain multiple drawings, so the txt-file will have a general name, let's say 'projectgegevens'. The idea is:

1. txt-file is created

2. new drawing is created, LISP runs and fills the custom properties

3. Drawing header contails fields for client and project, that are automatically filled when the custom properties are filled

4. When adding a new layout to the drawing, a new header is inserted and automatically filled.

At the start of the project the txt-file is created, so for every new drawing the txt-file exists.

 

Hope this helps

0 Likes
Message 4 of 12

pbejse
Mentor
Mentor

@geraldbossink wrote:

The dir will contain multiple drawings, so the txt-file will have a general name, let's say 'projectgegevens'.


Updated the code at post # 2

Hope that helps

 

0 Likes
Message 5 of 12

geraldbossink
Enthusiast
Enthusiast

I'm sorry to inform you, but it doesn't. Saved the code as LISP and ran it, but custom drawing properties remain empty. I tried adding/prefixing 'opdrachtgever' en 'project' in de txt-file, but that didn't work either. Changed 'defun c:demo' to 'defun c:pg', without the requested result.

0 Likes
Message 6 of 12

pbejse
Mentor
Mentor

This is key 

(setq CustomNames '("Opdrachtgever" "Project")))

 If those names does not match the custom property on your drawing the program will not continue.

 

Have a go with that and let me know

Otherwise attached your drawing here 

0 Likes
Message 7 of 12

geraldbossink
Enthusiast
Enthusiast

Hmmm... Yes, that works. But now I have to fill in "opdrachtgever' and 'project' in the custom properties or have these standard in my template. Isn't it possible to make the LISP do that?

0 Likes
Message 8 of 12

pbejse
Mentor
Mentor
Accepted solution

@geraldbossink wrote:

Hmmm... Yes, that works. But now I have to fill in "opdrachtgever' and 'project' in the custom properties or have these standard in my template. Isn't it possible to make the LISP do that?


That was my question early on. 


e Custom Opdrachtgever and Project already exists or sometimes not?

Better have those on the template files
Or

 

(defun c:demo (/ aDoc DwgProps availableData source inc opf data)
  (setq	aDoc	 (vla-Get-ActiveDocument (vlax-Get-Acad-Object))
	DwgProps (vla-Get-SummaryInfo aDoc)
  )
  (if
    (findfile
      (setq Source (Strcat (getvar 'dwgprefix) "projectgegevens.txt"))
    )
     (progn
       (setq opf (open Source "r"))
       (While (setq a (read-line opf))
	 (if (/= a "")
	   (setq data (cons a data))
	 )
       )
       (close opf)

       (repeat (setq inc (vla-numcustominfo DwgProps))
	 (vla-GetCustomByIndex
	   DwgProps
	   (setq inc (1- inc))
	   'key
	   'value
	 )
	 (setq availableData (cons (list key value inc) availableData))
       )

       (mapcar '(lambda	(a b)
		  (if (assoc a availableData)
		    (vla-SetCustomByKey DwgProps a b)
		    (vla-AddCustomInfo DwgProps a b)
		  )
		)
	       '("Opdrachtgever" "Project") (reverse data)
       )
       (vlax-release-object DwgProps)
     )
  )
  (princ)
)

 

HTH

 

Message 9 of 12

geraldbossink
Enthusiast
Enthusiast

Thnx, works like it should!!! I'm sorry I didn't understand your question in the first place, as English is not my native language.

Why would it be better to have the texts in the template already, instead of the LISP filling them?

0 Likes
Message 10 of 12

pbejse
Mentor
Mentor

@geraldbossink wrote:

Thnx, works like it should!!!


Good to know it helps.

 


@geraldbossink wrote:

Why would it be better to have the texts in the template already, instead of the LISP filling them?


It's just good practice really  IMO 🙂

 

Cheers @geraldbossink  🙂

 

 

0 Likes
Message 11 of 12

Cudal
Contributor
Contributor

@pbejse Hi Patrick. Thank you it works for me. what if two text file. One file for Custom Names and the other for values. The CAD file has no custom properties yet. Thank you!!! hope you can help

0 Likes
Message 12 of 12

pbejse
Mentor
Mentor

@Cudal wrote:

...what if two text file. One file for Custom Names and the other for values. The CAD file has no custom properties yet. Thank you!!! hope you can help


That can be arranged

 

@Cudal
can you check if this works on you AutoCAD

(setq sourceFolder( acet-ui-pickdir "Select Value source fodler" (getvar 'dwgprefix)))

 Copy and paste that on the command line