View Full Version : Block vs Xref: How to get the dxf code
piper
2007-07-03, 05:21 PM
Howdy,
I need to be able to select a Block in the current drawing or a Block in an Xref and get the dxf codes for the selected block.
I been trying for days, using/rewriting every bit of code I can find on the iNet...to no avail.
Any and all help will be appreciated.
Best Regards,
T.Willey
2007-07-03, 05:36 PM
To select an entity in the current drawing use 'entsel'. To select any nested entity use 'nentsel', but remember that the first entity listed is the very last (nested) entity.
Then you can just use 'entget' on the entity name.
What are you looking to do? specifically.
piper
2007-07-03, 05:56 PM
The command needs to work on either as the user will not know which they are selecting....a block in the drawing or an embedded block in an xref.
I have been going nuts with entsel & nentsel.
They return different data depending on what you select.
This code is to Label Blocks. Once I know the Block Name and the Layer it resides on, an ASCII file will be scanned for a description of the block. The user will make a couple of choices: Leader, Text Rotation, etc....Layers are created, Text is Formulated, Positions Cyphered....I digress....the concept is: No Typing With Consistency In Labeling. I wrote one program to label lines and it works fine (both current drawing line work or xreffed line work). It is the BLOCKS that are making my brain swim in it's own juices.
Right now, for this piece of magic, all I need are the Block Name & Residing Layer. For future projects, being able to access any of the Block dxf data might be useful.
T.Willey
2007-07-03, 06:08 PM
This should help you.
(defun GetTrail (Ent / EntData EntType LastEnt LastEntData LastEntType LastEntPath EndList TestList
tmpTest tmpEnt cnt1)
(setq LastEnt (last Ent))
(setq Ent (car Ent))
(setq EntData (entget Ent))
(setq EntType (cdr (assoc 0 EntData)))
(while (and EntData (/= EntType "BLOCK_RECORD"))
(setq EndList (cons (list Ent EntType) EndList))
(setq Ent (cdr (assoc 330 EntData)))
(setq EntData (entget Ent))
(setq EntType (cdr (assoc 0 EntData)))
)
(setq TestList (mapcar '(lambda (x) (= (type x) 'ENAME)) LastEnt))
(setq cnt1 0)
(foreach tmpTest TestList
(if (= tmpTest T)
(progn
(setq tmpEnt (nth cnt1 LastEnt))
(setq LastEntData (entget tmpEnt))
(setq LastEntType (cdr (assoc 0 LastEntData)))
(if (setq LastEntPath (cdr (assoc 1 (tblsearch "block" (cdr (assoc 2 LastEntData))))))
(setq EndList (cons (list tmpEnt LastEntType (strcat "XrPath - " LastEntPath)) EndList))
(setq EndList (cons (list tmpEnt LastEntType) EndList))
)
)
)
(setq cnt1 (1+ cnt1))
)
EndList
)
Use like
(GetTrail (nentsel))
Will return a list like (order of nesting, top -> bottom)
(
(<Entity name: 7e906768> "INSERT" "XrPath - NORT-009-A-1FL.DWG")
(<Entity name: 7e598cc8> "INSERT")
(<Entity name: 7e561a58> "LWPOLYLINE")
)
piper
2007-07-03, 07:49 PM
Thanks Tom....BUT, this code is doing what I've been doing.
If I select a block in the current dwg, I get the entity in the block: Line, Wipeout, etc. Nothing about the block the selected entity resides within.
The selected XREF also does not return the block name or residing layer of the block.
It returns the entity slected & the name of the xref.
here is the code I've put together that ALMOST works with blocks:
(setq en (car (nentsel)))
(setq ent (vlax-ename->vla-object en))
(setq ed (entget (vlax-vla-object->ename ent)))
(if (wcmatch (dxf 8 (vl-list* ed)) "*|*")
(progn
(princ (strcat "\n " (dxf 0 (vl-list* ed)) " Selected"))
(foreach n ed (print n))
)
(if (= (dxf 0 (vl-list* ed)) "INSERT")
(progn
(princ (strcat "\n " (dxf 0 (vl-list* ed)) " Selected"))
(foreach n ed (print n))
)
(progn
(princ (strcat "\n " (dxf 0 (vl-list* ed)) " Selected"))
(foreach n ed (print n))
)
)
)
(textscr)
; the dxf subroutine....
(defun dxf (ecode elist)
(cdr (assoc ecode elist))
)
T.Willey
2007-07-03, 08:59 PM
Mine does what you want to a point. You have to come up with the other code to get the item out of the list you want.
Run my code and post the results, and then we will work from there on how to get the information you want.
piper
2007-07-03, 09:25 PM
okey dokey
when a block in an xref is selected:
((<Entity name: 7e8555e0> "INSERT" "XrPath - S:\\_Historic
Project Data\\92189 Yonge\\dwg\\92189GND.dwg")
(<Entity name: 7e866bb8> "INSERT")
(<Entity name: 7e9e21a8> "LINE"))
when a pline in an xref is selected:
((<Entity name: 7e855900> "INSERT")
(<Entity name: 7e8558b0> "LWPOLYLINE"))
when a block in the dwg is selected:
((<Entity name: 7e855900> "INSERT")
(<Entity name: 7e8558b0> "LWPOLYLINE"))
piper
2007-07-03, 09:39 PM
when I add:
.......your code
)
(setq cnt1 (1+ cnt1))
)
EndList
(setq EN (nth 1 EndList))
(setq ED (entget (car EN)))
(foreach n ED (print n) )
)
entity data for any selected entity: block, line, etc. is listed. Part one complete!
Part 2: for the block in the dwg....still get the entity selected.
T.Willey
2007-07-03, 09:43 PM
You see how it list the "INSERT", that is the block (which you know). The way I would do it, is either have a dialog box that shows all the items (this is how I do it), or have it start at the top, and work its way down until either a non-Xref insert is found, or there is no more items in the list. The latter part is easier to do here, so...
(setq EntList (GetTrail (nentsel)))
(if (> (length EntList) 1)
(progn
(setq cnt 0)
(while (< cnt (length EntList))
(setq tempList (nth cnt EntList))
(if
(and
(equal (length tempList) 2)
(= (cadr tempList) "INSERT")
)
(progn
(setq cnt (length EntList))
(setq EntData (entget (car tempList)))
(setq LayName (cdr (assoc 8 EntData)))
(setq BlkName (cdr (assoc 2 EntData)))
)
(setq cnt (1+ cnt))
)
)
)
)
(print (list LayName BlkName))
piper
2007-07-03, 09:57 PM
I've tried applying this in a few places where I think it may go in the previous code....
I keep getting: bad argument type: listp 210
which leads me to believe I am doing something differently than what you have in mind.
piper
2007-07-03, 10:04 PM
i got it to....thanks ever so much....i put the call in front of the function instead of after....no time to load before execution....
best regards,
T.Willey
2007-07-03, 10:10 PM
Okay, try this.
(defun c:Test (/ EntList cnt EndData tempList LayName BlkName)
(defun GetTrail (Ent / EntData EntType LastEnt LastEntData LastEntType LastEntPath EndList TestList
tmpTest tmpEnt cnt1)
(setq LastEnt (last Ent))
(setq Ent (car Ent))
(setq EntData (entget Ent))
(setq EntType (cdr (assoc 0 EntData)))
(while (and EntData (/= EntType "BLOCK_RECORD"))
(setq EndList (cons (list Ent EntType) EndList))
(setq Ent (cdr (assoc 330 EntData)))
(setq EntData (entget Ent))
(setq EntType (cdr (assoc 0 EntData)))
)
(setq TestList (mapcar '(lambda (x) (= (type x) 'ENAME)) LastEnt))
(setq cnt1 0)
(foreach tmpTest TestList
(if (= tmpTest T)
(progn
(setq tmpEnt (nth cnt1 LastEnt))
(setq LastEntData (entget tmpEnt))
(setq LastEntType (cdr (assoc 0 LastEntData)))
(if (setq LastEntPath (cdr (assoc 1 (tblsearch "block" (cdr (assoc 2 LastEntData))))))
(setq EndList (cons (list tmpEnt LastEntType (strcat "XrPath - " LastEntPath)) EndList))
(setq EndList (cons (list tmpEnt LastEntType) EndList))
)
)
)
(setq cnt1 (1+ cnt1))
)
EndList
)
;---------------------------------------------------------------------------
(setq EntList (GetTrail (nentsel)))
(if (> (length EntList) 1)
(progn
(setq cnt 0)
(while (< cnt (length EntList))
(setq tempList (nth cnt EntList))
(if
(and
(equal (length tempList) 2)
(= (cadr tempList) "INSERT")
)
(progn
(setq cnt (length EntList))
(setq EntData (entget (car tempList)))
(setq LayName (cdr (assoc 8 EntData)))
(setq BlkName (cdr (assoc 2 EntData)))
)
(setq cnt (1+ cnt))
)
)
)
)
(prompt (strcat "\n Layer: " LayName " Block: " BlkName))
(princ)
)
T.Willey
2007-07-03, 10:11 PM
Glad you got it to work Raymond. You're welcome.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.