Jump to content

Polyline inside block coordinate


jan_ek

Recommended Posts

Hello, how to get coordinates of a polyline on a layer eg "Boundary" inside a block . I would like the coordinates to be in the global system.

I found some code but it returns coordinates relative to the base point of the block

https://forums.augi.com/showthread.php?83935-Creating-a-list-Polyline-vertices

Edited by jan_ek
Link to comment
Share on other sites

Unfortunately, the AutoCad substitute does not have this function

"Unable to recognize command "NCOPY". This error can occur when the command is not supported for the active license."

Link to comment
Share on other sites

The following routine should print the coordinates of all polylines that on layer "Boundary" in the selected block to the command line.

NOTE: Be sure to have all your layers unlocked.

(defun c:Test (/ sel lst pts)
  ;;	Tharwat - 26.Oct.2019	;;
  (and (setq sel (car (entsel "\nSelect a Block :")))
       (or (= (cdr (assoc 0 (entget sel))) "INSERT")
           (alert "Selected object is not a block <!>")
       )
       (setq lst (vlax-invoke (vlax-ename->vla-object sel) 'Explode))
       (mapcar
         '(lambda (obj)
            (and (= (vla-get-objectname obj) "AcDbPolyline")
                 (= (vla-get-layer obj) "Boundary")
                 (setq
                   pts (cons (mapcar 'cdr
                                     (vl-remove-if-not
                                       '(lambda (p) (= (car p) 10))
                                       (entget (vlax-vla-object->ename obj))
                                     )
                             )
                             pts
                       )
                 )
            )
          )
         lst
       )
       (mapcar 'vla-delete lst)
  )
  (if pts
    (princ pts)
    (princ)
  )
) (vl-load-com)

 

Link to comment
Share on other sites

For what it's worth, it is not necessary to perform a nested copy of the polyline, nor explode the block reference in order to ascertain the polyline vertex coordinates with respect to the block reference - instead, you can transform the polyline vertices obtained from the block definition using the position, scale, rotation & orientation of the block reference. This is a very similar question to that posed in this thread.

 

To offer an example, consider the following code:

(defun c:test ( / ent enx lst ocs )
    (while
        (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect block: ")))
            (cond
                (   (= 7 (getvar 'errno))
                    (princ "\nMissed, try again.")
                )
                (   (null ent)
                    nil
                )
                (   (/= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
                    (princ "\nThe selected object is not a block.")
                )
                (   (null (setq lst (blockreferencepolylinevertices ent)))
                    (princ "\nThe selected block does not contain a 2D polyline on the Boundary layer.")
                )
                (   (progn
                        (setq ocs (cdr (assoc 210 enx)))
                        (entmake
                            (append
                                (list
                                   '(000 . "LWPOLYLINE")
                                   '(100 . "AcDbEntity")
                                   '(100 . "AcDbPolyline")
                                    (cons 090 (length lst))
                                   '(070 . 0)
                                    (cons 038 (cadddr (assoc 010 enx)))
                                    (cons 210 ocs)
                                )
                                (mapcar '(lambda ( p ) (cons 10 (trans p 0 ocs))) lst)
                            )
                        )
                    )
                    nil
                )
                (   (princ "\nUnable to recreated nested polyline."))
            )
        )
    )
    (princ)
)

(defun blockreferencepolylinevertices ( ref / elv ent enx lst ocs )
   (setq ent (tblobjname "block" (cdr (assoc 2 (entget ref)))))
   (while
       (and
           (null lst)
           (setq ent (entnext ent))
           (setq enx (entget  ent))
       )
       (if
           (and
               (= "LWPOLYLINE" (cdr (assoc 0 enx)))
               (= "BOUNDARY" (strcase (cdr (assoc 8 enx))))
           )
           (setq elv (cdr (assoc 038 enx))
                 ocs (cdr (assoc 210 enx))
                 lst
               (mapcar
                  '(lambda ( v )
                       (trans (list (cadr v) (caddr v) elv) ocs 0)
                   )
                   (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) enx)
               )
           )
       )
   )
   (if lst
       (apply
           (function
               (lambda ( mat vec )
                   (mapcar
                       (function
                           (lambda ( vtx )
                               (mapcar '+ (mxv mat vtx) vec)
                           )
                       )
                       lst
                   )
               )
           )
           (refgeom ref)
       )
   )
)

;; RefGeom (gile)
;; Returns a list whose first item is a 3x3 transformation matrix and
;; second item the object insertion point in its parent (xref, block or space)

(defun refgeom ( ent / ang enx mat ocs )
    (setq enx (entget ent)
          ang (cdr (assoc 050 enx))
          ocs (cdr (assoc 210 enx))
    )
    (list
        (setq mat
            (mxm
                (mapcar '(lambda ( v ) (trans v 0 ocs t))
                   '(
                        (1.0 0.0 0.0)
                        (0.0 1.0 0.0)
                        (0.0 0.0 1.0)
                    )
                )
                (mxm
                    (list
                        (list (cos ang) (- (sin ang)) 0.0)
                        (list (sin ang) (cos ang)     0.0)
                       '(0.0 0.0 1.0)
                    )
                    (list
                        (list (cdr (assoc 41 enx)) 0.0 0.0)
                        (list 0.0 (cdr (assoc 42 enx)) 0.0)
                        (list 0.0 0.0 (cdr (assoc 43 enx)))
                    )
                )
            )
        )
        (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
            (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
        )
    )
)

;; Matrix Transpose  -  Doug Wilson
;; Args: m - nxn matrix

(defun trp ( m )
    (apply 'mapcar (cons 'list m))
)

;; Matrix x Matrix  -  Vladimir Nesterovsky
;; Args: m,n - nxn matrices

(defun mxm ( m n )
    ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
)

;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n

(defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

(princ)

 

Edited by Lee Mac
  • Thanks 1
Link to comment
Share on other sites

  • 3 years later...

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...