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

Send command from a showmodaldialog

8 REPLIES 8
Reply
Message 1 of 9
scarta
801 Views, 8 Replies

Send command from a showmodaldialog

Hi, I need to write a new command that convert a lot of DGN file to DWG

I use a FORM with a ListBox where I drag&drop dgn file. I use the form with Shwmodaldialog.

When i click ok button the SendStringToExecute don'work

 

There is a way for send a command from showmodaldialog form?

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Colors
Imports System
Imports System.Windows.Forms
Imports System.Math
'Imports System.Web.Mail
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop
'Imports Microsoft.Office.Interop.Outlook
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
Imports X = Microsoft.Office.Interop.Excel


Imports Autodesk.AutoCAD.DatabaseServices.Region
Imports System.Type
Imports System.IO
Imports AcWin = Autodesk.AutoCAD.Windows

'formati saveas
'MC0.0 - DWG from Rel. 1.1 
'AC1.2 - DWG from Rel. 1.2 
'AC1.4 - DWG from Rel. 1.4 
'AC1.50 - DWG from Rel. 2.0 
'AC2.10 - DWG from Rel. 2.10 
'AC1002 - DWG from Rel. 2.5 
'AC1003 - DWG from Rel. 2.6 
'AC1004 - DWG from Rel.9 
'AC1006 - DWG from Rel.10 
'AC1009 - DWG from Rel.11/12 (or LT R1/R2) 
'AC1012 - DWG from Rel.13 (or LT95) 
'AC1014 - DWG from Rel.14, 14.01 (or LT97/LT98) 
'AC1015 - DWG from AutoCAD 2000/2000i/2002 (or LT, Map or relative Desktop version) 
'AC1018 - DWG from AutoCAD 2004/2005/2006 (or other product in the "2004", "2005" or "2006" family) 
'AC1021 - DWG from AutoCAD 2007/2008/2009 (or other product of the "2007", "2008" and "2009" families) 
'AC1024 - DWG from AutoCAD 2010 (or other product of the "2010" family) 

Public Class DgnToDWG

    Public num_digit As Integer = 0
    <CommandMethod("dgn2dwg", CommandFlags.Session)> _
    Public Sub BatchDGN_Import()

        Dim DVLAY As New DivLAY

        Dim strNomeFile As String
      
        Dim myDWG As ApplicationServices.Document
        Dim myEd As EditorInput.Editor
        Dim LockMode As Boolean = False


        Dim Frm1 As New DgnToDWG_FRM
        Dim FrmProg As New Form1
        FrmProg.TopMost = True

        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Frm1)

        myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        myDWG.LockDocument()


        Frm1.ProgressBar1.Minimum = 0

        Frm1.ProgressBar1.Maximum = Frm1.ListDWG.Items.Count
        Frm1.Label_NT.Text = Frm1.ListDWG.Items.Count

        Frm1.TopMost = True

        Frm1.Show()
        Frm1.ButtonOk.Enabled = False
        Frm1.Label_NC.Text = 0
        Frm1.Close()

        For Each Item As Object In Frm1.ListDWG.Items

            Try

                Dim sendSTR As String
                sendSTR = "-dgnimport " & Item.ToString & " " & " " & " "
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("-dgnimport " & Item.ToString & vbCr & vbCr & vbCr & vbCr, False, False, True) ' Importo il file DGN

                myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

                myDWG.LockDocument()
                LockMode = True
                Dim pos_Last_BackSlash As Integer

                Dim strPath As String = ""
                Dim origin_file As String = ""
                Dim file_Sorg As String = ""

                'Dim strPath As String = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgprefix")
                'Dim origin_file As String = strPath & Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgname")
                'Dim file_Sorg As String = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgname")

                Dim n As Integer = 1



                pos_Last_BackSlash = Item.ToString.LastIndexOf("\")

                If pos_Last_BackSlash = -1 Then pos_Last_BackSlash = Item.ToString.IndexOf("\")

                origin_file = Item.ToString.Substring(pos_Last_BackSlash + 1)
                strPath = Item.ToString.Substring(0, pos_Last_BackSlash + 1)
                file_Sorg = origin_file.Remove(origin_file.Length - 4, 4)


                DVLAY.Show()

                DVLAY.TopMost = True

                DVLAY.ProgressBar1.Value = 0
                DVLAY.ProgressBar1.Minimum = 0
                DVLAY.ProgressBar1.Maximum = Frm1.ListDWG.Items.Count



                strNomeFile = strPath & file_Sorg
                myDWG.Database.SaveAs(strNomeFile, True, DwgVersion.AC1024, myDWG.Database.SecurityParameters)
                'myDwg.LockDocument()
                myDWG.Editor.Regen()


                DVLAY.ProgressBar1.Value += 1

                System.Windows.Forms.Application.DoEvents()


                DocumentExtension.CloseAndDiscard(myDWG)

                DVLAY.Hide()




            Catch ex As Global.System.Runtime.InteropServices.COMException
                '  myFSTarg.WriteLine("errore" & txtLine)
                If LockMode = True Then
                    myDWG.LockDocument.Dispose()
                    myDWG.Editor.UpdateScreen()
                    DocumentExtension.CloseAndDiscard(myDWG)


                    Frm1.Close()
                End If
                GoTo a101

            End Try

            LockMode = False


            'aggiorno progressbar
            Frm1.ProgressBar1.Value = Frm1.ProgressBar1.Value + 1
            Frm1.Label_NC.Text = Frm1.Label_NC.Text + 1
            Frm1.ProgressBar1.Update()
            Frm1.ProgressBar1.Refresh()
            Frm1.Update()

            System.Windows.Forms.Application.DoEvents()
            ' Frm1.Label_NC.Text = Frm1.Label_NC.Text + 1


        Next


        'FrmProg.Button1.Visible = True
a101:

        Frm1.Button_Fatto.Enabled = True

        ' If int_Status = 0 Then GoTo a200

        'myTrans.Dispose()




        'a103:   MsgBox("Elaborazione terminata!!!")

    End Sub
End Class

 

8 REPLIES 8
Message 2 of 9
DesignProQuig
in reply to: scarta

You could just add the sub to the button event when it's pressed: "Public Sub BatchDGN_Import()"  Or look at send string to execute.

http://www.designprosoftware.co.uk/
Message 3 of 9
scarta
in reply to: DesignProQuig

Thank

Message 4 of 9
scarta
in reply to: DesignProQuig

Sorry but I don't understand..

 

Why the line SendStringToExecute("-dgnimport " & Item.ToString & vbCr & vbCr & vbCr & vbCr, False, False, True) don't work?

There is a way for send a command when a form is in modal mode?

Message 5 of 9
DesignProQuig
in reply to: scarta

I'm not at my PC today but did you also try putting the sub in the button click event?

Public Sub BatchDGN_Import().

 

Have a search on here there must be lots of examples of send string to execute. 😄

http://www.designprosoftware.co.uk/
Message 6 of 9
scarta
in reply to: DesignProQuig

I  have put the sub in the button, but not working....

Message 7 of 9
scarta
in reply to: DesignProQuig

There is an api procedure or class for import DGn file without use the send command?

Message 8 of 9
scarta
in reply to: DesignProQuig

I try another way: insert the dgn file as xref but I receive an error "efileaccesserr" on the line

 

Dim xrefObj As ObjectId = db.AttachXref("c:\prova1.dgn", "prova")

Why?

 

 <CommandMethod("dgn2dwg", CommandFlags.Session)> _
    Public Sub BatchDGN_Import()

        Dim DVLAY As New DivLAY
        Dim strNomeFile As String
        Dim myDWG As ApplicationServices.Document
        Dim LockMode As Boolean = False


        Dim Frm1 As New DgnToDWG_FRM




        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Frm1)


        Frm1.ProgressBar1.Minimum = 0

        Frm1.ProgressBar1.Maximum = Frm1.ListDWG.Items.Count
        Frm1.Label_NT.Text = Frm1.ListDWG.Items.Count


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


        For Each Item As Object In Frm1.ListDWG.Items


            Try

                'Dim sendSTR As String
                'sendSTR = "-dgnimport " & Item.ToString & " " & " " & " "
                'Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("-dgnimport " & Item.ToString & vbCr & vbCr & vbCr & vbCr, False, False, True) ' Importo il file DGN

                myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

                myDWG.LockDocument()
                LockMode = True

                Dim pos_Last_BackSlash As Integer

                Dim strPath As String = ""
                Dim origin_file As String = ""
                Dim file_Sorg As String = ""


                pos_Last_BackSlash = Item.ToString.LastIndexOf("\")

                If pos_Last_BackSlash = -1 Then pos_Last_BackSlash = Item.ToString.IndexOf("\")

                origin_file = Item.ToString.Substring(pos_Last_BackSlash + 1)
                strPath = Item.ToString.Substring(0, pos_Last_BackSlash + 1)
                file_Sorg = origin_file.Remove(origin_file.Length - 4, 4)


                ' Attach the Xref

                'myDWG = DocumentCollectionExtension.Open(acDocMgr, "C:\menu2000\ACA2014\acad2014.dwt", False)


                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

                Dim db As Database = doc.Database

                Using trans As Transaction = db.TransactionManager.StartTransaction()

                    ' Attach the Xref

                    Dim xrefObj As ObjectId = db.AttachXref("c:\prova1.dgn", "prova")

                    Dim br As New BlockReference(New Point3d(0, 0, 0), xrefObj)

                    ' Add the xref to model space

                    Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)

                    Dim modelSpace As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                    modelSpace.AppendEntity(br)

                    trans.AddNewlyCreatedDBObject(br, True)

                    trans.Commit()

                End Using



                DVLAY.Show()

                DVLAY.TopMost = True

                DVLAY.ProgressBar1.Value = 0
                DVLAY.ProgressBar1.Minimum = 0
                DVLAY.ProgressBar1.Maximum = Frm1.ListDWG.Items.Count




                strNomeFile = strPath & file_Sorg
                myDWG.Database.SaveAs(strNomeFile, True, DwgVersion.AC1024, myDWG.Database.SecurityParameters)
                'myDwg.LockDocument()
                myDWG.Editor.Regen()


                DVLAY.ProgressBar1.Value += 1

                System.Windows.Forms.Application.DoEvents()


                DocumentExtension.CloseAndDiscard(myDWG)

                DVLAY.Hide()




            Catch ex As Global.System.Runtime.InteropServices.COMException
                '  myFSTarg.WriteLine("errore" & txtLine)
                If LockMode = True Then
                    myDWG.LockDocument.Dispose()
                    myDWG.Editor.UpdateScreen()
                    DocumentExtension.CloseAndDiscard(myDWG)


                    Frm1.Close()
                End If
                GoTo a101

            End Try

            LockMode = False


            'aggiorno progressbar
            Frm1.ProgressBar1.Value = Frm1.ProgressBar1.Value + 1
            Frm1.Label_NC.Text = Frm1.Label_NC.Text + 1
            Frm1.ProgressBar1.Update()
            Frm1.ProgressBar1.Refresh()
            Frm1.Update()

            System.Windows.Forms.Application.DoEvents()
            ' Frm1.Label_NC.Text = Frm1.Label_NC.Text + 1


        Next


        'FrmProg.Button1.Visible = True
a101:

        Frm1.Button_Fatto.Enabled = True

        ' If int_Status = 0 Then GoTo a200

        'myTrans.Dispose()




        'a103:   MsgBox("Elaborazione terminata!!!")

    End Sub

 

Message 9 of 9
Virupaksha_aithal
in reply to: scarta

Hi,

 

Refer blog item http://adndevblog.typepad.com/autocad/2012/06/invoking-the-measure-command-using-sendcommand-in-vbne... which explains why the SendStringToExecute is not working as expected. As explained in blog, use ActiveX SendCommand API.

rmal>Note:

 

1.         Do not send us any information that you consider to be a trade secret to your company

2.         Code snippets will not help us, because they don't allow us to reproduce the context in which the code runs



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

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