Jump to content

Lisp for tagging hatch objects


mhmtlgrr

Recommended Posts

Dear folks,

 

We all know that Revit has tagging capabilities and I know that AutoCAD can also do that with Lisp.

 

Lets assume we have 5 different solid hatch each in different layers like:

Layer Name - L1

Layer Name - L2

Layer Name - L3

Layer Name - L4

Layer Name - L5

 

I want to place a block named LNAME with an attribute and place them onto hatches and get the Object->Layer proporty in it.

 

I attached the sample file.

 

Thanks in advanced.

 

MA

LISP-TAGGING.dwg

Link to comment
Share on other sites

Ran out of time but try this

 

(setvar "attdia" 0)
(while (setq obj (vlax-ename->vla-object (car (entsel "\nPick hatch"))))
(setq lay (vla-get-layer obj))
(command "-Insert" "lname" (getpoint) 1000 0.0 lay)
)

Link to comment
Share on other sites

(defun C:test ( / spc SS attr i o p ll ur )
 (vl-every 'eval
   '( 
     (setq spc (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object)))))
     (princ "\nSelect hatches to insert \"LNAME\" block on their centroids.")
     (setq SS (ssget '((0 . "HATCH")(2 . "SOLID")(8 . "L1,L2,L3,L4,L5"))))
     (setq attr (getvar 'attreq)) (setvar 'attreq 0)
     (progn 
       (repeat (setq i (sslength SS))
         (vla-GetBoundingBox (setq o (vlax-ename->vla-object (ssname SS (setq i (1- i))))) 'll 'ur)
         (setq p (apply 'mapcar (cons '(lambda (a b) (/ (+ a b) 2.)) (mapcar 'vlax-safearray->list (list ll ur))))) ; LM
         (vla-put-Layer (vlax-invoke spc 'InsertBlock p "LNAME" 1.0 1.0 1.0 0.0) (vla-get-Layer o))
       )
       (setvar 'attreq attr)
     )
   )
 )
 (princ)
) (vl-load-com) (princ)

Link to comment
Share on other sites

Dear Grrr,

 

Thanks for your reply. Your code places the tags onto hatch object centroids like a charm! But, I have two questions:

 

1. It only takes the solid hatches into account. I assume that you intentionally locked the selection range to solid ones but there might be even custom hatch patterns in the drawing. The one on the attachment was for demonstration.

2. Tags doesn't get the layer name attribute. They all keep showing "Lx" which is the default value for the attribute.

3. I think that the possible layer names are defined in the code as: (setq SS (ssget '((0 . "HATCH")(2 . "SOLID")(8 . "L1,L2,L3,L4,L5"))))

But they varies file to file like the hatch pattern names.

 

Please see the file attached.

 

Thanks in advanced.

 

MA

LISP-TAGGING_V2.dwg

Link to comment
Share on other sites

(setq SS (ssget '((0 . "HATCH")(2 . "SOLID")(8 . "L1,L2,L3,L4,L5"))))

 

Get a selection set of hatches (0 . "HATCH") with the pattern Solid (2 . "SOLID") and on any of the layers L1,L2,L3,L4,L5 (8 . "L1,L2,L3,L4,L5")

 

Get a selection set of hatches (setq SS (ssget '((0 . "HATCH"))))

 

Nice one Grr about the centroid.

Link to comment
Share on other sites

Dear Bigal,

 

Thanks for explanation. Can we set it to:

 

Get a selection set of hatches (0 . "HATCH") with the ANY PATTERN (2 . "SOLID") and on ANY LAYER (8 . "L1,L2,L3,L4,L5")

Link to comment
Share on other sites

Nice one Grr about the centroid.

 

Thank you, however this contribution is provided by Lee Mac.

So I always put comment with his initials on that same row.

 

Can we set it to:

 

Get a selection set of hatches (0 . "HATCH") with the ANY PATTERN (2 . "SOLID") and on ANY LAYER (8 . "L1,L2,L3,L4,L5")

 

 

You have two options:

a. Remove the group codes (2 . "SOLID") and (8 . "L1,L2,L3,L4,L5")

b. Use: (2 . "*") and (8 . "*")

 

2. Tags doesn't get the layer name attribute. They all keep showing "Lx" which is the default value for the attribute.

 

Ok now I see that you use attributed block with name "LNAME", that has attribute with tag "LNAME"

 

(defun C:test ( / spc SS attr i o p ll ur b lyr )
 (vl-every 'eval
   '( 
     (setq spc (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object)))))
     (princ "\nSelect hatches to insert \"LNAME\" block on their centroids.")
     (setq SS (ssget '((0 . "HATCH"))))
     (setq attr (getvar 'attreq)) (setvar 'attreq 0)
     (progn 
       (repeat (setq i (sslength SS))
         (vla-GetBoundingBox (setq o (vlax-ename->vla-object (ssname SS (setq i (1- i))))) 'll 'ur)
         (setq p (apply 'mapcar (cons '(lambda (a b) (/ (+ a b) 2.)) (mapcar 'vlax-safearray->list (list ll ur))))) ; LM
         (vla-put-Layer (setq b (vlax-invoke spc 'InsertBlock p "LNAME" 1.0 1.0 1.0 0.0)) (setq lyr (vla-get-Layer o)))
         (vl-some 
           '(lambda (x) 
             (cond
               ( (not (vlax-write-enabled-p x)) nil)
               ( (= "LNAME" (strcase (vla-get-TagString x))) (vla-put-TextString x (vla-get-Layer o)) T)
             )
           )
           (vlax-invoke b 'GetAttributes)
         )  
       )
       (setvar 'attreq attr)
     )
   )
 )
 (princ)
) (vl-load-com) (princ) 

Link to comment
Share on other sites

Hi,

 

Here is my attempt with Field object.

(defun c:taghatches (/ getid doc lst blk int hat sel ent q p)
 ;;------------------------------------;;
 ;;	Tharwat - Date: 26.07.2017	;;
 ;; Tag hatches with Attributed Block	;;
 ;; that entitled: LNAME then add the	;;
 ;; Hatch layer into the previous said	;;
 ;; Att.Block as a field object in Att.;;
 ;;------------------------------------;;
 (if (and (or (and (setq blk (tblsearch "BLOCK" "LNAME"))
                   (= (cdr (assoc 70 blk)) 2)
                   )
              (alert  "Block Name <LNAME> is either not found in drawing or it's not attributed block <!>")
              )
          (princ "\nSelect hatches :")
          (setq int -1
                doc (vla-get-activedocument (vlax-get-acad-object))
                sel (ssget "_:L" '((0 . "HATCH")))
                )
          
   )
   (progn
      (defun getid (obj doc / u)
        (if (vlax-method-applicable-p (setq u (vla-get-utility doc)) 'getobjectidstring)
          (vla-getobjectidstring u obj :vlax-false)
          (itoa (vla-get-objectid obj))
        )
      )
      (while (setq ent (ssname sel (setq int (1+ int))))
        (vla-getboundingbox (setq hat (vlax-ename->vla-object ent)) 'q 'p)
        (and
          (setq lst (mapcar 'vlax-safearray->list (list q p))
                blk (vlax-invoke
                      (vla-get-block (vla-get-activelayout doc))
                      'insertblock
                      (mapcar '(lambda (j k) (* (+ j k) 0.5)) (car lst) (cadr lst))
                      "LNAME"
                      1.0
                      1.0
                      1.0
                      0.0
                    )
          )
          (vl-some '(lambda (att)
                      (and (= (strcase (vla-get-tagstring att)) "LNAME")
                           (progn
                             (vla-put-textstring att (strcat "%<\\AcObjProp Object(%<\\_ObjId " (getid hat doc) ">%).Layer>%")) t)
                      )
                    )
                   (vlax-invoke blk 'getattributes)
          )
        )
      )
      (and doc (vla-regen doc acactiveviewport))
    )
 )
 (princ)
) (vl-load-com)

Link to comment
Share on other sites

Tharwat, here you are again!

 

Thank you guys both! Both are working but Tharwat's code places the block on the current layer as we require to place them on the layer for tags.

 

One last question:

 

The code places tags on centroids. So when I try to tag an L shaped object, tag places on void as presented on the attachment.

Is it possible to place the tag somewhere on the hatch pattern?

 

Thanks in advance.

 

MA

LISP-TAGGING_V3.dwg

Link to comment
Share on other sites

Hi,

 

Actually the codes not placing the Attributed block LNAME in the centriod point but the center point of the bounding box that is between the Bottom Left & the Top Right side points.

 

Back to your request about changing the location of the Attributed block, so is it always supposed to be placed on the Top Left side hand corner as shown into your last uploaded drawing?

And will it always be with the same offset distance?

Link to comment
Share on other sites

... so is it always supposed to be placed on the Top Left side hand corner as shown into your last uploaded drawing?

And will it always be with the same offset distance?

 

Dear Tharwat,

 

The main purpose it to keep tags touching to the visible part of the associated hatch pattern. Please check out the attachment for my reply.

 

MA.

LISP-TAGGING_V4.dwg

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