If it were me, my solution would be to select a shape, obtain all the shapes in the group that this shape belongs to, and then copy these elements. You need 4 related functions, one group related function; One is the dbx function (which can read dwg file information without opening the graph, and LeeMac has related functions); One is the copy function vla CopyObjects;
I'm sorry, all of my relevant codes are in Chinese. If you have a basic understanding of LISP, you should be able to understand it by studying it yourself.
The following code should meet your needs
;;;========================================================;
;;;取得图元所在的组名 by yjtdkj2021.08.01;
;;;========================================================;
(GetEntGroupName (car(entsel)))
(defun GetEntGroupName (gpe / el lst a g gpnlst)
(setq dic (cdr (assoc -1 (dictsearch (namedobjdict) "acad_group"))))
(setq el (entget gpe))
(if (setq lst (member '(102 . "{ACAD_REACTORS") el))
(while (and (setq lst (cdr lst)) (= 330 (car (setq a (car lst)))))
(if (= "GROUP"
(cdr (assoc 0 (entget (setq g (cdr a)))))
)
(setq grp
(cdadr
(member
(cons 350 g)
(reverse (entget dic))
)
)
)
)
)
)
)
;; Group Entities - Lee Mac;; Creates a Group with a given name containing all entities in the supplied list
;; grp - [str] Group name (use "*" for an anonymous group)
;; lst - [lst] List of entities to add to group
;; sel - [bol] If T, group is selectable
(defun LM:groupentities (grp lst sel / dic enx gde gdx tmp)
(if (setq dic (cdr (assoc -1 (dictsearch (namedobjdict) "acad_group"))))
(if (setq gdx (dictsearch dic grp)
gde (cdr (assoc -1 gdx))
)
(progn
(entmod (append gdx (mapcar '(lambda (x) (cons 340 x)) lst))
)
(foreach ent lst
(setq enx (entget ent)
tmp (member '(102 . "{ACAD_REACTORS") enx)
)
(if tmp
(setq tmp
(vl-list*
(car tmp)
(cons 330 gde)
(cdr tmp)
)
)
(setq tmp
(vl-list*
'(102 . "{ACAD_REACTORS")
(cons 330 gde)
'(102 . "}")
(cdr (member (assoc 5 enx) enx))
)
)
)
(entmod
(append (reverse (member (assoc 5 enx) (reverse enx))) tmp)
)
)
grp
)
(if
(and
(setq gde
(entmakex
(list
'(000 . "GROUP")
'(102 . "{ACAD_REACTORS")
(cons 330 dic)
'(102 . "}")
(cons 330 dic)
'(100 . "AcDbGroup")
(if (wcmatch grp "`*")
'(070 . 1)
'(070 . 0)
)
(if sel
'(071 . 1)
'(071 . 0)
)
)
)
)
(if (wcmatch grp "`*")
(if
(entmod
(append (entget dic) (list '(3 . "*") (cons 350 gde)))
);; thanks vk/rjp
(setq grp
(cdadr
(member
(cons 350 gde)
(reverse (entget dic))
)
)
)
)
(dictadd dic grp gde)
)
)
(LM:groupentities grp lst sel)
)
)
)
)
(defun c:test (/ grp idx lst sel)
(while
(not
(or (wcmatch (setq grp (getstring t "Specify group name: "))
"`*,"
)
(snvalid grp)
)
)
(princ "\nGroup name invalid.")
)
(if (and (/= "" grp) (setq sel (ssget)))
(progn
(repeat (setq idx (sslength sel))
(setq lst (cons (ssname sel (setq idx (1- idx))) lst))
)
(LM:groupentities grp lst t)
)
)
)
(defun gxl-Sel-SS->AX:Array (ss / c r en)
(vl-load-com)
(setq c -1)
(repeat (sslength ss)
(setq en (ssname ss (setq c (1+ c))))
(if (entget en)
(setq r (cons en r))
)
)
(setq r (reverse r))
(vlax-safearray-fill
(vlax-make-safearray
vlax-vbObject
(cons 0 (1- (length r)))
)
(mapcar 'vlax-ename->vla-object r)
)
)
;;;Creat Unname Group
(vla-AppendItems
(vla-add (vla-get-Groups
(vla-get-activedocument (vlax-get-acad-object))
)
"*"
)
(gxl-Sel-SS->AX:Array (ssget))
)
(vl-load-com)
(DEFUN c:GGG (/ E EL N SS)
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
;;清理空组
(C:DelEmptyGroup)
;;显示当有组数量
(princ (strcat "\n 当前有 " (itoa (c:GroupsCount)) " 个组"))
;;显示所有组名
(princ (C:AllGroups))
(_StartUndo *DOC*)
(SETQ E (CAR (ENTSEL "\n &#32452;[&#21019;&#24314;&#25110;&#22686;&#20943;&#25104;&#21592;/&#20998;&#35299;]<&#20998;&#35299;&#25152;&#26377;&#32452;>")))
(if e
(if (_GROUPNAMES e) ;&#26159;&#32452;
(_GroupAddOrDel e)
;;&#36873;&#25321;&#23545;&#35937;&#21019;&#24314;&#32452;
(if (setq ss (LM:ssget "\n &#36873;&#25321;&#23545;&#35937;&#21019;&#24314;&#26080;&#21517;&#32452;" nil))
(progn
(ssadd e ss)
(IF (> (SSLENGTH SS) 1)
(PROGN
(repeat (setq n (sslength ss))
(setq eL (cons (ssname ss (setq n (1- n))) eL))
)
(_CreateGroup (mapcar 'vlax-ename->vla-object eL))
(princ "\n &#25104;&#21151;&#21019;&#24314;&#26080;&#21517;&#32452;")
)
(princ "\n &#21482;&#26377;&#19968;&#20010;&#23545;&#35937;&#65292;&#19981;&#33021;&#21019;&#24314;&#32452;")
)
)
(princ "\n &#27809;&#26377;&#36873;&#25321;&#23545;&#35937;&#65292;&#19981;&#33021;&#21019;&#24314;&#32452;")
)
)
;;&#31354;&#36873;&#26102;, &#20998;&#35299;&#26377;&#30340;&#32452;
(C:DelAllGroups)
)
(_EndUndo *DOC*)
(gc)
(princ "\n &#32452;&#25805;&#20316;&#21629;&#20196; GGG")
(princ)
)
(princ "\n &#32452;&#25805;&#20316;&#21629;&#20196; GGG")
;;1 &#24102;&#25552;&#31034;&#30340;ssget
(defun LM:ssget (msg arg / sel)
(princ msg)
(setvar 'nomutt 1)
(setq sel (vl-catch-all-apply 'ssget arg))
(setvar 'nomutt 0)
(if (not (vl-catch-all-error-p sel))
sel
)
)
;;2 &#21019;&#24314;&#32452;
(defun _CreateGroup (Objlst)
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
(vla-appenditems
(vla-add (vla-get-groups *DOC*) "*")
(vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray
vlax-vbobject
(cons 0 (1- (length objlst)))
)
objlst
)
)
)
)
;;3 &#32479;&#35745;&#32452;&#23450;&#20041;&#20010;&#25968;
(defun c:GroupsCount ()
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
(vla-get-count (vla-get-groups *DOC*))
)
;;4 &#21024;&#38500;&#25152;&#26377;&#32452;&#23450;&#20041;
(defun c:DelAllGroups ()
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
(vlax-for obj (vla-get-groups *DOC*)
(vla-delete obj)
)
)
;;5 &#21024;&#38500;&#31354;&#32452;
(defun c:DelEmptyGroup ()
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
(vlax-for obj (vla-get-groups *DOC*)
(if (< (vla-get-count obj) 2)
(vla-delete obj)
)
)
)
;;7 &#27714;&#25152;&#26377;&#32452;&#21517; -> ("*A1" "*A2" "2")
(defun C:AllGroups (/ LST)
(setq lst (dictsearch (namedobjdict) "ACAD_GROUP"))
(mapcar 'cdr
(vl-remove-if '(lambda (x) (/= (car x) 3)) lst)
)
)
;;8 &#23454;&#20307;&#25152;&#22312;&#32452;&#21517; => ("*A4")
;;(_GROUPNAMES (car(entsel)))
(defun _GROUPNAMES (ENAME / KEY DICT RESULT)
(setq KEY (cons 340 ENAME)
DICT (dictsearch (namedobjdict) "acad_group")
)
(while (setq DICT (member (assoc 3 DICT) DICT))
(if (member KEY (entget (cdadr DICT)))
(setq RESULT (cons (cdar DICT) RESULT))
)
(setq DICT (cddr DICT))
)
(reverse RESULT)
)
;;9 &#20998;&#35299;&#29305;&#23450;&#32452;&#21517;&#30340;&#32452;
;;(_DelOneGroup "*A4")
(mapcar '_DelOneGroup (_GROUPNAMES (car(entsel))))
(defun _DelOneGroup (N)
(or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
(or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
(vlax-for obj (vla-get-groups *DOC*)
(if (= (vla-get-name obj) N)
(vla-delete obj)
)
)
)
;;;;;&#33719;&#21462;&#32452;&#20869;&#25152;&#26377;&#22270;&#20803;&#21015;&#34920;
(defun _groupent (name / lst EnsLst)
(or *groups*
(setq *groups* (vla-get-groups
(vla-get-activedocument (vlax-get-acad-object))
)
)
)
(setq EnsLst (vlax-for X (vla-item *groups* name)
(setq lst (cons (vlax-vla-object->ename X) lst))
)
)
EnsLst
)
(SETQ E1 (car(entsel)))
(SETQ E2 (car(entsel)))
(SETQ LST (LIST E1 E2))
(SETQ GROUPNAMELST(LM:ListsUnion(MAPCAR '_GROUPNAMES LST)))
(_groupent (CAR GROUPNAMELST))
(SETQ ENTLST (CAR(mapcar '_groupent GROUPNAMELST)))
(SETQ I 0)
(REPEAT (LENGTH ENTLST)
(_DelOneGroup (CAR(_GROUPNAMES (NTH I ENTLST))))
(SETQ I (1+ I))
)
(FOREACH X ENTLST
(_DelOneGroup (CAR(_GROUPNAMES X)))
)
(mapcar 'vlax-ename->vla-object ENTLST)
(_CreateGroup (mapcar 'vlax-ename->vla-object ENTLST))
;;(LM:ListsUnion '((1 2 3 4 5) (1 3 5 7 9) (2 4 6 8)))
;;;(1 2 3 4 5 7 9 6 8)
(defun LM:ListsUnion ( l )
( (lambda ( f ) (f (apply 'append l)))
(lambda ( l ) (if l (cons (car l) (f (vl-remove (car l) (cdr l))))))
)
)
;;;;;;(LY:CreateGroup (LIST E1 E2))
(DEFUN LY:CreateGroup (LST )
(SETQ LST (CAR (mapcar '_groupent (LM:ListsUnion(MAPCAR '_GROUPNAMES LST)))))
(FOREACH X LST
(_DelOneGroup (CAR (_GROUPNAMES X)))
)
(_CreateGroup (mapcar 'vlax-ename->vla-object LST))
)
(vl-load-com)
(defun c:Example_CopyObjects()
;; This example creates a Circle object and uses the CopyObjects
;; method to make a copy of the new Circle.
(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
;; Load the ObjectDBX library
(if (= acLibImport nil)
(progn
(vlax-import-type-library :tlb-filename "C:\\Program Files\\Common Files\\Autodesk Shared\\axdb19enu.tlb"
:methods-prefix "acdbm-"
:properties-prefix "acdbp-"
:constants-prefix "acdbc-"
)
(setq acLibImport T)
)
)
;; Create a reference to the ObjectDBX object
(setq acdbObj (vlax-create-object "ObjectDBX.AxDbDocument.19"))
;; Open an external drawing file
(acdbm-open acdbObj (findfile ".\\Sample\\VBA\\Tower.dwg"))
;; Add two circles to the drawing
(setq objCollection (vlax-make-safearray vlax-vbObject (cons 0 (- (vla-get-Count (vla-get-ModelSpace acdbObj)) 1)))
count 0)
;; Copy objects
(vlax-for eachObj (vla-get-ModelSpace acdbObj)
(vlax-safearray-put-element objCollection count eachObj)
(setq count (1+ count))
)
;; Copy object and get back a collection of the new objects (copies)
(setq retObjects (vla-CopyObjects acdbObj objCollection (vla-get-ModelSpace (vla-get-Database doc))))
(vla-ZoomAll acadObj)
(alert "Model space objects copied.")
;; Close the in memory drawing
(vlax-release-object acdbObj)
)