Author Topic: copy entity from block  (Read 10669 times)

0 Members and 1 Guest are viewing this topic.

taner

  • Guest
copy entity from block
« on: June 02, 2013, 05:06:56 AM »
Dear all,

Can anyone solve this problem? please see the below link.
 http://bbs.xdcad.net/thread-667613-1-1.html

hugha

  • Newt
  • Posts: 103
Re: copy entity from block
« Reply #1 on: June 02, 2013, 05:43:36 AM »
It's all in Chinese.

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: copy entity from block
« Reply #2 on: June 02, 2013, 06:31:02 AM »
ncopy ?!?
« Last Edit: June 02, 2013, 06:34:03 AM by GP »

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #3 on: June 02, 2013, 07:19:15 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / a b )
  2.     (if
  3.         (and
  4.             (setq a (nentselp))
  5.             (= 4 (length a))
  6.             (setq b (entmakex (entget (car a))))
  7.         )
  8.         (vla-transformby (vlax-ename->vla-object b) (vlax-tmatrix (caddr a)))
  9.     )
  10.     (princ)
  11. )

taner

  • Guest
Re: copy entity from block
« Reply #4 on: June 02, 2013, 11:19:58 AM »
yes,it is similar to ncopy.

Hi, Lee, your routine can only copy one entity at one time. But the request is copy all entity at one time.Explode is not allowed.

thanks.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #5 on: June 02, 2013, 12:26:52 PM »
But the request is copy all entity at one time.Explode is not allowed.

Code - Auto/Visual Lisp: [Select]
  1. ;; Multiple Nested Copy  -  Lee Mac
  2. ;; Copies all components of a selected block reference to the active space.
  3.  
  4. (defun c:ncopym ( / ent lst mat obj )
  5.     (while
  6.         (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect Block Reference: ")))
  7.             (cond
  8.                 (   (= 7 (getvar 'errno))
  9.                     (princ "\nMissed, try again.")
  10.                 )
  11.                 (   (= 'ename (type ent))
  12.                     (setq obj (vlax-ename->vla-object ent))
  13.                     (cond
  14.                         (   (/= "AcDbBlockReference" (vla-get-objectname obj))
  15.                             (princ "\nSelected object is not a block reference.")
  16.                         )
  17.                         (   (= :vlax-true (vla-get-hasattributes obj))
  18.                             (princ "\nSelected block must not be attributed.")
  19.                         )
  20.                         (   (= :vlax-true (vla-get-isdynamicblock obj))
  21.                             (princ "\nSelected block must not be dynamic.")
  22.                         )
  23.                         (   (not
  24.                                 (and
  25.                                     (equal (vla-get-xscalefactor obj) (vla-get-yscalefactor obj) 1e-8)
  26.                                     (equal (vla-get-xscalefactor obj) (vla-get-zscalefactor obj) 1e-8)
  27.                                 )
  28.                             )
  29.                             (princ "\nSelected block must be uniformly scaled.")
  30.                         )                                    
  31.                         (   t
  32.                             (setq mat
  33.                                 (vlax-tmatrix
  34.                                     (apply
  35.                                         (function
  36.                                             (lambda ( mat vec )
  37.                                                 (append (mapcar 'append mat (mapcar 'list vec))
  38.                                                    '((0.0 0.0 0.0 1.0))
  39.                                                 )
  40.                                             )
  41.                                         )
  42.                                         (refgeom ent)
  43.                                     )
  44.                                 )
  45.                             )
  46.                             (vlax-for obj (vla-item (vla-get-blocks (LM:acdoc)) (vla-get-name obj))
  47.                                 (setq lst (cons obj lst))
  48.                             )
  49.                             (foreach obj
  50.                                 (vlax-invoke (LM:acdoc) 'copyobjects (reverse lst)
  51.                                     (vlax-ename->vla-object (cdr (assoc 330 (entget ent))))
  52.                                 )
  53.                                 (vla-transformby obj mat)
  54.                             )
  55.                         )
  56.                     )
  57.                 )
  58.             )
  59.         )
  60.     )
  61.     (princ)
  62. )
  63.  
  64. ;; RefGeom (gile)
  65. ;; Returns a list whose first item is a 3x3 transformation matrix and
  66. ;; second item the object insertion point in its parent (xref, block or space)
  67.  
  68. (defun refgeom ( ent / ang enx mat ocs )
  69.     (setq enx (entget ent)
  70.           ang (cdr (assoc 050 enx))
  71.           ocs (cdr (assoc 210 enx))
  72.     )
  73.     (list
  74.         (setq mat
  75.             (mxm
  76.                 (mapcar '(lambda ( v ) (trans v 0 ocs t))
  77.                    '(
  78.                         (1.0 0.0 0.0)
  79.                         (0.0 1.0 0.0)
  80.                         (0.0 0.0 1.0)
  81.                     )
  82.                 )
  83.                 (mxm
  84.                     (list
  85.                         (list (cos ang) (- (sin ang)) 0.0)
  86.                         (list (sin ang) (cos ang)     0.0)
  87.                        '(0.0 0.0 1.0)
  88.                     )
  89.                     (list
  90.                         (list (cdr (assoc 41 enx)) 0.0 0.0)
  91.                         (list 0.0 (cdr (assoc 42 enx)) 0.0)
  92.                         (list 0.0 0.0 (cdr (assoc 43 enx)))
  93.                     )
  94.                 )
  95.             )
  96.         )
  97.         (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
  98.             (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
  99.         )
  100.     )
  101. )
  102.  
  103. ;; Matrix Transpose  -  Doug Wilson
  104. ;; Args: m - nxn matrix
  105.  
  106. (defun trp ( m )
  107.     (apply 'mapcar (cons 'list m))
  108. )
  109.  
  110. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  111. ;; Args: m,n - nxn matrices
  112.  
  113. (defun mxm ( m n )
  114.     ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
  115. )
  116.  
  117. ;; Matrix x Vector  -  Vladimir Nesterovsky
  118. ;; Args: m - nxn matrix, v - vector in R^n
  119.  
  120. (defun mxv ( m v )
  121.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  122. )
  123.  
  124. ;; Active Document  -  Lee Mac
  125. ;; Returns the VLA Active Document Object
  126.  
  127. (defun LM:acdoc nil
  128.     (LM:acdoc)
  129. )
  130.  

taner

  • Guest
Re: copy entity from block
« Reply #6 on: June 02, 2013, 12:50:10 PM »
HI,Lee,Hope you download the attched test file for testing.

the test result:命令:  NCOPYM

Select Block Reference:
Selected block must be uniformly scaled.
Select Block Reference: *取消*
; 错误: 函数被取消

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #7 on: June 02, 2013, 02:11:53 PM »
HI,Lee,Hope you download the attched test file for testing.

the test result:命令:  NCOPYM

Select Block Reference:
Selected block must be uniformly scaled.
Select Block Reference: *取消*
; 错误: 函数被取消

Yes, the program is restricted to uniformly scaled blocks as stated by the message.

taner

  • Guest
Re: copy entity from block
« Reply #8 on: June 03, 2013, 06:24:11 AM »
Hi,LEE,Can you try to write one that can copy not uniformly scaled block(the attachment is uniformly scaled block )? Thanks!

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #9 on: June 03, 2013, 06:29:53 AM »
Can you try to write one that can copy not uniformly scaled block

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ncopym ( / ent obj )
  2.     (while
  3.         (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect Block Reference: ")))
  4.             (cond
  5.                 (   (= 7 (getvar 'errno))
  6.                     (princ "\nMissed, try again.")
  7.                 )
  8.                 (   (= 'ename (type ent))
  9.                     (setq obj (vlax-ename->vla-object ent))
  10.                     (cond
  11.                         (   (/= "AcDbBlockReference" (vla-get-objectname obj))
  12.                             (princ "\nSelected object is not a block reference.")
  13.                         )
  14.                         (   (= :vlax-true (vla-get-hasattributes obj))
  15.                             (princ "\nSelected block must not be attributed.")
  16.                         )
  17.                         (   (= :vlax-true (vla-get-isdynamicblock obj))
  18.                             (princ "\nSelected block must not be dynamic.")
  19.                         )
  20.                         (   (not (vlax-write-enabled-p obj))
  21.                             (princ "\nSelected block is on locked layer.")
  22.                         )
  23.                         (   (vla-explode obj)) ;; Thanks roy
  24.                     )
  25.                 )
  26.             )
  27.         )
  28.     )
  29.     (princ)
  30. )
« Last Edit: June 03, 2013, 06:48:59 PM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: copy entity from block
« Reply #10 on: June 03, 2013, 01:23:51 PM »
(vla-explode) does not erase the original object so there is no need for (vla-copy).

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #11 on: June 03, 2013, 06:48:38 PM »
(vla-explode) does not erase the original object so there is no need for (vla-copy).

Good catch roy, the code was written in haste  :oops:
Now updated.

Ketxu

  • Newt
  • Posts: 109
Re: copy entity from block
« Reply #12 on: June 04, 2013, 07:08:30 AM »
Lee, please tell me what benefit of use LM:acdoc funtion in #6 post. Can be simple (vla-get-activedocument (vlax-get-acad-object)) ? . I haven't understand clearly :(. Thank you

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: copy entity from block
« Reply #13 on: June 04, 2013, 08:45:17 AM »
Lee, please tell me what benefit of use LM:acdoc funtion in #6 post. Can be simple (vla-get-activedocument (vlax-get-acad-object)) ? . I haven't understand clearly :(. Thank you

The use of defun-q will help to explain the difference:

Consider the following functions:
Code - Auto/Visual Lisp: [Select]
Code - Auto/Visual Lisp: [Select]
  1. (defun-q f2 nil
  2.     (f2)
  3. )

Before function evaluation, the respective function definitions are as follows:
Code - Auto/Visual Lisp: [Select]

When evaluated, both functions will return the Active Document object:
Code - Auto/Visual Lisp: [Select]
  1. _$ (f1)
  2. #<VLA-OBJECT IAcadDocument 0ceb5b6c>
  3. _$ (f2)
  4. #<VLA-OBJECT IAcadDocument 0ceb5b6c>

However, observe the respective function definitions following function evaluation:
Code - Auto/Visual Lisp: [Select]
  1. _$ f1
  2. _$ f2
  3. (nil #<VLA-OBJECT IAcadDocument 0ceb5b6c>)

Note that function f2 now no longer retrieves the Application Object (vlax-get-acad-object) before retrieving the Active Document Property (vla-get-activedocument), but simply returns the Active Document object directly. The method is similar to setting a global variable pointing to the Active Document Object.

Of course, for this program, I could have equivalently used a local variable assigned with the Active Document Object, however, such local variable would then be re-assigned this value for every use of the program.
« Last Edit: June 04, 2013, 08:53:20 AM by Lee Mac »

Ketxu

  • Newt
  • Posts: 109
Re: copy entity from block
« Reply #14 on: June 04, 2013, 09:47:21 AM »
Oh now i see ^^ Great, It's new for me.