TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: debgun on February 24, 2010, 02:28:19 PM

Title: Need guidance with batch processing
Post by: debgun on February 24, 2010, 02:28:19 PM
I need guidance from the experienced users.  I’ve been working on a project to automate IO (input/output) drawings and have asked several questions pertaining to a small portion of the project.  Of course, it doesn’t help that this is my first attempt at LISP.

Let me start by describing the project, please keep in mind that this is based on AutoCAD Electrical.
1.   Excel document is prepared for AutoCAD.
2.   AutoCAD will read excel document.
3.   Create new drawing based on given filename indicated in excel document.
4.   Insert circuits and prepare drawing.
5.   Create next new drawing
6.   Repeat steps 4 & 5 to EOF
Yesterday, I learned that LISP is document-centric and cannot communicate between drawings.  Please direct me to the most efficient route in order to batch process this routine.  I would like to avoid using VBA since it could disappear soon.  I have dabbled in VB.NET, but not put it to use, yet.  I’m not comfortable with using scripts to complete complicated process such as the electrical commands are.

I’ve attached a sample of the excel document and the lisp routines that will work per drawing.

Thank you for your help!
Debbie
Title: Re: Need guidance with batch processing
Post by: T.Willey on February 24, 2010, 02:38:53 PM
Either you have to change the code to use none of the Electrical commands ( as I'm sure they are coded to only use the current drawing ), and no ' ssget ' calls, or...

You can use a lisp on multiple files, if you use Acad in SDI mode, or....

You can create a lisp, that will write a script, that will run your commands ( lisps and/or commands ), but this cannot have any user interactions.
Title: Re: Need guidance with batch processing
Post by: VovKa on February 24, 2010, 04:10:35 PM
here's an unintelligent way to communicate between drawings :)
Code: [Select]
(defun test (/ f fn)
  (and (setq c (getint "\nEnter starting number: "))
       (setq f (open (setq fn (vl-filename-mktemp nil nil ".lsp")) "w"))
       (write-line (vl-prin1-to-string
     '(progn (alert (itoa c)) (setq c (1+ c)) (vl-propagate 'c) (princ))
   )
   f
       )
       (not (close f))
       (vl-propagate 'c)
       (vl-load-all fn)
  )
)
don't forget to delete any temporary files created
Title: Re: Need guidance with batch processing
Post by: debgun on February 24, 2010, 05:16:50 PM
Thanks for the quick responses.

Tim,

I was reading http://through-the-interface.typepad.com/through_the_interface/2009/06/batch-processing-autocad-drawings-from-lisp-without-sdi.html (http://through-the-interface.typepad.com/through_the_interface/2009/06/batch-processing-autocad-drawings-from-lisp-without-sdi.html) about SDI.  Should I be concerned about using the SDI method?
Title: Re: Need guidance with batch processing
Post by: JohnK on February 24, 2010, 05:34:11 PM
> Batch-processing AutoCAD drawings from LISP without SDI ...
what does that mean? ...How old is that article? ...2009?! *gack*

I will take part in a group Batch processing project if there are any other takers?  I already have one Ive been using for years but it would need some updating because its not really that "user friendly" but i would offer it up so i can get us started.


BTW
>  "choose to break binary application compatibility"
what da heck does that mean? Who comes up with this crap?
Title: Re: Need guidance with batch processing
Post by: Lee Mac on February 24, 2010, 05:45:55 PM
Would this help you at all?   8-)

http://www.theswamp.org/index.php?topic=31827.0 (http://www.theswamp.org/index.php?topic=31827.0)
Title: Re: Need guidance with batch processing
Post by: JohnK on February 24, 2010, 05:58:02 PM
Dont put on those "Mr. cool" glasses just yet.

What can i do TO a drawing without:
Quote
;;            - No SelectionSets               (ssget,ssname etc.)               ;;
;;            - No Command calls                                                 ;;
;;            - No *Ent Methods                (entget,entmod etc.)              ;;
;;            - No Access to System Variables  (vla-Set/GetVariable, etc)        ;;



;P
Title: Re: Need guidance with batch processing
Post by: T.Willey on February 24, 2010, 05:58:42 PM
I have written quite a bit of batch processing code in Lisp.  Now if I need to do certain things, I just use a script writer, and let it to it's thing.  If I have to do the same thing over, and over again.  If it doesn't deal with changing text/attributes, then I use ObjectDBX.  If it does change those items, then I use C#.

Lee,

ObjectDBX would not work in this case, as the code is written.

7,

Not sure how the OP wants to handle this, but I can help also, as time allows.  I have posted my script writing routine before.  I'll see if that can be found.

Edit: [ http://www.theswamp.org/index.php?topic=8264.msg105871#msg105871 ] here is the script writing program.

fyi.. The Tim that posted in there was me also.
Title: Re: Need guidance with batch processing
Post by: Lee Mac on February 24, 2010, 06:03:04 PM
Dont put on those "Mr. cool" glasses just yet.

What can i do TO a drawing without:
Quote
;;            - No SelectionSets               (ssget,ssname etc.)               ;;
;;            - No Command calls                                                 ;;
;;            - No *Ent Methods                (entget,entmod etc.)              ;;
;;            - No Access to System Variables  (vla-Set/GetVariable, etc)        ;;


Hehe - quite a lot actually  :-)
Title: Re: Need guidance with batch processing
Post by: debgun on February 24, 2010, 06:10:03 PM
So I've been digging around in the Electrical help files and noticed it creates batches with script files.  I'm considering this option some more.  Apparently, it uses the DCL file called ace_proj_matrix.dcl, which I don't totally understand.

Thanks for all your input.  It has benefited me.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 24, 2010, 06:12:38 PM
Lee Mac,
but you are limited.

T.Willey,
Let's exchange some email's tomorrow. I'll send you some files so you can check out my method (-i.e. tell me if its worth while) but I dont use script files.

debgun,
So does that mean your out?
Title: Re: Need guidance with batch processing
Post by: T.Willey on February 24, 2010, 06:36:35 PM
T.Willey,
Let's exchange some email's tomorrow. I'll send you some files so you can check out my method (-i.e. tell me if its worth while) but I dont use script files.

Sounds good.  You were easy to work with before, and you're knowledgeable, so should be fun.   :-D
Title: Re: Need guidance with batch processing
Post by: JohnK on February 24, 2010, 07:00:32 PM
I dont know about `knowledgeable' but if you deem that its a good enough method, then i will be willing to post it as-is so we can get a few others involved.
Title: Re: Need guidance with batch processing
Post by: T.Willey on February 24, 2010, 07:15:42 PM
Whatever you want to do, I will take a look at it.

I do seriously respect you knowledge as a coder though.
Title: Re: Need guidance with batch processing
Post by: uncoolperson on February 24, 2010, 07:56:22 PM
could this all be done via VBA in excel?
Title: Re: Need guidance with batch processing
Post by: JohnK on February 24, 2010, 09:20:00 PM
i dont see why not.
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 09:16:19 AM
Se7en,

I didn't mean to sound like my question was answered.  I'd love to see what you all come up with since I'm slow at all of this.  I was considering all of the options that Tim had supplied.

Debbie
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 09:32:19 AM
cool.

can you code (for when we decide upon a method)?
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 09:43:43 AM
Possibly.. The internet and help files are a great resource.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 09:49:08 AM
lol  yeah that's where i get all my stuff too. *blink* Wait a minute! Aren't we on the internet?! Oh great, now what are we gonna do?
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 09:59:07 AM
Yea!
Either switching ACAD to SDI mode and running LISP on multiple files.  This sounds easy!  I'm assuming setting variable SDI to 1 and starting creating new drawings.

Or running a LISP that writes a script file that will run the commands.  This sounds advanced!  I'm not sure where to begin on this one; I'd have to do more research.

What is your thought?
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 10:12:09 AM
sorta like option one. but i wanted to send T.Willey my files so he can "ground" me if need be. I wrote my own batch processing THING a few years back but 1. i tend to get a bit carried away with my code and do some CRAZY stuff and 2. i feel it may be a bit un-user-friendly.

So if T.Willey likes my idea then i will offer up what i have and we can start hacking away at it. If he doesnt like it then we can decide upon a method and just write code for that. Sound like a plan?
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 10:23:42 AM
Yup!  If it helps, feel free to contact me directly at daguenthner@gmail.com.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 02:43:52 PM
I think my method i had was too complicated (I did need the "grounding"; glad i checked first.). So now we are back to deciding upon a method for this little task.

Method #1:
> Either switching ACAD to SDI mode and running LISP on multiple files.  

Method #2:
> Or running a LISP that writes a script file that will run the commands.  

hummm....

which works better for you debgun?
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 04:12:10 PM
I'm in favor of method 2.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 04:15:39 PM
okay. Have you tried T.Willey's example? Does it work as advertised? Do we need to modify it?
Title: Re: Need guidance with batch processing
Post by: debgun on February 25, 2010, 04:28:30 PM
Let me try BScript.  I hadn't noticed it earlier.
Title: Re: Need guidance with batch processing
Post by: T.Willey on February 25, 2010, 04:55:55 PM
If that version doesn't work for you, let me know, as I still use that function nowadays, and can post the one I use, but I don't think I have done much ( if anything ) to the code.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 25, 2010, 05:21:08 PM
here's an unintelligent way to communicate between drawings :)
don't forget to delete any temporary files created

Whoa!? I just noticed VovKa's post. :surprised:
VovKa, thats cool. Ive never used vl-load-all before.
Title: Re: Need guidance with batch processing
Post by: debgun on February 26, 2010, 09:45:50 AM
O.K. I finally got bscript to work (my brain shutdown early yesterday).  This code won't work as is since bscript is opening existing drawings and in my instance the drawings need to be created.
Title: Re: Need guidance with batch processing
Post by: JohnK on February 26, 2010, 10:27:44 AM
If i think i understand (i hope you're not gonna ask what i think you're gonna ask :lol:) that changes the complexity just a bit.

In the off chance that this might appease you...okay its more like a quick joke but. Here is a quick reproduction of the "FILE NEW" button. Does that work?
Code: [Select]
(if (= 0 (getvar 'SDI))
  (vla-add
    (vla-get-documents
      (vla-get-application
(vlax-get-acad-object)))))

Map out what you think the progy should do in plain english.

EX:

Start  a new dwg
copy in blocks from <1.dwg>
copy in layers from <2.dwg>
...
Title: Re: Need guidance with batch processing
Post by: cmwade77 on February 26, 2010, 11:11:38 AM
I would strongly recommend against using SDI mode, because SDI mode will most likely be gone with the next file format change.
Title: Re: Need guidance with batch processing
Post by: debgun on March 01, 2010, 09:33:27 AM
Okay! So here is my attempt at batch processing.  I haven't had a chance to try it out, yet.

Code: [Select]
(defun c:inscircuit (/ Alldata Header bName actdoc actspace en x y scale colnum sheetnum tblock terminal)
  (setq Alldata (GetExcel "C:\\Documents and Settings\\dguenthner\\My Documents\\GenerateIO.xlsx" nil nil))
  (setq Header (car Alldata)) ;stores the column names separate from data
  (setq Alldata (cdr Alldata)) ;stores the data separate from the column names
  (setq path (BrowseFolder)) ;stores folder location ;User Interaction Required
 
  (foreach data Alldata ;cycles through all lines of data to retrieve block path
    (setq bName (nth 1 data) ;assigns variable to block path
        dwgname (nth 0 data) ;assigns variable the dwg name
actdoc (vla-get-activedocument (vlax-get-acad-object)) ;retrieves the active document
actspace (vla-get-block (vla-get-activelayout actdoc))) ;retrieves the active space e.g. model / paper
    (if (<> dwgname "") ;if variable contains a value then start a new dwg
       (progn
          (setq newdoc (vla-add (vla-get-documents (vlax-get-acad-object)) "Kice 11X17"))
          (vla-saveas newdoc (strcat path "/" dwgname) ac2007_dwg)
          (command (DwgSetup))
       ) ;progn
    ) ;if
    (vla-startundomark actdoc) ;allows undo to undo one step at a time, whereas all at once
    (setq x (atof (nth 3 data))
  y (atof (nth 4 data))
  scale (atof (nth 5 data)))
    (c:wd_ins_circ2 bName (list x y) scale 7) ;insert collection of prewired components
    (setq colnum 6) ;setting column number to fill-in attribute values
    (setq en (entlast)) ;selecting plc block object
    (foreach head Header
      (c:wd_modattrval en (nth colnum Header) (nth colnum data) nil) ;modifying attributes
      (setq colnum (1+ colnum))
    ) ;foreach close
    (command (Retag_terminal)) ;calling next function
    (command (EditLadder)) ;calling next function
   ) ;foreach close
  (CloseExcel nil) ;killing excel instance
  (princ) ;quiet exit
 ) ;defun close


(defun DwgSetup (/ DName SheetDName)
   (setq Dwg "IO"
         DName (getvar "DWGNAME"))
   (setq SheetDName (substr DName 3))
   (setq ss (ssget "X" '((0 . "INSERT") (2 . "wd_m"))))
   (setq en (ssname ss))
   (c:wd_modattrval en "SHEET" Dwg nil)
   (c:wd_modattrval en "SHEETDWGNAME" SheetDName nil)
   (c:wd_ladr_reref)
)
Title: Re: Need guidance with batch processing
Post by: debgun on March 11, 2010, 05:22:53 PM
With the help of an expert (T.Willey) on The Swamp I was able to get a working batch script.  Here is the final result.
Code: [Select]
;This program creates a batch process for IO drawings.  It starts by creating the IO drawings then runs 2 commands.
;Command 1, sets up the drawing and adds it to the active project.  Command 2, inserts the IO for that drawing.
;Need in conjuntion with this program: tb_concatenate.lsp, match_proj_properties.lsp, Insert IO.lsp, GetExcel.lsp,
;KillExcel.vbs
;Created March 11, 2010
;Debbie Guenthner
                 ;      ;        ;;;;     ;;;;;  ;;   ;
  ; ;    ; ;      ;    ;      ;    ; ;  ;
       ;  ;   ;   ;    ;;;;;;;;    ;    ;  ; ;
      ;    ; ;     ;  ;        ;    ;    ;   ;;
        ;     ;      ; ;          ; ;;;;;  ;    ;

;Main Program

(defun C:Batch (/ script Alldata path dwglst dwgs)
  (setq script "c:/newdwgs.scr")
  (alert "Close Excel documents before proceeding.")
  (setq Alldata (GetExcel "G:\\Automation\\Drawing Patterns\\AutoCAD STD\\AutoLisp Files\\Excel to IO\\GenerateIO.xlsx" nil nil))
  (setq Alldata (cdr Alldata) ;stores the data separate from the column names
        path (strcat (BrowseFolder) "\\"))
  (foreach data Alldata ;cycles through all lines of data to retrieve dwg name
    (setq dwglst (cons (nth 0 data) dwglst))
  ) ;foreach
  (foreach dwg dwglst
    (if (null (member dwg dwgs)) ;remove duplicates
      (setq dwgs (cons dwg dwgs))
    ) ;if
  ) ;foreach
;;;  (setq dwgs (apply 'path (dwgs)))
  (CreateScript script path dwgs)
  (command "_.SCRIPT" script)
  (vl-file-delete script)
  (princ)
)

;-------------------------------------------------------------------------------------------------------------------------
;This function creates the script file
(defun CreateScript (script path dwgs / f io lsp)
  (setq f (open script "w"))
  (setq io "G:/Automation/Drawing Patterns/AutoCAD STD/AutoLisp Files/Excel to IO/Insert IO.lsp") ;2nd routine
  (setq lsp "G:/Automation/Drawing Patterns/AutoCAD STD/AutoLisp Files/Excel to IO/StartIO.LSP") ;1st routine
  (foreach dwg dwgs
    (write-line "_.new \"Kice 11X17\"" f)
    (write-line (strcat "_.saveas 2007 \"" path dwg ".dwg\"") f)
    (write-line (strcat "(load \"" lsp "\")") f)
    (write-line "DwgSetup" f)
    (write-line (strcat "(load \"" io "\")") f)
    (write-line "insertio" f)
    (write-line "_.qsave" f)
    (write-line "_.close" f)
    ) ;foreach
    (write-line "" f)
    (close f)
) ;defun

;-------------------------------------------------------------------------------------------------------------------------
;This function prepares the drawing to add to active project and update titleblock
(defun C:DwgSetup (/ DName Dwg SheetDName bk ss en) ;1st rountine
  (setq Dwg "IO"
DName (getvar "DWGNAME"))
  (setq SheetDName (substr DName 4 2)) ;retrieving sheet no i.e. AA
  (setq ss (ssget "X" '((0 . "INSERT") (2 . "wd_m")))) ;locating block to modify attributes
  (setq sslen (sslength ss)) ;# of blocks inserted
  (setq bk -1) ;counter
  (while (< (setq bk (1+ bk)) sslen)
    (setq en (ssname ss bk))
    (c:wd_modattrval en "SHEET" SheetDName nil)
    (c:wd_modattrval en "SHEETDWGNAME" Dwg nil)
    (c:wd_ladr_reref) ;update ladder numbers
    (c:ace_add_dwg_to_project nil nil) ;add active drawing to active project
    (c:wd_tb_process_one 0 (list (list 0 0 0 0 0 0 1 1 0 0 1 1 1) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)) ;update titleblock
    (c:match_proj_properties)
    ;(c:insertio)
  ) ;while
) ;defun

;-------------------------------------------------------------------------------------------------------------------------
;This function opens a file dialog box for user to select a folder
(defun BrowseFolder (/ ShlObj Folder FldObj OutVal)
  (setq ShlObj (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application")
Folder (vlax-invoke-method ShlObj 'BrowseForFolder 0 "" 0)
  ) ;setq
  (vlax-release-object ShlObj)
  (if Folder
    (progn
      (setq FldObj (vlax-get-property Folder 'Self)
    OutVal (vlax-get-property FldObj 'Path)
      ) ;setq
      (vlax-release-object Folder)
      (vlax-release-object FldObj)
      OutVal
    ) ;progn
  ) ;if
) ;defun