TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: rude dog on May 02, 2005, 09:29:09 PM
-
Need some tips on this one:
(defun c:cblk ()
(setq minq (getstring "\nEnter pipe size to generate B.O.M: ")
msrch "_cs*"
)
(setq ms (strcat minq msrch)
LST (tblnext "block" t)
)
(while
(setq 1nm (cdr (assoc 2 LST)))
;;entity name
(if (wcmatch 1nm ms)
(progn
(setq 2nm (cons 2 1nm)
getem (ssget "X" (list 2nm))
qty (sslength getem)
)
(princ "\n")
(prompt 1nm)
(setq LST (tblnext "BLOCK"))
)
;;while
)
)
;;if progn
(princ)
)
-
First tip:
[/code]
:)
-
(defun c:cblk ( / pmt size spec data name ss )
(setq
pmt "Enter pipe size to generate B.O.M.: "
size (getstring pmt)
spec (strcase (strcat size "_cs*"))
)
(while (setq data (tblnext "block" (null data)))
(if
(wcmatch
(strcase
(setq name
(cdr
(assoc 2 data)
)
)
)
spec
)
(princ
(strcat
name
": "
(itoa
(if
(setq ss
(ssget "x"
(list
'(0 . "insert")
(cons 2 name)
)
)
)
(sslength ss)
0
)
)
" instance(s)\n"
)
)
)
)
(princ)
)
Try this. I coded it quick and blind but it should work (please let me know if it doesn't).
Couple things you might wish to note:
• Local variable declarations.
• How block collection is iterated.
• Case sensitivity of wcmatch function
• Dealing with null selection set.
• How indention improves read-ability (imo).
...
Also, and this is important, this only tallies non nested blocks. If you have blocks within blocks they will not be counted by this routine. For that one generally has to write a recursive routine that's a little more ambitious than the above. Perhaps we can discuss that at a later date.
Cheers.
:)
-
Thanks for the hand MP
Is there a length limit you can name a block?
I have blocks in the dwg with names similar to this
and its not counting them accurately:
4_cs_150#_sch.40_weld neck flange
-
A spec of "4_cs*" should find them.
Zip up one of those drawings and plunk it into lilly pond (http://www.theswamp.org/lilly_pond) (use your logon name / password) and I'll try to have a look at it before my wife pulls me away from this 'puter.
PS I already made a directory rude_dog so it would be easy to get going.
:)
-
10-4
-
Bad news there rood doog.
You're using wildcard characters as part of the block name, namely the hash or pound character (#). As such it is being interpretted according, so when we go --
(ssget "x"
'( (0 . "insert")
(2 . "12_cs_150#_sch.40_weld neck flange")
)
)
The # character is being evaluated as "any number here" or
(ssget "x"
'( (0 . "insert")
(2 . "12_cs_1500_sch.40_weld neck flange")
;; ^
)
)
(ssget "x"
'( (0 . "insert")
(2 . "12_cs_1501_sch.40_weld neck flange")
;; ^
)
)
(ssget "x"
'( (0 . "insert")
(2 . "12_cs_1502_sch.40_weld neck flange")
;; ^
)
)
(ssget "x"
'( (0 . "insert")
(2 . "12_cs_1503_sch.40_weld neck flange")
;; ^
)
)
Etc.
Of course theres no blocks by those names so it tallies: 0.
You can escape said character using a reverse quote (`) but that means you'll have to write a function (which isn't hard, I'll write it in a separate post if you want, might be good to have anyway, but I digress) to walk thru the block name, escaping any wild card characters it encounters.
However, my recommendation is that you reconsider your block naming convention. You're gong to continue to run into all kinds of curious behaviors like this if you include wildcard characters in any table entries (blocks, layers, styles etc.), including the inability to rename the table entry using the native rename command.
Here are the characters you should avoid:
(http://www.theswamp.org/screens/mp/wcmatch.png)
You're currently using pound chars (#) and periods (.).
Cheers.
/messenger
-
As promised --
(defun EscapeWildCards ( name )
(vl-list->string
(apply 'append
(mapcar
'(lambda (code)
(if (member code
'(35 64 46 42 63 126 45 44)
)
(list 96 code)
(list code)
)
)
(vl-string->list name)
)
)
)
)
Example --
(ssget "x"
(list
'(0 . "insert")
(cons 2
(EscapeWildcards "12_cs_150#_sch.40_weld neck flange")
)
)
)
Or use in the original utility --
(defun c:cblk ( / pmt size spec data name ss )
(setq
pmt "Enter pipe size to generate B.O.M.: "
size (getstring pmt)
spec (strcase (strcat size "_cs*"))
)
(while (setq data (tblnext "block" (null data)))
(if
(wcmatch
(strcase
(setq name
(cdr
(assoc 2 data)
)
)
)
spec
)
(princ
(strcat
name
" ("
(itoa
(if
(setq ss
(ssget "x"
(list
'(0 . "insert")
(cons 2 (EscapeWildcards name))
)
)
)
(sslength ss)
0
)
)
")\n"
)
)
)
)
(princ)
)
I'm going to guess that it tallies correct now.
Still -- let me underscore again, I'd really consider avoiding the use of wildcards in your table name entries.
(Ok, my conscience is clear)
:)
-
MP WOW :shock:
was I off the beaten path I will rename my blocks properly
Thanks for the help....it is still pretty cool you found a way to get around it though
8)
-
First tip:
[/code]
:)
I added it. Good try RD.
-
MP WOW :shock:
was I off the beaten path I will rename my blocks properly
Thanks for the help....
My pleasure RD.
It is still pretty cool you found a way to get around it though 8)
I don't like to be beaten by machines. :twisted:
I added it. Good try RD.
I tried too, but she still piled up on the left side. :(
-
That's because of the formatting that was already set. I've been able to successfully add it on other posts and the formatting would work. I can fix this formatting problem too. BRB.... Done. Of course, I had to format it a little more like I'd like to see it. Now that I've had a chance to look at it like my eyes can handle it, one thing I'd like to say to RD is, don't forget to clear your local variables when you code. You don't realize how important this is until stuff doesn't quite work right because other programs have been introduced that also practice this bad habit.
Reason #1 as to why I like Python: All formatting will always be the same, because it is tab delimited. Now, if I could just get some time to get learning it more.
-
I know I could have re-indented it => too lazy. :roll:
-
I was originally, but you spurred it on. See above edited post.
-
Good job Daron. :)