PDA

View Full Version : Newbie Question: Running code across multiple documents



wmassie
2009-08-14, 07:46 PM
I am writing my first practical .NET app. So I am very much a beginner. In the code below, after executing the following line:

Dim acDoc As Document = acDocMgr.Open(strFileName, False)

inspection of "acDoc" shows that it is not the active document (rather the document in which the NETLOAD command was issued remains active). Is there a way to run the app over multiple documents so that the document returned from the "open" function automatically becomes the active document or is there a way to make it active?

By the way, I am also open to any suggestions on improving any other aspect of the code below. The app is supposed to open a folder browser dialog allowing the user to select a folder, opens each drawing file in the folder one-at-a-time, edits the file (these editing steps are not shown in the code below), and then saves and closes the drawing before going to the next drawing file in the folder.

Thanks,
Will

Using: Acad 2007, Visual Basic Express 2005



Public Sub SlideUpdate()

Dim FolderBrowserDialog1 As New FolderBrowserDialog 'declare variable for folder browser dialog object
Dim BrowseFolderResult As DialogResult 'declare varialbe for result of dialog
Dim strDirName As String = "" 'declare variable for the path of the folder that will be searched
BrowseFolderResult = FolderBrowserDialog1.ShowDialog() 'show dialog and store result as either OK or cancel
If BrowseFolderResult = DialogResult.OK Then 'check if user clicked OK
strDirName = FolderBrowserDialog1.SelectedPath 'store path of folder selected
End If
'cycle through each file in the folder that is an AutoCAD drawing
For Each strFileName As String In My.Computer.FileSystem.GetFiles(strDirName, FileIO.SearchOption.SearchAllSubDirectories, "*.dwg")
'declare variable for drawing file object
Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager

If (File.Exists(strFileName)) Then 'check if file exists
Dim acDoc As Document = acDocMgr.Open(strFileName, False) 'open the file in the editor

'perform operations to edit the drawing here

'close and save the current drawing
acDoc.CloseAndSave(strFileName)
Else
'give error message if drawing file does not exist
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
End If

Next 'cycle to the next drawing file

End Sub

RobertB
2009-08-14, 08:11 PM
MdiActiveDocument is a read/write property.

However, in many cases there is no need to make a document active just to modify it. Once you have the document's database, you can use the object model on that database to do lots of tasks.

wmassie
2009-08-14, 08:47 PM
I tried what you said, but when it makes "acDoc" active, the active drawing is shown in the editor but the app suspends execution until I manually make the original document active again, then the app continues. But, of course, that would means that "acDoc" is no longer active.

I need the app to run irrespective of which document is active.

Is that possible?

Thanks,
Will

Here is the new code which the before mentioned addition:




Public Sub SlideUpdate()

Dim FolderBrowserDialog1 As New FolderBrowserDialog 'declare variable for folder browser dialog object
Dim BrowseFolderResult As DialogResult 'declare varialbe for result of dialog
Dim strDirName As String = "" 'declare variable for the path of the folder that will be searched
BrowseFolderResult = FolderBrowserDialog1.ShowDialog() 'show dialog and store result as either OK or cancel
If BrowseFolderResult = DialogResult.OK Then 'check if user clicked OK
strDirName = FolderBrowserDialog1.SelectedPath 'store path of folder selected
End If
'cycle through each file in the folder that is an AutoCAD drawing
For Each strFileName As String In My.Computer.FileSystem.GetFiles(strDirName, FileIO.SearchOption.SearchAllSubDirectories, "*.dwg")
'declare variable for drawing file object
Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager

If (File.Exists(strFileName)) Then 'check if file exists
Dim acDoc As Document = acDocMgr.Open(strFileName, False) 'open the file in the editor

acDocMgr.MdiActiveDocument = acDoc

'perform operations to edit the drawing here

'close and save the current drawing
acDoc.CloseAndSave(strFileName)
Else
'give error message if drawing file does not exist
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
End If

Next 'cycle to the next drawing file

End Sub

wmassie
2009-08-14, 09:54 PM
I have discovered the solution.

I needed to place "CommandFlags.Session"
in the CommandMethod attribute, like so:

<CommandMethod("SlideUpdate", CommandFlags.Session)> _


Thanks,
Will