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
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.
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?
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. 😄
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
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
Can't find what you're looking for? Ask the community or share your knowledge.