1

I'm struggling with changing the right DXF, there is 9 DXF of 300.

I get the following list, with the command (entget (car (entsel))):

((-1 . <Entity name: 223791faf20>)
(0 . "HSB_BEAMENT")
(330 . <Entity name: 21b6fa889f0>) 
(5 . "5A612") 
(100 . "AcDbEntity") 
(67 . 0) 
(410 . "Model") 
(8 . "E-01") 
(62 . 92) 
(100 . "Hsb_BeamEnt") 
(70 . 3) 
(10 -86756.4 43492.7 3022.5) 
(15 -86756.4 43492.7 530.094) 
(142 . 5469.91) 
(143 . -485.094) 
(11 0.0 0.0 1.0) 
(12 -1.83697e-16 -1.0 0.0) 
(13 1.0 -1.83697e-16 0.0) 
(14 0.0 0.0 0.0) 
(140 . 45.0) 
(141 . 295.0) 
(300 . "") 
(70 . 10) 
(79 . 0) 
(332 . <Entity name: 0>) 
(144 . 0.0) 
(300 . "STUTZ") 
(300 . "Bearbejdet") 
(300 . "490") 
(300 . "E-01") 
(300 . "") 
(300 . "") 
(300 . "Ribbe C24 295x45") 
(300 . "C24") 
(301 . "") 
(302 . "") 
(71 . -1) 
(72 . 0) 
(73 . 0) 
(74 . 6))

Here I want to change the DXF 300 at index 4 (300 . "E-01")

I have created the following code: But how can I choose a index? and get DXF I want.

(defun c:changeattr()
    (setq a (car (entsel "\nSelect a object: ")))
    (setq b (entget a))

    (setq c (subst (cons 300 "F200") (assoc 300 b) b))  ; Trying to update and change the Label Property
    (entmod c)
    (entupd a)
    (prompt "\nAttribute entity updated.")
(princ)
)

Thx in advanced for the help. I appreciate it :)

Math
  • 141
  • 2
  • 16

2 Answers2

1

Try this:

(setq def (entget (car(entsel ))) )

(defun DXF:Put ( def code index val / subs cp
    *error* )   (defun *error* ( msg / ) 
        (if (not (null msg ) )  (progn (princ "\nDXF:Pu:*error*: " ) (princ msg ) (princ "\n")  ) )
    )
    (setq cp def )
    ( while (setq item (assoc code cp ))
        (setq subs (append subs (list item ) ) )
        (setq cp (cdr (member item cp ) ))
    )
    (subst (cons code val) (nth index subs ) def )
)

(setq code 300 
    index 4
    val "test"
)


(DXF:Put def code index val )

then of course endmod, entupd

CAD Developer
  • 1,532
  • 2
  • 21
  • 27
  • Thanks @CAD Developer :D Is this correct? (entmod code) (entupd def) I'm getting the following error message, when I run the command: bad list in entity tail: 300 What does it meen? – Math Sep 19 '18 at 10:39
1

As per your problem here I have doubt to updating associative list on the basis of index because, some time we can't predict exact index position. please check possibility of index change.

Here I am write two function which can helpful to change associative list.
1. Update associative list on the basic of Index.
2. Update associative list on the basis of OldValue.

This function included into your code as below.

(defun c:changeattr()

;------------------------------------------New Function To change associate list---------------------------------------------;

;This function Update Associative list by index
;[ key ]-> Key Value in associate list
;[ NewValue ]-> New Value to replace
;[ index ]-> Index
;[ InputList ]-> Associate List  

(defun UpdateAssocListByIndex(key NewValue index InputList / index)

;set index value to 1 if input is nil or empty string
;Change on 21-09-218 By Dinesh Pawar to remove bug if index input is nil or blank string 
;(setq index (if (or (= nil index) (= "" index) ) 0 index))--Before 21-09-2018
(setq index (if (or (= nil index) (= "" index) ) 1 index));change 0-1    
(mapcar '(lambda (x)
       (if (= (car x) key);check if current key is equal to inout key
         (if (= index 1)
           (setq index (- index 1)
         x (cons key NewValue));if index zero then add list with new value and lower index

           (setq index (- index 1); lower index
             x     x ;this act as output to lambda function
           )
         )
         x;ouput to lambda
       )
     );Lambda end here

    InputList ; input list to run lambda loop for each element in InputList
)

)


;This function update List by Old Key value.
;[ key ]-> Key Value in associate list
;[ NewValue ]-> New Value to replace
;[ OldKeyValue ]-> Old Value
;[ InputList ]-> Associate List  

(defun UpdateAssocListBykeyValue (key NewValue OldKeyValue InputList / index)
(subst (cons key NewValue) (cons key OldKeyValue)   InputList) 
)

;-------------------------------------Your Code ----------------------------------------------------------------------------;

    (setq a (car (entsel "\nSelect a object: ")))
    (setq b (entget a))
    ;(UpdateAssocListByIndex key NewValue index InputList)
    (setq c (UpdateAssocListByIndex 300 "F200" 4 b));see modification.
    ;(setq c (subst (cons 300 "F200") (assoc 300 b) b))  ; Trying to update and change the Label Property
    (entmod c)
    (entupd a)
    (prompt "\nAttribute entity updated.")
(princ)


)


Hope This helps.

Dinesh Vilas Pawar
  • 493
  • 2
  • 5
  • 18
  • Thanks @Dinesh This works perfectly and I think I understand most of it! :D and changing the index I can control the different DXF entity. Really awesome! – Math Sep 19 '18 at 11:21
  • 1
    You welcome, In this code you may found `Lambda` function is hard to understand, You can found good Tutorial for this function on internet. – Dinesh Vilas Pawar Sep 20 '18 at 08:23
  • 1
    I request you change line `(setq index (if (or (= nil index) (= "" index) ) 0 index))` to `(setq index (if (or (= nil index) (= "" index) ) 1 index))` because it may create problem if index parameter input is `nil` or blank string. once done please comment so I will update it in Answer which can help if anyone try to use this code. – Dinesh Vilas Pawar Sep 21 '18 at 09:37
  • I've change the line as you requested and it works perfectly. So you can easily update your answer. :) – Math Sep 21 '18 at 10:41