Wblock Automation Using Attributes as File Name

Wblock Automation Using Attributes as File Name

matthew.butkevitch
Explorer Explorer
1,376 Views
14 Replies
Message 1 of 15

Wblock Automation Using Attributes as File Name

matthew.butkevitch
Explorer
Explorer

I'm very new to AutoLisp and I am looking to create a window selection of a sheet and it find the block using its name and save the Attribute (TITLE) as a variable. It will do it again for a new block with a new name which was inside of the original window and using it's name find the Attribute (TITLE) and save it as a variable. It will run a Wblock command and save the file name for example "H-001 Turning Vane". The second window selection will output the file named "H-002 Duct Transition" . If possible to delete the blocks only in the saved file afterwards as well that would be great. I can provide much more in detail on what I would like if I need to.

0 Likes
Accepted solutions (1)
1,377 Views
14 Replies
Replies (14)
Message 2 of 15

ronjonp
Mentor
Mentor

Why do you have two "title blocks" with title information? Couldn't this be simplified by combing that information into one?

ronjonp_0-1652396971161.png

 

0 Likes
Message 3 of 15

matthew.butkevitch
Explorer
Explorer

The thing is I have hundreds of them which have varying "H-XXX" and differing names. If I could automatically change the name for each one that would be great instead of having to manually edit every single one. The title block which has "H-001" in it has a different block name and a different attribute name. I've attached a new file which makes it easier to see what I am trying to accomplish. Thanks in advance.

0 Likes
Message 4 of 15

ronjonp
Mentor
Mentor

@matthew.butkevitch 

Are the surrounding rectangles always the same size or can they be differentiated by a layer?

0 Likes
Message 5 of 15

Sea-Haven
Mentor
Mentor

Bounding box of "FILE BLOCK" then a bpoly pick point just off lower left, look at what was made ? A pline of rectang maybe just a guess idea. Need time to play. 

0 Likes
Message 6 of 15

trevor.bird.au
Advocate
Advocate

Hi Matthew,

 

Please find my solution below.

It's based on the later sample drawing you provided which uses 2 blocks.

Checks were required on the attribute values used for constructing the block name because some of the values were suffixed with spaces which are not valid for block naming.

I've also included the polyline box in the block definition; if it shouldn't be included then the code can easily be updated to exclude it.

;;  WBFileBlock.lsp by Trevor Bird
;;
;;  2022-05-15

;;------------------------------------------------------------------------------
(defun c:wbfileblock
  (
    /

    ascii_list
    attribs_list
    attribs_var

    bl_wcs_list

    col_Blocks
    col_Block_area

    Coordinates_ocs_list
    Coordinates_ocs_var
    Coordinates_ucs_list
    Coordinates_wcs_list

    Coordinate_ocs_list
    Coordinate_ocs_var
    Coordinate_wcs_list

    detail_block_str
    dp_410

    ename_polyline

    file_block_str
    file_name_str

    insert1_ename
    insert2_ename
    insert_name

    list_block_objects

    mt_4x4_wcs1_list
    mt_4x4_wcs2_list

    ObjectName
    objects1_sa
    objects2_list
    objects2_var
    obj_ACAD
    obj_AD
    obj_insert

    ss_insert1
    ss_insert2
    ss_objects
    ss_objects__enames
    ss_objects__objects
    ss_polylines
    ss_polylines__enames
    ss_polylines__objects

    tagstring_str
    textstring_str

    vertex_count
    vertex_index
    vertex_Z
  )
  (if (= (getvar 'CVPORT) 1)
    (setq dp_410 (cons 410 (getvar 'CTAB)))
    (setq dp_410 '(410 . "Model"))
  );if (= (getvar 'CVPORT) 1)


  (setq ss_polylines
    (ssget
      (list
        '(0 . "*POLYLINE")
        '(-4 . "<AND")
          '(-4 . "&")
          '(70 . 1)       ;  1 = This is a closed polyline
          '(-4 . "<NOT")  ;  Exclude 3D polylines
            '(-4 . "&=")  ;  8 = This is a 3D polyline
            '(70 . 8)
          '(-4 . "NOT>")
        '(-4 . "AND>")
        dp_410
      );list
    );ssget
  );setq


  (cond
    ( (not ss_polylines)
      (princ "\nNo closed polylines selected.")
    );(not ss_polylines)


    (ss_polylines
      (setq obj_ACAD   (vlax-get-acad-object)
            obj_AD     (vlax-get-property obj_ACAD 'ActiveDocument)
            col_Blocks (vlax-get-property obj_AD 'Blocks)
      );setq

      (setq ss_polylines__enames  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss_polylines)))
            ss_polylines__objects (mapcar 'vlax-ename->vla-object ss_polylines__enames)
      );setq

      (foreach fe__obj_polyline ss_polylines__objects
        (setq ename_polyline       (vlax-vla-object->ename fe__obj_polyline)

              ObjectName           (vlax-get-property fe__obj_polyline 'ObjectName)
              Coordinates_ocs_var  (vlax-get-property fe__obj_polyline 'Coordinates)
              Coordinates_ocs_list (vlax-safearray->list (vlax-variant-value Coordinates_ocs_var))

              Coordinates_wcs_list nil
        );setq


        (cond
          ( (= ObjectName "AcDbPolyline")
            ;;  Lightweight Polyline
            (setq vertex_count (/ (length Coordinates_ocs_list) 2)
                  vertex_Z     (vlax-get-property fe__obj_polyline 'Elevation)
            );setq
          );(= ObjectName "AcDbPolyline")

          ( (or
                (= ObjectName "AcDb2dPolyline")
                (= ObjectName "AcDb3dPolyline")
            );or
            ;;  Heavy Polyline
            (setq vertex_count (/ (length Coordinates_ocs_list) 3)
                  vertex_Z     nil
            );setq
          );
        );cond


        (setq vertex_index 0)

        (while (< vertex_index vertex_count)
          (setq Coordinate_ocs_var  (vlax-get-property fe__obj_polyline 'Coordinate vertex_index)
                Coordinate_ocs_list (vlax-safearray->list (vlax-variant-value Coordinate_ocs_var))
                vertex_index        (1+ vertex_index)
          );setq

          (if vertex_Z
            (setq Coordinate_ocs_list (append Coordinate_ocs_list (list vertex_Z)))
          );if vertex_Z

          (setq Coordinate_wcs_list  (trans Coordinate_ocs_list ename_polyline 0)
                Coordinates_wcs_list (append Coordinates_wcs_list (list Coordinate_wcs_list))
          );setq
        );while (< vertex_index vertex_count)

        (setq Coordinates_ucs_list (mapcar (function (lambda ( _point_wcs ) (trans _point_wcs 0 1))) Coordinates_wcs_list))

        (cond
          ( (not
              (setq ss_insert1
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(0 . "INSERT")
                    '(2 . "FILE BLOCK")
                    '(66 . 1)
                    dp_410
                  );list
                );ssget
              );setq
            );not
            (princ "\n") (prin1 "FILE BLOCK") (princ " not found.")
          );


          ( (not
              (progn
              (setq ss_insert2
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(0 . "INSERT")
                    '(2 . "DETAIL TITLE BLOCK")
                    '(66 . 1)
                    dp_410
                  );list
                );ssget
              );setq
              );progn
            );not
            (princ "\n") (prin1 "DETAIL TITLE BLOCK") (princ " not found.")
          );


          ( (not
              (setq ss_objects
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(-4 . "<NOT")
                      '(-4 . "<AND")
                        '(0 . "INSERT")
                        '(2 . "FILE BLOCK,DETAIL TITLE BLOCK")
                      '(-4 . "AND>")
                    '(-4 . "NOT>")
                    dp_410
                  );list
                );ssget
              );setq
            );not
            (princ "\nNo objects found for file block creation.")
          );


          ( (not
              (progn
                (setq insert1_ename       (ssname ss_insert1 0)
                      insert2_ename       (ssname ss_insert2 0)
                      ss_objects__enames  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss_objects)))
                      ss_objects__objects (mapcar 'vlax-ename->vla-object ss_objects__enames)
                      file_block_str      nil
                      detail_block_str    nil
                );setq

                ;;  Process attributes of blocks to determine file name components
                (foreach fe__ename
                  (list
                    insert1_ename
                    insert2_ename
                  );list
                  (setq obj_insert   (vlax-ename->vla-object fe__ename)
                        insert_name  (vlax-get-property obj_insert 'Name)
                        attribs_var  (vlax-invoke-method obj_insert 'GetAttributes)
                        attribs_list (vlax-safearray->list (vlax-variant-value attribs_var))
                  );setq

                  (foreach fe__obj_attrib attribs_list
                    (setq tagstring_str (vlax-get-property fe__obj_attrib 'TagString))

                    (cond
                      ( (and
                            (/= tagstring_str "DPS-#")
                            (/= tagstring_str "TITLE")
                        );and
                      );

                      ( (not
                          (progn
                            (setq textstring_str (vlax-get-property fe__obj_attrib 'TextString)
                                  ascii_list     (vl-remove-if (function (lambda ( _chr_code ) (= (ascii " ") _chr_code))) (vl-string->list textstring_str))
                            );setq
                          );progn
                        );not
                        ;;  Attribute value is empty.
                        (princ "\nRequired attribute value is empty in block ") (prin1 insert_name) (princ ".")
                      );

                      ( (not
                          (setq textstring_str (vl-string-right-trim " " textstring_str)
                                textstring_str (vl-string-left-trim " " textstring_str)
                                ascii_list     (vl-remove-if (function (lambda ( _chr_code ) (= (ascii " ") _chr_code))) (vl-string->list textstring_str))
                          );setq
                        );not
                      );

                      ( (and
                            (not file_block_str)
                        );and
                        (setq file_block_str textstring_str)
                      );

                      ( (and
                            (not detail_block_str)
                        );and
                        (setq detail_block_str textstring_str)
                      );
                    );cond

                    (vlax-release-object fe__obj_attrib)
                  );fe__obj_attrib

                  (vlax-release-object obj_insert)
                );fe__ename

                (and
                    file_block_str
                    detail_block_str
                );and
              );progn
            );not
            (princ "\nFile block name cannot be determined from the attributed blocks.")
          );


          ( (and
                file_block_str
                tagstring_str
            );and
            (setq file_name_str (strcat file_block_str " " detail_block_str))

            (cond
              ( (not (tblsearch "BLOCK" file_name_str))
                (setq col_Block_area (vlax-invoke-method col_Blocks 'Add (vlax-3D-point '(0.0 0.0 0.0)) file_name_str))

                (vlax-put-property col_Block_area 'BlockScaling acUniform)
                (vlax-put-property col_Block_area 'Explodable :vlax-true)
                (vlax-put-property col_Block_area 'Units (getvar 'INSUNITS))

                ;;  Determine bottom left corner of selected area
                (setq bl_wcs_list
                  (list
                    (apply 'min (mapcar 'car Coordinates_wcs_list))
                    (apply 'min (mapcar 'cadr Coordinates_wcs_list))
                    (apply 'min (mapcar 'caddr Coordinates_wcs_list))
                  );list
                );setq

                (setq list_block_objects (list fe__obj_polyline)
                      list_block_objects (append list_block_objects ss_objects__objects)
                      objects1_sa        (vlax-make-safearray vlax-vbObject (cons 0 (1- (length list_block_objects))))
                );setq

                (vlax-safearray-fill objects1_sa list_block_objects)

                (setq objects2_var  (vlax-invoke-method obj_AD 'CopyObjects objects1_sa col_Block_area nil)
                      objects2_list (vlax-safearray->list (vlax-variant-value objects2_var))
                );setq

                ;;  Insertion base point of block definition is 0,0,0.
                ;;  The bottom left corner of each selected polyline is not 0,0,0.
                ;;  Transform objects in block definition to be relative to 0,0,0.
                (setq mt_4x4_wcs1_list
                  (list
                    '(1.0 0.0 0.0 0.0)
                    '(0.0 1.0 0.0 0.0)
                    '(0.0 0.0 1.0 0.0)
                    (append (mapcar '- bl_wcs_list) '(1.0))
                  );list

                      mt_4x4_wcs2_list (apply 'mapcar (cons 'list mt_4x4_wcs1_list))
                );setq

                (foreach fe__object2 objects2_list
                  (vlax-invoke-method fe__object2 'TransformBy (vlax-tmatrix mt_4x4_wcs2_list))
                );fe__object2

                (princ "\nFile block created: ") (prin1 file_name_str)
              );(not (tblsearch "BLOCK" file_name_str))


              (T
                (princ "\nFile block exists: ") (prin1 file_name_str)
              );T
            );cond
            ;;  Create block  -->
          );
        );cond

        (vlax-release-object fe__obj_polyline)
      );fe__obj_polyline

      (vlax-release-object col_Blocks)
      (vlax-release-object obj_ACAD)
      (vlax-release-object obj_AD)
    );ss_polylines
  );cond

  (princ)
);c:wbfileblock


;;------------------------------------------------------------------------------
(princ "\nWBFileBlock loaded.  Start command with WBFILEBLOCK.")
(princ)

Regards,

Trevor Bird

0 Likes
Message 7 of 15

trevor.bird.au
Advocate
Advocate

Hi Matthew,

 

My apologies, I forgot to include the Screencast.

 

https://knowledge.autodesk.com/community/screencast/3e65cb20-32e3-4cb2-84f5-0e1f786d3d03https://foru...

 

Regards,

Trevor Bird

0 Likes
Message 8 of 15

Sea-Haven
Mentor
Mentor

To many picks should be able to just select all that is it, you know the name of the 2 blocks or at worst 2 picks to get their names and make a  selection set can look for the top right block one at a time. I am busy at moment or would do code, a quick check dummy layer, do bpoly pick pt just off the "FILE BLOCK" multi polys are made, just find the 1 with largest area. This gives the window select for wblock, it also allows select the other block "DETAIL TITLE BLOCK" using "WP" within polygon. so you have everything you need. Erase dummy bpolys and do next. The code should be much shorter also.

 

The green is the bpoly's

 

SeaHaven_0-1652585385458.png

 

Ps erase the 2 output results when testing dwg supplied. Need to check wblock result.

 

0 Likes
Message 9 of 15

trevor.bird.au
Advocate
Advocate

Hi Alan,

 

The solution I provided allows for a single "select all" and filters for the closed polylines, the 2 attributed blocks and all other objects required for creating each file block, inside each of the closed polyline boundaries.

This is the only user interaction required for all the required file blocks to be automatically created.

 

The insertion point (bottom left corner of the polyline boundary) of each block is determined programmatically from the polyline boundary without the need for the user to select a start point.

 

Additional checks are made during execution of the code, ensuring that all the required information is valid for final processing and creation of the file blocks.

 

Your assertion about my code should be reserved until you're actually able to provide a solution yourself.

An effective and efficient solution is not necessarily reflected in the number of lines of code.

 

Regards,

Trevor Bird

0 Likes
Message 10 of 15

Sea-Haven
Mentor
Mentor

Try this as a alternative, need more details about where the wblock will end up. I have hard set a directory for now. 

 

It needs also a does wblock dwg already exist check.

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/wblock-automation-using-attributes-as-file-name/td-p/11165560
; a alternative by AlanH May 2022

(defun c:wblocks ( / pt ll ent ent2 APL BPAREA BPREC CO-ORD MAXPOINT MINPOINT OLDLAY OLDSNAP PATH PT SS SS2 TSR1 TSR2 WBFNAME X Y)

(defun ll (entpl / bll bur )
(vla-GetBoundingBox (vlax-ename->vla-object entpl) 'minpoint 'maxpoint)
(setq bll (vlax-safearray->list minpoint))
(setq bur (vlax-safearray->list maxpoint))
(setq pt (mapcar '+ bll (list -0.1 -0.1 0.0)))
)

(command "-layer" "m" "dummyplines" "c" "3" "" "")

(setq path "d:\\acadtemp\\")
(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setq oldlay (getvar 'clayer))

(setq ss (ssget (list (cons 0 "INSERT")(cons 2 "FILE BLOCK"))))

(if (= ss nil)
(progn (alert "No selections will now exit\n\ntry again")(Exit))
(progn
(repeat (setq x (sslength ss))
(setq ent (ssname ss (setq x (1- x))))
(ll ent)
(command "zoom" "extents")
(command "bpoly" pt "")

(setq ss2 (ssget "X" (list (cons 0 "Lwpolyline")(cons 8 "dummyplines"))))
(if (= ss2 nil)
(progn (alert "No bpolys will now exit\n\ntry again")(Exit))
(progn
(setq apl 0.0)
(repeat (setq y (sslength ss2))
(setq ent2 (ssname ss2 (setq y (1- y))))
(setq bparea (vla-get-area (vlax-ename->vla-object ent2)))
(if (> bparea apl)
(setq bprec ent2 apl bparea)
)
)
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget bprec))))

(setq ss2 (ssget "wp" co-ord (list (cons 0 "INSERT")(cons 2 "FILE BLOCK"))))
(foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS2 0)) 'getattributes)
(if (= "DPS-#" (strcase (vla-get-tagstring att)))
(setq tsr1 (vla-get-textstring att))
)
)

(setq ss2 (ssget "WP" co-ord (list (cons 0 "INSERT")(cons 2 "DETAIL TITLE BLOCK"))))
(foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS2 0 )) 'getattributes)
(if (= "TITLE"(vla-get-tagstring att))
(setq tsr2 (vla-get-textstring att))
)
)

(setq wbfname (strcat path tsr1 "-" tsr2))
(command "erase" (ssget "X" '((8 . "dummyplines"))) "")

(setq ss2 (ssget "CP" co-ord ))
(command "-wblock" wbfname "" "0,0" ss2 "" ) ; for plain autocad and Bricscad
;(command "-wblock" wbfname "" "0,0" ss2 "" "N") ; for CIV3D

)
)

)
)
)

(setvar 'clayer oldlay)
(command "-purge" "LA" "dummyplines" "n" "")
(setvar 'osmode oldsnap)
(princ)
)

 

 

0 Likes
Message 11 of 15

trevor.bird.au
Advocate
Advocate

Hi Matthew,

 

I've updated my code to WBLOCK the block to a drawing file in the same folder as the currently open drawing.

If the block drawing file exists in the folder, it will be deleted and replaced.

 

Also, if the block already exists in the drawing, it won't be redefined if new objects are selected.

If the block needs to be redefined, then all inserts will need to be deleted, then PURGE the block from the drawing.

If required, I can modify my code to always redefine the block if it exists.

;;  WBFileBlock.lsp by Trevor Bird
;;
;;  2022-05-17

;;------------------------------------------------------------------------------
(defun c:wbfileblock
  (
    /

    acadver_int
    acadver_str
    ascii_list
    attribs_list
    attribs_var

    bl_wcs_list

    col_Blocks
    col_Block_area

    Coordinates_ocs_list
    Coordinates_ocs_var
    Coordinates_ucs_list
    Coordinates_wcs_list
    Coordinate_ocs_list
    Coordinate_ocs_var
    Coordinate_wcs_list

    detail_block_str
    dp_410

    ename_polyline

    file_block_str
    file_fqn
    file_name_str

    insert1_ename
    insert2_ename
    insert_name

    list_block_objects

    mt_4x4_wcs1_list
    mt_4x4_wcs2_list

    ObjectName
    objects1_sa
    objects2_list
    objects2_var
    obj_ACAD
    obj_AD
    obj_insert

    ss_insert1
    ss_insert2
    ss_objects
    ss_objects__enames
    ss_objects__objects
    ss_polylines
    ss_polylines__enames
    ss_polylines__objects

    sv_acadver
    sv_dwgprefix

    tagstring_str
    textstring_str

    vertex_count
    vertex_index
    vertex_Z
  )
  (if (= (getvar 'CVPORT) 1)
    (setq dp_410 (cons 410 (getvar 'CTAB)))
    (setq dp_410 '(410 . "Model"))
  );if (= (getvar 'CVPORT) 1)


  (setq ss_polylines
    (ssget
      (list
        '(0 . "*POLYLINE")
        '(-4 . "<AND")
          '(-4 . "&")
          '(70 . 1)       ;  1 = This is a closed polyline
          '(-4 . "<NOT")  ;  Exclude 3D polylines
            '(-4 . "&=")  ;  8 = This is a 3D polyline
            '(70 . 8)
          '(-4 . "NOT>")
        '(-4 . "AND>")
        dp_410
      );list
    );ssget
  );setq


  (cond
    ( (not ss_polylines)
      (princ "\nNo closed polylines selected.")
    );(not ss_polylines)


    (ss_polylines
      (setq obj_ACAD     (vlax-get-acad-object)
            obj_AD       (vlax-get-property obj_ACAD 'ActiveDocument)
            col_Blocks   (vlax-get-property obj_AD 'Blocks)

            sv_acadver   (getvar 'ACADVER)
            acadver_int  (atoi sv_acadver)
            acadver_str  (itoa acadver_int)

            sv_dwgprefix (getvar 'DWGPREFIX)
      );setq

      (setq ss_polylines__enames  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss_polylines)))
            ss_polylines__objects (mapcar 'vlax-ename->vla-object ss_polylines__enames)
      );setq

      (foreach fe__obj_polyline ss_polylines__objects
        (setq ename_polyline       (vlax-vla-object->ename fe__obj_polyline)
              ObjectName           (vlax-get-property fe__obj_polyline 'ObjectName)
              Coordinates_ocs_var  (vlax-get-property fe__obj_polyline 'Coordinates)
              Coordinates_ocs_list (vlax-safearray->list (vlax-variant-value Coordinates_ocs_var))
              Coordinates_wcs_list nil
        );setq

        (cond
          ( (= ObjectName "AcDbPolyline")
            ;;  Lightweight Polyline
            (setq vertex_count (/ (length Coordinates_ocs_list) 2)
                  vertex_Z     (vlax-get-property fe__obj_polyline 'Elevation)
            );setq
          );(= ObjectName "AcDbPolyline")

          ( (or
                (= ObjectName "AcDb2dPolyline")
                (= ObjectName "AcDb3dPolyline")
            );or
            ;;  Heavy Polyline
            (setq vertex_count (/ (length Coordinates_ocs_list) 3)
                  vertex_Z     nil
            );setq
          );
        );cond

        (setq vertex_index 0)

        (while (< vertex_index vertex_count)
          (setq Coordinate_ocs_var  (vlax-get-property fe__obj_polyline 'Coordinate vertex_index)
                Coordinate_ocs_list (vlax-safearray->list (vlax-variant-value Coordinate_ocs_var))
                vertex_index        (1+ vertex_index)
          );setq

          (if vertex_Z
            (setq Coordinate_ocs_list (append Coordinate_ocs_list (list vertex_Z)))
          );if vertex_Z

          (setq Coordinate_wcs_list  (trans Coordinate_ocs_list ename_polyline 0)
                Coordinates_wcs_list (append Coordinates_wcs_list (list Coordinate_wcs_list))
          );setq
        );while (< vertex_index vertex_count)

        (setq Coordinates_ucs_list (mapcar (function (lambda ( _point_wcs ) (trans _point_wcs 0 1))) Coordinates_wcs_list))

        (cond
          ( (not
              (setq ss_insert1
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(0 . "INSERT")
                    '(2 . "FILE BLOCK")
                    '(66 . 1)
                    dp_410
                  );list
                );ssget
              );setq
            );not
            (princ "\n") (prin1 "FILE BLOCK") (princ " not found.")
          );


          ( (not
              (setq ss_insert2
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(0 . "INSERT")
                    '(2 . "DETAIL TITLE BLOCK")
                    '(66 . 1)
                    dp_410
                  );list
                );ssget
              );setq
            );not
            (princ "\n") (prin1 "DETAIL TITLE BLOCK") (princ " not found.")
          );


          ( (not
              (setq ss_objects
                (ssget "_WP"
                  Coordinates_ucs_list
                  (list
                    '(-4 . "<NOT")
                      '(-4 . "<AND")
                        '(0 . "INSERT")
                        '(2 . "FILE BLOCK,DETAIL TITLE BLOCK")
                      '(-4 . "AND>")
                    '(-4 . "NOT>")
                    dp_410
                  );list
                );ssget
              );setq
            );not
            (princ "\nNo objects found for file block creation.")
          );


          ( (not
              (progn
                (setq insert1_ename       (ssname ss_insert1 0)
                      insert2_ename       (ssname ss_insert2 0)
                      ss_objects__enames  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss_objects)))
                      ss_objects__objects (mapcar 'vlax-ename->vla-object ss_objects__enames)
                      file_block_str      nil
                      detail_block_str    nil
                );setq

                ;;  Process attributes of blocks to determine file name components
                (foreach fe__ename
                  (list
                    insert1_ename
                    insert2_ename
                  );list
                  (setq obj_insert   (vlax-ename->vla-object fe__ename)
                        insert_name  (vlax-get-property obj_insert 'Name)
                        attribs_var  (vlax-invoke-method obj_insert 'GetAttributes)
                        attribs_list (vlax-safearray->list (vlax-variant-value attribs_var))
                  );setq

                  (foreach fe__obj_attrib attribs_list
                    (setq tagstring_str (vlax-get-property fe__obj_attrib 'TagString))

                    (cond
                      ( (and
                            (/= tagstring_str "DPS-#")
                            (/= tagstring_str "TITLE")
                        );and
                      );

                      ( (not
                          (setq textstring_str (vlax-get-property fe__obj_attrib 'TextString)
                                ascii_list     (vl-remove-if (function (lambda ( _chr_code ) (= (ascii " ") _chr_code))) (vl-string->list textstring_str))
                          );setq
                        );not
                        ;;  Attribute value is empty.
                        (princ "\nRequired attribute value is empty in block ") (prin1 insert_name) (princ ".")
                      );

                      ( (not
                          (setq textstring_str (vl-string-right-trim " " textstring_str)
                                textstring_str (vl-string-left-trim " " textstring_str)
                                ascii_list     (vl-remove-if (function (lambda ( _chr_code ) (= (ascii " ") _chr_code))) (vl-string->list textstring_str))
                          );setq
                        );not
                      );

                      ( (not file_block_str)
                        (setq file_block_str textstring_str)
                      );(not file_block_str)

                      ( (not detail_block_str)
                        (setq detail_block_str textstring_str)
                      );(not detail_block_str)
                    );cond

                    (vlax-release-object fe__obj_attrib)
                  );fe__obj_attrib

                  (vlax-release-object obj_insert)
                );fe__ename

                (and
                    file_block_str
                    detail_block_str
                );and
              );progn
            );not
            (princ "\nFile block name cannot be determined from the attributed block values.")
          );


          ( (and
                file_block_str
                tagstring_str
            );and
            (setq file_name_str (strcat file_block_str " " detail_block_str)
                  file_fqn      (strcat sv_dwgprefix file_name_str ".dwg")
            );setq

            (if (findfile file_fqn)
              (vl-file-delete file_fqn)
            );if (findfile file_fqn)

            (setq list_block_objects (list fe__obj_polyline)
                  list_block_objects (append list_block_objects ss_objects__objects)
                  objects1_sa        (vlax-make-safearray vlax-vbObject (cons 0 (1- (length list_block_objects))))
            );setq

            (vlax-safearray-fill objects1_sa list_block_objects)

            ;;  Determine bottom left corner of selected area.
            ;;  This point will be the insertion point of the block.
            (setq bl_wcs_list
              (list
                (apply 'min (mapcar 'car Coordinates_wcs_list))
                (apply 'min (mapcar 'cadr Coordinates_wcs_list))
                (apply 'min (mapcar 'caddr Coordinates_wcs_list))
              );list
            );setq

            ;;  Insertion base point of block definition is 0,0,0.
            ;;  The bottom left corner of each selected polyline is not 0,0,0.
            ;;  Transform objects in block definition to be relative to 0,0,0.
            (setq mt_4x4_wcs1_list
              (list
                '(1.0 0.0 0.0 0.0)
                '(0.0 1.0 0.0 0.0)
                '(0.0 0.0 1.0 0.0)
                (append (mapcar '- bl_wcs_list) '(1.0))
              );list

                  mt_4x4_wcs2_list (apply 'mapcar (cons 'list mt_4x4_wcs1_list))
            );setq

            ;;  <!--  Create block
            (if (not (tblsearch "BLOCK" file_name_str))
              (progn
                ;;  Block does not exist.
                (setq col_Block_area (vlax-invoke-method col_Blocks 'Add (vlax-3D-point '(0.0 0.0 0.0)) file_name_str))

                (vlax-put-property col_Block_area 'BlockScaling acUniform)
                (vlax-put-property col_Block_area 'Explodable :vlax-true)
                (vlax-put-property col_Block_area 'Units (getvar 'INSUNITS))

                (setq objects2_var  (vlax-invoke-method obj_AD 'CopyObjects objects1_sa col_Block_area nil)
                      objects2_list (vlax-safearray->list (vlax-variant-value objects2_var))
                );setq

                (foreach fe__object2 objects2_list
                  (vlax-invoke-method fe__object2 'TransformBy (vlax-tmatrix mt_4x4_wcs2_list))
                );fe__object2

                (vlax-release-object col_Block_area)

                (princ "\nFile block created: ") (prin1 file_name_str)
              );progn
            );if
            ;;  Create block  -->

            ;;  <!--  WBlock using Command Line Script
             (vl-cmdf "_.EXPORT" file_fqn file_name_str)
            ;;  WBlock using Command Line Script  -->
          );
        );cond

        (vlax-release-object fe__obj_polyline)
      );fe__obj_polyline


      (vlax-release-object col_Blocks)
      (vlax-release-object obj_ACAD)
      (vlax-release-object obj_AD)
    );ss_polylines
  );cond

  (princ)
);c:wbfileblock


;;------------------------------------------------------------------------------
(princ "\nWBFileBlock loaded.  Start command with WBFILEBLOCK.")
(princ)

Regards,

Trevor Bird 

0 Likes
Message 12 of 15

matthew.butkevitch
Explorer
Explorer

Thanks for your help Trevor, I have attached a file which is a better representation of what needs to be copied. I realized that the information I included did not fully encompass the needs of my drawing. The file I've attached has text and dimensions which would also need to included in the block. If you can do this it would be amazing. Thanks in advance.

0 Likes
Message 13 of 15

trevor.bird.au
Advocate
Advocate
Accepted solution

Hi Matthew,

 

In the latest example drawing you provided the required block already exists and won't be redefined with the additional text and dimension objects.

Please purge the drawing of all blocks and then run the program.

 

If you prefer, I can update my program to always redefine the block which would negate the need to purge.

 

I'm glad I can help too, thanks.

 

Regards,

Trevor Bird

0 Likes
Message 14 of 15

matthew.butkevitch
Explorer
Explorer
Thanks! Works perfectly will definitely help with the hundreds of blocks which need to be created.
0 Likes
Message 15 of 15

trevor.bird.au
Advocate
Advocate

Hi Matthew,

 

Thanks for your feedback, I'm glad I was able to help.

 

Regards,

Trevor Bird

0 Likes