Jump to content

Automation


mfkozlow

Recommended Posts

Hello everyone!

 

I tried the following script: (the OPEN_003 is a lisp that opens a drawing file that ends with "E003" & the GET_ATT is a lisp to retrieve a specific attribute from a block)

 

FILEDIA 0
ZOOM
E
GET_ATT
OPEN_003
ZOOM
E
GET_ATT
QUIT

When the above is ran, everything is done to the file that is opened initially. What I want is for the script beneath "OPEN_003" to run on the newly opened drawing. Is there a way to do this?

 

If you have any other suggestions, let me know. Thanks.

Link to comment
Share on other sites

Of course here it is:

 

GET_ATT:

(defun C:get_att()

 (if (setq ss (ssget "_F" '((7 2.5) (13.5 2.5)) '((0 . "INSERT"))))
(progn  
(setq en (ssname ss 0))                                                   

 (setq enlist(entget en))                                          ;- Get the DXF group codes

 (setq blkType(cdr(assoc 0 enlist)))                 ;- Save the type of entity

 (if (= blkType "INSERT")                                          ;- If the entity type is an Insert entity

  (progn

   (if(= (cdr(assoc 66 enlist)) 1)    ;- See if the attribute flag equals one (if so, attributes follow)

   (progn

     (setq en2(entnext en))                                    ;- Get the next sub-entity

     (setq enlist2(entget en2))                           ;- Get the DXF group codes
   
     (setq enlist3(cdr(assoc 1 enlist2)))

     (while (/= (cdr(assoc 0 enlist2)) "SEQEND")  ;- Start the while loop and keep                                                                                                                   ;-  looping until SEQEND is found.

       (princ "\n ")                                                 ;-Print a new line

       (princ enlist2)                                               ;- Print the attribute DXF group codes

       (setq en2(entnext en2))                             ;- Get the next sub-entity

       (setq enlist2(entget en2))                      ;- Get the DXF group codes

     )

    )

   )  ;- Close the if group code 66 = 1 statement

  )

 )   ;- Close the if block type = "ATTRIB" statement

)

)   ;- Close the if an Entity is selected statement

)

OPEN_003:

(defun c:open_003()

(vl-load-com); [if needed]
(vla-activate
 (vla-open
   (vla-get-documents
     (vlax-get-acad-object)
   )
   (strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 5) "E003.dwg")

 )

)

)

The GET_ATT stores the attribute value in the "enlist3" variable. This is used later on. (Not in this script)

Link to comment
Share on other sites

Firstly

"The GET_ATT stores the attribute value in the "enlist3" variable. This is used later on."

you are calling the GET_ATT twice and store the att value to the same variable, only the last time will be stored....

Then you are quitting AutoCAD, the stoted value will be lost...

Can you try to explain us what's your final goal?

 

 

Henrique

Link to comment
Share on other sites

Not sure, but I would write your script without OPEN_003 function like this :

 

FILEDIA 0
ZOOM
E
GET_ATT
OPEN
(strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 5) "E003.dwg")
ZOOM
E
GET_ATT
QUIT

M.R.

Link to comment
Share on other sites

What I want to do is the following:

 

1. Run batch file to start AutoCAD and open a drawing and run this script (I have the batch file working):

 

FILEDIA 0 
ZOOM
E
GET_ATT
EXBOM
OPEN_003
ZOOM
E
GET_ATT
EXBOM
FILEDIA 1
QUIT

Where EXBOM:

(defun C:EXBOM()

   (if (= enlist3 "1")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP1.txt" "SHT002")
   )

   
   (if (= enlist3 "2")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP2.txt" "SHT002")
   )


   (if (= enlist3 "3")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP3.txt" "SHT002")
   )


   (if (= enlist3 "4")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP4.txt" "SHT002")
   )


   (if (= enlist3 "5")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP5.txt" "SHT002")
   )

   (if (= enlist3 "6")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP6.txt" "SHT002")
   )

   (if (= enlist3 "7")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP7.txt" "SHT002")
   )

   (if (= enlist3 "8")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP8.txt" "SHT002")
   )

   (if (= enlist3 "9")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP9.txt" "SHT002")
   )

   (if (= enlist3 "10")

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SP10.txt" "SHT002")
   )

   (if (= enlist3 nil)

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/SPNIL.txt" "SHT003")
   )

   (if (not (= enlist3 nil))

       (command "attext" "cdf" "g:/_001_auto_bom/TEMPLATES/QTY_SYSTEM.txt" "QTY")
   )


)

NOTE* I am aware that the variable is overwritten, but this does not matter. Once EXBOM is ran, the variable serves no purpose. It SHOULD get overwritten when it is ran on the new drawing (file ending in E003).

 

2. Once the script is ran the directory that holds the drawing files should contain the following text files (this is what I am after):

 

QTY.txt

SHT002.txt

SHT003.txt

 

3. The final process my batch file executes is opening an Excel spreadsheet. This spreadsheet uses those text files to create a Bill of Materials.

 

The problem I have with this process is that after the script calls the OPEN_003, the rest of the script is ran on the initial drawing...not the one that was just opened by the OPEN_003.

Is there anyway to change this so that after the command OPEN_003 is ran the rest of the script is executed on the drawing that was just opened?

Link to comment
Share on other sites

Have you tried my suggestion :

 

FILEDIA 0 
ZOOM
E
GET_ATT
EXBOM
OPEN
(strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 5) "E003.dwg")
ZOOM
E
GET_ATT
EXBOM
FILEDIA 1
QUIT

 

M.R.

Link to comment
Share on other sites

Not sure, but I would write your script without OPEN_003 function like this :

 

Code:

FILEDIA 0 ZOOM E GET_ATT OPEN (strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 5) "E003.dwg") ZOOM E GET_ATT QUIT

M.R.

I just tried this and AutoCAD returned "Invalid File Name." All the files necessary are present and they are named correctly. Thanks for all the help everyone.
Link to comment
Share on other sites

Something like this perhaps.

 

(defun c:demo ( / file fname ofile)
 (if (setq file (strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 5) "E003.dwg"))
   (progn
     (setq fname (strcat (getvar 'dwgprefix) "temp.scr")
    ofile (open fname "w"))
     (write-line "FILEDIA" ofile)
     (write-line "0" ofile)
     (write-line "ZOOM" ofile)
     (write-line "E" ofile)
     (write-line "GET_ATT" ofile)
     (write-line "EXBOM" ofile)
     (write-line "OPEN" ofile)
     (write-line (strcat (chr 34) file (chr 34)) ofile)
     (write-line "ZOOM" ofile)
     (write-line "E" ofile)
     (write-line "GET_ATT" ofile)
     (write-line "EXBOM" ofile)
     (write-line "FILEDIA" ofile)
     (write-line "1" ofile)
     (write-line "QUIT" ofile)
     (close ofile)
     (command "_.script" fname)
     )
   (prompt "\nDwg E003, not found at the current directory!!!")
   )
 (princ)
 )

 

HTH

Henrique

Link to comment
Share on other sites

Hi Henrique :)

 

Won't this be better than repeating the same function many times ?

 

(mapcar '(lambda (s) (write-line s ofile))
             (list "FILEDIA" "0" "ZOOM" "E" "GET_ATT" "EXBOM" "OPEN" (strcat (chr 34) file (chr 34))
                   "ZOOM" "E" "GET_ATT" "EXBOM" "FILEDIA" "1" "QUIT"))

Link to comment
Share on other sites

Hi Tharwat,

defining an anonymous function, should be faster, and requires much less writing work... :)

But depending on the OP understanding, I try to write the code in a comprehensive manner to the OP. ;)

 

 

Cheers

Henrique

Link to comment
Share on other sites

Thank you Henrique! This does exactly what I am looking for. Amazing...only a few hours and my problem is solved. Thanks everyone who contributed.

Link to comment
Share on other sites

Thank you Henrique! This does exactly what I am looking for. Amazing...only a few hours and my problem is solved. Thanks everyone who contributed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...