Author Topic: Back in it  (Read 3939 times)

0 Members and 1 Guest are viewing this topic.

rude dog

  • Guest
Back in it
« on: May 02, 2005, 09:29:09 PM »
Need some tips on this one:
Code: [Select]

(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)
)

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #1 on: May 02, 2005, 09:29:46 PM »
First tip:

[/code]

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #2 on: May 02, 2005, 10:12:56 PM »
Code: [Select]
(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.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

rude dog

  • Guest
Back in it
« Reply #3 on: May 02, 2005, 10:22:38 PM »
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

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #4 on: May 02, 2005, 10:37:11 PM »
A spec of "4_cs*" should find them.

Zip up one of those drawings and plunk it into 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.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

rude dog

  • Guest
Back in it
« Reply #5 on: May 02, 2005, 11:45:21 PM »
10-4

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #6 on: May 03, 2005, 01:02:29 AM »
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 --

Code: [Select]
(ssget "x"
   '(   (0 . "insert")
        (2 . "12_cs_150#_sch.40_weld neck flange")
    )
)

The # character is being evaluated as "any number here" or

Code: [Select]
(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:



You're currently using pound chars (#) and periods (.).

Cheers.

/messenger
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #7 on: May 03, 2005, 01:33:19 AM »
As promised --

Code: [Select]
(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 --

Code: [Select]
(ssget "x"
    (list
       '(0 . "insert")
        (cons 2
            (EscapeWildcards "12_cs_150#_sch.40_weld neck flange")
        )
    )
)

Or use in the original utility --

Code: [Select]
(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)

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

rude dog

  • Guest
Back in it
« Reply #8 on: May 03, 2005, 07:59:06 AM »
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)

daron

  • Guest
Back in it
« Reply #9 on: May 03, 2005, 08:05:43 AM »
Quote from: MP
First tip:

[/code]

:)

I added it. Good try RD.

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #10 on: May 03, 2005, 08:15:59 AM »
Quote from: rude dog
MP WOW  :shock:
was I off the beaten path I will rename my blocks properly
Thanks for the help....

My pleasure RD.

Quote from: rude dog
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:

Quote from: Daron
I added it. Good try RD.

I tried too, but she still piled up on the left side. :(
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

daron

  • Guest
Back in it
« Reply #11 on: May 03, 2005, 08:21:19 AM »
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.

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #12 on: May 03, 2005, 08:25:57 AM »
I know I could have re-indented it => too lazy. :roll:
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

daron

  • Guest
Back in it
« Reply #13 on: May 03, 2005, 08:28:15 AM »
I was originally, but you spurred it on. See above edited post.

MP

  • Seagull
  • Posts: 17488
Back in it
« Reply #14 on: May 03, 2005, 08:30:57 AM »
Good job Daron. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox