+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Full Member RMS's Avatar
    Using
    AutoCAD 2009
    Join Date
    Aug 2005
    Location
    South Coast MA
    Posts
    76

    Drawing Issue looping through my cad files

    Registered forum members do not see this ad.

    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!

    Code:
    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:
    Code:
    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
    DIY Guitar Amps
    http://www.dreamtone.org/Forum/
    Also using AutoCAD2008, UGS NX5, and SA

  2. #2
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,185

    Default

    What happens if the CommandMethod is setup as such:

    <CommandMethod("comp", CommandFlags.Session)>

  3. #3
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,185

    Default

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

    Code:
     
                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".

  4. #4
    Full Member RMS's Avatar
    Using
    AutoCAD 2009
    Join Date
    Aug 2005
    Location
    South Coast MA
    Posts
    76

    Default

    Quote Originally Posted by SEANT View Post
    What happens if the CommandMethod is setup as such:

    <CommandMethod("comp", CommandFlags.Session)>
    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!
    DIY Guitar Amps
    http://www.dreamtone.org/Forum/
    Also using AutoCAD2008, UGS NX5, and SA

  5. #5
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,185

    Default

    Quote Originally Posted by RMS View Post
    . . . . but I am getting an eLockViolation . . . .


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

  6. #6
    Full Member RMS's Avatar
    Using
    AutoCAD 2009
    Join Date
    Aug 2005
    Location
    South Coast MA
    Posts
    76

    Default

    Quote Originally Posted by SEANT View Post
    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) .....oops just smashed my pc!!
    DIY Guitar Amps
    http://www.dreamtone.org/Forum/
    Also using AutoCAD2008, UGS NX5, and SA

  7. #7
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,185

    Default

    Quote Originally Posted by RMS View Post
    .....oops just smashed my pc!!
    It deserved it.


    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

  8. #8
    Full Member RMS's Avatar
    Using
    AutoCAD 2009
    Join Date
    Aug 2005
    Location
    South Coast MA
    Posts
    76

    Default

    Registered forum members do not see this ad.

    Well it seems like roughing up the PC worked!

    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.

    Code:
    '//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


    Credits go to fixo:

    http://forums.augi.com/showthread.php?t=62473
    DIY Guitar Amps
    http://www.dreamtone.org/Forum/
    Also using AutoCAD2008, UGS NX5, and SA

Similar Threads

  1. Complex looping
    By Delta-Sword in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 11th Jul 2009, 01:06 am
  2. polyline while looping
    By msm54 in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 8th Apr 2009, 01:20 pm
  3. Plotting issue - cpc files
    By ANDY0871 in forum AutoCAD General
    Replies: 4
    Last Post: 22nd Jan 2009, 06:44 pm
  4. Saving files issue............need some help.
    By jsankey in forum AutoCAD General
    Replies: 6
    Last Post: 23rd Oct 2008, 05:38 pm
  5. Looping Layers
    By scottpops in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 7th Jun 2005, 07:54 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts