Author Topic: Get block list and rename blocks....  (Read 17313 times)

0 Members and 1 Guest are viewing this topic.

KewlToyZ

  • Guest
Get block list and rename blocks....
« on: August 10, 2009, 04:56:50 PM »
I'm trying to get through a routine to rename the blocks in the current drawing.
But I'm not sure how to prevent the list from getting XREF names and nested blocks.
This returns all block names, XREF's & nested blocks:

Code: [Select]
(defun c:listblocks (/ blockName bName tempList)
  (vlax-for blockName (vla-get-blocks
                (vla-get-ActiveDocument (vlax-get-acad-object))
              )
    (if (= (vla-get-islayout blockName) :vlax-false)
      (setq tempList (cons (vla-get-name blockName) tempList))
    )
  )
  (reverse tempList)
)

I am just looking to rename each block with a number from a counter,
i.e 1, 2, 3, 4, etc...

i tried playing around with this too but it doesn't see any blocks in the drawing:
Code: [Select]
(defun c:BLRENAME()
;===========================================================================Turn off command line responses
(command "CMDECHO" 0)
;===========================================================================
(if (setq AllBlocks (ssget "_X" '((0 . "BLOCK")))) ; begin if
(progn
(setq NumBlocks (sslength AllBlocks))
(setq Count 0)

  (repeat NumBlocks ;;this cycles number of items
    (setq Ename (ssname AllBlocks Count))  ;;get entity name
    (setq Edata (entget Ename))
   
    (prompt "\n   Block found!! Please Wait......") ;amusement
    (setq Count (+ 1 Count))
(princ "\n Count = [ ")
(princ Count)
(command "-rename" "block" AllBlocks Count )
(princ " ] sorting next.....")
  )

 ) ;then
 (prompt "\n   No Block objects found!") ;else
 )
(princ)
;===========================================================================Turn on command line responses
(setvar "CMDECHO" 1)
;===========================================================================
)

I am just trying to compensate for -ExportToAutoCAD failing on their own aec symbol names being too long.
I can't remove aec content because of it.
I think they limited the memory for the blockname field too much in their function instead of matching what they allow.

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #1 on: August 10, 2009, 05:01:38 PM »
This is essentially what I run into trying to remove aec objects from files:

Quote
Command: -exporttoautocad File format: r14
Bind xrefs: No
Bind type: Insert
Filename prefix:
Filename suffix: -AEC-Removed


Export options [Format/Bind/bind Type/Maintain/Prefix/Suffix/?] <Enter for
filename>: Prefix
Filename prefix, or "." for none <>: .

Export options [Format/Bind/bind Type/Maintain/Prefix/Suffix/?] <Enter for
filename>: Suffix
Filename suffix, or "." for none <-AEC-Removed>: -AEC-Removed

Export options [Format/Bind/bind Type/Maintain/Prefix/Suffix/?] <Enter for
filename>: Bind
Bind xrefs [Yes/No] <No>: No

Export options [Format/Bind/bind Type/Maintain/Prefix/Suffix/?] <Enter for
filename>:
Export drawing name <J:\B\B09006.00\CADD\ELEC\B09006.00_E409-AEC-Removed.dwg>:
Skipping Xref "Zbase9"...
Skipping Xref "ZBorder 30x42"...
ERROR: could not shorten all symbol names!
Command:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get block list and rename blocks....
« Reply #2 on: August 10, 2009, 05:40:51 PM »
This may help some:
Code: [Select]
;;  CAB  10/09/2007
;;  Strip Mtext within blocks
(defun c:StripBlockMtext (/ adoc text_style_name text_height)
  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark adoc)
  (vlax-for blk (vla-get-blocks adoc)
    [color=red];; Exclude model and paper spaces, xref and anonymous blocks[/color]
    (if (and  (equal (vla-get-IsLayout blk) :vlax-false)
              (equal (vla-get-IsXref blk) :vlax-false) ;  Skip if xref
              (/= (substr (vla-get-Name blk) 1 1) "*") ;  Skip if anonymous
    )
(vlax-for ent blk
 (if (= (vla-get-objectname ent) "AcDbMText")
   (progn
     (setq str (strip_text (vla-get-textstring ent) "*"))
     (vl-catch-all-apply 'vla-put-textstring (list ent str))
   )
 )
      )
    )
  )
  (vla-regen adoc acactiveviewport)
  (vla-endundomark adoc)
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get block list and rename blocks....
« Reply #3 on: August 10, 2009, 05:46:06 PM »
In your second code, change this
Code: [Select]
(if (setq AllBlocks (ssget "_X" '((0 . "BLOCK")))) ; begin ifto this
Code: [Select]
(if (setq AllBlocks (ssget "_X" '((0 . "[color=red]INSERT[/color]")))) ; begin if
As you know block objects seen in the dwg are INSERT's while the definition of the INSERT is in the database and is called a BLOCK.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get block list and rename blocks....
« Reply #4 on: August 10, 2009, 06:14:38 PM »
Code: [Select]
(ai_table "BLOCK" 8)
Will list all blocks except Xrefs  :-)

Not sure if that will help you or not.  :-)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get block list and rename blocks....
« Reply #5 on: August 10, 2009, 06:16:57 PM »
Alternatively,

Code: [Select]
  (while (setq tdef (tblnext "BLOCK" (not tdef)))
    (if (not (eq (logand 4 (cdr (assoc 70 tdef))) 4))
      (setq lst (cons (cdr (assoc 2 tdef)) lst))))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get block list and rename blocks....
« Reply #6 on: August 10, 2009, 07:22:14 PM »
More on the alternate 8-)
Code: [Select]
;;  by MP
;;  http://groups.google.com/group/autodesk.autocad.customization/msg/d4f72d2a44e261e7
 ;  Note: 21 = 01 (anonymous) + _
 ;           04 (xref) + _
 ;           16 (xref dependent)


(defun getprimaryblocknames (/ data result)
  (while (setq data (tblnext "block" (null data)))
    ;; if it's not [xref, xref dependent, anonymous] ...
    (if (zerop (logand 21 (cdr (assoc 70 data))))
      (setq result (cons (cdr (assoc 2 data)) result))
    )
  )
  (if result
    (acad_strlsort result)
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #7 on: August 11, 2009, 08:55:56 AM »
Thanks Gents, I'll digest this for a bit and get it working.
I think I may be erroring out renaming aecb blocks which is what the -ExportToAutoCAD function is failing on in the symbol names being too long as it says. It doesn't make much sense that the aecb block content names are too long when AutoCAD MEP generated the names in the first place. I'm beginning to wonder if these are the symbols it is referring to or if it is the spaces in the regular block names causing the failure. Or I am looking at entirely the wrong objects/entities altogether.

Joe Burke

  • Guest
Re: Get block list and rename blocks....
« Reply #8 on: August 11, 2009, 09:08:09 AM »
I thiink it would help if you post an example file

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #9 on: August 11, 2009, 10:47:22 AM »
Sure, Here is an example file.
I am using MEP 2010 and saving the files back to 2007 format.

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #10 on: August 11, 2009, 12:50:18 PM »
Hmmm unloading the XREF's fixed the symbol name issue.
Going to check and see what else I can do to work around it.

Takes some of the mystery way.
Always helps me when I discuss things with others to find answers at times.
Right now I feel my avatar is closer to reality :lol:
« Last Edit: August 11, 2009, 12:53:19 PM by KewlToyZ »

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get block list and rename blocks....
« Reply #11 on: August 11, 2009, 12:56:38 PM »
I get this return on your posted drawing, is it not correct?

Code: [Select]
Command: (ai_table "BLOCK" 8)
("kta_eex006" "kta_eex003" "E-Fire - Smoke Detector" "*U22" "E-Fire - AV Device
Wall - DYN" "E-Fire - Pull Station" "*U19" "E-Receptacle - Telecom-Data - DYN"
"kta_efi003" "E40" "kta_efi006" "Aecb_Default_Rect_1Line_Tee_Drop"
"Aecb_Default_Rect_1Line_Rise" "Aecb_Default_Rect_1Line_Drop"
"Aecb_Default_Rect_2Line_Rise" "Aecb_Default_Rect_2Line_Drop"
"Aecb_Default_Rnd_1Line_Tee_Drop" "Aecb_Default_Rnd_1Line_Rise"
"Aecb_Default_Rnd_1Line_Drop" "Aecb_Default_Rnd_2Line_Rise"
"Aecb_Default_Rnd_2Line_Drop" "1-8_1Ft" "KTA Note - Circle")

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get block list and rename blocks....
« Reply #12 on: August 11, 2009, 12:59:44 PM »
Or perhaps this to ignore the anons:

Code: [Select]
Command: (ai_table "BLOCK" 10)
("kta_eex006" "kta_eex003" "E-Fire - Smoke Detector" "E-Fire - AV Device Wall -
DYN" "E-Fire - Pull Station" "E-Receptacle - Telecom-Data - DYN" "kta_efi003"
"E40" "kta_efi006" "Aecb_Default_Rect_1Line_Tee_Drop"
"Aecb_Default_Rect_1Line_Rise" "Aecb_Default_Rect_1Line_Drop"
"Aecb_Default_Rect_2Line_Rise" "Aecb_Default_Rect_2Line_Drop"
"Aecb_Default_Rnd_1Line_Tee_Drop" "Aecb_Default_Rnd_1Line_Rise"
"Aecb_Default_Rnd_1Line_Drop" "Aecb_Default_Rnd_2Line_Rise"
"Aecb_Default_Rnd_2Line_Drop" "1-8_1Ft" "KTA Note - Circle")

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #13 on: August 11, 2009, 01:55:47 PM »
Oh yeah, I was using an abbreviated version of CAB's to get my list after you gents had helped me:

Code: [Select]
(defun c:GBN (/ data result)
   (while
(setq data (tblnext "block" (null data)))
;(princ "\n   Data :")
;(princ data)
(princ "\n ------------- cdr assoc 2 data ------------\n")
(princ (cdr(assoc 2 data)))
;(princ "\n +++++++++++++ cdr assoc 70 data +++++++++++\n")
;(princ (cdr(assoc 70 data)))
(princ "\n=============================================\n")
;; if it's not [xref, xref dependent, anonymous] ...
(if (zerop (logand 21 (cdr (assoc 70 data))))
(setq result (cons (cdr (assoc 2 data)) result))
) ; end if

   ) ; end while
)

I am going through altering my export routine to compensate for unloading the XREF's to see if that solves the long symbol names issue. Ultimately I am trying to open the file created by the export once completed. It would be nice if I could close the previous file, but that ends the session for the routine so I am not going to mess with it too much. Not sure this opening of the file is going to work either but I thought it would be slick for helping when handling background file creation.

Here is where I am so far with that:
Code: [Select]
(DEFUN c:aec()
(command "TILEMODE" 1)
(command "Zoom" "Extents")
(command "qsave")
(command "Undo" "Begin")
; rem out for speed
;(command "audit" "y")
;(command "qsave")

(command "purge" "all" "*" "n")
(command "purge" "all" "*" "n")
(command "purge" "all" "*" "n")
(command "qsave")

(vl-load-com) 
(defun unloadxrefs (/ blks)

  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-for blk blks
    (if (equal (vla-get-isxref blk) :vlax-true)
      (progn (vla-unload blk)
             (princ (strcat "\n   Unloading XREF : " (vla-get-name blk) ".....next"))
      )
    )
  )
  (princ "\n XREF Unloading complete!!!\n")
  (princ)

(unloadxrefs) 
 
  (setq myPath (getvar "DWGPREFIX"))
  (princ "\n   Current File Path : ")
  (princ myPath)
  (setq myFile (getvar "DWGNAME"))
  (princ "\n   Current File Name : ")
  (princ myFile)
  (princ "\n   File suffix :")
  (setq newSuffix (strcase "-AEC-Removed"))
  (princ newSuffix)
  (setq newName (strcat myFile newSuffix))
  (princ "\n   New file name : ")
  (princ newName)
  (setq newFile (strcat myPath newName))
  (princ "\n   New file to Open :\n")
  (princ newFile)
  (princ "\n continue.....")


  (alert "\n AEC Objects will be removed. \n XREF's may require purging due to nesting of AEC objects. \n You will find your file with the '-AEC-Removed' suffix in the filename. \n Over-write the orginal files with these to clear the problematic objects.")
  (command "-exporttoautocad")
  (command "Prefix" ".")
  (command "Suffix")
  (command "-AEC-Removed")
  (command "Bind")
  (command "No" "" "")
  (prompt "\n   AEC Objects Removed and saved in the drawing with '-AEC-Removed.dwg' in the name. \n   But... XREF's may still have them. \n   If so, run the XREF's first then perform the removal on this file")
  ;(princ)
  (command "Undo" "End")
  (command "Undo" "Back" "Y")
  (command "qsave")
  ;(command "open" newFile)
  ;(command "close")
  (princ)
)

Trying to lookup/remember what I need to get rid of the .dwg on the end of the DWGNAME variable.
I know I am jumping around a bit, but the XREF pathed blocks could have been the cause of the long symbol names error on aec export. I may have been approaching the issue the wrong way. I may be way off with this AEC routine too, but I'm trying  :ugly:

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get block list and rename blocks....
« Reply #14 on: August 11, 2009, 01:58:06 PM »
*hint*

Code: [Select]
(substr (setq dw (getvar "DWGNAME")) 1 (- (strlen dw) 4))