Jump to content

Polyline to block with attributes


BlackDev1l

Recommended Posts

(defun C:P2B (/ ss base n blk pl); = Polylines [to] Blocks
  (if (setq ss (ssget "_:L" '((0 . "*POLYLINE"))))
    (progn ; then
    (setq base 0)
    (while (tblsearch "block" (strcat "" (itoa (1+ base))))
      (setq base (1+ base))
    ); while
    (repeat (setq n (sslength ss))
      (command
        "_.block" (setq blk (strcat "" (itoa (+ base n)))); increment Block name
          "_none" (vlax-curve-getStartPoint (setq pl (ssname ss (setq n (1- n)))))
          pl ""
        "_.insert" blk "_none" "@" "" "" ""
      ); command
    ); repeat
    ); progn
  ); if
); defun

This code works correctly. Please help me how to add an attribute to the created block with area of the selected polyline.

Link to comment
Share on other sites

Here you are :

 

(defun C:P2B (/ *error* atd ss base n pl ar ll ur mp att blk); = Polylines [to] Blocks

  (vl-load-com)

  (defun *error* (m)
    (if atd
      (setvar 'attdia atd)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq atd (getvar 'attdia))
  (setvar 'attdia 0)
  (if (setq ss (ssget "_:L" '((0 . "*POLYLINE") (-4 . "&=") (70 . 1))))
    (progn ; then
      (setq base 0)
      (while (tblsearch "block" (strcat "" (itoa (1+ base))))
        (setq base (1+ base))
      ); while
      (repeat (setq n (sslength ss))
        (setq pl (ssname ss (setq n (1- n))))
        (setq ar (vlax-curve-getArea pl))
        (vla-getBoundingBox (vlax-ename->vla-object pl) 'll 'ur)
        (mapcar 'set '(ll ur) (mapcar 'safearray-value (list ll ur)))
        (setq mp (mapcar '(lambda (a b) (/ (+ a b) 2.0)) ll ur))
        (command "_.attdef" "" (itoa (+ base n)) "" "" "_none" mp (getvar 'textsize) "")
        (setq att (entlast))
        (command
          "_.block" (setq blk (itoa (+ base n))); increment Block name
            "_none" (trans (vlax-curve-getStartPoint pl) 0 1)
            pl att ""
          "_.insert" blk "_none" "@" "" "" "" ar
        ); command
      ); repeat
    ); progn
  ); if
  (*error* nil)
); defun

 

HTH.

M.R.

Edited by marko_ribar
Link to comment
Share on other sites

11 hours ago, marko_ribar said:
(defun C:P2B (/ *error* atd ss base n pl ar ll ur mp att blk); = Polylines [to] Blocks

  (vl-load-com)

  (defun *error* (m)
    (if atd
      (setvar 'attdia atd)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq atd (getvar 'attdia))
  (setvar 'attdia 0)
  (if (setq ss (ssget "_:L" '((0 . "*POLYLINE") (-4 . "&=") (70 . 1))))
    (progn ; then
      (setq base 0)
      (while (tblsearch "block" (strcat "" (itoa (1+ base))))
        (setq base (1+ base))
      ); while
      (repeat (setq n (sslength ss))
        (setq pl (ssname ss (setq n (1- n))))
        (setq ar (vlax-curve-getArea pl))
        (vla-getBoundingBox (vlax-ename->vla-object pl) 'll 'ur)
        (mapcar 'set '(ll ur) (mapcar 'safearray-value (list ll ur)))
        (setq mp (mapcar '(lambda (a b) (/ (+ a b) 2.0)) ll ur))
        (command "_.attdef" "" (itoa (+ base n)) "" "" "_none" mp (getvar 'textsize) "")
        (setq att (entlast))
        (command
          "_.block" (setq blk (itoa (+ base n))); increment Block name
            "_none" (trans (vlax-curve-getStartPoint pl) 0 1)
            pl att ""
          "_.insert" blk "_none" "@" "" "" "" ar
        ); command
      ); repeat
    ); progn
  ); if
  (*error* nil)
); defun

Thank you very much @marko_ribar.Only the increment of the block name doesn't work. And i can use  only one time this command.

Link to comment
Share on other sites

Here, check it now...

 

(defun C:P2B (/ *error* atd ss base n pl ar ll ur mp att blk); = Polylines [to] Blocks

  (vl-load-com)

  (defun *error* (m)
    (if atd
      (setvar 'attdia atd)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq atd (getvar 'attdia))
  (setvar 'attdia 0)
  (if (setq ss (ssget "_:L" '((0 . "*POLYLINE") (-4 . "&=") (70 . 1))))
    (progn ; then
      (setq base 0 n -1)
      (while (tblsearch "block" (itoa (+ (1+ base) n)))
        (setq base (1+ base))
      ); while
      (while (tblsearch "block" (itoa (+ base (1+ n))))
        (setq n (1+ n))
      ); while
      (setq n -1)
      (repeat (sslength ss)
        (setq pl (ssname ss (setq n (1+ n))))
        (while (tblsearch "block" (itoa (+ (1+ base) n)))
          (setq base (1+ base))
        ); while
        (setq ar (vlax-curve-getArea pl))
        (vla-getBoundingBox (vlax-ename->vla-object pl) 'll 'ur)
        (mapcar 'set '(ll ur) (mapcar 'safearray-value (list ll ur)))
        (setq mp (mapcar '(lambda (a b) (/ (+ a b) 2.0)) ll ur))
        (command "_.attdef" "" "TAG" "" "" "_none" mp (getvar 'textsize) "")
        (setq att (entlast))
        (command
          "_.block" (setq blk (itoa (+ base n))); increment Block name
            "_none" (trans (vlax-curve-getStartPoint pl) 0 1)
            pl att ""
          "_.insert" blk "_none" "@" "" "" "" ar
        ); command
      ); repeat
    ); progn
  ); if
  (*error* nil)
); defun

 

Edited by marko_ribar
  • Like 1
Link to comment
Share on other sites

I have said before. You want more then just a generic block name if you are going to at any point move blocks between drawings.

Link to comment
Share on other sites

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...