Author Topic: Copy nested object(s) - how to work with XRefs?  (Read 8960 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Copy nested object(s) - how to work with XRefs?
« on: October 27, 2011, 02:02:21 PM »
I'm using the vla-copyobjects method to copy a nested object and if it's just a nested object within a block, it works fine, but I can't figure out how to get it to work with an Xref. Any help would be greatly appreciated.

Here's a snippet of code that I'm trying to work with...

Code: [Select]
(defun c:Test (/ doc space ent obj new)

  (setq doc   (vla-get-activedocument (vlax-get-acad-object))
        space (if (eq (getvar 'CVPORT) 1)
                (vla-get-paperspace doc)
                (vla-get-modelspace doc)
              )
  )

  (if (setq ent (nentselp "\nSelect nested object to copy: "))
    (progn (if (eq (cdr (assoc 0 (entget (car ent)))) "VERTEX")
             (setq obj (vlax-ename->vla-object (cdr (assoc 330 (entget (car ent))))))
             (setq obj (vlax-ename->vla-object (car ent)))
           )
           (setq new (car (vlax-invoke doc 'CopyObjects (list obj) space)))
           (vla-transformby new (vlax-tmatrix (caddr ent)))
    )
  )
  (princ)
)

Do I have to use ODBX? I keep getting the following error: Error: AutoCAD.Application: Object not in database
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Copy nested object(s) - how to work with XRefs?
« Reply #1 on: October 27, 2011, 02:06:05 PM »
I believe there's a LSP for Autodesk's NCOPY command (I can't see my C:\ drive so I can't tell you were to look - I think it's part of the Express Tools).  You could disect that file and see how it works.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Copy nested object(s) - how to work with XRefs?
« Reply #2 on: October 27, 2011, 02:07:55 PM »
I believe there's a LSP for Autodesk's NCOPY command (I can't see my C:\ drive so I can't tell you were to look - I think it's part of the Express Tools).  You could disect that file and see how it works.
Yeah, I looked there. It uses a series of entmake(x) functions - not what I was hoping for, but if I can't get copyobjects to work, I'll have to go that route. I was just thinking this would be a little less aggravating.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Copy nested object(s) - how to work with XRefs?
« Reply #3 on: October 27, 2011, 03:20:36 PM »
Quote
Code: [Select]
(vlax-invoke doc 'CopyObjects (list obj) space)
i think 'doc' is wrong, it must be XRefDatabase not Document

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Copy nested object(s) - how to work with XRefs?
« Reply #4 on: October 27, 2011, 03:49:47 PM »
Why not just:

Code: [Select]
(defun c:test ( / a b )
    (if
        (and
            (setq a (nentselp))
            (= 4 (length a))
            (setq b (entmakex (entget (car a))))
        )
        (vla-transformby (vlax-ename->vla-object b) (vlax-tmatrix (caddr a)))
    )
    (princ)
)

(With the check for the Vertex Entity)

Or did I miss something?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Copy nested object(s) - how to work with XRefs?
« Reply #5 on: October 27, 2011, 03:53:31 PM »
Quote
Code: [Select]
(vlax-invoke doc 'CopyObjects (list obj) space)
i think 'doc' is wrong, it must be XRefDatabase not Document
That make sense. Will have to investigate more.

I'm such a retard. I was thinking it would be heavily convoluted to use entmake, but it's actually pretty simple since I only care about curves.

sample (tested on nested 3dpolyline, arc, line, lwpolyline, ellipse, arc, circle, xline, ray)...

Code: [Select]
(defun _entmakeNestedCurve (ent matrix / e d)
  (if (eq (cdr (assoc 0 (entget ent))) "VERTEX")
    (progn (entmake (entget (setq ent (cdr (assoc 330 (entget ent))))))
           (while (/= (cdr (assoc 0 (setq d (entget (setq ent (entnext ent)))))) "SEQEND")
             (setq e (entmakex d))
           )
           (setq e (cdr (assoc 330 (entget (entmakex '((0 . "SEQEND")))))))
    )
    (setq e (entmakex (entget ent)))
  )
  (vla-transformby (vlax-ename->vla-object e) (vlax-tmatrix matrix))
  e
)



(defun c:Test (/ ent)
  (if (setq ent (nentselp "\nSelect nested curve to copy: "))
    (vla-put-color (vlax-ename->vla-object (_entmakeNestedCurve (car ent) (caddr ent))) 3)
  )
  (princ)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Copy nested object(s) - how to work with XRefs?
« Reply #6 on: October 27, 2011, 03:55:25 PM »
Why not just:

(With the check for the Vertex Entity)

Or did I miss something?
Oops, missed your post.
I was concerned about 3d/2dpolylines, but it was a lot easier than I'd imagined.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Copy nested object(s) - how to work with XRefs?
« Reply #7 on: October 27, 2011, 03:57:40 PM »
That make sense. Will have to investigate more.
that's the way
Code: [Select]
(setq new
                        (car
                            (vlax-invoke
                                (vla-get-XRefDatabase
                                    (vla-Item
                                        (vla-get-Blocks doc)
                                        (vla-get-Name
                                            (vlax-ename->vla-object (car (last ent))
                                            )
                                        )
                                    )
                                )
                                'CopyObjects
                                (list obj)
                                space
                            )
                        )
               )
« Last Edit: October 27, 2011, 04:01:53 PM by VovKa »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Copy nested object(s) - how to work with XRefs?
« Reply #8 on: October 27, 2011, 03:59:46 PM »
That make sense. Will have to investigate more.
that's the way
Code: [Select]
(setq new
                        (car
                            (vlax-invoke
                                (vla-get-XRefDatabase
                                    (vla-Item
                                        (vla-get-Blocks doc)
                                        (vla-get-Name
                                            (vlax-ename->vla-object (last (last ent))
                                            )
                                        )
                                    )
                                )
                                'CopyObjects
                                (list obj)
                                space
                            )
                        )
               )
Ahhh, that makes total sense. 
Thanks so much Vovka. :)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Copy nested object(s) - how to work with XRefs?
« Reply #9 on: October 27, 2011, 04:42:05 PM »
Thanks so much Vovka. :)
i've edited my post, must be car not last

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Copy nested object(s) - how to work with XRefs?
« Reply #10 on: October 27, 2011, 04:52:45 PM »
Thanks so much Vovka. :)
i've edited my post, must be car not last
Cool. I hadn't had a chance to test your code, only look at it to see what you were doing. Really, thanks again. :)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.