Code Red > .NET
Objetcs inside a block
latour_g:
Hi,
Is there a way to select a block and change the properties of the objetcs inside it such as linetype, layer ?
The only way I know to access objetcs is by exploding the block but I don't wawnt to explode the block.
I have search on the net but it doesn't seem to be an easy task...
Thank for your help !
dgorsman:
Think of blocks like XREFs, and XREFs like blocks. To modify a block (fully "block reference"), modify the definition.
latour_g:
Thanks, I've just remember I already done that by seeking AttributeDefinition in all entities of a block. I'm okay then !
kpblc:
--- Code - Auto/Visual Lisp: ---(defun norm-blocks (bit / adoc *error* fun_conv-vla-to-list fun_layer-save fun_layer-restore lst_layer fun_get-ent-name fun_property-set ) ;|http://forum.dwg.ru/showthread.php?t=21492* Changes properties of eninties inside blocks* Call parameters: bit summ of bits: 1 ; change layer to "0" 2 ; Linetype to ByBlock 4 ; Linewegith to ByBlock 8 ; Color to ByBlock 16 ; Line scale to 1 nil or less 1 - exit* Call samples:(norm-blocks 1) ; change layer of all entities to "0". Do not change other properties(norm-blocks 7) ; change layer of all entities to "0", linetype to ByBlock, lineweight to ByBlock|; (defun *error* (msg) (fun_layer-restore (lst_layer)) (vla-regen adoc acallviewports) (vla-endundomark adoc) (princ) ) ;_ end of defun (defun fun_get-attr (blk-ref) (append (fun_conv-vla-to-list (vla-getattributes blk-ref)) (fun_conv-vla-to-list (vla-getconstantattributes blk-ref)) ) ;_ end of append ) ;_ end of defun (defun fun_conv-vla-to-list (value / res) (cond ((= (type value) 'variant) (setq res (fun_conv-vla-to-list (vlax-variant-value value))) ) ((and (= (type value) 'safearray) (>= (vlax-safearray-get-u-bound value 1) 0) ) ;_ end of and (setq res (vlax-safearray->list value)) ) ((and (= (type value) 'safearray) (< (vlax-safearray-get-u-bound value 1) 0) ) ;_ end of and (setq res nil) ) (t (if (vlax-property-available-p value 'count) (setq res ((lambda (/ lst) (vlax-for item value (setq lst (cons item lst)) ) ;_ end of vlax-for (reverse lst) ) ;_ end of lambda ) ) ;_ end of setq (setq res (list value)) ) ;_ end of if ) ) ;_ end of cond res ) ;_ end of defun (defun fun_get-ent-name (ent) (cond ((vlax-property-available-p ent 'effectivename) (vla-get-effectivename ent) ) ((vlax-property-available-p ent 'name) (vla-get-name ent) ) (t nil) ) ;_ end of cond ) ;_ end of defun (defun fun_layer-save (/ lst res) (setq res (mapcar '(lambda (x) (list x (cons "freeze" (vla-get-freeze x)) (cons "lock" (vla-get-lock x)) ) ;_ end of list ) ;_ end of lambda (setq lst_layer (vl-remove-if '(lambda (a) (wcmatch (fun_get-ent-name a) "*|*") ) ;_ end of lambda (fun_conv-vla-to-list (vla-get-layers adoc)) ) ;_ end of vl-remove-if ) ;_ end of setq ) ;_ end of mapcar ) ;_ end of setq (foreach item lst (fun_property-set item "freeze" :vlax-false) (fun_property-set item "lock" :vlax-false) ) ;_ end of foreach ) ;_ end of defun (defun fun_layer-restore (lst) (foreach item lst (foreach prop (cdr item) (fun_property-set (car item) (car x) (cdr x)) ) ;_ end of foreach ) ;_ end of foreach ) ;_ end of defun (defun fun_property-set (ent prop value) (if (vlax-property-available-p ent prop) (vl-catch-all-apply '(lambda () (vlax-put-property ent prop value))) ) ;_ end of if ) ;_ end of defun (vl-load-com) (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))) ) ;_ end of vla-startundomark (if (and bit (>= bit 1) ) ;_ end of and (progn (setq lst_layer (fun_layer-save)) (foreach blk_def (vl-remove-if '(lambda (x) (or (equal (vla-get-islayout x) :vlax-true) (equal (vla-get-isxref x) :vlax-true) ) ;_ end of or ) ;_ end of lambda (fun_conv-vla-to-list (vla-get-blocks adoc)) ) ;_ end of vl-remove-if (vlax-for sub blk_def (if (/= (logand bit 16) 0) (fun_property-set sub 'linetypescale 1.) ) ;_ end of if (if (/= (logand bit 8) 0) (fun_property-set sub 'color 0) ) ;_ end of if (if (/= (logand bit 4) 0) (fun_property-set sub 'lineweight acLnWtByBlock) ) ;_ end of if (if (/= (logand bit 2) 0) (fun_property-set sub 'linetype "byblock") ) ;_ end of if (if (/= (logand bit 1) 0) (fun_property-set sub 'layer "0") ) ;_ end of if ) ;_ end of vlax-for ) ;_ end of foreach ;; В принципе, следующий цикл не особо требуется, но я оставил (foreach ent (fun_conv-vla-to-list (vla-get-blocks adoc)) (vlax-for sub ent (vl-catch-all-apply '(lambda () (vla-update sub))) ) ;_ end of vlax-for ) ;_ end of foreach ) ;_ end of progn ) ;_ end of if (fun_layer-restore lst_layer) (vla-regen adoc acallviewports) (vla-endundomark adoc) (princ) ) ;_ end of defun
latour_g:
Thanks KPL. I found out how to iterate throught objets, it wasn't complicated but I couldn't figure out how to do it ...
--- Code - C#: --- Document doc = AcadApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (doc.LockDocument()) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; DBObjectCollection objs = new DBObjectCollection(); BlockTableRecord btr = tr.GetObject(bt[blkName], OpenMode.ForWrite) as BlockTableRecord; // blkName = BlockReference name foreach (ObjectId objID in btr) { DBObject obj = tr.GetObject(objID, OpenMode.ForRead) as DBObject; if (obj is Entity) { Entity acEnt = obj as Entity; acEnt.UpgradeOpen(); // change color, linetype } } // Update existing block references foreach (ObjectId objID in btr.GetBlockReferenceIds(false, true)) { BlockReference acBlkRef = tr.GetObject(objID, OpenMode.ForWrite) as BlockReference; acBlkRef.RecordGraphicsModified(true); } tr.Commit(); } }
Navigation
[0] Message Index
[#] Next page
Go to full version