Jump to content

Getstring & Strcat


leong1221

Recommended Posts

Hi,

 

I'm trying to write a lisp to help me do the elevation leader & material code easier.

 

But seems like I miss understand some function.

 

I want to setq the Material Code by only input 2 digit, for example, "1" + "3" should return WC03. But right now !Material return 1, !Code return 3..... and !MaterialCode return MaterialCode......:ouch:

 

 

(setq Material (getstring "\nSpecify material [1_WC/2_WD/3_ST/]: "))

(setq Code (getstring "\nSpecify material [1_01/2_02/3_03/4_04]: "))

(setq MaterialCode (strcat "Material" "Num"))

Link to comment
Share on other sites

Hey Guys,

 

I just help myself out, i should use getkword and it should look like below

 

(setq Mat1 (getkword "\n [bL/CP/GL/FA/FB/ML/P/PL/SP/ST/TL/WC/WD]: "))

(initget "01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20")

(setq Num1 (getkword "\n [01/02/03/04/05/06/07/08/09/10/11/12/13/14/15/16/17/18/19/20]: "))

(setq Mat1Num1 (strcat Mat1 Num1))

 

And then get what i want

But to make it more friendly, how do type 1=01 without the clarify window asking which number has1 ?

Link to comment
Share on other sites

hi please use code tag

 

And then get what i want

But to make it more friendly, how do type 1=01 without the clarify window asking which number has1 ?

 

This code with code tags


(defun [color="blue"]matNum[/color] (/ l i s mat1 Num1)
 (setq	i 1
l '("BL" "CP" "GL" "FA"	"FB" "ML" "P" "PL" "SP"	"ST" "TL" "WC"
    "WD")
 )
 (repeat (length l)
   (setq s (cons (strcat (itoa i) "_" (nth (1- i) l) " / ") s))
   (setq i (1+ i))
 )

 (initget 7)
 (if
   (and
     (setq i (getint (strcat "\nIndex number ["
		      (apply 'strcat (reverse s))
		      "]? : "
	      )
      )
     )
     (<= i (length l))

     (setq mat1 (nth (1- i) l))


     (progn
(initget 7)
(setq i (getint "Enter number 1~20 ? : "))
(setq Num1 (itoa i))
(<= i 20)
     )
   )

 (princ (strcat Mat1
	 (if (< i 10)
	   (strcat "0" Num1)
	   Num1
	 )
 )
 )
 ([color="blue"]prompt[/color] "\nInvalid Input.. ")
)
[color="green"];(princ) ; removed[/color]
)

Edited by hanhphuc
Link to comment
Share on other sites

Ths may be useful we want D01-d09 then D10+ taken from some code.

 

; if less than 10
   (if (< (car dwgnum) 10.0) 
     (setq newstr2 (strcat dwgname "-D0"  (rtos sheetnum 2 0)))
     (setq newstr2 (strcat dwgname "-D"  (rtos sheetnum 2 0)))
   )

Link to comment
Share on other sites

hi please use code tag

 

 

 

This code with code tags

(defun c:test (/ l i s mat1 Num1)
 (setq	i 1
l '("BL" "CP" "GL" "FA"	"FB" "ML" "P" "PL" "SP"	"ST" "TL" "WC"
    "WD")
 )
 (repeat (length l)
   (setq s (cons (strcat (itoa i) "_" (nth (1- i) l) " / ") s))
   (setq i (1+ i))
 )

 (initget 7)
 (if
   (and
     (setq i (getint (strcat "\nIndex number ["
		      (apply 'strcat (reverse s))
		      "]? : "
	      )
      )
     )
     (<= i (length l))

     (setq mat1 (nth (1- i) l))


     (progn
(initget 7)
(setq i (getint "Enter number 1~20 ? : "))
(setq Num1 (itoa i))
(<= i 20)
     )
   )

 (princ (strcat Mat1
	 (if (< i 10)
	   (strcat "0" Num1)
	   Num1
	 )
 )
 )
 (princ "\nInvalid Input.. ")
)

(princ)

)

 

The reason I setq Mat1 and Num1 is to help -insert an attribute block in next step and to rename the block according to what just input, for example by running your lisp and input "1" "1" then i got an attribute block named BL01.

 

Any hint how to do it? Many thanks:)

Link to comment
Share on other sites

The reason I setq Mat1 and Num1 is to help -insert an attribute block in next step and to rename the block according to what just input, for example by running your lisp and input "1" "1" then i got an attribute block named BL01.

 

Any hint how to do it? Many thanks:)

 

I was think the command Gatte, and then I write it to my lisp

(command "gatte") and return GATTE Unknown command "GATTE" :unsure:

Link to comment
Share on other sites

GATTE is a Global Attribute Edit function.

 

If you want to rename the block, i would just 'insert' the regular block, and use a RENAME function on the ENTLAST?

Link to comment
Share on other sites

GATTE is a Global Attribute Edit function.

 

If you want to rename the block, i would just 'insert' the regular block, and use a RENAME function on the ENTLAST?

 

Hi, i attached an image to clarify me idea:)

 

a123.png

 

Thanks

Leo

Link to comment
Share on other sites

I found below lisp by SMadsen which is what I close except it changes tag in all block

 

Credit: SMadsen

https://www.theswamp.org/index.php?topic=5917.0

 

(defun changeAttribValue (ent atttag oldval newval / entl)

(while (and ent (/= "SEQEND" (cdr (assoc 0 (setq entl (entget ent))))))

(and (= atttag (cdr (assoc 2 entl)))

(= oldval (cdr (assoc 1 entl))) ;

(entmod (subst (cons 1 newval) (assoc 1 entl) entl))

(entupd ent)

(mapcar 'princ (list "\n" oldval " -> " newval))

)

(setq ent (entnext ent))

)

)

 

(defun C:CHATTRIB (/ ss a attag bname oldval newval)

(and (/= "" (setq bname (getstring "\nBlock name: ")))

(/= "" (setq attag (getstring T "\nTag: ")))

(/= "" (setq oldval (getstring T "\nOld value: ")))

(/= "" (setq newval (getstring T "\nNew value: ")))

(setq a 0

ss (ssget "X" (list '(0 . "INSERT") '(66 . 1) (cons 2 bname)))

)

(repeat (sslength ss)

(changeAttribValue (ssname ss a) attag oldval newval)

(setq a (1+ a))

)

)

)

Link to comment
Share on other sites

Do you want to set the values before or after you place the block?

If you want to set the values before you place the blocks the code could look like...

 

(command "_insert" "<BLOCKNAME>" l "1" "1" "" <ATTRIBUTE1> <ATTRIBUTE2>)

 

And you could define the attributes before you place the block.

set attribute1 with your Kword method.

set attribute with a cond function, wich evaluates the first attribute...

 

If you want to be able to change the attributes when the block is already placed, you might be able to use Lee Mac's functions below...

;; Set Attribute Value  -  Lee Mac
;; Sets the value of the first attribute with the given tag found within the block, if present.
;; blk - [ent] Block (Insert) Entity Name
;; tag - [str] Attribute TagString
;; val - [str] Attribute Value
;; Returns: [str] Attribute value if successful, else nil.

(defun LM:setattributevalue ( blk tag val / enx )
   (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
       (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
           (if (entmod (subst (cons 1 val) (assoc 1 (reverse enx)) enx))
               (progn
                   (entupd blk)
                   val
               )
           )
           (LM:setattributevalue blk tag val)
       )
   )
)

 

First click the block,

Ask KWORD for first value

COND the KWORD for the second value.

Use above function twice to set the values?

Link to comment
Share on other sites

my suggestion if you have many options to select, activate the dynamic input

 

(setvar 'dynmode 1)

then you are able to click the value (acts like a radio button in dcl)

but remember restore it at the end of the code.

 

if you insert more than 1 block, just call the defun

see update in post#3

 

example:


(if
(and
(setq Mat1Num1 ([color="blue"]MatNum[/color]))
(setq Mat2Num2 ([color="blue"]MatNum[/color]))
(setq p1 (getpoint "\nSpecify point1 : "))
(setq p2 (getpoint "\nSpecify point2 : "))
     )

(progn

(command "-insert" Mat1Num1 p1 1 1 0 ) [color="green"]; do something[/color]
(command "-insert" Mat2Num2 p2 1 1 0 ) [color="green"]; do something else[/color]


)

)

 

 

p/s: please don't ignore the moderator's advice to edit your previous post with code tags

Link to comment
Share on other sites

Hi Hanhphnc,

 

Thanks for helping me to get this far, I have merge together and the lisp seems almost working .

 

Below lisp is my attempt of using command "-Attedit" to change the attribute block tags value, it works fine when only 1 and the last tag is change by using "L" to select the tag.

 

However it fails when I specify the block names & tag value:(

 

Any idea? Is it because I cannot use "last" after specifying block name?

 

 (command "-attedit" "y" "LT_FIN" "TAG1" "MATERIALCODE" [color="darkorange"]"L"[/color] "value" "Replace" MAT1NUM1 "") ; <--fail
 (command "-attedit" "y" "LT_FIN" "TAG2" "MATERIAL" [color="darkorange"]"L"[/color] "value" "Replace" MAT1NUM1 "") ; <--fail
 (command "-attedit" "y" "" "" "" "L" "value" "Replace" MAT1NUM1 "") ; works, but it only allows to change the last tag

Edited by leong1221
Clarify
Link to comment
Share on other sites

 (command "-attedit" "y" "LT_FIN" "TAG1" "MATERIALCODE" [color="darkorange"]"L"[/color] "value" "Replace" MAT1NUM1 "") ; <--fail
 (command "-attedit" "y" "LT_FIN" "TAG2" "MATERIAL" [color="darkorange"]"L"[/color] "value" "Replace" MAT1NUM1 "") ; <--fail
 (command "-attedit" "y" "" "" "" "L" "value" "Replace" MAT1NUM1 "") ; works, but it only allows to change the last tag

 

Did you try Lee's function by supplying valid arguments? read the header of his code

;; Set Attribute Value - Lee Mac

;; Sets the value of the first attribute with the given tag found within the block, if present.

;; blk - [ent] Block (Insert) Entity Name

;; tag - [str] Attribute TagString

;; val - [str] Attribute Value

;; Returns: [str] Attribute value if successful, else nil.

(if 
(setq en (car(entsel "\nSelect an attributed block entity.. "))) 
([color="blue"]LM:setattributevalue[/color] en "TAG1" [color="red"]MAT1NUM1[/color])[color="green"];whatever TAGs associated [/color]
)

 

;This is an activeX method to populate the attributes list


(defun c:test2 (/ lst s obj)

[color="green"];we assume you have MAT1NUM1 global variable or set default "ML01"[/color]
(setq [color="red"]MAT1NUM1[/color] (cond (MAT1NUM1)("ML01")))

 (prompt "\nSelect an attributed block entity.. ")
 (if (and (setq s (ssget "_+.:S:E:L" '((0 . "INSERT") (66 . 1))))
   (setq obj (vlax-ename->vla-object (ssname s 0)))
   )
   (progn
	[color="green"]
	;documented [/color]
;(setq lst (vlax-safearray->list(variant-value([color="blue"]vla-getattributes[/color] obj)))) [color="green"]; conversion is required[/color]
     
	[color="green"];undocumented [/color]
     (setq lst (vlax-invoke obj '[color="blue"]getattributes[/color])) [color="green"]; commonly practiced by Lee Mac, Tharwat etc.. credits to both [/color]
     (mapcar ''((x o) (vla-put-TextString o x)) ;or vlax-put
      (list [color="red"][color="red"]MATNUM1[/color][/color] [color="magenta"]"SECOND" "" "FOURTH" "ETC"[/color]) [color="green"]; <-- This is just an example, you can edit any replacement TextString associated to attribute positions [/color]
      lst
      )
     )
   (princ "\nInvalid selection")
   )
 (princ)
 )
(vl-load-com)

Link to comment
Share on other sites

  • 2 weeks later...

Hi hanhphuc, Lee and everyone,

 

Thanks very much for helping.

 

Just amended a little bit the lisp from you guys to how i need it, please bear with me some of them are using silly methods.

 

I'd love to attache the files if it may be of use to anybody.:)

 

P.S. Any further advises are very welcome

 

MaterialCodeTest2.dwg

 

(defun C:QW1 (/ i s mat1 Num1)

 (defun LM:setattributevalue ( blk tag val / enx )
   (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
       (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
           (if (entmod (subst (cons 1 val) (assoc 1 (reverse enx)) enx))
               (progn
                   (entupd blk)
                   val
               )
           )
           (LM:setattributevalue blk tag val)
       )
   )
)

 (defun LM:setattributevalue ( blk tag val / end enx )
   (while
       (and
           (null end)
           (setq blk (entnext blk))
           (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))
       )
       (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
           (if (entmod (subst (cons 1 val) (assoc 1 (reverse enx)) enx))
               (progn
                   (entupd blk)
                   (setq end val)
               )
           )
       )
   )
)

(defun dtr ( deg ) (* pi (/ deg 180.0)))
(defun rtd (a)(/ (* a 180.0) pi))
(setq dist 300)
(setq Area "BR")
(setq yline 21900)
(setq ylineDist 350)
(setq ML02 "H50MM RECESSED S/S SKIRTING")

 (setq cmde (getvar "cmdecho"))
 (setq osmde (getvar "osmode"))
 (setq oldlayer (getvar "CLAYER"))
 (setq dynmde (getvar "Dynmode"))
 (setvar 'cmdecho 0)
 (setvar 'osmode 0)
 (setvar 'dynmode 1)
 
(defun matNum (/ l i s mat1 Num1)
 (setq	i 1
l '("BL" "CP" "GL" "FA"	"FB" "ML" "P" "PL" "SP"	"ST" "TL" "WC"
    "WD")
 )
 (repeat (length l)
   (setq s (cons (strcat (itoa i) "_" (nth (1- i) l) " / ") s))
   (setq i (1+ i))
 )

 (initget 7)
 (if
   (and
     (setq i (getint (strcat "\nIndex number ["
		      (apply 'strcat (reverse s))
		      "]? : "
	      )
      )
     )
     (<= i (length l))

     (setq mat1 (nth (1- i) l))


     (progn
(initget 7)
(setq i (getint "Enter number 1~20 ? : "))
(setq Num1 (itoa i))
(<= i 20)
     )
   )

 (princ (strcat Mat1
	 (if (< i 10)
	   (strcat "0" Num1)
	   Num1
	 )
 )
 )
 (prompt "\nInvalid Input.. ")
)
)

 (setq pt1 (getpoint "\nEnter first point:"))
 (setq pt2 (getpoint "\nEnter sceond point:"))
 (setvar "osmode" osmde)
 (setq pt1B (car pt1))
 (setq pt1C (list pt1B yline))
 (setq pt1D (list (+ (car pt1c) (* '2 dist)) yline))

 (setq pt2B (car pt2))
 (setq pt2C (list pt2B (+ yline ylineDist)))
 (setq pt2D (list (+ (car pt1c) (* '2 dist)) (+ yline ylineDist)))

 (setvar "osmode" 0)
 (Command "-layer" "make" "LEGEND" "c" 4 "" "")
 
(setq Mat1Num1 (MatNum))
(setq Mat2Num2 (MatNum))

   (setq M1 (substr Mat1Num1 1 2))
   (cond
   ((= M1 "BL")(setq M1 "BLIND")))
   (cond
   ((= M1 "CP")(setq M1 "CARPET")))
   (cond
   ((= M1 "GL")(setq M1 "GLASS")))
   (cond
   ((= M1 "FA")(setq M1 "FABRIC")))
   (cond
   ((= M1 "FB")(setq M1 "FABRIC")))
   (cond
   ((= M1 "ML")(setq M1 "METAL")))
   (cond
   ((= M1 "P")(setq M1 "PAINT")))
   (cond
   ((= M1 "PL")(setq M1 "PLASTIC LAMINATE")))
   (cond
   ((= M1 "SP")(setq M1 "SPECIAL FINISH")))
   (cond
   ((= M1 "ST")(setq M1 "STONE")))
   (cond
   ((= M1 "TL")(setq M1 "TILE")))
   (cond
   ((= M1 "WC")(setq M1 "WALL COVERING")))
   (cond
   ((= M1 "WD")(setq M1 "WALL PAPER")))
   (cond
     ((= Mat1Num1 "ML02")(setq M1 ML02)))

 (command "Qleader" pt1 pt1c pt1d "LT_FIN" pt1d "" "" "")
 (command "-attedit" "y" "" "" "" "L" "value" "Replace" Mat1Num1 "")
(setq en (entlast))
(LM:setattributevalue en "TAG1" M1);whatever TAGs associated
(LM:setattributevalue en "TAG2" Mat1Num1);whatever TAGs associated

   (command "Qleader" pt2 pt2c pt2d "LT_FIN" pt2d "" "" "")
   (command "-attedit" "y" "" "" "" "L" "value" "Replace" MAT2NUM2 "")

 (setq M2 (substr mat2num2 1 2))
   (cond
   ((= M2 "BL")(setq M2 "BLIND")))
   (cond
   ((= M2 "CP")(setq M2 "CARPET")))
   (cond
   ((= M2 "GL")(setq M2 "GLASS")))
   (cond
   ((= M2 "FA")(setq M2 "FABRIC")))
   (cond
   ((= M2 "FB")(setq M2 "FABRIC")))
   (cond
   ((= M2 "ML")(setq M2 "METAL")))
   (cond
   ((= M2 "P")(setq M2 "PAINT")))
   (cond
   ((= M2 "PL")(setq M2 "PLASTIC LAMINATE")))
   (cond
   ((= M2 "SP")(setq M2 "SPECIAL FINISH")))
   (cond
   ((= M2 "ST")(setq M2 "STONE")))
   (cond
   ((= M2 "TL")(setq M2 "TILE")))
   (cond
   ((= M2 "WC")(setq M2 "WALL COVERING")))
   (cond
   ((= M2 "WD")(setq M2 "WALL PAPER")))
     (cond
     ((= Mat2Num2 "ML02")(setq M2 ML02)))

 
 (if 
(setq en (entlast))
(LM:setattributevalue en "TAG1" M2)
(LM:setattributevalue en "TAG2" MAT2NUM2)
)

(princ)
 
(vl-load-com)
 
 (setvar 'cmdecho cmde)
 (setvar 'osmode osmde)
 (setvar 'dynmode dynmde)
 (setvar 'CLAYER oldlayer)
 (princ)
 )

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