Jump to content

Open, Macro, Close And Then Loop???


Recommended Posts

Posted

Hi all,

 

Tackling a simple way to open a whole stack of dwg's, run a macro or two on them, and close again.

 

Possibly one at a time - eg: have a bacth file call the dwg to acad 2009, run the macro or script to call the macro, and then close the dwg and open the next to repeat the tasks.

 

I found a way to do this in DIESEL but it works for a max of 8 drg's.

 

I found a way to do this with a lisp code, script and batch file in unisen, but it's far too slow to be effective.

 

At present all I need to do is click a macro once after loading a dir full of files into autocad, and click a button, the macro closes the file when done.

 

Short of getting an auto-click program, how can I automate this so that the macro will re-run for the next dwg?

Posted

here might be a VBA solution.

 

This is a modul* that recursively finds all files of a given type, then calls a subroutine for each one.

 

If you already have some code that does these certain operations, then put a call to your main routine in the "DoSomethingWithFile" routine.

 

This is a generic module, and can be (re)used for any VBA operations, whether run in Acad, Excel, etc, and as such, has no Autocad commands in it - including opening or closing the drawing.

 

Call the main "FindAllFiles" routine with the filetype "*.dwg", for example; and the start path , "Y\MyDwgs\Temp\", for example.

 

The "DoSomethingWithFile" routine, unmodified, merely lists all the files it finds to the debug window. Remember that this routine is recursive - it tunnels down into all subfolders beneath the folder you pass in the "StartPath" parameter of the "FindAllFiles" main routine.

 

' ---- snip ------------------------------------


Option Explicit
Private Const vbDot = 46
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"
Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type
Private Declare Function FindClose Lib "kernel32" _
   (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" _
   Alias "FindNextFileA" (ByVal hFindFile As Long, _
   lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function lstrlen Lib "kernel32" _
   Alias "lstrlenW" (ByVal lpString As Long) As Long
Private Declare Function PathMatchSpec Lib "shlwapi" _
   Alias "PathMatchSpecW" (ByVal pszFileParam As Long, _
   ByVal pszSpec As Long) As Long

Dim sFileExt As String
Dim sFileRoot As String

Public Sub FindAllFiles(FileType As String, StartPath As String)
   ' recursively searches a passed path for files of a given type
   ' FileType: a wildcard string of the file extension, ie; "*.dwg" or "*.xls"

   sFileRoot = QualifyPath(StartPath) 'start path
   sFileExt = FileType 'file type of interest

   Call SearchForFiles(sFileRoot)
End Sub

Private Sub SearchForFiles(sRoot As String)
   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long

   hFile = FindFirstFile(sRoot & ALL_FILES, WFD)

   If hFile <> INVALID_HANDLE_VALUE Then

       Do
       'if a folder, and recurse specified, call method again
       If (WFD.dwFileAttributes And vbDirectory) Then
           If Asc(WFD.cFileName) <> vbDot Then
               SearchForFiles sRoot & TrimNull(WFD.cFileName) & vbBackslash
           End If
       Else
           'must be a file..
           If MatchSpec(WFD.cFileName, sFileExt) Then
               DoSomethingWithFile sRoot & TrimNull(WFD.cFileName)
           End If 'If MatchSpec
       End If 'If WFD.dwFileAttributes

       Loop While FindNextFile(hFile, WFD)
   End If 'If hFile

   Call FindClose(hFile)
End Sub

Private Function QualifyPath(sPath As String) As String
   ' formats passed path string to be used in recursive API search
   If Right$(sPath, 1) <> vbBackslash Then
       QualifyPath = sPath & vbBackslash
   Else
       QualifyPath = sPath
   End If
End Function

Private Function TrimNull(startstr As String) As String
   ' trims NULL char (ascii 0) from strings returned by API calls
   TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))
End Function

Private Function MatchSpec(sFile As String, sSpec As String) As Boolean
   ' uses API version of the "LIKE" command
   MatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec))
End Function

Private Sub DoSomethingWithFile(FoundFileName As String)
   ' use this routine to do something with each file found
   ' we'll do nothing but print out the filename found
   Dim FoundName As String: FoundName = FoundFileName

   Debug.Print FoundName
End Sub




Posted
Hi all,

 

Tackling a simple way to open a whole stack of dwg's, run a macro or two on them, and close again.

 

Possibly one at a time - eg: have a bacth file call the dwg to acad 2009, run the macro or script to call the macro, and then close the dwg and open the next to repeat the tasks.

 

I found a way to do this in DIESEL but it works for a max of 8 drg's.

 

I found a way to do this with a lisp code, script and batch file in unisen, but it's far too slow to be effective.

 

At present all I need to do is click a macro once after loading a dir full of files into autocad, and click a button, the macro closes the file when done.

 

Short of getting an auto-click program, how can I automate this so that the macro will re-run for the next dwg?

 

If you are able to use VB.net then I can provide some code that will do it for you without even opening the file. On some fairly complex scripts I'm getting performance of 100's of drawings in a few seconds.

 

Let me know and I could provide some pointers if it is of interest.

 

Cheers

Posted

I am familiar with how vb dot net operates, but not how to apply it to acad 09.

 

I may need a little more detail on how to actually implment the VBA.

  • 3 weeks later...
Posted
If you are able to use VB.net then I can provide some code that will do it for you without even opening the file. On some fairly complex scripts I'm getting performance of 100's of drawings in a few seconds.

 

Let me know and I could provide some pointers if it is of interest.

 

Cheers

 

I am beginning to use.net and would ve very interested in the code you mention. Please post it!.

 

Zen

Posted

Let me know and I could provide some pointers if it is of interest.

 

Cheers

 

Well basically I just need a foothold on how to get VBA going in autocad, from there I can learn a little and experiment. But until I read this forum, I thought lisp and deisel were my only options.

 

so, as simple and obvious as it may seem - what is the first step?

Posted
Well basically I just need a foothold on how to get VBA going in autocad, from there I can learn a little and experiment. But until I read this forum, I thought lisp and deisel were my only options.

 

so, as simple and obvious as it may seem - what is the first step?

 

Since you haven't done VBA before I would start by looking up generral VBA Sites the offer tutorials on the basics, the you could go to some AutoCAD specific sites: The following two are just some of many but I find these two most helpful so far:

 

http://rkmcswain.blogspot.com/ This is from on the folks here in this forum I believe.

 

http://blog.jtbworld.com Is another site I find useful not only for AutoCAD but for other office products as well.

 

I can post the VBA Tutorial sites I have found if you want them...

 

Zen

Posted

Well I am familiar with VBA from ACCESS, have had much database work in the past.

 

TY muchly for the pointer, I shall begin my journey into the miles of reading that await me.

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...