Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace block by selection routine help.

Status
Not open for further replies.

vbcad

Technical User
Jul 12, 2002
159
US
Below is some code for a routine i copied from the autodesk website. It works fine for a single block, however what i would like to do is more complicated(of course) I would like to select multiple blocks with different names, for example on block named "fpr" and another named "dr". after the selection is made i would like to replace the blocks with "fprd" and "drd" respectivley. Our block naming convention is "XXX" for our standard block and "xxxd" for our demo blocks. how can this code be modified so i can search our libraries for the blocks with the same names but with the "d" at the end of the block name. I assume a wildcard of some type. or maybe an if then argument.
Code:
(defun c:REPL (/ ENT1 BL1 NWNM OLD ODNM)
(prompt "Select blocks to replace: ")
(setq ENT1 (ssget))
;(setq NEWBL ("fprd"))
(command "insert" fprd nil)
(setq N (sslength ENT1))
(setq I 0)
(repeat N
(setq BL1 (entget (ssname ENT1 I)))
(setq NWNM (cons 2 "fprd"))
(setq OLD (assoc 2 BL1))
(setq ODNM (cdr OLD))
(entmod (subst NWNM OLD BL1))
(command "change" "p" "" "p" "la" "e-demo-powr" "" "")
(setq I (1+ I))
)
(prin1)
)
 
Hi vbcad,

I've just about forgotten LISP, but, take a look into DXF filters for selection sets. This should get you what you need.

HTH
Todd
 
What I am trying to do is replace blocks of one name with the demolition block that corresponds with that block. block one is named "fpr" the corresponding demolition block is "fprd". block two is named "dr" and the corresponding demolition block is "drd" and so on and so on with the block library we use. The block names are not limited to 3 characters. I would like to use a selection window to select the blocks, filter for the block names and replace them all with just a few clicks. What i am doing now is inserting them individually and then erasing the block i want replaced. this is very time consuming. Block replace will not work because it replaces blocks globally. here is some code with a block filter added. I hope this makes it more clear.
Code:
(defun c:REPL (/ ENT1 BL1 NWNM OLD ODNM)
(prompt "Select quad receptacles to replace: ")
(setq ENT1 (ssget ":n" '((2 . "fpr"))))
;(setq NEWBL ("fprd"))
(command "insert" fprd.DWG nil)
(setq N (sslength ENT1))
(setq I 0)
(repeat N
(setq BL1 (entget (ssname ENT1 I)))
(setq NWNM (cons 2 "fprd"))
(setq OLD (assoc 2 BL1))
(setq ODNM (cdr OLD))
(entmod (subst NWNM OLD BL1))
(command "change" "p" "" "p" "la" "e-demo-powr" "" "")
(setq I (1+ I))
)
(prin1)
)
 
Hi vbcad,

Sorry, LISP just isn't my thing, but here's how I would tackle it using VB/VBA:

Code:
Public AcadDoc As AcadDocument

Sub ReplaceBlocks()
  
  Dim ssBlock As AcadSelectionSet
  Dim intData() As Integer
  Dim varData() As Variant
  Dim dblPnt(0 To 2) As Double

  Set AcadDoc = AcadApplication.ActiveDocument
  
  BuildFilter intData, varData, -4, "<and", _
                                   0, "INSERT", _
                                   0, "fpr*", _
                                -4, "and>"
                                
  ' Ensure a selection set is not already in memory.
  '
  Set ssBlock = vbdPowerSet("SSET_BLOCKS")
  
  ' Build the selection set.
  '
  ssBlock.SelectOnScreen intData, varData
  
  ' Was anything actually found?
  '
  If ssBlock.Count = 0 Then
    Exit Sub
  Else
    ' Process here...
  End If
    
End Sub

HTH
Todd
 
Now we are getting to where I get a bit lost. I tried running the vba code and I get a compile error "sub or function not defined". I dont really know where to go from here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top