Author Topic: Objetcs inside a block  (Read 456 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 120
Objetcs inside a block
« on: February 14, 2017, 04:40:50 pm »
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

  • Water Moccasin
  • Posts: 2227
Re: Objetcs inside a block
« Reply #1 on: February 14, 2017, 04:43:24 pm »
Think of blocks like XREFs, and XREFs like blocks.  To modify a block (fully "block reference"), modify the definition.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

latour_g

  • Newt
  • Posts: 120
Re: Objetcs inside a block
« Reply #2 on: February 14, 2017, 04:47:34 pm »
Thanks, I've just remember I already done that by seeking AttributeDefinition in all entities of a block.  I'm okay then ! 

kpblc

  • Bull Frog
  • Posts: 248
Re: Objetcs inside a block
« Reply #3 on: February 15, 2017, 03:51:24 am »
Code - Auto/Visual Lisp: [Select]
  1. (defun norm-blocks (bit                /                  adoc
  2.                    *error*            fun_conv-vla-to-list
  3.                    fun_layer-save     fun_layer-restore  lst_layer
  4.                    fun_get-ent-name   fun_property-set
  5.                    )
  6.                   ;|
  7. http://forum.dwg.ru/showthread.php?t=21492
  8. *    Changes properties of eninties inside blocks
  9. *    Call parameters:
  10. bit summ of bits:
  11.  1   ; change layer to "0"
  12.  2   ; Linetype to ByBlock
  13.  4   ; Linewegith to ByBlock
  14.  8   ; Color to  ByBlock
  15.  16  ; Line scale to 1
  16.  nil or less 1 - exit
  17. *    Call samples:
  18. (norm-blocks 1) ; change layer of all entities to "0". Do not change other properties
  19. (norm-blocks 7) ; change layer of all entities to "0", linetype to ByBlock, lineweight to ByBlock
  20. |;
  21.  
  22.  (defun *error* (msg)
  23.    (fun_layer-restore (lst_layer))
  24.    (vla-regen adoc acallviewports)
  25.    (vla-endundomark adoc)
  26.    (princ)
  27.    ) ;_ end of defun
  28.  
  29.  (defun fun_get-attr (blk-ref)
  30.    (append (fun_conv-vla-to-list (vla-getattributes blk-ref))
  31.            (fun_conv-vla-to-list (vla-getconstantattributes blk-ref))
  32.            ) ;_ end of append
  33.    ) ;_ end of defun
  34.  
  35.  (defun fun_conv-vla-to-list (value / res)
  36.    (cond
  37.      ((= (type value) 'variant)
  38.       (setq res (fun_conv-vla-to-list (vlax-variant-value value)))
  39.       )
  40.      ((and (= (type value) 'safearray)
  41.            (>= (vlax-safearray-get-u-bound value 1) 0)
  42.            ) ;_ end of and
  43.       (setq res (vlax-safearray->list value))
  44.       )
  45.      ((and (= (type value) 'safearray)
  46.            (< (vlax-safearray-get-u-bound value 1) 0)
  47.            ) ;_ end of and
  48.       (setq res nil)
  49.       )
  50.      (t
  51.       (if (vlax-property-available-p value 'count)
  52.         (setq res ((lambda (/ lst)
  53.                      (vlax-for item value
  54.                        (setq lst (cons item lst))
  55.                        ) ;_ end of vlax-for
  56.                      (reverse lst)
  57.                      ) ;_ end of lambda
  58.                    )
  59.               ) ;_ end of setq
  60.         (setq res (list value))
  61.         ) ;_ end of if
  62.       )
  63.      ) ;_ end of cond
  64.    res
  65.    ) ;_ end of defun
  66.  
  67.  (defun fun_get-ent-name (ent)
  68.    (cond
  69.      ((vlax-property-available-p ent 'effectivename)
  70.       (vla-get-effectivename ent)
  71.       )
  72.      ((vlax-property-available-p ent 'name)
  73.       (vla-get-name ent)
  74.       )
  75.      (t nil)
  76.      ) ;_ end of cond
  77.    ) ;_ end of defun
  78.  
  79.  (defun fun_layer-save (/ lst res)
  80.    (setq
  81.      res (mapcar '(lambda (x)
  82.                     (list x
  83.                           (cons "freeze" (vla-get-freeze x))
  84.                           (cons "lock" (vla-get-lock x))
  85.                           ) ;_ end of list
  86.                     ) ;_ end of lambda
  87.                  (setq lst_layer (vl-remove-if
  88.                                    '(lambda (a)
  89.                                       (wcmatch (fun_get-ent-name a) "*|*")
  90.                                       ) ;_ end of lambda
  91.                                    (fun_conv-vla-to-list (vla-get-layers adoc))
  92.                                    ) ;_ end of vl-remove-if
  93.                        ) ;_ end of setq
  94.                  ) ;_ end of mapcar
  95.      ) ;_ end of setq
  96.    (foreach item lst
  97.      (fun_property-set item "freeze" :vlax-false)
  98.      (fun_property-set item "lock" :vlax-false)
  99.      ) ;_ end of foreach
  100.    ) ;_ end of defun
  101.  
  102.  (defun fun_layer-restore (lst)
  103.    (foreach item lst
  104.      (foreach prop (cdr item)
  105.        (fun_property-set (car item) (car x) (cdr x))
  106.        ) ;_ end of foreach
  107.      ) ;_ end of foreach
  108.    ) ;_ end of defun
  109.  
  110.  (defun fun_property-set (ent prop value)
  111.    (if (vlax-property-available-p ent prop)
  112.      (vl-catch-all-apply '(lambda () (vlax-put-property ent prop value)))
  113.      ) ;_ end of if
  114.    ) ;_ end of defun
  115.  
  116.    ) ;_ end of vla-startundomark
  117.  (if (and bit
  118.           (>= bit 1)
  119.           ) ;_ end of and
  120.    (progn
  121.      (setq lst_layer (fun_layer-save))
  122.      (foreach blk_def (vl-remove-if
  123.                         '(lambda (x)
  124.                            (or (equal (vla-get-islayout x) :vlax-true)
  125.                                (equal (vla-get-isxref x) :vlax-true)
  126.                                ) ;_ end of or
  127.                            ) ;_ end of lambda
  128.                         (fun_conv-vla-to-list (vla-get-blocks adoc))
  129.                         ) ;_ end of vl-remove-if
  130.        (vlax-for sub blk_def
  131.          (if (/= (logand bit 16) 0)
  132.            (fun_property-set sub 'linetypescale 1.)
  133.            ) ;_ end of if
  134.          (if (/= (logand bit 8) 0)
  135.            (fun_property-set sub 'color 0)
  136.            ) ;_ end of if
  137.          (if (/= (logand bit 4) 0)
  138.            (fun_property-set sub 'lineweight acLnWtByBlock)
  139.            ) ;_ end of if
  140.          (if (/= (logand bit 2) 0)
  141.            (fun_property-set sub 'linetype "byblock")
  142.            ) ;_ end of if
  143.          (if (/= (logand bit 1) 0)
  144.            (fun_property-set sub 'layer "0")
  145.            ) ;_ end of if
  146.          ) ;_ end of vlax-for
  147.        ) ;_ end of foreach
  148. ;; &#1042; &#1087;&#1088;&#1080;&#1085;&#1094;&#1080;&#1087;&#1077;, &#1089;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081; &#1094;&#1080;&#1082;&#1083; &#1085;&#1077; &#1086;&#1089;&#1086;&#1073;&#1086; &#1090;&#1088;&#1077;&#1073;&#1091;&#1077;&#1090;&#1089;&#1103;, &#1085;&#1086; &#1103; &#1086;&#1089;&#1090;&#1072;&#1074;&#1080;&#1083;
  149.      (foreach ent (fun_conv-vla-to-list (vla-get-blocks adoc))
  150.        (vlax-for sub ent
  151.          (vl-catch-all-apply '(lambda () (vla-update sub)))
  152.          ) ;_ end of vlax-for
  153.        ) ;_ end of foreach
  154.      ) ;_ end of progn
  155.    ) ;_ end of if
  156.  (fun_layer-restore lst_layer)
  157.  (vla-regen adoc acallviewports)
  158.  (vla-endundomark adoc)
  159.  (princ)
  160.  ) ;_ end of defun
  161.  
Sorry for my English.

latour_g

  • Newt
  • Posts: 120
Re: Objetcs inside a block
« Reply #4 on: February 17, 2017, 10:44:29 am »
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#: [Select]
  1.         Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  2.         Database db = doc.Database;
  3.  
  4.                using (doc.LockDocument())
  5.                {
  6.                    using (Transaction tr = db.TransactionManager.StartTransaction())
  7.                    {
  8.                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  9.                        DBObjectCollection objs = new DBObjectCollection();                        
  10.                        BlockTableRecord btr = tr.GetObject(bt[blkName], OpenMode.ForWrite) as BlockTableRecord;
  11.                        // blkName = BlockReference name
  12.  
  13.                        foreach (ObjectId objID in btr)
  14.                        {
  15.                            DBObject obj = tr.GetObject(objID, OpenMode.ForRead) as DBObject;
  16.  
  17.                            if (obj is Entity)
  18.                            {
  19.                                Entity acEnt = obj as Entity;
  20.                                acEnt.UpgradeOpen();
  21.                                // change color, linetype
  22.                            }
  23.                        }
  24.  
  25.                        // Update existing block references
  26.                        foreach (ObjectId objID in btr.GetBlockReferenceIds(false, true))
  27.                        {
  28.                            BlockReference acBlkRef = tr.GetObject(objID, OpenMode.ForWrite) as BlockReference;
  29.                            acBlkRef.RecordGraphicsModified(true);
  30.                        }
  31.                        tr.Commit();
  32.                    }
  33.                }
  34.  

kpblc

  • Bull Frog
  • Posts: 248
Re: Objetcs inside a block
« Reply #5 on: February 18, 2017, 12:02:14 am »
Oops, sorry for lisp code :(
Sorry for my English.