Concatenate text with variable for in list

Concatenate text with variable for in list

Anonymous
Not applicable
4,923 Views
35 Replies
Message 1 of 36

Concatenate text with variable for in list

Anonymous
Not applicable

How can I create a list that concatenates text and an item from a list?

 

(defun LM:changevis (lst1 lst2 / idx lst obj sel vis )
    (setq lst
       '(  ;i know i need to remove this ' so that i can evaluate below
            strcat("\"PANEL SIZE\"  . \" (nth 1 lst2))    ;SET 2nd ITEM OF lst 1 to 2nd item of lst2
            ("BOM1" . (nth 2 lst2))    ;SET 3rd ITEM OF lst 1 to 3rd item of lst2
	    ("BOM2"  . (nth 3 lst2))    ;SET 4th ITEM OF lst 1 to 4th item of lst2

        )
    )

 

Alternatively, if i can loop thru each item in ls1 and create a list ("lst1 item 2" . "lst2 item 2") that would be better. The original code looked like this: 

(defun LM:changevis ( / idx lst obj sel vis )
    (setq lst
       '( 
            ("BOM1" . "ITEM1")
	    ("BOM2"  . "ITEM2")
            ("BOM3" . "ITEM3")
        )
    )

I was hoping to take it and pass 2 lists (lst1 lst2) and rewrite the section of code for concatenating the strings to be the values of the lists instead. I'm sure there's a way to loop this, but i'm more of a vba guy.

0 Likes
Accepted solutions (1)
4,924 Views
35 Replies
Replies (35)
Message 21 of 36

CodeDing
Advisor
Advisor

@Anonymous ,

 

Ok, not gonna lie, this thread is hard to follow with so much going on.. So I took a shot in the dark at what I think you wanted.. see attached Lisp.

BE SURE to update the "fname" variable in the (UDB_readexcel) function...

 

Hope it helps, I am not able to fully test without block names.. (What are your block names btw?)

Best,

~DD

Message 22 of 36

ВeekeeCZ
Consultant
Consultant

Again, we need to have a sample file to test.

Helpless otherwise. 

Message 23 of 36

Anonymous
Not applicable

Thank you both for your help. I do agree this thread could be difficult to follow if not followed from the start, but we are moving along fine. I have gathered some sample data for each piece so anyone who wants to jump in and help can do so hopefully with more ease now.

 

There are 3 pieces to look at. 1) The spreadsheet which has a list of dynamic block names (which are generic and make no sense on purpose) and the visibility state that each block will be set to (also generic on purpose). Please do not fret over the generic nature of the sample data. 2) The drawing which has 3 dynamic blocks with each one having a name that exists in the list in the first column on the excel list. Also, each block has at least 1 state that matches the 2nd list item in the excel sheet. And 3) The lisp which should operate as follows when run on the dwg.

>Open excel

>grab the list

>find the first dynamic block in the drawing

>check the list for a block name that matches using the first column of the list

>once found, set the visibility state of the block in the drawing to what the 2nd column of the list

>if at anytime a block is in a drawing but not found in the list , notify the user

>if at any time a visibility state is found in the list but not in a dynamic block found in the drawing, notify the user. 

 

One change I would like to make eventually is, I will be calling the dynamic block update lisp from a script and would like to pass the directory/excel file name to the lisp instead of hardcoding it. The shname, range1, range2 can be hardcoded.

 

I have attached only included 2 of the files required to do a complete test since both of you guys code is different I'm not sure which lisp to include. Again, thank you guys for your help.

Message 24 of 36

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, here's the update.

 

Made some changes - the most significant is that I call the EXCEL just once. I think that immediate multiple calling is what caused the troubles. It's quite a complex process which takes some time to...

 

And about our stringp issue. Just make sure that your XLS is formatted as it is - all values are formatted as a text

 

HTH

 

 

(vl-load-com)


(defun c:UDB ( / sel idx obj vis lst)
  
  (if (and (setq sel (ssget "_X" (append '((000 . "INSERT")
					   (-04 . "<OR")
					   (002 . "`*U*"))
					 (mapcar '(lambda ( x ) (cons 2 (car x))) lst)
					 '((-004 . "OR>")
					   (410 . "Model")))))
	   (setq lst (:ReadRangeFromXLS "c:\\workbook.xls" "sheet1" "A2:B14"))
	   )
    (repeat (setq idx (sslength sel))
      (if (setq idx (1- idx)
		obj (vlax-ename->vla-object (ssname sel idx))
		vis (cadr (assoc (strcase (LM:blockname obj)) lst))
		)
	(LM:SetVisibilityState obj vis))))
  (princ)
  )


; -------------------------------------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------------------------------------


(defun :ReadRangeFromXLS (fileName sheetName cellName / :ListOfRows xl workbook sheet range cellValue)
  
  (defun :ListOfRows (lvs / itm lst) ; BeekeeCZ
    (if (listp lvs)
      (reverse (foreach itm lvs (setq lst (cons (:ListOfRows itm) lst))))
      (vlax-variant-value lvs)))
  
  (setq xl (vlax-get-or-create-object "Excel.Application"))
  (vla-put-visible xl :vlax-false)
  (vlax-put-property xl 'DisplayAlerts :vlax-false)
  (setq workbook (vl-catch-all-apply 'vla-open (list (vlax-get-property xl "WorkBooks") fileName)))
  (setq sheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property workbook "Sheets") "Item" sheetName)))
  (vlax-invoke-method sheet "Activate")
  (setq range (vlax-get-property (vlax-get-property sheet 'Cells) "Range" cellName))
  (setq cellValue (vlax-variant-value (vlax-get-property range 'Value2)))
  (vl-catch-all-apply 'vlax-invoke-method (list workbook "Close"))
  (vl-catch-all-apply 'vlax-invoke-method (list xl "Quit"))
  (foreach itm (list range sheet workbook xl)
    (or (vlax-object-released-p itm)
	(vlax-release-object itm)))
  
  (if (= 'safearray (type cellValue))
    (:ListOfRows (vlax-safearray->list cellValue)))
  )


;; Block Name  -  Lee Mac
;; Returns the true (effective) name of a supplied block reference

(defun LM:blockname ( obj )
  (if (vlax-property-available-p obj 'effectivename)
    (defun LM:blockname ( obj ) (vla-get-effectivename obj))
    (defun LM:blockname ( obj ) (vla-get-name obj))
    )
  (LM:blockname obj)
  )

;; Set Dynamic Block Visibility State  -  Lee Mac
;; Sets the Visibility Parameter of a Dynamic Block (if present) to a specific value (if allowed)
;; blk - [vla] VLA Dynamic Block Reference object
;; val - [str] Visibility State Parameter value
;; Returns: [str] New value of Visibility Parameter, else nil

(defun LM:SetVisibilityState ( blk val / vis )
  (if
    (and
      (setq vis (LM:getvisibilityparametername blk))
      (member (strcase val) (mapcar 'strcase (LM:getdynpropallowedvalues blk vis)))
      )
    (LM:setdynpropvalue blk vis val)
    )
  )

;; Set Dynamic Block Property Value  -  Lee Mac
;; Modifies the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
;; val - [any] New value for property
;; Returns: [any] New value if successful, else nil

(defun LM:setdynpropvalue ( blk prp val )
  (setq prp (strcase prp))
  (vl-some
    '(lambda ( x )
       (if (= prp (strcase (vla-get-propertyname x)))
	 (progn
	   (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
	   (cond (val) (t))
	   )
	 )
       )
    (vlax-invoke blk 'getdynamicblockproperties)
    )
  )

;; Get Dynamic Block Property Allowed Values  -  Lee Mac
;; Returns the allowed values for a specific Dynamic Block property.
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
;; Returns: [lst] List of allowed values for property, else nil if no restrictions

(defun LM:getdynpropallowedvalues ( blk prp )
  (setq prp (strcase prp))
  (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'allowedvalues)))
	   (vlax-invoke blk 'getdynamicblockproperties)
	   )
  )

;; Get Visibility Parameter Name  -  Lee Mac
;; Returns the name of the Visibility Parameter of a Dynamic Block (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; Returns: [str] Name of Visibility Parameter, else nil

(defun lm:getvisibilityparametername ( blk / vis )
  (if
    (and
      (vlax-property-available-p blk 'effectivename)
      (setq blk
	     (vla-item
	       (vla-get-blocks (vla-get-document blk))
	       (vla-get-effectivename blk)
	       )
	    )
      (= :vlax-true (vla-get-isdynamicblock blk))
      (= :vlax-true (vla-get-hasextensiondictionary blk))
      (setq vis
	     (vl-some
	       '(lambda ( pair )
		  (if
		    (and
		      (= 360 (car pair))
		      (= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair)))))
		      )
		    (cdr pair)
		    )
		  )
	       (dictsearch
		 (vlax-vla-object->ename (vla-getextensiondictionary blk))
		 "ACAD_ENHANCEDBLOCK"
		 )
	       )
	    )
      )
    (cdr (assoc 301 (entget vis)))
    )
  )

(princ (strcat
	 "\n:: UpdateDynamicBlock.lsp"
	 "\n:: Type \"UDB\" to Invoke"))

 

 
0 Likes
Message 25 of 36

Anonymous
Not applicable

ok, i copied the code and made a lisp from it. WHen I run it I get this:

DontBelieveMeJustWatch_0-1603299016143.png

0 Likes
Message 26 of 36

john.uhden
Mentor
Mentor

I think it's simpler than that...

Command: (setq x '(1 2 3) y '("A" "B" "C"))
("A" "B" "C")

Command: (mapcar 'cons x y)
((1 . "A") (2 . "B") (3 . "C"))

 

Oops, I see that @ВeekeeCZ (formerly @BeekeeCZ) beat me to it (as usual).  Good girl, but I don't like the name change.  But there's no wonder; I think she's like 8 hours ahead of EST.  More likely though is that I am at least 8 hours tardy.  Maybe that explains why no one was at the office today when I arrived.  Does the sun rise in the East or the West?  Does the sun set in the morning or the evening?

John F. Uhden

0 Likes
Message 27 of 36

ВeekeeCZ
Consultant
Consultant

@john.uhden wrote:

...  But there's no wonder; I think she's like 8 hours ahead of EST.  More likely though is that I am at least 8 hours tardy.  Maybe that explains why no one was at the office today when I arrived.  Does the sun rise in the East or the West?  Does the sun set in the morning or the evening?


 

You definitely deserve to be left undisturbed in your thoughts.

0 Likes
Message 28 of 36

john.uhden
Mentor
Mentor
Seriously LOL!

John F. Uhden

0 Likes
Message 29 of 36

Anonymous
Not applicable

Do you have any idea why I'm getting the error? I feel like this was so close to working before.

0 Likes
Message 30 of 36

john.uhden
Mentor
Mentor

Apparently you caught an error...

(setq workbook (vl-catch-all-apply 'vla-open (list (vlax-get-property xl "WorkBooks") fileName)))
;; If workbook returns an error, you should add...
(if (vl-catch-all-error-p workbook)
   (progn
      (princ (vl-catch-all-error-message workbook))
      (exit)
   )
   (setq sheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property workbook "Sheets") "Item" sheetName)))
)

There's something way wrong in your  'vla-open construct.   I've never seen it done that way.  I think that vla-open is used only for opening a DWG.

John F. Uhden

Message 31 of 36

Anonymous
Not applicable

The code was reading excel and performing the action on a single dynamic block earlier in the discussion. @ВeekeeCZ  made some adjustments with good intent and I'm not sure what happened to the reading portion. I was sort of following the original code, but it's kind of over my head since the rewrite. @ВeekeeCZ can you help?

0 Likes
Message 32 of 36

Anonymous
Not applicable

Is there anything specific i need to do to continue receiving help on this post?

0 Likes
Message 33 of 36

pbejse
Mentor
Mentor

@Anonymous wrote:

Is there anything specific i need to do to continue receiving help on this post?


Yes, its callled patience 🙂 

 

0 Likes
Message 34 of 36

Anonymous
Not applicable

I was able to get the error to go away. Thanks for all of your help.

Message 35 of 36

john.uhden
Mentor
Mentor
I love hearing that people have helped themselves.

John F. Uhden

0 Likes
Message 36 of 36

ВeekeeCZ
Consultant
Consultant

Glad to help.

0 Likes