.NET

.NET

Reply
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 1 of 6 (471 Views)
Accepted Solution

Open a .dxf file from windows form button

471 Views, 5 Replies
11-26-2012 09:08 AM

HI ,

I got a problem to open a dxf file. If I use command method then I can easily do that. But I want to open that file by clicking a button from my windows form. I did the coding for that specified button like below:

 

Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click

 

Dim strFileName AsString

LoadDXF.InitialDirectory = "C:\"

LoadDXF.Title = "Open DXF File"

LoadDXF.Filter = "DXF Files|*.dxf"

LoadDXF.ShowDialog()

strFileName = LoadDXF.FileName

Dim acDocMgr AsDocumentCollection = Application.DocumentManager  

If (File.Exists(strFileName)) Then

acDocMgr.Open(strFileName, False) 

Else

acDocMgr.MdiActiveDocument.Editor.WriteMessage( "File" & strFileName & "does not exist.")

EndIf

EndSub

 

After invoke that button I got a error like below:

Capture.JPG

 

I have searched to solve this problem in the forum. But did not get any productive answer. Can anybody help me in this regards. How can I open the drawing without commandFlags.session? I want to use the button of my form to open that dxf file. Please give me a details hints because I am pretty new in programming.

 

Thanks in advance.

Zakir

Try something like this.  I haven't tried but I think it should work.  Let me know if it doesn't and please next time post the full text from the exception detail. (FYI: I had to correct my original response!)

 

 

 

            Dim LoadDXF As New System.Windows.Forms.OpenFileDialog

            LoadDXF.InitialDirectory = "C:\"

            LoadDXF.Title = "Open DXF File"

            LoadDXF.Filter = "DXF Files|*.dxf"

            Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager

            Try

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

                    Dim files() As String = LoadDXF.FileNames

                    For Each _file In files

                        If (File.Exists(_file)) Then

                            Dim acDoc As Document = DocumentCollectionExtension.Add(acDocMgr, _file)

                            acDocMgr.MdiActiveDocument = acDoc

                        Else

                            acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & _file & " does not exist.")

                        End If

                    Next

                End If

            Catch

                acDocMgr.MdiActiveDocument.Editor.WriteMessage("ERROR: " & Err.Description)

            End Try

 

 

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 2 of 6 (457 Views)

Re: Open a .dxf file from windows form button

11-26-2012 11:08 AM in reply to: mzakiralam

Try something like this.  I haven't tried but I think it should work.  Let me know if it doesn't and please next time post the full text from the exception detail. (FYI: I had to correct my original response!)

 

 

 

            Dim LoadDXF As New System.Windows.Forms.OpenFileDialog

            LoadDXF.InitialDirectory = "C:\"

            LoadDXF.Title = "Open DXF File"

            LoadDXF.Filter = "DXF Files|*.dxf"

            Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager

            Try

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

                    Dim files() As String = LoadDXF.FileNames

                    For Each _file In files

                        If (File.Exists(_file)) Then

                            Dim acDoc As Document = DocumentCollectionExtension.Add(acDocMgr, _file)

                            acDocMgr.MdiActiveDocument = acDoc

                        Else

                            acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & _file & " does not exist.")

                        End If

                    Next

                End If

            Catch

                acDocMgr.MdiActiveDocument.Editor.WriteMessage("ERROR: " & Err.Description)

            End Try

 

 

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 3 of 6 (446 Views)

Re: Open a .dxf file from windows form button

11-26-2012 11:49 AM in reply to: sszabo

Another important piece of Info that's needed here is what version of ACAD are we talking about?!  I assumed the latest (2013).  If not opening the Document object is going to be slightly different:

 

http://exchange.autodesk.com/autocadmep/enu/online-help/BLDSYS/2012/ENU/pages/WS73099cc142f48755-385...

 

 

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 4 of 6 (428 Views)

Re: Open a .dxf file from windows form button

11-27-2012 01:39 AM in reply to: sszabo

Thanks for your reply. I am sorry that I did not mention my ACAD version and VB version. I am using ACAD 2011 and VB2010 (premium). I am also sorry that I did not understand one of your sentence "please next time post the full text from the exception detail". Actually I do not know how to post from exception detail.

 

I try to write according you show. But when I try to write the line below

 

 Dim acDoc As Document = DocumentCollectionExtension, it tells me that DocumentCollectionExtension is not declared.

 

my code is now look like below:

Capture.JPG

 

 

 

 

If you have time please let me share your suggestion. Thanks anyway

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 5 of 6 (422 Views)

Re: Open a .dxf file from windows form button

11-27-2012 02:46 AM in reply to: mzakiralam

I have done it. Thanks a lot for your hints

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 6 of 6 (416 Views)

Re: Open a .dxf file from windows form button

11-27-2012 05:36 AM in reply to: mzakiralam

Ok, that's great. 

 

You can find the original ACAD .NET exception in the dialog box you captured and posted with your question if you just push the "Details" button. 

 

Also, the "For Each _file In files" loop only makes sense if you allow the user to select multiple files.  I just noticed that later but if you do that you wil need some additional acad code to make it work so you can probably just lose the loop and go with the original idea of allowing only 1 file to be selected and simply assigning _file to LoadDXF.FileName instead of LoadDXF.FileNames.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.