Author Topic: autocad batch plot Visual Basic.NET  (Read 4359 times)

0 Members and 1 Guest are viewing this topic.

dan503

  • Guest
autocad batch plot Visual Basic.NET
« on: September 18, 2007, 01:14:51 AM »
Hey Everyone,
I have been using vb.net for about3 or 4 months now. i have made a program that reads the location of drawings from a word file and then copies and renames the file. i am interested in having my program print the drawings using autocad. Here is a copy of the code i am using.
Code: [Select]
        ' Autodesk AutoCAD 2000 or above and Universal Document Converter
        ' should be installed on your PC

        ' Define constants
        Const acExtents = 1
        Const acScaleToFit = 0

        ' Run AutoCAD as COM-server
        On Error Resume Next
        Dim App As Object
        'Dim App As New AutoCAD.AcadApplication
        Dim Drawing As Object
        Dim Layout As Object
        Dim ActiveSpace As Object
        Dim n As Integer = 0
        Dim DrawType As String = ""
        Dim FilePath As String = ""
        Dim MyDwg As String = ""
        Dim Dwg As Object
        Dim DwgClass As String = ""
        Dim k As Integer = 0
        Dim strFilePath As String = ""



        App = CreateObject("AutoCAD.Application")

        For n = 0 To DataGridView1.RowCount - 1 'Determines what File Type the user selected it was, and whether or not the user select a file type.
            If DataGridView1.Item(2, n).Value = "False" And DataGridView1.Item(3, n).Value = "False" And DataGridView1.Item(4, n).Value = "False" Then 'Determines whether or not the user select a file type.
                MsgBox("You must select what type of drawing " & DataGridView1.Item(0, n).Value & " is.", CType(vbOKOnly + vbCritical, MsgBoxStyle))
                Exit Sub
            End If
        Next n
        For n = 0 To DataGridView1.RowCount - 1 'Determines what File Type that the user selected
            MyDwg = DataGridView1.Item(0, n).Value
            MsgBox(MyDwg)
            If DataGridView1.Item(2, n).Value = "True" Then
                DrawType = ".dwg"
            ElseIf DataGridView1.Item(3, n).Value = "True" Then
                DrawType = ".idw"
            ElseIf DataGridView1.Item(4, n).Value = "True" Then
                DrawType = ".pdf"
            Else
                MsgBox("You must select what type of drawing " & DataGridView1.Item(0, n).Value & " is.", CType(vbOKOnly + vbCritical, MsgBoxStyle))
                Exit Sub
            End If

            Dwg = Split(MyDwg, "-") 'Splits the Drawing Name (MyDwg) into an array
            DwgClass = Dwg(2)
            Trim$(DwgClass)
            MsgBox(DwgClass)
            For k = 0 To UBound(CType(Dwg, Array))
                Dwg(k) = Trim$(Dwg(k))
            Next k
            If DwgClass Like "###" Then '### stands for 3 numeric values
                MyDwg = Dwg(0) & "-" & Dwg(1) & "-" & Dwg(2)
            ElseIf DwgClass Like "###[A-Z]" Then '### stands for 3 numeric values and [A-Z] is a wildcard of only these letters and are Capital
                MyDwg = Dwg(0) & "-" & Dwg(1) & "-" & Dwg(2)
            Else
                MsgBox("There seems to be a problem with the drawings class.", vbOKOnly + vbCritical)
            End If
            MsgBox(DwgClass)
            Select Case Microsoft.VisualBasic.Left$(DwgClass, 1)
                Case "2"
                    strFilePath = strelement & My.Settings.PIDText & MyDwg & DrawType 'My.Settings.PIDText gets folder structure from config file)
                Case "3"
                    strFilePath = strelement & My.Settings.VesselText & MyDwg & DrawType 'My.Settings.VesselText gets folder structure from config file
                Case "4"
                    strFilePath = strelement & My.Settings.StructuralText & MyDwg & DrawType 'My.Settings.StructuralText gets folder structure from config file
                Case "5"
                    strFilePath = strelement & My.Settings.PipingText & MyDwg & DrawType ''My.Settings.PipingText gets folder structure from config file
                Case "6"
                    strFilePath = strelement & My.Settings.ISOText & MyDwg & DrawType 'My.Settings.ISOText gets folder structure from config file
                Case "7"
                    strFilePath = strelement & My.Settings.InstElecText & MyDwg & DrawType 'My.Settings.InstElecText gets folder structure from config file
                Case "8"
                    strFilePath = strelement & My.Settings.MembraneTubesText & MyDwg & DrawType 'My.Settings.MembraneTubesText gets folder structure from config file
                Case Else
                    MsgBox(DwgClass & " - This Is An Invalid Drawing Class", CType(vbOKOnly + vbCritical, MsgBoxStyle))
            End Select

            App.Preferences.System.SingleDocumentMode = False

            Drawing = App.Documents.Open(strFilePath, False)
            If Drawing = Not Nothing Then

                ' Change AutoCAD preferences for scaling the drawing to page
                If Drawing.ActiveSpace = 0 Then

                    ActiveSpace = Drawing.PaperSpace
                    Layout = ActiveSpace.Layout

                Else

                    ActiveSpace = Drawing.ModelSpace
                    Layout = ActiveSpace.Layout

                End If

                Layout.PlotType = acExtents
                Layout.UseStandardScale = True
                Layout.StandardScale = acScaleToFit

                ' Plot the drawing
                Call Drawing.Plot.PlotToDevice("Dell AIO Printer A920.pc3")

                ' Close drawing
                Drawing.Close(False)
                Drawing = Nothing

            End If
            System.Threading.Thread.Sleep(30000) 'added so that there is a chance for the drawing to print out before the other one starts.
        Next n
I cant use the AutoCAD API because this is suppose to work for multiple versions of autocad. I am sure That there is a better way but i do not know how. Can anyone help me.
Thanks,
Dan

<KEB edited to add code tags>
« Last Edit: September 18, 2007, 08:40:11 AM by Keith™ »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autocad batch plot Visual Basic.NET
« Reply #1 on: September 18, 2007, 03:01:58 AM »
I really hate VB too much to have a good look ..
but you may want to fix the quote character in the Case "5" conditional.

and, does
On Error Resume Next really work with the VB.Net API ?


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Nathan Taylor

  • Guest
Re: autocad batch plot Visual Basic.NET
« Reply #2 on: September 18, 2007, 06:18:48 PM »
Regarding the use of On Error Resume Next it should not be used how you have done so in any version of VB because it is hiding errors rather than dealing with them. With .NET forget about the unstructured error handling of On Error statements and use the staructured error handling of Try/Catch blocks.

Regards - Nathan

dan503

  • Guest
Re: autocad batch plot Visual Basic.NET
« Reply #3 on: September 19, 2007, 01:24:52 PM »
On the rest on my program i have used the try, catch error handling. I just haven't change it yet. i prints fine when it is just 1 drawing. But when there are multiple drawings it only prints one of them. I added
Code: [Select]
System.Threading.Thread.Sleep(30000) 'added so that there is a chance for the drawing to print out before the other one starts.to fix that problem but i was looking for a way that autocad would communicate back that a drawings has been sent to the printer so that it can open and print another drawing. This is so that the program will run faster. i also want it so that it will work even when i am using autocad at the same time and then close the instance that it opened when it is finished. I want it to run similar to the batch plot utility on autocad 2000i.
Thanks for the help.
Dan