+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Junior Member
    Computer Details
    mhmtlgrr's Computer Details
    Operating System:
    Windows 7
    Computer:
    HP Workstation Z240
    Discipline
    Architectural
    mhmtlgrr's Discipline Details
    Occupation
    Architect
    Discipline
    Architectural
    Using
    AutoCAD 2015
    Join Date
    Jul 2012
    Posts
    11

    AutoCAD Lisp for tagging hatch objects

    Registered forum members do not see this ad.

    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
    Attached Files

  2. #2
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,751

    Default

    Ran out of time but try this

    Code:
    (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)
    )
    A man who never made mistakes never made anything

  3. #3
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,080

    Default

    Code:
    (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)

  4. #4
    Junior Member
    Computer Details
    mhmtlgrr's Computer Details
    Operating System:
    Windows 7
    Computer:
    HP Workstation Z240
    Discipline
    Architectural
    mhmtlgrr's Discipline Details
    Occupation
    Architect
    Discipline
    Architectural
    Using
    AutoCAD 2015
    Join Date
    Jul 2012
    Posts
    11

    AutoCAD

    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
    Attached Files

  5. #5
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,751

    Default

    (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.
    A man who never made mistakes never made anything

  6. #6
    Junior Member
    Computer Details
    mhmtlgrr's Computer Details
    Operating System:
    Windows 7
    Computer:
    HP Workstation Z240
    Discipline
    Architectural
    mhmtlgrr's Discipline Details
    Occupation
    Architect
    Discipline
    Architectural
    Using
    AutoCAD 2015
    Join Date
    Jul 2012
    Posts
    11

    Default

    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")

  7. #7
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,080

    Default

    Quote Originally Posted by BIGAL View Post
    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.

    Quote Originally Posted by mhmtlgrr View Post
    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 . "*")

    Quote Originally Posted by mhmtlgrr View Post
    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"

    Code:
    (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)

  8. #8
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,053

    Default

    Hi,

    Here is my attempt with Field object.
    Code:
    (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)

  9. #9
    Junior Member
    Computer Details
    mhmtlgrr's Computer Details
    Operating System:
    Windows 7
    Computer:
    HP Workstation Z240
    Discipline
    Architectural
    mhmtlgrr's Discipline Details
    Occupation
    Architect
    Discipline
    Architectural
    Using
    AutoCAD 2015
    Join Date
    Jul 2012
    Posts
    11

    Default

    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
    Attached Files

  10. #10
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,053

    Default

    Registered forum members do not see this ad.

    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?

Similar Threads

  1. Replies: 2
    Last Post: 12th Apr 2016, 10:56 pm
  2. Replies: 14
    Last Post: 21st Jan 2015, 06:23 am
  3. Lisp to select and hatch objects
    By pixel8er in forum AutoLISP, Visual LISP & DCL
    Replies: 26
    Last Post: 4th Aug 2011, 05:09 pm
  4. Tagging A Grid
    By Dynamooo in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 13
    Last Post: 1st Jun 2010, 02:01 pm
  5. Tagging a object
    By Trebuchet in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 14th Oct 2008, 07:51 pm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts