.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reading DWG File with VB.NET without opening Autocad Editor

4 REPLIES 4
Reply
Message 1 of 5
dderoeck
9639 Views, 4 Replies

Reading DWG File with VB.NET without opening Autocad Editor

Hello,

I'm rather new in .Net, though I want to make a Tool that will help me to analyze Used layers in a Folder with a lot of DWG's.

The subject is simple:

(For now)

Open File Dialogue Box, Select a DWG file, Read the DWG file to extract all layers to a text file.

(Later; read all DGW's of a slected Folder, read all the DWG's, export the layers and relative DWG to a TXT file, reading the txt file with another .Net application, entering a layer Keyword, get the DWG from the TXT file, open the DWG in True View.) 

 

Here is my code for reading 1 DWG:

 

'***************************************************************************

Imports System.IO

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

 

Public Class Form1

Private Sub SelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFile.Click

Dim myStream As Stream = Nothing

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "D:\"

openFileDialog1.Filter = "DWG files (*.dwg)|*.dwg" openFileDialog1.FilterIndex = 1

openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Try

myStream = openFileDialog1.OpenFile()

Finally

End Try

End If

 

If openFileDialog1.FileName.Length = 0 Then Exit Sub

Dim TextLine As String = Nothing

Dim objReader As New System.IO.StreamReader(myStream)

 

'Set path to DWGfile:

Dim myfile As String = Nothing

myfile = openFileDialog1.FileName

myfile = myfile.Replace(".dwg", ".txt")

 

'Delete Txt file if exists

Dim FileToDelete As String

FileToDelete = myfile

If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete)

 

Dim objWriter As New System.IO.StreamWriter(myfile)

 

'Open the DWG File For Reading

Dim myDB As New DatabaseServices.Database

myDB.ReadDwgFile(openFileDialog1.FileName, FileOpenMode.OpenForReadAndAllShare, True)

Dim myBT As DatabaseServices.BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)

Dim myBTR As DatabaseServices.BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)

 

For Each myObjID As ObjectId In myBTR

Dim myEntity As Entity = myObjID.GetObject(OpenMode.ForRead)

If myEntity.Layer.Length > 2 Then objWriter.Write(myEntity.Layer.Length)

Next

 

objWriter.Close()

myStream.Close()

MessageBox.Show("Writing succesful!", "Layer list", MessageBoxButtons.OK, MessageBoxIcon.Information)

Me.Close()

 

End Sub 

End Class

'*********************************************************************

 

Copying this into VS 2008 does not give any Errors

The 2 necessary autocad dll's acdbmgd.dll and acmgd.dll are loaded and not copied.

 

Still when I want to run this code I receive the error:

Could not load assembly acdbmgd, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null ...

 

Can anyone solve this problem? Which dll I have to add? Do I have to use other autocad dll ?

 

Thanx for reading this Subject.

Hope to receive an awnser.

 

 

 

 

 

 

 

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: dderoeck

In general there are 3 AutoCAD DLL's to load: AcDbMgd.dll, AcMgd.dll, AcCui.dll

But the versions depend on the AutoCAD version, AutoCAD 2008..2011

Also the version of VisStudio and the framework to compile to depends on the AutoCAD version.

Try to figure out the required combinations with just a simple program displaying a MsgBox.

 

 

Message 3 of 5
dderoeck
in reply to: Anonymous

Hello Frits,

I'm working with VS 2008, Autocad 2010 (Civil or Map) - 32 bit version, Framework 3.5.

I'm gonna try your proposal.

Hopefully this works.

Message 4 of 5
dgorsman
in reply to: dderoeck

Not sure if your title is fully accurate.  Under most circumstances (like creating a DLL) you *must* run AutoCAD.  To clarify, do you mean doing this task without opening each drawing on screen, or without running the AutoCAD application?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 5 of 5
ehsan_bahrani
in reply to: dderoeck


@dderoeck wrote:

Hello,

I'm rather new in .Net, though I want to make a Tool that will help me to analyze Used layers in a Folder with a lot of DWG's.

The subject is simple:

(For now)

Open File Dialogue Box, Select a DWG file, Read the DWG file to extract all layers to a text file.

(Later; read all DGW's of a slected Folder, read all the DWG's, export the layers and relative DWG to a TXT file, reading the txt file with another .Net application, entering a layer Keyword, get the DWG from the TXT file, open the DWG in True View.) 

 

Here is my code for reading 1 DWG:

 

'***************************************************************************

Imports System.IO

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

 

Public Class Form1

Private Sub SelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFile.Click

Dim myStream As Stream = Nothing

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "D:\"

openFileDialog1.Filter = "DWG files (*.dwg)|*.dwg" openFileDialog1.FilterIndex = 1

openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Try

myStream = openFileDialog1.OpenFile()

Finally

End Try

End If

 

If openFileDialog1.FileName.Length = 0 Then Exit Sub

Dim TextLine As String = Nothing

Dim objReader As New System.IO.StreamReader(myStream)

 

'Set path to DWGfile:

Dim myfile As String = Nothing

myfile = openFileDialog1.FileName

myfile = myfile.Replace(".dwg", ".txt")

 

'Delete Txt file if exists

Dim FileToDelete As String

FileToDelete = myfile

If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete)

 

Dim objWriter As New System.IO.StreamWriter(myfile)

 

'Open the DWG File For Reading

Dim myDB As New DatabaseServices.Database

myDB.ReadDwgFile(openFileDialog1.FileName, FileOpenMode.OpenForReadAndAllShare, True)

Dim myBT As DatabaseServices.BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)

Dim myBTR As DatabaseServices.BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)

 

For Each myObjID As ObjectId In myBTR

Dim myEntity As Entity = myObjID.GetObject(OpenMode.ForRead)

If myEntity.Layer.Length > 2 Then objWriter.Write(myEntity.Layer.Length)

Next

 

objWriter.Close()

myStream.Close()

MessageBox.Show("Writing succesful!", "Layer list", MessageBoxButtons.OK, MessageBoxIcon.Information)

Me.Close()

 

End Sub 

End Class

'*********************************************************************

 

Copying this into VS 2008 does not give any Errors

The 2 necessary autocad dll's acdbmgd.dll and acmgd.dll are loaded and not copied.

 

Still when I want to run this code I receive the error:

Could not load assembly acdbmgd, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null ...

 

Can anyone solve this problem? Which dll I have to add? Do I have to use other autocad dll ?

 

Thanx for reading this Subject.

Hope to receive an awnser.

 

 

 

 

 


@dderoeck wrote:

Hello,

I'm rather new in .Net, though I want to make a Tool that will help me to analyze Used layers in a Folder with a lot of DWG's.

The subject is simple:

(For now)

Open File Dialogue Box, Select a DWG file, Read the DWG file to extract all layers to a text file.

(Later; read all DGW's of a slected Folder, read all the DWG's, export the layers and relative DWG to a TXT file, reading the txt file with another .Net application, entering a layer Keyword, get the DWG from the TXT file, open the DWG in True View.) 

 

Here is my code for reading 1 DWG:

 

'***************************************************************************

Imports System.IO

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

 

Public Class Form1

Private Sub SelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFile.Click

Dim myStream As Stream = Nothing

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "D:\"

openFileDialog1.Filter = "DWG files (*.dwg)|*.dwg" openFileDialog1.FilterIndex = 1

openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Try

myStream = openFileDialog1.OpenFile()

Finally

End Try

End If

 

If openFileDialog1.FileName.Length = 0 Then Exit Sub

Dim TextLine As String = Nothing

Dim objReader As New System.IO.StreamReader(myStream)

 

'Set path to DWGfile:

Dim myfile As String = Nothing

myfile = openFileDialog1.FileName

myfile = myfile.Replace(".dwg", ".txt")

 

'Delete Txt file if exists

Dim FileToDelete As String

FileToDelete = myfile

If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete)

 

Dim objWriter As New System.IO.StreamWriter(myfile)

 

'Open the DWG File For Reading

Dim myDB As New DatabaseServices.Database

myDB.ReadDwgFile(openFileDialog1.FileName, FileOpenMode.OpenForReadAndAllShare, True)

Dim myBT As DatabaseServices.BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)

Dim myBTR As DatabaseServices.BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)

 

For Each myObjID As ObjectId In myBTR

Dim myEntity As Entity = myObjID.GetObject(OpenMode.ForRead)

If myEntity.Layer.Length > 2 Then objWriter.Write(myEntity.Layer.Length)

Next

 

objWriter.Close()

myStream.Close()

MessageBox.Show("Writing succesful!", "Layer list", MessageBoxButtons.OK, MessageBoxIcon.Information)

Me.Close()

 

End Sub 

End Class

'*********************************************************************

 

Copying this into VS 2008 does not give any Errors

The 2 necessary autocad dll's acdbmgd.dll and acmgd.dll are loaded and not copied.

 

Still when I want to run this code I receive the error:

Could not load assembly acdbmgd, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null ...

 

Can anyone solve this problem? Which dll I have to add? Do I have to use other autocad dll ?

 

Thanx for reading this Subject.

Hope to receive an awnser.

 

 

 

 

 

 

 


you have to set "Copy Local" to True

after that when using transAction i will get an error! (System.InvalidProgramException: 'Common Language Runtime detected an invalid program.')

 

any suggestion?

 


 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost