Author Topic: Get block list and rename blocks....  (Read 17380 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: 12914
  • 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: 12914
  • 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: 12914
  • 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: 12914
  • 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: 12914
  • 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))

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #15 on: August 11, 2009, 02:19:15 PM »
*hint*

Code: [Select]
(substr (setq dw (getvar "DWGNAME")) 1 (- (strlen dw) 4))
:lmao:
strlen.... thanks bro!
I'm pulling triple duty for the IT folks being out of office today, and getting ready to test a new driver with Oce' for 2010 to fix an OLE (Excel spreadsheet) plotting issue when linked or embedded in a CAD dwg file.
Catch up with you guys soon!
Your help is greatly appreciated sirs! ;-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get block list and rename blocks....
« Reply #16 on: August 11, 2009, 03:07:51 PM »
*hint*

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

You can use vl-filename-base too:
Code: [Select]
(vl-filename-base (setq dw (getvar "DWGNAME")))
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #17 on: August 11, 2009, 03:54:14 PM »
Another:

Code: [Select]
(cadr (fnsplitl (getvar "DWGNAME")))

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #18 on: August 11, 2009, 05:12:13 PM »
*hint*

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

You can use vl-filename-base too:
Code: [Select]
(vl-filename-base (setq dw (getvar "DWGNAME")))
I tried both and it never seemed to trim the last 4 letters off:

Quote
Current File Path : C:\Documents and Settings\cmoor.KTAGROUP\Desktop\
   Stripped File Name : B09006.00_E209-later.dwg
   Current File Name : B09006.00_E209-later.dwg
   File suffix :-AEC-REMOVED
   New file name : B09006.00_E209-later.dwg-AEC-REMOVED
   New file to Open :
C:\Documents and
Settings\cmoor.KTAGROUP\Desktop\B09006.00_E209-later.dwg-AEC-REMOVED

Code: [Select]
  (setq myPath (getvar "DWGPREFIX"))
  (princ "\n   Current File Path : ")
  (princ myPath)
 
  ;(setq myFile (getvar "DWGNAME"))
  ;(princ "\n   Current File Name : ")
  ;(princ myFile)
 
  (setq newlen (- (strlen myFile) 4))
  (substr myFile 1 newlen)
  (princ "\n   Stripped File Name : ")
  (princ myFile)
 
  ;(substr (setq myFile (getvar "DWGNAME")) 1 (- (strlen myFile) 4))
 
  ;(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.....")

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get block list and rename blocks....
« Reply #19 on: August 11, 2009, 05:53:15 PM »
Code: [Select]
  (setq myPath (getvar "DWGPREFIX"))
  (princ "\n   Current File Path : ")
  (princ myPath)
 
  (setq myFile (getvar "DWGNAME"))
  (princ "\n   Current File Name : ")
  (princ myFile)
 
  (princ "\n   Stripped File Name : ")
  (princ (vl-filename-base 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   Locating New file to Open :\n")
  (if (findfile newFile) (princ "File found.")(princ "Could not locate file."))
  (princ "\n continue.....")
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 #20 on: August 12, 2009, 10:06:50 AM »
Thanks CAB,

I ran into some issues with Opening a file and found the solution pretty easily for it.

Finished routine works well so far:
Code: [Select]
; AEC removal routine for file repairs and management.
; Special thanks to CAB, Lee Mac, & Gile in the swamp for their incredible help
; http://www.theswamp.org/index.php?topic=29839.0;all
; 8-12-2009
(defun c:aec()
(vl-load-com)
;===========================================================================Turn off command line responses
(command "CMDECHO" 0)
;===========================================================================

(command "TILEMODE" 1)
(command "Zoom" "Extents")
(command "qsave")
(command "Undo" "Begin")
; rem out for speed
;(command "audit" "y")
;(command "qsave")
(princ "\n   File purge begin........")
(setq doc (vla-get-ActiveDocument
            (vlax-get-acad-object)))

(repeat 3 (vla-purgeall doc))
(princ "\n   File purge complete.")
(command "saveas" "2007" "" "y")

 
(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) 
;============================================== begin file name structure
(setq myPath (getvar "DWGPREFIX"))
(princ "\n   Current File Path : ")
(princ myPath)
 
(setq myFile (getvar "DWGNAME"))
(princ "\n   Current File Name : ")
(princ myFile)
 
(princ "\n   Stripped File Name : ")
(setq strippedFile (vl-filename-base myFile))
(princ strippedFile)

(princ "\n   File suffix :")
(setq newSuffix (strcase "-AEC-Removed"))
(princ newSuffix)

(princ "\n   File Extension :")
(setq newExtension (strcase ".dwg"))
(princ newExtension)

(setq newName (strcat strippedFile newSuffix newExtension))
(princ "\n   New file name : ")
(princ newName)

(setq newFile (strcat myPath newName))
(princ "\n   New file to Open :\n")
(princ newFile)


(princ "\n   continue to Export.....\n")

(alert "\n *-----------------------AEC Objects will be removed.--------------------------*
\n 1.) XREF's may require purging due to nesting of AEC objects.
\n 2.) You will find your file with the '-AEC-Removed' suffix in the filename.
\n 3.) Over-write the orginal files with the new one to clear the problematic objects.
\n 4.) File will be opened once export is complete.
\n 5.) Make sure the original file is closed so you can over write it.
\n 6.) Reload all XREF's with the RAX command if necessary."
)


(command "-exporttoautocad")
(command "Prefix" ".")
(command "Suffix")
(command newSuffix)
(command "Bind")
(command "No" "" "")

;=================================== Undo & save to leave original file as found
(command "Undo" "End")
(command "Undo" "Back" "Y")
(command "saveas" "2007" "" "y")
;(command "qsave")
;===================================
(princ "\n   Locating New file to Open :\n")
(if (= (findfile newFile) nil)
(alert "\n Could not locate file.")
) ; end if

(defun OpenFile ()
(vla-open
(vla-get-documents
(vla-get-application (vlax-get-acad-object))
)
newFile
)
) ;_defun

(OpenFile)
(princ "\n   File open successful!!")
(command "close")
;===========================================================================Turn on command line responses
(setvar "CMDECHO" 1)
;===========================================================================
(princ)
)

But I found it still doesn't solve the long symbol names error in this file preventing export.
So I am back to looking at block renaming to see if it solves the problem.
I tried renaming manually, and the aecb blocks returned to their original names unfortunately. :ugly:
« Last Edit: August 12, 2009, 11:25:13 AM by KewlToyZ »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #21 on: August 12, 2009, 10:29:42 AM »
Just to offer a quick alternative, you could use this to purge also:

Code: [Select]
(setq doc (vla-get-ActiveDocument
            (vlax-get-acad-object)))

(repeat 3 (vla-purgeall doc))

 :-)

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #22 on: August 12, 2009, 11:25:54 AM »
Thanks Lee, I put it in the code above  :wink:

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #23 on: August 12, 2009, 12:40:21 PM »
Hmmm one of those odd things that bother me,
I try from the command line to reload all XREF's.
If one is found the command errors and discontinues instead of moving on through the list.

Quote
Command:
-XREF
Enter an option [?/Bind/Detach/Path/Unload/Reload/Overlay/Attach] <Attach>: R

Enter xref name(s) to reload: *

Reload Xref "Zbased9": ..\XREF\Zbased9.dwg
"Zbased9.dwg" cannot be found.
*Invalid*
Command:

I try the same thing here and get the same result.
I don't mind if it tells me a file is missing,
I just want it to continue repeating through the list until done.
so if blk is xref it reloads but where do I check if exist to prevent it from erroring?
Use findfile again?

Code: [Select]
(defun c:rax (/ blks)
(vl-load-com)
  (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-reload blk)
             (princ (strcat "\n   Reloading XREF : " (vla-get-name blk) ".....next"))
      )  ; end progn
    ) ; end if
  )
  (princ "\n XREF reloading complete!!!\n")
  (princ)
)

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #24 on: August 12, 2009, 12:43:55 PM »
Not applicable...
Code: [Select]
(defun c:rax (/ blks)
(vl-load-com)
  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-for blk blks
    (if (= (vla-get-isxref blk) :vlax-true)
(if (= (findfile blk) nil)
(alert "\n Could not locate file.")
) ; end if
      (progn (vla-reload blk)
             (princ (strcat "\n   Reloading XREF : " (vla-get-name blk) ".....next"))
      )  ; end progn
    ) ; end if
  )
  (princ "\n XREF reloading complete!!!\n")
  (princ)
)

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #25 on: August 12, 2009, 12:58:10 PM »
Hmmmmm, not quite sure how to frame it yet.

Code: [Select]
(defun c:rax (/ blks)
(vl-load-com)
  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-for blk blks
    (if (and (= (vla-get-isxref blk) :vlax-true)(vlax-method-applicable-p (vla-reload blk)))     
      ;(vlax-method-applicable-p WhatsMyLine "AddBox")
      ;(vlax-method-applicable-p (vla-reload blk))
      (progn
      (vla-reload blk)
      (princ (strcat "\n   Reloading XREF : " (vla-get-name blk) ".....next"))
      )  ; end progn
    ) ; end if
  )
  (princ "\n XREF reloading complete!!!\n")
  (princ)
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #26 on: August 12, 2009, 02:15:55 PM »
Perhaps this may help you  :-)

Code: [Select]
[color=RED]([/color][color=BLUE]defun[/color] xrReload [color=RED]([/color][color=RED])[/color]
 
  [color=RED]([/color][color=BLUE]vlax-for[/color] Obj [color=RED]([/color][color=BLUE]vla-get-Blocks[/color]
                  [color=RED]([/color][color=BLUE]vla-get-ActiveDocument[/color]
                    [color=RED]([/color][color=BLUE]vlax-get-acad-object[/color][color=RED])[/color][color=RED])[/color][color=RED])[/color]

    [color=#990099];; Get hold of the block collection for[/color]
    [color=#990099];; the active document and iterate through it.[/color]

    [color=#990099];; Note:-[/color]
    [color=#990099];; To make the program faster, set the ActiveDocument Object[/color]
    [color=#990099];; to a variable defined in the main function, and use it in[/color]
    [color=#990099];; the sub-function to save calling vlax-get-acad-object multiple[/color]
    [color=#990099];; times.[/color]

    [color=RED]([/color][color=BLUE]if[/color] [color=RED]([/color][color=BLUE]eq[/color] [color=blue]:vlax-true[/color] [color=RED]([/color][color=BLUE]vla-get-IsXRef[/color] Obj[color=RED])[/color][color=RED])[/color]
     
      [color=#990099];; Check Block is an XRef[/color]

      [color=RED]([/color][color=BLUE]if[/color] [color=RED]([/color][color=BLUE]vl-catch-all-error-p[/color]
            [color=RED]([/color][color=BLUE]vl-catch-all-apply[/color] [color=DARKRED]'[/color][color=BLUE]vla-reload[/color] [color=RED]([/color][color=BLUE]list[/color] Obj[color=RED])[/color][color=RED])[/color][color=RED])[/color]

        [color=#990099];; Use vl-catch-all-apply to trap any errors that[/color]
        [color=#990099];; may occur, and prevent program from crashing![/color]
       
        [color=RED]([/color][color=BLUE]princ[/color]
          [color=RED]([/color][color=BLUE]strcat[/color] [color=#ff00ff]"\n** "[/color] [color=RED]([/color][color=BLUE]vla-get-Name[/color] Obj[color=RED])[/color] [color=#ff00ff]" Failed to Reload **"[/color][color=RED])[/color][color=RED])[/color][color=RED])[/color][color=RED])[/color][color=RED])[/color]

        [color=#990099];; Notify of an error during reloading.[/color]

  [color=RED]([/color][color=BLUE]princ[/color][color=RED])[/color][color=RED])[/color]


 
     

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #27 on: August 12, 2009, 02:25:43 PM »
Thanks Lee Mac,
I was going back to the catch all apply and reading through it but i wasn't sure on the order of where to place the error trap.
I'll read this through. Sooner or later I need to get it in my head for the order. I keep stumbling because blocks apply to xref's too.

I was back to the block renaming and I keep getting an error on a simple strcat that is just blowing my progress.
Code: [Select]
(defun c:BLR()
;===========================================================================Turn off command line responses
;(command "CMDECHO" 0)
;===========================================================================

(if (setq AllBlocks (ssget "_X" '((0 . "INSERT")))) ; begin if

(progn
(setq NumBlocks (sslength AllBlocks))
(setq Count 0)
(princ "\n   Total Blocks found : ")
(princ NumBlocks)

(repeat NumBlocks ;;this cycles number of items
(setq Ename (ssname AllBlocks Count))  ;;get entity name
(setq Edata (entget Ename))   

(setq Count (+ 1 Count))
(princ "\n\n   Count : ")
(princ Count)
(princ " of ")
(princ Numblocks)
(princ "\n\n   Edata :")
(princ Edata)

(princ "\n\n ------------- cdr assoc 2 data ------------\n")
(setq bName (cdr(assoc 2 Edata)))
(princ bName)
(princ "\n=============================================\n")

(setq nPrefix (strcase "Block-"))
(princ "\n   nPrefix : ")
(princ nPrefix)

(setq rName (strcat nPrefix Count))
(princ "\n   rName :")
(princ rName)

(princ "\n   Block found!! Please Wait......") ;amusement
(princ "\n Count = [ ")
(princ Count)
(princ " ] sorting next.....\n :")

;(princ rName)
(princ "\n   Renaming block found....")
(command "-rename" "block" bName rName )

  )

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

it errors here
Quote
Command: blr

   Total Blocks found : 42

   Count : 1 of 42

   Edata :((-1 . <Entity name: 7eb22af0>) (0 . INSERT) (330 . <Entity name:
7eb51c10>) (5 . 1C6) (100 . AcDbEntity) (67 . 0) (410 . Model) (8 . E-FIRE-E)
(100 . AcDbBlockReference) (66 . 1) (2 . kta_efi006) (10 2263.99 1794.52 0.0)
(41 . 96.0) (42 . 96.0) (43 . 96.0) (50 . 0.785398) (70 . 0) (71 . 0) (44 .
0.0) (45 . 0.0) (210 0.0 0.0 1.0))

 ------------- cdr assoc 2 data ------------
kta_efi006
=============================================

   nPrefix : BLOCK-; error: bad argument type: stringp 1

Command:

I don't get why it stops there.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #28 on: August 12, 2009, 02:38:22 PM »
Ok, I haven't gone through it with a fine toothed comb, but I can see some obvious stuff already:

1. Make sure you localise your variables, (good programming practice).
2. You are supplying princ with data types that are not STRINGS, either convert the data to a STRING (perhaps using something like "itoa" or "rtos"), or look into the prin1 or print functions. Also, you could use strcat a lot more. :-)


Code: [Select]
(defun c:BLR  (/ Allblocks num count ename edata bname nprefix rname)
  ;; Localise your variables buddy, good programming practice. :)
 
  (if (setq AllBlocks (ssget "_X" '((0 . "INSERT")))) ; begin if

    (progn
      (setq num (sslength AllBlocks) Count 0)
     
      (princ (strcat "\n   Total Blocks found : " (itoa num)))
     
      ;; This is where your program was failing, remember,
      ;; "princ" requires a STRING, you supplied it with an
      ;; INTEGER.

      (repeat num
        ;;this cycles number of items
       
        (setq Ename (ssname AllBlocks Count))
        ;;get entity name
        (setq Edata (entget Ename))

        (setq Count (+ 1 Count))
        (princ "\n\n   Count : ")
        (print Count)
        (princ " of ")
        (print Numblocks)
        (princ "\n\n   Edata :")
        (print Edata)

        (princ "\n\n ------------- cdr assoc 2 data ------------\n")
        (setq bName (cdr (assoc 2 Edata)))
        (print bName)
        (princ "\n=============================================\n")

        (setq nPrefix (strcase "Block-"))
        (princ "\n   nPrefix : ")
        (princ nPrefix)

        (setq rName (strcat nPrefix (itoa Count)))
        (princ "\n   rName :")
        (princ rName)

        (princ "\n   Block found!! Please Wait......") ;amusement
        (princ "\n Count = [ ")
        (print Count)
        (princ " ] sorting next.....\n :")

  ;(princ rName)
        (princ "\n   Renaming block found....")
        (command "-rename" "block" bName rName)

        )

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




KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #29 on: August 12, 2009, 02:46:39 PM »
Thanks again Lee.
I had long forgotten the basics of c in Autolisp with princ vs. print.

I notice in conditional statements the variation of (equal, (eq, (=
Is there a significance about these, I was going to look it up in the help files but I thought I would ask.

Localising variables is still something I need to read through about the exact rules regarding their order and whn they need localised and when not to.
« Last Edit: August 12, 2009, 02:49:42 PM by KewlToyZ »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #30 on: August 12, 2009, 02:50:49 PM »
Ok, with the equal, eq, and = functions, this is my take on things:

equal, I would use on 2 numerical expressions (where a fuzz factor may be needed).
eq, I would use on 2 strings/numerical expressions (where the expressions must match exactly).
=, I would normally use when I need to compare more than two expressions.

Bear in mind that eq and equal will only take 2 args, whereas = will take an arbitrary number of args.

Lee




Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #31 on: August 12, 2009, 02:55:51 PM »
Regarding localising of variables,

I don't think the order matters (don't quote me on that), but I normally order them in the order they are defined in the program.

Just look at it like this, if any values are bound to the symbols that are localised, those values will be set to nil upon the program commencing and completing.

As an example:

Code: [Select]
(defun c:test ( )

  (setq a "Hello")

  (printer)

  (princ))


(defun printer ( )

  (princ a)

  (princ))

^^ "a" remains global.

Code: [Select]
(defun c:test (/ a)

  (setq a "Hello")

  (printer)

  (princ))


(defun printer ( )

  (princ a)

  (princ))

^^ "a" is localised in the main function, and hence will still hold its value in the sub-function, but nil after program completion.

Code: [Select]
(defun c:test (/ a)

  (setq a "Hello")

  (printer)

  (princ))


(defun printer (/ a)

  (princ a)

  (princ))

^^ "a" is localised in the sub-function, hence its value is set to nil upon invoking the sub-function, hence the above will print nil.

Hope this helps,

Lee


KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #32 on: August 12, 2009, 04:34:46 PM »
FYI, I finally realized why I was getting the long symbol names error when I used -ExportToAutoCAD.
I had set the format in the profile to R14 quite awhile ago by mistake.
Apparanetly it retained the format version and r14 had 16 bit file path names. 
I never thought to check it.
:realmad: :pissed: :lmao: :ugly: Yeah my Avatar is spot on.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get block list and rename blocks....
« Reply #33 on: August 12, 2009, 05:14:54 PM »
Pay attention when comparing things other than strings and numbers!
Code: [Select]
_$ (setq e1 (car(entsel)))
<Entity name: 1b95418>
_$ (setq e2 (car(entsel)))
<Entity name: 1b95418>
_$ (eq e1 e2)
T
_$ (equal e1 e2)
T
_$ (= e1 e2)
nil
_$ (setq el1 (entget e1))
((-1 . <Entity name: 1b95418>) (0 . "MTEXT") (330 . <Entity name: 1b95010>) (5 . "9B") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "zDtl Light 1") (100 . "AcDbMText") (10 33.9178 5.10906 0.0) (40 . 0.0625) (41 . 1.42614) (71 . 1) (72 . 5) (3 . "NOTES:")(7 . "PS1-16") (210 0.0 0.0 1.0) (11 1.0 -2.44921e-016 0.0) (42 . 1.41207) (43 . 1.1875) (50 . 0.0) (73 . 1) (44 . 0.9))
_$ (setq el2 (entget e2))
((-1 . <Entity name: 1b95418>) (0 . "MTEXT") (330 . <Entity name: 1b95010>) (5 . "9B") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "zDtl Light 1") (100 . "AcDbMText") (10 33.9178 5.10906 0.0) (40 . 0.0625) (41 . 1.42614) (71 . 1) (72 . 5) (3 . "NOTES:")(7 . "PS1-16") (210 0.0 0.0 1.0) (11 1.0 -2.44921e-016 0.0) (42 . 1.41207) (43 . 1.1875) (50 . 0.0) (73 . 1) (44 . 0.9))
_$ (eq el1 el2)
nil
_$ (equal el1 el2)
T
_$ (= el1 el2)
nil
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: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #34 on: August 12, 2009, 07:13:49 PM »
Ooo, didn't know that Alan, thanks  8-)

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #35 on: August 14, 2009, 09:01:11 AM »
That is tricky, makes me want to go back and recode everything out of paranoia now  :lol: ;-)
Actually it would likely do me some good, I need the repetition anyhow.

Is there a post/guide about how to use Atoms Family as sort of a vla programming guide?
More or less help me along with recognizing the progression to drill down into objects and the drawing database?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #36 on: August 14, 2009, 10:08:54 AM »
I'm not sure what you mean by using the Atoms-Family as a programming guide.

But on the lines of the Atoms-family, be sure to check out Atoms.vlx by MP on here - a superb reference facility...

Also, the VLIDE help files have a ton of info.  :-)

KewlToyZ

  • Guest
Re: Get block list and rename blocks....
« Reply #37 on: August 14, 2009, 11:40:40 AM »
Thanks Lee, yeah I have MP's vlx.
I keep looking for a means to follow the code like in Visual Studio inteligent highlighting (Intellisense?);
Lets me know what portions are available as an option while I type.
.... yeah I know lazy but it is a nice feature. :lmao:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get block list and rename blocks....
« Reply #38 on: August 14, 2009, 01:14:00 PM »
Ahh, I see what you mean - I don't believe VL has Intellisense, but try the Apropos, ctrl+shift+space or, if you are halfway through a code, and the function has already been used, you can use the auto-complete ctrl+space   :-)