;;;若海提供;qq:363496390
;;;函数名称:ImportObjectFromFile
;;;函数说明:从模板图纸当中导入块定义、线型等对象到当前图纸
;;;参 数:objectNameLst:需要拷贝的对象的lst
;;;参 数:fileName:模板所在全路径
;;;参 数:property:需要拷贝的对象的集合
;;;返 回 值:无
;;;示 例:(ImportObjectFromFile objectNameLst fileName property)
;;;(ImportObjectFromFile '("TG_BC" "TG_DJ") "c:\\11.dwg" 'Linetypes)
;;;(ImportObjectFromFile '("aaa" "bbb" "ccc") "c:\\11.dwg" 'Blocks)
;;;(vla-InsertBlock (vlax-get-property (vla-get-ActiveDocument (vlax-get-acad-object)) (if (= 1 (getvar 'CVPORT)) 'PaperSpace 'ModelSpace)) (vlax-3D-point 0 0 0) "ccc" 1 1 1 0)
(defun ImportObjectFromFile (objectNameLst fileName property / acver collection dbx objlst synbolname)
(setq synbolName
(cond
((equal property 'Blocks)
"BLOCK"
)
((equal property 'Layers)
"LAYER"
)
((equal property 'Linetypes)
"LTYPE"
)
((equal property 'DimStyles)
"DIMSTYLE"
)
)
)
;;获取当前图纸中没有的对象的lst
(setq objectNameLst (vl-remove-if '(lambda(x) (tblsearch synbolName x)) objectNameLst))
;;如果需要拷贝
(if objectNameLst
(progn
;;创建dbx对象
(setq dbx
(vla-GetInterfaceObject
(vlax-get-acad-object)
(if (< (setq acVer (atoi (getvar "ACADVER"))) 16)
"ObjectDBX.AxDbDocument"
(strcat "ObjectDBX.AxDbDocument." (itoa acVer))
)
)
)
(if (vl-catch-all-error-p (vla-open dbx fileName :vlax-true))
(vlax-release-object dbx)
)
;;从模板中获得对象集合
(setq Collection (vlax-get dbx property))
;;将需要拷贝的对象lst转换成模板中存在的vla对象lst
(setq objLst (vl-remove nil (mapcar '(lambda(x) (if (vl-catch-all-error-p (vl-catch-all-apply '(lambda nil (setq lam (vla-Item Collection x)))nil)) nil lam)) objectNameLst)))
;;创建安全数组方式复制
;(vla-copyobjects
; dbx
; (vlax-safearray-fill
; (vlax-make-safearray
; vlax-vbobject
; (cons 0 (1- (length objLst)))
; )
; objLst
; )
; (vlax-get (vla-get-ActiveDocument (vlax-get-acad-object)) property)
;)
;;不通过安全数组方式
(vlax-invoke dbx 'CopyObjects objLst (vlax-get (vla-get-ActiveDocument (vlax-get-acad-object)) property))
(vlax-release-object dbx)
)
)
(princ)
)