I have some DWG files that I can not get to open with the following code to process them as some batch operation, these files were exported from Solid Works and are not real DWG files. can I open them in some other way to get simple batch operations to work with non Autodesk DWG files?
<CommandMethod("Batch")> Public Sub bathcfiles() Dim acDialog As New System.Windows.Forms.OpenFileDialog acDialog.Multiselect = True If acDialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then For Each i As String In acDialog.FileNames Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = acDocMgr.Open(i, True) ' do something with the file acDoc.CloseAndDiscard() Next End If End Sub
Solved! Go to Solution.
Solved by hgasty1001. Go to Solution.
@Mike.Wohletz wrote:I have some DWG files that I can not get to open with the following code to process them as some batch operation, these files were exported from Solid Works and are not real DWG files. can I open them in some other way to get simple batch operations to work with non Autodesk DWG files?
<CommandMethod("Batch")> Public Sub bathcfiles() Dim acDialog As New System.Windows.Forms.OpenFileDialog acDialog.Multiselect = True If acDialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then For Each i As String In acDialog.FileNames Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = acDocMgr.Open(i, True) ' do something with the file acDoc.CloseAndDiscard() Next End If End Sub
Hello
Could you include a msgbox so as to view the path of the file you want to open because i'm not sure the opendialogfille reply the complete path.
Bye
DWGCHECK is set to 1
This works fine on all normal DWG files created by an Autodesk application, the problem that I have is if one of these gets into the mix of my batch it will crash the application and will go no farther.
Imports Autodesk.AutoCAD.DatabaseServices Public Class resave Sub testit() ' resave the drawing before opening it in Autocad Interface. ReSaveFile("c:\test1.dwg") End Sub Function ReSaveFile(ByVal FileName As String) As Boolean If FileExists(FileName) Then If Not isFileInUse(FileName) Then Using db As New Database(False, False) Try ' Read File db.ReadDwgFile(FileName, FileOpenMode.OpenForReadAndReadShare, True, "") ' Write File db.SaveAs(FileName, True, DwgVersion.Current, db.SecurityParameters) Return True Catch ex As System.Exception Return False End Try End Using Else Return False End If Else Return False End If End Function Public Function FileExists(ByVal FileFullPath As String) _ As Boolean Try Dim f As New IO.FileInfo(FileFullPath) Return f.Exists Catch ex As Exception Return False End Try End Function Public Function isFileInUse(ByVal sFile As String) As Boolean If System.IO.File.Exists(sFile) Then Try Dim F As Short = CShort(FreeFile()) FileOpen(F, sFile, Microsoft.VisualBasic.OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite) FileClose(F) Catch Return True End Try End If End Function End Class
I am a bit lost, tried all sorts of commandflags and still nothing. Tried the resave options and it craps out at "db.ReadDwgFile(FileName, FileOpenMode.OpenForReadAndReadShare, True, "")"
the error is "eNoInputFiler"
I have attached a sample drawing with this, this drawing has had all the proprietary information removed and resaved as an AutoCAD Mechanical 2008 file.
this is a screenshot of my debug window.
db.ReadDwgFile(FileName, System.IO.FileShare.Read, True, "")
You need to lock the document first
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.
Application.DocumentManager.MdiActiveDocument
Using lock As DocumentLock = dwg.LockDocument
Using db As New Database(False, False)
....
End Using
End Using
Hi,
I think you need to set DWGCHECK system variable to 2 first. And if you need to take some action depending if the dwg is a trusted one, you can check the DwgFileWasSavedByAutodeskSoftware of the database object.
Gaston Nunez
Thanks that solved the problem, my first issue was not being able to open the files in a batch and the DWGCHECK set to 2 solved that. This will allow me to batch plot or some other routine that will not make changes to the file. the second is the DwgFileWasSavedByAutodeskSoftware will solve the problem of modifying drawing, if I hit this and it is false I know that it was not done in AutoCAD and can move on to the next.
My testing code that is working:
Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Public Class Class1 <CommandMethod("Batch", CommandFlags.Session)> Public Sub bathcfiles() Dim acDialog As New System.Windows.Forms.OpenFileDialog acDialog.Multiselect = True acDialog.Filter = "Drawing Files (*.DWG)|*.DWG" Dim acDwgCheck As Integer = Application.GetSystemVariable("DWGCHECK") Application.SetSystemVariable("DWGCHECK", 2) Try If acDialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then For Each i As String In acDialog.FileNames Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = acDocMgr.Open(i) ' do something with the file If acDoc.Database.DwgFileWasSavedByAutodeskSoftware Then ' this is an Autodesk DWG so we can make changes MsgBox(String.Format("file: {0} was created by an Autodesk application", acDoc.Name)) Else MsgBox(String.Format("file: {0} was not created by an Autodesk application", acDoc.Name)) End If acDoc.CloseAndDiscard() Next End If Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.Message) End Try Application.SetSystemVariable("DWGCHECK", acDwgCheck) End Sub End Class