Jump to content

Issue looping through my cad files


RMS

Recommended Posts

I am doing some .net testing and I am running into so problems. I am looping through a directory that hold multiple Autocad files. I then open one at a time and try to do a selection set, but here comes my problem just as the drawing opens the default Drawing1.dwg (blank) drawing opens right on top of it; and if I close it out the same thing happens on the next drawing.

 

Here is my code if anyone would like to solve this. Thanks!

 


Public Class Class1

   Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

   <CommandMethod("comp")> _
   Public Sub createLine()

       Dim mPath As String = ""
       Dim mFxdNm As String = ""

       ' First create a FolderBrowserDialog object
       Dim FolderBrowserDialog1 As New Forms.FolderBrowserDialog
       With FolderBrowserDialog1
           .RootFolder = Environment.SpecialFolder.Desktop
           .SelectedPath = "c:\dxfTest"
           .Description = "Select the source directory"
           If .ShowDialog = Forms.DialogResult.OK Then
               mPath = .SelectedPath.ToString
               Dim last_char = mPath.Substring(mPath.Length - 1)
               If last_char <> "\" Then
                   mPath = (mPath & "\")
               End If
           End If
       End With

       Dim files() As String ' keeps a list of all nest sums
       files = System.IO.Directory.GetFiles(mPath, "*.dwg") ' gets list of files names in folder
       For Each fname In files  ' Loops Through files() array and parces every nest sum

           Dim strFileName As String = fname
           Dim acDocMgr As DocumentCollection = Application.DocumentManager
           If (File.Exists(strFileName)) Then
               acDocMgr.Open(strFileName, False)

               ' Get the current document and database
               Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
               Dim acCurDb As Database = acDoc.Database

               ' Start a transaction
               Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                   ' Request for objects to be selected in the drawing area
                   Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()

                   ' If the prompt status is OK, objects were selected
                   If acSSPrompt.Status = PromptStatus.OK Then

                       Dim acSSet As SelectionSet = acSSPrompt.Value

                       ' Step through the objects in the selection set
                       For Each acSSObj As SelectedObject In acSSet

                           ' Check to make sure a valid SelectedObject object was returned
                           If Not IsDBNull(acSSObj) Then

                               ' Open the selected object for write
                               Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)

                               If Not IsDBNull(acEnt) Then
                                   ' Change the object's color to Green
                                   acEnt.ColorIndex = 3

                               End If
                           End If
                       Next

                       ' Save the new object to the database
                       acTrans.Commit()

                   End If
                   ' Dispose of the transaction
               End Using
           Else
               acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
           End If
       Next
   End Sub

End Class

and the imports:

Imports System
Imports System.IO
Imports System.Windows
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Windows

Link to comment
Share on other sites

I can’t test the notion but something like this may also help:

 

 
           If (File.Exists(strFileName)) Then
               Dim acDoc As Document = acDocMgr.Open(strFileName, False)

               ' Get the current document and database
               Application.DocumentManager.MdiActiveDocument = acDoc 
               Dim acCurDb As Database = acDoc.Database 

 

Files opened in this fashion may also require a "Document.LockDocument".

Link to comment
Share on other sites

What happens if the CommandMethod is setup as such:

 

 

This seems to have done the trick! I can now make my selection on each file but I am getting an eLockViolation; so I have more problems that I need to address. I also tried your second posted block of code, but with no luck, although that gave me some clues.

 

Thanks for the assistance!

Link to comment
Share on other sites

That is when "Document.LockDocument" comes into play.

 

Well, I am trying some different Lock methods but I think I have an underlying problem either with how my program see each new dxf file/drawing in this loop, but the Drawing1.dwg is still affecting my (attempted coding approach) what I mean is that I have 4 dxf files in a dir; but my code is counting the Drawing1 and then stops loop early and does not fully look at the last dxf file. In VBA ThisDrawing is used, and never looks at Drawing1.dwg at all! (wtf) :x .....oops just smashed my pc!!

Link to comment
Share on other sites

.....oops just smashed my pc!!

 

It deserved it.:wink:

 

 

I couldn’t say what the problem may be but, if deadlines are looming, a .NET routine can employ the same methods as VBA via Interop. That method may be a more familiar.

I know that type of procedure is possible with .NET, though I don’t have that much experience in the area.

This thread (starting at about mid-thread) has some info on interop:

http://www.cadtutor.net/forum/showthread.php?39166

Link to comment
Share on other sites

Well it seems like roughing up the PC worked! :unsure:

 

I sifted through the web some more and found my missing puzzle piece. Here is the block of code I found, maybe someone else here might find it useful too.

 


'//Get a database from a file
Public Function GetDatabaseFromFile(ByVal fileName As String) As Database
Dim newDB As Database = New Database(False, True)
newDB.ReadDwgFile(fileName, System.IO.FileShare.None, False, Nothing)
Return newDB
End Function
[/Code]

[i]

 

Credits go to fixo:[/i]

http://forums.augi.com/showthread.php?t=62473

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