ThatGermanFool Posted June 12, 2019 Posted June 12, 2019 Hi Everyone, how can I access the objects inside a given block using Lisp? As an example take this structure: myParentBlock - myChildLine - myChildPolyline - myChildBlock ---- myGrandchildLine ---- myGrandchildCircle - myChildArc What Lisp Routine could I use to Input the Entity "myPArentBlock" and get a reference to all the Children Entities (not the grandchildren)? (For a start, I just Need the Entity names. Any hints? Cheers Seb Quote
Lee Mac Posted June 12, 2019 Posted June 12, 2019 (edited) For primary entities only, use a combination of tblobjname & entnext: (defun blockcomponents ( blk / ent lst ) (if (setq ent (tblobjname "block" blk)) (while (setq ent (entnext ent)) (setq lst (cons ent lst)) ) ) (reverse lst) ) Call the above with a block name argument, e.g.: _$ (blockcomponents "YourBlockName") (<Entity name: 7ffff706950> <Entity name: 7ffff706960> <Entity name: 7ffff706970>) To include nested objects, check for the presence of a block reference (INSERT) entity and include a recursive call, e.g.: (defun blockcomponents ( blk / ent enx lst ) (if (setq ent (tblobjname "block" blk)) (while (setq ent (entnext ent)) (if (= "INSERT" (cdr (assoc 0 (setq enx (entget ent))))) (setq lst (vl-list* (blockcomponents (cdr (assoc 2 enx))) ent lst)) (setq lst (cons ent lst)) ) ) ) (reverse lst) ) The above will return a list of entity names with sublists containing the entity names corresponding to the components of nested block references, e.g.: _$ (blockcomponents "block1") (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) _$ (blockcomponents "block2") (<Entity name: 7ffff706a50> (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>) _$ (blockcomponents "block3") (<Entity name: 7ffff706ad0> (<Entity name: 7ffff706a50> (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>) <Entity name: 7ffff706ae0> <Entity name: 7ffff706af0>) Here, Block1 is nested within Block2 is nested within Block3. If you don't want the nested list structure, use append in place of vl-list*, e.g.: (defun blockcomponents ( blk / ent enx lst ) (if (setq ent (tblobjname "block" blk)) (while (setq ent (entnext ent)) (if (= "INSERT" (cdr (assoc 0 (setq enx (entget ent))))) (setq lst (append (blockcomponents (cdr (assoc 2 enx))) (cons ent lst))) (setq lst (cons ent lst)) ) ) ) (reverse lst) ) This now returns a flat list: _$ (blockcomponents "block1") (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) _$ (blockcomponents "block2") (<Entity name: 7ffff706a50> <Entity name: 7ffff706a00> <Entity name: 7ffff7069f0> <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>) _$ (blockcomponents "block3") (<Entity name: 7ffff706ad0> <Entity name: 7ffff706a70> <Entity name: 7ffff706a60> <Entity name: 7ffff7069f0> <Entity name: 7ffff706a00> <Entity name: 7ffff706a50> <Entity name: 7ffff706ae0> <Entity name: 7ffff706af0>) Edited June 12, 2019 by Lee Mac 3 2 Quote
Recommended Posts
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.