Jump to content

AutoFill Attribute Values and Edit


Mystogan

Recommended Posts

Hi guys need your expertise

 

Hope someone can manage to solve this issue and I think LSP can handle this situation.

 

In the title block, I have this on the top Row title Rev, Modification, Date, Drawn, Check. I need to fill in everything and got so many layout in one file.

 

I have inserted the sample part of title block. and the corresponding default/prompt of attribute

 

Thank you in advance

Image1.PNG

Image2.jpg

Link to comment
Share on other sites

I think I have solution for that.

Here is main function:

(defun c:updateblocks ( / *error* _ini_fln _frm_bkn _blk_ref _set_lst _ss)
 (setq
   _ini_fln "ProjectData.ini"
 )
 (if (setq _ini_fln (findfile "ProjectData.ini"))
   (progn
     (setq _frm_bkn (car(pz:INI-Get _ini_fln nil nil nil)))
     (setq _blk_ref
       (apply 'strcat (cons _frm_bkn
           (mapcar '(lambda ( x ) (strcat ",`" x))
             (LM:getanonymousreferences _frm_bkn)))))
     (setq _set_lst 
       (mapcar
        '(lambda (%)
           (list % (pz:INI-Get _ini_fln _frm_bkn % nil))
         )
         (pz:INI-Get _ini_fln _frm_bkn nil nil)
       )
     )
     (if (setq _ss (ssget "_X" (list '(0 . "INSERT") (cons 2 _blk_ref))))
       (foreach %1 (cd:SSX_Convert _ss 0)
         (foreach %2 _set_lst
           (cd:BLK_SetAttValueVLA %1 (car %2) (cadr %2))
         )
       )
     )
   )
 )
)

and subs:

(defun LM:getanonymousreferences ( blk / ano def lst rec ref )
   (setq blk (strcase blk))
   (while (setq def (tblnext "block" (null def)))
       (if
           (and (= 1 (logand 1 (cdr (assoc 70 def))))
               (setq rec
                   (entget
                       (cdr
                           (assoc 330
                               (entget
                                   (tblobjname "block"
                                       (setq ano (cdr (assoc 2 def)))
                                   )
                               )
                           )
                       )
                   )
               )
           )
           (while
               (and
                   (not (member ano lst))
                   (setq ref (assoc 331 rec))
               )
               (if
                   (and
                       (entget (cdr ref))
                       (wcmatch (strcase (LM:al-effectivename (cdr ref))) blk)
                   )
                   (setq lst (cons ano lst))
               )
               (setq rec (cdr (member (assoc 331 rec) rec)))
           )
       )
   )
   (reverse lst)
)
(defun LM:al-effectivename ( ent / blk rep )
   (if (wcmatch (setq blk (cdr (assoc 2 (entget ent)))) "`**")
       (if
           (and
               (setq rep
                   (cdadr
                       (assoc -3
                           (entget
                               (cdr
                                   (assoc 330
                                       (entget
                                           (tblobjname "block" blk)
                                       )
                                   )
                               )
                              '("AcDbBlockRepBTag")
                           )
                       )
                   )
               )
               (setq rep (handent (cdr (assoc 1005 rep))))
           )
           (setq blk (cdr (assoc 2 (entget rep))))
       )
   )
   blk
)

; ziele_o2k 2017-01-26

; =========================================================================================== ;
; (pz:INI-Get file section key value)                                                         ;
;  file    [sTR]     - short or full path file name                                           ;
;  section [sTR/nil] - name of section to read key                                            ;
;                      if nil then function will return all section names from .ini file      ;
;  key     [sTR/nil] - name of key to read value                                              ;
;                      if nil then function will return all key names from section            ;
;                      if value of key is "" then returns nil                                 ;
;  value   [sTR/nil] - default value if section or key not found                              ;
; ------------------------------------------------------------------------------------------- ;
; (pz:INI-Get "example.ini" "SECTION1" "KEY2" nil)                                            ;
; =========================================================================================== ;
(defun pz:INI-Get ( file section key value / l res tmpl )
 (if (and section (=(type section) 'STR))
   (setq section (strcat "[" section "]"))
 )
 (if (setq l (pz:INI-Read file))
   (cond
     ( (not section)
       (foreach %1 l
         (if (= (type (car %1)) 'STR)
           (setq res 
             (cons 
               (substr 
                 (pz:TrimStr (car %1))
                 2 
                 (- (strlen (pz:TrimStr (car %1))) 2)
               )
               res
             )
           )
         )
       )
       (setq res (reverse res))
     )
     ( (and 
         (not key)
         (setq tmpl (pz:AssocCI section l))
       )
       (foreach %1 (cdr tmpl)
         (if (= (length %1) 2)
           (setq res (cons (pz:TrimStr(car %1)) res))
         )
       )
       (setq res (reverse res))
     )
     ( (and
         key
         (setq section (cdr (pz:AssocCI section l)))
       )
       (if (setq res (pz:AssocCI key section))
         (if (= (setq res (pz:TrimStr(cadr res))) "")
           (setq res nil)
         )
         (setq res value)
       )
     )
     ( T (setq res value))
   )
   (setq res value)
 )
 res
)
; =========================================================================================== ;
; (pz:INI-Set file section key value)                                                         ;
;  file    [sTR] - short or full path file name                                               ;
;  section [sTR] - name of section to set key value                                           ;
;  key     [sTR] - name of key to set value                                                   ;
;  value   [sTR] - value to set                                                               ;
; ------------------------------------------------------------------------------------------- ;
; If section not found then function will create section with given key and value             ;
; If key in given section not found, function will create key with supplied value in section  ;
; ------------------------------------------------------------------------------------------- ;
; (pz:INI-Set "example.ini" "SECTION1" "KEY2" "Value 1_2")                                    ;
; =========================================================================================== ;

(defun pz:INI-Set ( file section key value / l sl fvalue i )
 (if
   (and
     (=(type file) 'STR)
     (=(type section) 'STR)
     (=(type key) 'STR)
     (=(type value) 'STR)
     (setq section (strcat "[" section "]"))
     (setq l (pz:INI-Read file))
   )
   (progn
     (if (setq sl (pz:AssocCI section l))
       (setq 
         sl (cdr sl)
         sl 
         (if (pz:AssocCI key sl)
           (progn
             (setq 
               fvalue (vl-string-subst value (pz:TrimStr(cadr(pz:AssocCI key sl))) (cadr(pz:AssocCI key sl)))
             )
             (subst 
               (list (car(pz:AssocCI key sl)) fvalue)
               (pz:AssocCI key sl)
               sl
             )
           )
           (progn
             (setq i (1-(length sl)))
             (while (= (length(nth i sl)) 1)
               (setq i (1- i))
             )
             (cd:LST_InsertItem (1+ i) sl (list key value))
           )
         )
         l (subst (cons (car(pz:AssocCI section l)) sl) (pz:AssocCI section l) l)
       )
       (setq l (reverse (append (list(list section;|(car(pz:AssocCI section l))|; 
       (list key value))) (reverse l) )))
     )
     (if 
     (and 
       (if (not (findfile file))
         (setq file (strcat PZ:GT-CADkat "Settings\\" file)) ; delete this row in final version
         (setq file (findfile file))
       )
       ;(setq file (findfile (strcat PZ:GT-CADkat "Settings\\" file))) 
       (setq file (open file "w"))
     )
       (progn
         (foreach %1 l
           (if (= (type (car %1)) 'STR)
             (progn
               (write-line (car %1) file)
               (foreach key (cdr %1)
                 (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
               )
             )
             (foreach key %1
               (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
             )
           )
         )
         (not (close file))
       )
     )
   )
   nil
 )
)

; =========================================================================================== ;
; (pz:INI-Del file section key value)                                                         ;
;  file    [sTR]     - short or full path file name                                           ;
;  section [sTR]     - name of section to delete key or key value                             ;
;  key     [sTR/nil] - name of key to delete value or key                                     ;
;                      if nil then function will delete whole section with all it's keys      ;
;                      but will leave comments                                                ;
;  value   [nil/T]   - nil - delete key with value                                            ;
;                    - T   - delete only value of given key                                   ;
; ------------------------------------------------------------------------------------------- ;
; (pz:INI-Del "example.ini" "SECTION4" "KEY2" nil)                                            ;
; =========================================================================================== ;

(defun pz:INI-Del ( file section key value / l sectionlst sectionl  )
 (if
   (and
     (=(type file) 'STR)
     (if (and section (=(type section) 'STR))
       (setq section (strcat "[" section "]"))
       nil
     )
     (setq l (pz:INI-Read file))
   )
   (cond
     ( (not key)
       (setq 
         sectionlst (pz:AssocCI section l )
         sectionlst 
         (vl-remove-if-not
           '(lambda (%1)
             (if (=(type %1) 'LIST)
               (=(length %1) 1)
               nil
             )
           )
           sectionlst
         )
         l (subst sectionlst (pz:AssocCI section l) l)
       )
     )
     ( (not value)
       (if 
         (and
           (setq
             sectionl (cdr(pz:AssocCI section l ))
           )
           (pz:AssocCI key sectionl)
         )
         (setq 
           sectionl (vl-remove (pz:AssocCI key sectionl) sectionl)
           l (subst (cons (car(pz:AssocCI section l)) sectionl) (pz:AssocCI section l) l)
         )
       )
     )
     ( T
       (if 
         (and
           (setq
             sectionl (cdr(pz:AssocCI section l ))
           )
           (pz:AssocCI key sectionl)
         )
         (setq sectionl
           (subst 
             (list (car(pz:AssocCI key sectionl)) "")
             (pz:AssocCI key sectionl)
             sectionl
           )
           l (subst (cons (car(pz:AssocCI section l)) sectionl) (pz:AssocCI section l) l)
         )
       )
     )
   )
 )
 (if 
   (and 
     (if (not (findfile file))
       (setq file (strcat PZ:GT-CADkat "Settings\\" file)) ; delete this row in final version
       (setq file (findfile file))
     )
     ;(setq file (findfile (strcat PZ:GT-CADkat "Settings\\" file))) 
     (setq file (open file "w"))
   )
   (progn
     (foreach %1 l
       (if (= (type (car %1)) 'STR)
         (progn
           (write-line (car %1) file)
           (foreach key (cdr %1)
             (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
           )
         )
         (foreach key %1
           (write-line (if (cadr key) (strcat (car key) "=" (cadr key)) (car key)) file)
         )
       )
     )
     (not (close file))
   )
 )
)
; =========================================================================================== ;
; Subroutines                                                                                 ;
; =========================================================================================== ;
(defun pz:TrimStr ( s )
 (vl-string-trim " \t" s)
)
(defun pz:AssocCI (e l / )
 (vl-some 
  '(lambda (%1) 
     (if 
       (and 
         (= (type (car %1)) 'STR) 
         (= (strcase e) (strcase (pz:TrimStr(car %1) )) )
       )
       %1
     )
   )
   l
 )
)
(defun pz:INI-Read ( f / l res sub i )
 (if 
   (and
     (setq f (findfile f))
     (setq l (cd:SYS_ReadFile nil f))
     (= (type l) 'LIST)
   )
   (progn
     (foreach %1 l
       (cond
         ( (and 
             (/= (substr (pz:TrimStr %1) 1 1) ";")
             (/= (substr (pz:TrimStr %1) 1 1) "#")
             (wcmatch (pz:TrimStr %1) "`[*`]")
           )
           (if sub 
             (setq res (cons (reverse sub) res))
           )
           (setq sub (list %1))
         )
         ( (and 
             (/= (substr (pz:TrimStr %1) 1 1) ";")
             (/= (substr (pz:TrimStr %1) 1 1) "#")
             (wcmatch %1 "*=*")
           )
           (setq i 0)
           (while (/= "=" (substr %1 (setq i (1+ i)) 1)))
           (setq sub (cons (list (substr %1 1 (1- i)) (substr %1 (1+ i))) sub))
         )
         ((setq sub (cons (list %1) sub)))
       )
     )
     (if sub (setq res (cons (reverse sub) res)))
   )
 )
 (reverse res)
)


(defun cd:SYS_ReadFile (Line File / fn fd l res)
 (if (setq fn (findfile File))
   (if (setq fd (open fn "r"))
     (progn
       (if Line
         (progn
           (repeat Line (read-line fd))
           (setq res (read-line fd))
         )
         (progn
           (setq l T)
           (while l
             (setq res
               (cons
                 (setq l (read-line fd))
                 res
               )
             )
           )
           (setq res (reverse (cdr res)))
         )
       )
       (close fd)
     )
     (setq res 0)
   )
   (setq res -1)
 )
 res
)

(defun cd:SSX_Convert (Ss Mode / n res)
 (if
   (and
     (member Mode (list 0 1 2))
     (not
       (minusp
         (setq n
           (if Ss (1- (sslength Ss)) -1)
         )
       )
     )
   )
   (progn
     (while (>= n 0)
       (setq res
         (cons
           (if (zerop Mode)
             (ssname Ss n)
             (vlax-ename->vla-object (ssname Ss n))
           )
           res
         )
             n (1- n)
       )
     )
     (if (= Mode 2)
       (vlax-safearray-fill
         (vlax-make-safearray 9
           (cons 0 (1- (length res)))
         ) res
       )
       res
     )
   )
 )
)


(defun cd:BLK_SetAttValueVLA (Obj Tag Value)
 (if (= (type Obj) (quote ENAME))
   (setq Obj (vlax-ename->vla-object Obj))
 )
 (vl-some
   (function
     (lambda (%)
       (if (eq (strcase tag) (strcase (vla-get-TagString %)))
         (progn
           (vla-put-TextString % Value)
           Value
         )
       )
     )
   )
   (vlax-invoke Obj (quote GetAttributes))
 )
)

Now all you have to do is:

  1. Create text file ProjectData.ini in folder where your dwg resides
  2. In section name type block block name
  3. as keys use atts names
  4. as keys values type your new atts values

Example of ProjectData.ini file:

[MyBlockName]
Att-1=NewValue
Att-2=NewValue
Att-3=NewValue

I hope that all of subroutines are included :)

Link to comment
Share on other sites

Like previous post each layout can be updated independantly so where is the info for each layout ? We would have a revision number some where but that would be maybe the only item that can be found.

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