Author Topic: redefining a block without inserting, can-do or no-can-do?  (Read 2371 times)

0 Members and 1 Guest are viewing this topic.

diarmuid

  • Bull Frog
  • Posts: 417
redefining a block without inserting, can-do or no-can-do?
« on: November 09, 2011, 07:03:13 AM »
Hi,  I want to automatically update a block from a network location.  Is it possilbe via Autolisp to redefine the block from the network location without inserting it.  I know you can do it via the desing centre, just wonderin is all.

At the moment my routnine looks like this. but i'm left with an extra block that i have to delete.  no problem, but i'm just wonderin if there is a neater way.

(command "-insert" "ABC_IE0310543=P:/ACAD/Logo/ABC_IE0Xxxxxx.dwg" "0,0" "1" "1" "0")
(command "erase" "l" "")
(command "attsync" "n" "ABC_IE0310543")

Any help will be greatly appreciated.

Regards

Diarmuid
If you want to win something run the 100m, if you want to experience something run a marathon

Chris

  • Swamp Rat
  • Posts: 548
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #1 on: November 09, 2011, 08:09:48 AM »
easy can-do
Code: [Select]
(command "-insert"
       (strcat "CI=" imagelocation "CI.dwg")
       *cancel*
      ) ;_ end of command
where imagelocation is the location of the block you want to insert and CI is the name of the block
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #2 on: November 09, 2011, 08:55:52 AM »
easy can-do
Code: [Select]
(command "-insert"
          (strcat "CI=" imagelocation "CI.dwg")
          *cancel*
      ) ;_ end of command
where imagelocation is the location of the block you want to insert and CI is the name of the block
Just be careful of this non-standard *cancel* global variable. It's not declared and someone may give it a wrong value - it's not as if there's any documentation like you get with *error*, so there's not even a suggestion about not changing its value from nil. For me I just use nil instead of some arbitrary variable name.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #3 on: November 09, 2011, 09:08:20 AM »
Maybe:

Code: [Select]
(command "_.INSERT"  "ABC_IE0310543=P:/ACAD/Logo/ABC_IE0Xxxxxx.dwg")
(command)

-David
R12 Dos - A2K

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #4 on: November 09, 2011, 11:53:40 AM »
Please note that the method suggested above will reset any attributes and dynamic block properties. I found this out when we changed the standards for our detail boxes, which were dynamic blocks, so I had to write some code to overcome this.

The following code will retain all dynamic properties and attributes, providing that they use the same name as in the original block:

Code: [Select]
;Written by: Chris Wade - 11/23/2010
(defun c:BkReplace ( / ss ent obtyp Doc i j attribs1 attribs2 attribs-list1 attribs-list2 tst tsttyp bname ss2)
(vl-load-com)
(setq Doc (vla-get-activedocument (vlax-get-acad-object))); Gets the current document for use later on
;*********************************************************************************************************************
;Modify the following line as needed
;  ************ Replace block name with the new block name that you wish to use ************
(vl-cmdf "._-insert" "DETAIL BOX - REVISION 2")(command);Insert the block, but do not place it on the drawing, so that there is not an error later on.
;*********************************************************************************************************************

;*********************************************************************************************************************
;Modify the following line as needed
(setq ss (GetBlockSelectionSet "detail*box*")); Change this to the old block name - Wild Cards are allowed here
;*********************************************************************************************************************
    (cond
((or (= (sslength ss) 0) (= ss nil)); If no objects were selected above, ask the user to manually select a block to get the name.
(while (= tst nil)
(setq tst (entsel "\nCould not automatically find detail boxes, please select one manually: "))
)
(setq tst (vlax-ename->vla-object (car tst))
  tsttyp (vla-get-objectname tst)
)
(cond
((= tstyp "AcDbBlockReference")
(setq bname (vla-get-name tst)
  ss (GetBlockSelectionSet bname)
)
)
)
(cond
((or (= (sslength ss) 0) (= ss nil)); If for some reason the selection set is still empty, ask the user to manually select the blocks that they want to update.
(while (or (= ss nil) (= (sslength ss) 0))
(princ "\rManually select detail boxes: ")
(setq ss (ssget))
)
)
)
)
)
(while (and (/= ss nil) (> (sslength ss) 0)); Loop through all instances of the block(s) to make sure they all get replaced
(setq ent (vlax-ename->vla-object (ssname ss 0)); Make an object out of the first entity in the selection set
  obtyp (vla-get-objectname ent); Find the type of object that we are working with
)
(cond
((= obtyp "AcDbBlockReference"); If we are working with a block continue on, otherwise remove the entity and move on to the nect one.
(setq ent2 (vla-InsertBlock (vla-objectidtoobject doc (vla-get-ownerid ent)) (vla-get-insertionpoint ent) "BEI DETAIL BOX - MALIBU" (vla-get-XScaleFactor ent) (vla-get-yscalefactor ent) (vla-get-zscalefactor ent) (vla-get-rotation ent))); Insert the new block based on how the old block was inserted.
(vla-put-layer ent2 (vla-get-layer ent)); Make sure that the new object is placed on the same layer as the old object
; Code to match dynamic properties adapted from T. Willey's code at http://www.theswamp.org/index.php?topic=31549.msg370876#msg370876
(foreach i (vlax-invoke ent 'getdynamicblockproperties)
(foreach j (vlax-invoke ent2 'getdynamicblockproperties)
(cond
((= (strcase (vla-get-propertyname j)) (strcase (vla-get-PropertyName i)))
(cond
((/= (vla-get-propertyname j) "Origin")
(vla-put-Value j (vla-get-value i))
)
)
)
)
)
(setq j nil)
)
; End of code to match dynamic block properties
; The following code makes sure that attribute values match.
(setq attribs1 (vla-GetAttributes ent)
  attrib-list1 (vlax-safearray->list (vlax-variant-value attribs1))
  attribs2 (vla-GetAttributes ent2)
  attrib-list2 (vlax-safearray->list (vlax-variant-value attribs2))
)
(foreach i attrib-list1
(foreach j attrib-list2
(cond
((= (strcase (vla-get-tagstring j)) (strcase (vla-get-tagstring i)))
(vla-put-textstring j (vla-get-textstring i))
)
)
)
)
; End of attribute code
(vla-delete ent); Delete old object
)
)
(ssdel (ssname ss 0) ss); Remove object from selection set.
)
(princ)
)
;Code to select dynamic blocks from Lee Mac at http://www.theswamp.org/index.php?topic=35754.msg409742#msg409742
(defun GetBlockSelectionSet ( name / ss ss4) (vl-load-com)
(setq ss4 (ssadd))
  (if (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 (strcat name ",`*U*")))))
    (
      (lambda ( i / e )
        (while (setq e (ssname ss (setq i (1+ i))))
          (if (wcmatch (strcase (vla-get-EffectiveName (vlax-ename->vla-object e))) (strcase name)); Line modified by Chris Wade to not be case sensitive and to use wild card matching
  (ssadd e ss4)
          )
        )
        ss4
      )
      -1
    )
  )
)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #5 on: November 09, 2011, 12:49:32 PM »
I've done the (command "-INSERT") thing before, just grabbing the inserted block and deleting it.  I've also changed graphics, through vla-methods to update the block definition to add lines and text (but not attributes).
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: redefining a block without inserting, can-do or no-can-do?
« Reply #6 on: November 09, 2011, 12:52:38 PM »
I've done the (command "-INSERT") thing before, just grabbing the inserted block and deleting it.  I've also changed graphics, through vla-methods to update the block definition to add lines and text (but not attributes).
Yeah, but the problem that I had with any method was that it would reset the dynamic block properties, so I wrote the code above to maintain it when replacing a block and I also designed it to maintain attribute values.

All in all, it works really well for our needs, thought it could be useful here as well.