Saturday, June 23, 2012

Everybody draws Process and Instrumentation Drawings...don't they?

This just goes on and on.  Just when I thought I was nearing half way, the "man" said to me:
"Bill, of course you are doing a Bill of Material for every drawing?"
After picking myself up off the floor after a fainting attack, I replied:
"This will take me centuries!"
Fortunately, I had sort of prior knowledge of this (I appear to filter out unwanted information),
so I had devised a block named "TAG" for valves, motors and so on, and one called "INSTRUMENT" as explained in my March 6th 2012 blog.

Writing a lisp to collect all the tag ones and list them in a text file was pretty easy and went well. You might be asking why did I not use Eattext, a newish command to do exactly this? It is OK, but involves lots of button pressing dialog box travelling.

No, I wanted a one button approach.  Silly me. Turns out my dynamic block is a nasty little beast.  Do it as per the tag system and it makes a mess of the count. 

Searching the net showed me that there are really clever lisp programmers out there, but none had written exactly what I needed.

One came close, a man called Doug Broad, who put up a bare bones solution, using Visual Lisp.

It turns out that Visual Lisp extends ordinary lisp, and allows you to solve such a problem as this.

To use visual lisp in a lisp routine, just type: (vl-load-com).  After much frustration, this routine seems to work:
This is a scrap from a typcial P & ID, the instruments are the ones in bubbles.You can see there are quite a few non-essential lines, and maybe it is not done in slick elegant manner, but it it is relatively easy to follow.

Here is the routine:

;Program written by Bill Le Couteur
;Auckland NZ
;Rev 0 date 24 June 2012

(defun  c:PIDE()

;;;this file is the LISTING file
   ;;;;this makes sure file is empty
   (setq scrfile ( open "C:/Bilro/Bilro4/LISTING.TXT" "w"))
   (close scrfile)

 (setq scrfile ( open "C:/Bilro/Bilro4/LISTING.TXT" "w"))

(setq count 0)

 (setq SS1 (ssget "X" (list '(0 . "INSERT") (cons 2 "TAG"))))

(setq no_of_entities ( sslength ss1))

(while (/= count no_of_entities)
(setq en (ssname ss1 count))
(setq ed (entget en))
(setq the_attrib (entget (entnext en)))
(setq the_tag (dxf 1 the_attrib))
(print the_tag)
(write-line the_tag scrfile)

(setq count (+ count 1))
);end progn
);end while

;;;;;;;;;;;;;;;;;;;;;;;;;NOW TO FIND THE DYMNAMIC BLOCKS CALLED "INSTRUMENT";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        (setq ss2 (ssget "_X" '((0 . "INSERT"))));;;ESSENTIAL-FIND ALL THE BLOCKS FIRST!!!!!!
    (setq no_of_entities (sslength ss2))
    (setq count 0)
    (setq innercount 0)

    (print no_of_entities)

    (while (< count no_of_entities)
        (setq obj (vlax-ename->vla-object (ssname ss2 count)))
        (setq itsrealname (vla-get-Name obj))
        (setq itseffectivename (vla-get-EffectiveName obj))

        ;(print obj)
        ;(print itsrealname)
        ;(print itseffectivename)
        ;(if (eq itseffectivename "INSTRUMENT")(PRINT "HELLO THERE"))
                (setq en (ssname ss2 count))
                (setq ed (entget en))
                ;(print ed)

;;;;;;;;;;INNER WHILE LOOP::::::::::::::::::::::::::-only if its name is "INSTRUMENT"!
(if (eq itseffectivename "INSTRUMENT")
     (while  (< innercount  2)

         (setq en (entnext en))
         (setq ed (entget en))

         (if (eq innercount 0)
            (setq atag (dxf 1 ed))
            ;(print atag)
        );end little if progn
         );end little if

        (if (eq innercount 1)
                    (setq the_no (dxf 1 ed))
            ;(print the_no)
            (setq instrument_tag (strcat atag the_no))
            (write-line instrument_tag scrfile)
            ;(print instrument_tag)
            );end little if progn
         );end little if
    ;(print count)
    ;(print innercount)
    ;(print ed)
    (setq innercount (+ innercount 1))
     );end while progn
    );end while /= sequend


;;;;;;;;;;INNER WHILE LOOP::::::::::::::::::::::::::

                (setq innercount 0)
                (setq count (+ count 1))
         );end while progn
         );end while

    (close scrfile)


(defun dxf(code elist)
  (cdr (assoc code elist))     ;Finds the association pair, strips 1st element