
;;;最后编辑2020.2.27 修改内容支持匿名属性块，动态属性块。

; 本程序仅提供作为应用上的参考, 而未声明或隐含任何保证; 对于任何特殊用途之适
; 应性, 以及商业销售所隐含作出的保证, 在此一概予以否认，作者尽力将本程序做得完善，但不会因本软件的错失而造成的损失承担任何责任。
; 你可以自由拷贝与复制、修改本程序用于非商业目的。
; 程序编写: LPACMQ  2019.7.7

(defun c:mini_bat_attedit ( )
	(Cq-GetFrameAttributes)
)

;;子程序
;匹配属性块信息
(defun Cq-GetFrameAttributes ( / old_cmd sel bln tags textstr bl ktitle Text_lst Check_lst Texts
									Checks dcl_Check dcl_Text n i rows dcl_check_text dcl_id Dialog_Return keys key n ss
									cq-putattributes  bat_attblk_load Dcl_File Action_bat_attblk_Keys Write_Dcl_bat_attblk
									mj:puttagtextstringbyref mj:changeattributes Get_bat_attblk_Data
							)


;;子程序
;;;勾选属性修改
(defun cq-putattributes ( ss / en Check )
	(setq ss (mai_ssget_2_list ss))
	(if (equal "1" all_sel)
		(foreach en ss
			(mj:changeattributes
				(cons
					(vlax-ename->vla-object en)
					(mapcar
						'(lambda(x y) (list (strcase x) (eval (read y))))
						(reverse tags)
						text_lst
					)
				)
			)
		);for 全选

		(foreach en ss
			(foreach Check (mapcar 'list Check_lst (reverse Tags) Text_lst)
				(if (= "1" (eval (read (car Check))))
					(MJ:PutTagTextStringByRef (vlax-ename->vla-object en) (cadr Check) (eval (read (caddr Check))))
				)
			)
		);for	勾选
	)
	(princ)
); defun


(defun bat_attblk_load ( / n )
	(vl-load-com)
	;;设置对话框位置
	(or #dlg_pnt20180503 (setq #dlg_pnt20180503 '(-1 -1)))
	(setq dcl_id (load_dialog (setq Dcl_File (Write_Dcl_bat_attblk))));对话框加载
	(vl-file-delete Dcl_File);加载后删除DCL文件
	(setq Dialog_Return 2)
	(while (> Dialog_Return 1) ;循环控制对话框是否结束
		(new_dialog "属性块" dcl_id "" #dlg_pnt20180503);建立窗体
;-->-->-对话框初始化->-->--
		(setq keys (append checks texts '("check0" "command1" "command2" "command3" "accept" "cancel"))) ;列表全部控件名称
		(foreach key keys  ;全部控件的初始化1
			(if (setq n (eval (read (strcat key "_bak20180503"))))
				(set_tile key n)
			) ;控件内容
			(action_tile key "(Action_bat_attblk_Keys $key $value)");点击动作
		)

;-------设置文本框状态------
		(foreach n (mapcar 'list checks texts)
			(mode_tile (cadr n) (if (equal (eval (read (strcat (car n) "_bak20180503"))) "0") 1 0))
		)

;--<--<-对话框初始化完成-<--<--
		(setq Dialog_Return (start_dialog));开启对话框(用户可见)

;-->-->-LPACMQ加入按键功能>>>>>>>
		(cond
			(	(= Dialog_Return 3)
				(C:mini_bat_attedit)
				(unload_dialog dcl_id)
			)

			(	(= Dialog_Return 4)
				(princ "\n★修改块参照属性")
				(if (= (substr bln 1 1) "*") (setq bln (strcat "`" bln)));;;by lpacmq 2020.2.26 修改
				(Cq-PutAttributes
					(setq ss (ssget (list '(0 . "INSERT")'(-4 . "<OR") (cons 2 inputBN) (cons 2 bln) (cons 2 "*FRAME*") '(-4 . "OR>"))))
				)
			)
		);cond
;--<--<-LPACMQ按键功能完成<<<<<<<

	);While
	(unload_dialog dcl_id);退出时卸载对话框
	(princ);防止函数回显
)

(defun Action_bat_attblk_Keys (key value / n ) ;全部控件的点击动作触发

	(eval
		(append
			'(cond
				(	(= key "accept") ;{确认按钮}
					(Get_bat_attblk_Data)
					;;保存对话框位置坐标 -by LPACMQ 2015-6-8
					(setq #dlg_pnt20180503 (done_dialog 1));对话框退出返回主函数 传递给Dialog_Return值为1
				)
				
				(	(= key "cancel") ;{取消按钮}
					(done_dialog 0);对话框退出返回主函数 传递给Dialog_Return值为0
				)
			)
			;参考
			;	((= key "check1")(if (equal "0" $value )(progn (mode_tile "text1" 1) (set_tile "check0" "0")) (mode_tile "text1" 0)))
			(mapcar
				'(lambda(a b)
					(read
						(strcat
							"((= key "
							(vl-prin1-to-string(eval a))
							")(if (equal \"0\" $value)(progn(mode_tile "
							(vl-prin1-to-string(eval b))
							" 1)(set_tile \"check0\" \"0\"))(mode_tile "
							(vl-prin1-to-string(eval b)) " 0)))"
						)
					)
				)
				checks texts
			)
	
			'(
				(	(= key "check0") ; {"全选"} (多选按钮)
					(setq i $value)
					(foreach n checks (set_tile n i))
					(setq i (if (equal "1" i) 0 1))
					(foreach n texts (mode_tile n i))
				)
				
				(	(= key "command3") ; {"默认"} (按钮)
					;;先清零
					(set_tile "check0" "0")
					(foreach n checks (set_tile n "0"))
					(foreach n texts (mode_tile n 1))
					;;默认配置
					(foreach n (list "text1" "text2" "text3" "text4" "text5" "text8" "text9" "text10" "text12") (mode_tile n 0))
					(foreach n (list "check1" "check2" "check3" "check4" "check5" "check8" "check9" "check10" "check12") (set_tile n "1"))
					(Get_bat_attblk_Data)
				)
				
				(	(= key "command1") ; {"匹配"} (按钮)
					(Get_bat_attblk_Data)
					(done_dialog 3)
				)

				(	(= key "command2") ; {"修改"} (按钮)
					(setq all_sel (get_tile "check0"))
					(setq inputBN (get_tile "replaceBN"))
					(Get_bat_attblk_Data)
					(done_dialog 4)
				)
			)
		);Append
	);eval

)



(defun Get_bat_attblk_Data ( / key ) ;临时生成Dcl文件 返回文件名
	(foreach key keys
		(set (read (strcat key "_bak20180503")) (get_tile key));每个控件都赋给一个变量 用于下次开启初始化
	)
)

(defun Write_Dcl_bat_attblk ( / )
	(setq Dcl_File (vl-filename-mktemp nil nil ".Dcl"))
	(setq file (open Dcl_File "w"))
	(foreach str
		(append
			(list
				"属性块:dialog{label = \" 属性块编辑工具v1.1  by LPACMQ  \";"
				"	:row{"
				"		:boxed_column{"
							(strcat "label = \"块名：" title "\" ;")
							(strcat ":edit_box { key = \"replaceBN\" ; label = \"目标块名\" ; value = \"" title "\" ;}")
				"			:row{"
				"				:column{"
			)
			(apply 'append dcl_check_text)
			(list
				"					}"
				"				:column{"
				"					:toggle{key = \"check0\" ;label = \"全选\" ;width = 0.5 ;height = 0.5 ;}"
				"					:button{key = \"command3\" ;label = \"默认\" ;width = 0.5 ; height = 2.5 ;}"
				"					:button{key = \"command1\" ;label = \"匹配\" ;width = 0.5 ; height = 5 ;}"
				"					:button{key = \"command2\" ;label = \"修改\" ;width = 0.5 ;height = 10 ;}"
				"					}"
				"				}"
				"			}"
				"		}"
				"	ok_cancel;}"
			)
		);append
		(write-line str file)
	)
	(close file)
	Dcl_File
)

;;30.2 [功能] 更改选定块的指定属性
;; (MJ:PutTagTextStringByRef (*En2Obj* (car (entsel))) "设计" "new value")
(defun mj:puttagtextstringbyref	(br tagname textstring / atts tag)
	(and
		(= (vla-get-hasattributes br) :vlax-true)
		(safearray-value
			(setq atts (vlax-variant-value (vla-getattributes br)))
		)
		(foreach tag (vlax-safearray->list atts)
			(if (= (strcase tagname) (strcase (vla-get-tagstring tag)))
				(vla-put-textstring tag textstring)
			)
		)
	)
	(vla-update br)
)

;;30.3 [功能] 更改块多个属性
;;(setq blk (car (entsel)))
;;(MJ:ChangeAttributes (list blk (list "设计" "AA")(list "名称" "BB")))
(defun mj:changeattributes ( lst / atts blk)
	(setq
		blk (car lst)
		lst (cdr lst)
	)
	(if (= (vla-get-hasattributes blk) :vlax-true) ;如果有属性
		(progn
			(mapcar
				'(lambda (x / y )
					(and
						(setq y (assoc (strcase (vla-get-tagstring x)) lst))
						(vla-put-textstring x (cadr y))
					)
				)
				(vlax-safearray->list (vlax-variant-value (vla-getattributes blk)))
			)
			(vla-update blk)
		)
	)
)

	(mai_start nil)
	(or (setq sel (car (mai_entsel nil "选取批量修改块属性的源属性块:<退出>" "没有选择属性块." '((0 . "insert") (66 . 1)) nil)))
		(exit)
	)
	(setq
		bln (mai_dxf 2 sel)
		blk (vlax-ename->vla-object sel)
		title (mai_block_name blk)
	)
	(if (= (vla-get-hasattributes blk) :vlax-true);判断是否有属性
		(foreach n (vlax-safearray->list (vlax-variant-value (vla-getattributes blk)));属性集合
			(setq
				tags (cons (vla-get-tagstring n) tags)
				textstr (cons (vla-get-textstring n) textstr)
			)
		)
	)
	(setq i (length Tags))
	(repeat i
		(setq
			n (itoa i)
			Text_lst (cons (strcat "text" n "_bak20180503") Text_lst)
			Check_lst (cons (strcat "check" n "_bak20180503") Check_lst)
			Texts (cons (strcat "text" n) Texts)
			Checks (cons (strcat "check" n) Checks)
			i (1- i)
		)
	)
	(mapcar '(lambda(a b) (set (read a) b)) text_lst (reverse textstr))

;;--------初始化check#去nil---------------
	(foreach n Check_lst (or (eval (read n)) (set (read n) "0")))
	(setq
		i (length Tags)
		rows (cond ( (< 60 i ) 30) (15))
	)

	(foreach n Tags
		(if (= rows (gcd rows i))
			(setq
				dcl_check_text (vl-list* (append dcl_check (list "}:column{") dcl_text) (list "}:column{") dcl_check_text)
				dcl_check nil
				dcl_text nil
			)
		)
		(setq
			dcl_check (cons (strcat ":toggle{key = \"check" (itoa i) "\";label = \" " n "：\";height=0.5;}") dcl_check)
			dcl_text (cons (strcat ":edit_box{key=\"text" (itoa i) "\";width=20;height=0.5;}") dcl_text)
			i (1- i)
		)
	)
	(setq dcl_check_text (vl-list* (append dcl_check (list "}:column{") dcl_text) (list "}:column{") dcl_check_text))
	(bat_attblk_load)
	(mai_end)
)
