Export to AutoCAD using Visual Basic
Export to AutoCAD using Visual Basic
(OP)
I'm trying to write a routine in Visual Basic to export Pro/Engineer drawings to AutoCAD but I can't find the correct command for this and I can't discover how to run map keys from code.
The VB command are the same as JLink as far as I can tell.
I can open loop through drawing files in a folder and erase them from session without any problem but I can't save/publish/export to dwg.
Does anyone have any experience in this?
The VB command are the same as JLink as far as I can tell.
I can open loop through drawing files in a folder and erase them from session without any problem but I can't save/publish/export to dwg.
Does anyone have any experience in this?





RE: Export to AutoCAD using Visual Basic
Pro/Batch allowed me to to DWG/DXF + PDF exports all in one go. I didn't use visual basic (I just used DOS *.BAT files), but you should be able to do the same from a VB routine.
Try doing a search here and elsewhere for Pro/Batch stuff. Unfortunately, my brain has taken an early friday and I can't recall any details.
RE: Export to AutoCAD using Visual Basic
I took a lot of the code from the VB parameters example that can be found in the loadpoint
There isn't a great deal of error handling as I only need it to work once but maybe someone can improve on it.
CODE
Imports System.IO
Public Class User_Form
Dim asyncConnection As IpfcAsyncConnection = Nothing
Dim dValue As Double
#Region "Form Events"
'======================================================================
'Function : FormPD_Load
'Purpose : Event handler to check if required environmental
' variables have been set and name server is running
'======================================================================
Private Sub User_Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim errMsg As String = ""
If Not System.Diagnostics.Process.GetProcessesByName("nmsd").Length > 0 Then
errMsg = "Name service is not running on the system."
End If
If System.Environment.GetEnvironmentVariable("PRO_COMM_MSG_EXE") = "" Then
If errMsg = "" Then
errMsg = "Environment variable 'PRO_COMM_MSG_EXE' has not been set."
Else
errMsg = errMsg + Chr(13).ToString + "Environment variable 'PRO_COMM_MSG_EXE' has not been set."
End If
End If
If System.Environment.GetEnvironmentVariable("PRO_DIRECTORY") Is Nothing Then
If errMsg = "" Then
errMsg = "Environment variable 'PRO_DIRECTORY' has not been set."
Else
errMsg = errMsg + Chr(13).ToString + "Environment variable 'PRO_DIRECTORY' has not been set."
End If
End If
'Set the opening status of the buttons
Me.ctl_Connect.Enabled = True
Me.ctl_Disconnect.Enabled = False
Me.ctl_Convert.Enabled = False
If Not errMsg = "" Then
errMsg = errMsg + Chr(13).ToString + "These may lead to errors in running the application."
MsgBox(errMsg, MsgBoxStyle.Critical)
End If
End Sub
'======================================================================
'Function : FormPD_FormClosing
'Purpose : Event handler to disconnect from Pro/E if aldready
' connected.
'======================================================================
Private Sub User_Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not asyncConnection Is Nothing AndAlso asyncConnection.IsRunning Then
asyncConnection.Disconnect(1)
End If
End Sub
'======================================================================
'Function : btnConnect_Click
'Purpose : Event handler to connect to existing Pro/E session.
'======================================================================
Private Sub ctl_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctl_Connect.Click
Try
lblStatus.Text = "Connecting..."
Me.lblStatus.Refresh()
Me.Cursor = Cursors.WaitCursor
If asyncConnection Is Nothing OrElse Not asyncConnection.IsRunning Then
asyncConnection = (New CCpfcAsyncConnection).Connect(Nothing, Nothing, Nothing, Nothing)
End If
'Re-set the status of the buttons
Me.ctl_Connect.Enabled = False
Me.ctl_Disconnect.Enabled = True
Me.ctl_Convert.Enabled = True
lblStatus.Text = "Connection established to Pro/Engineer Session"
Catch ex As Exception
MsgBox(ex.Message.ToString + Chr(13) + ex.StackTrace.ToString)
If Not asyncConnection Is Nothing AndAlso asyncConnection.IsRunning Then
asyncConnection.Disconnect(1)
End If
lblStatus.Text = "Error occurred while connecting to existing Pro/E Session"
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
'======================================================================
'Function : btnDisconnect_Click
'Purpose : Event handler to disconnect from existing Pro/E session
'======================================================================
Private Sub ctl_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctl_Disconnect.Click
Try
lblStatus.Text = ""
If Not asyncConnection Is Nothing AndAlso asyncConnection.IsRunning Then
asyncConnection.Disconnect(1)
lblStatus.Text = "Connection to Pro/Engineer Session terminated"
Else
lblStatus.Text = "No Connection is present to any Pro/Engineer Session"
End If
'Re-set the status of the buttons
Me.ctl_Connect.Enabled = True
Me.ctl_Disconnect.Enabled = False
Me.ctl_Convert.Enabled = False
Catch ex As Exception
MsgBox(ex.Message.ToString + Chr(13) + ex.StackTrace.ToString)
End Try
End Sub
#End Region
Private Sub ctl_Convert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctl_Convert.Click
'Start by checking to see if the folder text boxes are set
If Me.txtProEFolder.Text.Trim.Length = 0 Then
MessageBox.Show("The ProE Folder is not set", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
If Me.txtAcadFolder.Text.Trim.Length = 0 Then
MessageBox.Show("The AutoCAD Folder is not set", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
'Check that both folders exist
If Not IO.Directory.Exists(Me.txtProEFolder.Text.Trim) Then
MessageBox.Show("The ProE Folder specified does not exist", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
If Not IO.Directory.Exists(Me.txtAcadFolder.Text.Trim) Then
MessageBox.Show("The AutoCAD Folder specified does not exist", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
'Set the working directory of Pro/E
asyncConnection.Session.ChangeDirectory(Me.txtProEFolder.Text.Trim)
'Collect the list of files in the ProE Folder
Dim strFileList As String()
strFileList = IO.Directory.GetFiles(Me.txtProEFolder.Text.Trim, "*.drw.*")
'Declare a list of drawings
Dim strDrawingList As New ArrayList
'Loop through the list collecting a list of all drawings
For Each strFileName As String In strFileList
'Collect the drawing name without the path or the last integer
Dim strDrgName As String = strFileName.Remove(0, Me.txtProEFolder.Text.Trim.Length + 1)
strDrgName = strDrgName.Remove(strDrgName.IndexOf(".drw.") + 4)
'Add to the drawing list if it doesn't already exist
If Not strDrawingList.Contains(strDrgName) Then
strDrawingList.Add(strDrgName)
End If
Next
'Declare counter objects for the progress bar
Dim iItemIndex As Integer = 0
Me.ProgressBar1.Maximum = strDrawingList.Count
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Value = 0
'Loop through the list and open the drawing
For Each strNextDrg As String In strDrawingList
'Update the current drawing label
Me.lblCurrentFile.Text = "Current Drawing: " & strNextDrg
Me.lblCurrentFile.Refresh()
'Increment the item index
iItemIndex += 1
'Create a model descriptor
Dim myModelDesc = (New pfcls.CCpfcModelDescriptor).CreateFromFileName(strNextDrg)
'Open the drawing
asyncConnection.Session.OpenFile(myModelDesc)
'Collect the model
Dim myModel As IpfcModel = asyncConnection.Session.GetModelFromFileName(strNextDrg)
'Construct the file name
Dim strFilePath As String
strFilePath = Me.txtAcadFolder.Text.Trim & "/" & strNextDrg.Replace(".drw", ".dxf")
'Create a DXF export instruction
Dim myDXFInstr = (New CCpfcDXFExportInstructions).Create
'Export the drawing to dxf
myModel.Export(strFilePath, myDXFInstr)
'Delete the log file in the autocad output folder
IO.File.Delete(strFilePath.Replace(".dxf", "_dxf__out.log.1"))
'Erase the drawing and all associated models that have been placed in session
myModel.EraseWithDependencies()
myModel = Nothing
'Update the progress bar
Me.ProgressBar1.Value = iItemIndex
Me.ProgressBar1.Refresh()
Next
'Inform the user
MessageBox.Show("Process Complete", "Pro E to ACAD", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub
End Class