Plot from model space

Plot from model space

Anonymous
Not applicable
824 Views
7 Replies
Message 1 of 8

Plot from model space

Anonymous
Not applicable
I’m new to VB and looking for some help on this...I’m writing a windows app that will batch plot acad dwgs...I found this example code in the developers help section
but get this error - Use the "new" keyword to create an object instance.

I tried a few different scenarios with no luck
I haven’t got a clue where to use the "new" keyword

Here is the code

Sub Ch9_PrintModelSpace()
' Verify that the active space is model space
If acadDoc.ActiveSpace = AcActiveSpace.acModelSpace Then
acadDoc.MSpace = True
acadDoc.ActiveSpace = AcActiveSpace.acModelSpace
End If

' Set the extents and scale of the plot area
acadDoc.ModelSpace.Layout.PlotType = AcPlotType.acExtents
acadDoc.ModelSpace.Layout.StandardScale = AcPlotScale.acScaleToFit

' Set the number of copies to one
acadDoc.Plot.NumberOfCopies = 1

' Initiate the plot
acadDoc.Plot.PlotToDevice()
End Sub
0 Likes
825 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
do you have more code you could post? i.e. did you set acaddoc to anything?
0 Likes
Message 3 of 8

Anonymous
Not applicable
Sounds like you have not instantiated your AcadDoc object. Based on your
posting I don't see where that occurs. Basically, you must do the following
somewhere within your application.

Dim AcadApp As AcadApplication

On Error Resume Next

Set AcadApp = GetObject(Class:="AutoCAD.Application")

If Err Then
Err.Clear

Set AcadApp = CreateObject(Class:="AutoCAD.Application")

If Err Then
MsgBox "Error connecting to AutoCAD", vbCritical, "AutoCAD Fatal Error"
Exit Sub
End If
End If

Set AcadDoc = AcadApp.ActiveDocument

Joe ...




"smiche" wrote in message news:6023496@discussion.autodesk.com...
I’m new to VB and looking for some help on this...I’m writing a windows app
that will batch plot acad dwgs...I found this example code in the developers
help section
but get this error - Use the "new" keyword to create an object instance.

I tried a few different scenarios with no luck
I haven’t got a clue where to use the "new" keyword

Here is the code

Sub Ch9_PrintModelSpace()
' Verify that the active space is model space
If acadDoc.ActiveSpace = AcActiveSpace.acModelSpace Then
acadDoc.MSpace = True
acadDoc.ActiveSpace = AcActiveSpace.acModelSpace
End If

' Set the extents and scale of the plot area
acadDoc.ModelSpace.Layout.PlotType = AcPlotType.acExtents
acadDoc.ModelSpace.Layout.StandardScale = AcPlotScale.acScaleToFit

' Set the number of copies to one
acadDoc.Plot.NumberOfCopies = 1

' Initiate the plot
acadDoc.Plot.PlotToDevice()
End Sub
0 Likes
Message 4 of 8

Anonymous
Not applicable
much better said! that's why you're an author 🙂
0 Likes
Message 5 of 8

Anonymous
Not applicable
Sorry, I posted before hitting refresh.

Joe ...


"cadger" wrote in message news:6023781@discussion.autodesk.com...
much better said! that's why you're an author 🙂
0 Likes
Message 6 of 8

Anonymous
Not applicable
Hi Joe, Thanks for looking at my issue!
Im all tangled up and not sure were to add your example in my code? I'm still gettin error saying the "new" keyword.

Here is the rest of my code if that helps...

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class Form1
Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument

Public Sub GetAcadApplication()
Try
AcadApp = GetObject(, "AutoCAD.Application.17.1")
AcadApp.Visible = True
Catch
Try
AcadApp = CreateObject("AutoCAD.Application.17.1")
AcadApp.Visible = True
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
Exit Sub
End Try
End Try
End Sub

Private Sub RetrieveDwgs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RetrieveDwgs.Click
Dim openFileDialog1 As New OpenFileDialog()
Try
openFileDialog1.InitialDirectory = "j:\"
Catch
openFileDialog1.InitialDirectory = "c:\"
End Try
openFileDialog1.Filter = "dwg files (*.dwg)|*.dwg"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
openFileDialog1.Multiselect = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim count As Integer = openFileDialog1.FileNames.Length.ToString
Dim I
For I = 0 To count - 1
ListBox1.Items.Add(openFileDialog1.FileNames.GetValue(I).ToString)
Next
End If
End Sub

Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub

Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = System.IO.Directory.GetFiles(targetDirectory)
Dim fileName As String
For Each fileName In fileEntries
If LCase(System.IO.Path.GetExtension(fileName)) = ".dwg" Then
ListBox1.Items.Add(fileName)
End If
Next fileName
Dim subdirectoryEntries As String() = System.IO.Directory.GetDirectories(targetDirectory)
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub

Private Sub WholeDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WholeDirectory.Click
If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
ProcessDirectory(FolderBrowserDialog1.SelectedPath)
End If
End Sub

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
If ListBox1.Items.Count < 1 Then
MsgBox("You haven't selected any drawings to process.", MsgBoxStyle.Information, "Totals")
Exit Sub
End If
Call GetAcadApplication()
Dim I
Dim R As Integer = 0
Dim DwgName As String = ""
For I = 0 To ListBox1.Items.Count - 1
Try
acadDoc = AcadApp.ActiveDocument
Catch
acadDoc = AcadApp.Documents.Add
End Try
DwgName = ListBox1.Items.Item(0)
ProcessDwg(DwgName)
Try
acadDoc.Close(False, DwgName)
Catch ex As Exception
'MsgBox("Some Error closing the drawing." & vbCrLf & ex.ToString)
End Try
ListBox1.Items.RemoveAt(0)
R = R + 1
Next
MsgBox("Processed " & R & " Drawings")
End Sub

Sub ProcessDwg(ByVal ThisDwg As String)
acadDoc.Application.Documents.Open(ThisDwg)
acadDoc = AcadApp.ActiveDocument

acadDoc = AcadApp.ActiveDocument

'Add code to do something with the drawing here.
'code....
'code....
'code....

Call TEST()

End Sub

Sub TEST()

Dim ACADPref As AcadPreferencesOpenSave
Dim originalValue As Object
ACADPref = acadDoc.Application.Preferences.OpenSave

originalValue = ACADPref.SaveAsType

MsgBox(originalValue.ToString)

ACADPref.SaveAsType = AcSaveAsType.ac2000_dwg

Exit Sub
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call GetAcadApplication()
Call Ch9_PrintModelSpace()
acadDoc = AcadApp.ActiveDocument
Dim ACADPref As AcadPreferencesOpenSave
ACADPref = acadDoc.Application.Preferences.OpenSave
ACADPref.SaveAsType = AcSaveAsType.ac2000_dwg
acadDoc.Save()

End Sub

Sub Ch9_PrintModelSpace()
' Verify that the active space is model space
If acadDoc.ActiveSpace = AcActiveSpace.acModelSpace Then
acadDoc.MSpace = True
acadDoc.ActiveSpace = AcActiveSpace.acModelSpace
End If

' Set the extents and scale of the plot area
acadDoc.ModelSpace.Layout.PlotType = AcPlotType.acExtents
acadDoc.ModelSpace.Layout.StandardScale = AcPlotScale.acScaleToFit

' Set the number of copies to one
acadDoc.Plot.NumberOfCopies = 1

' Initiate the plot
acadDoc.Plot.PlotToDevice()

End Sub

End Class
0 Likes
Message 7 of 8

Anonymous
Not applicable
What line of code causes the error?

Joe ...


wrote in message news:6024032@discussion.autodesk.com...
Hi Joe, Thanks for looking at my issue!
Im all tangled up and not sure were to add your example in my code? I'm
still gettin error saying the "new" keyword.

Here is the rest of my code if that helps...

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class Form1
Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument

Public Sub GetAcadApplication()
Try
AcadApp = GetObject(, "AutoCAD.Application.17.1")
AcadApp.Visible = True
Catch
Try
AcadApp = CreateObject("AutoCAD.Application.17.1")
AcadApp.Visible = True
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
Exit Sub
End Try
End Try
End Sub

Private Sub RetrieveDwgs_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RetrieveDwgs.Click
Dim openFileDialog1 As New OpenFileDialog()
Try
openFileDialog1.InitialDirectory = "j:\"
Catch
openFileDialog1.InitialDirectory = "c:\"
End Try
openFileDialog1.Filter = "dwg files (*.dwg)|*.dwg"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
openFileDialog1.Multiselect = True
If openFileDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK Then
Dim count As Integer = openFileDialog1.FileNames.Length.ToString
Dim I
For I = 0 To count - 1
ListBox1.Items.Add(openFileDialog1.FileNames.GetValue(I).ToString)
Next
End If
End Sub

Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub

Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() =
System.IO.Directory.GetFiles(targetDirectory)
Dim fileName As String
For Each fileName In fileEntries
If LCase(System.IO.Path.GetExtension(fileName)) = ".dwg" Then
ListBox1.Items.Add(fileName)
End If
Next fileName
Dim subdirectoryEntries As String() =
System.IO.Directory.GetDirectories(targetDirectory)
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub

Private Sub WholeDirectory_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles WholeDirectory.Click
If FolderBrowserDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK Then
ProcessDirectory(FolderBrowserDialog1.SelectedPath)
End If
End Sub

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Go.Click
If ListBox1.Items.Count < 1 Then
MsgBox("You haven't selected any drawings to process.",
MsgBoxStyle.Information, "Totals")
Exit Sub
End If
Call GetAcadApplication()
Dim I
Dim R As Integer = 0
Dim DwgName As String = ""
For I = 0 To ListBox1.Items.Count - 1
Try
acadDoc = AcadApp.ActiveDocument
Catch
acadDoc = AcadApp.Documents.Add
End Try
DwgName = ListBox1.Items.Item(0)
ProcessDwg(DwgName)
Try
acadDoc.Close(False, DwgName)
Catch ex As Exception
'MsgBox("Some Error closing the drawing." & vbCrLf &
ex.ToString)
End Try
ListBox1.Items.RemoveAt(0)
R = R + 1
Next
MsgBox("Processed " & R & " Drawings")
End Sub

Sub ProcessDwg(ByVal ThisDwg As String)
acadDoc.Application.Documents.Open(ThisDwg)
acadDoc = AcadApp.ActiveDocument

acadDoc = AcadApp.ActiveDocument

'Add code to do something with the drawing here.
'code....
'code....
'code....

Call TEST()

End Sub

Sub TEST()

Dim ACADPref As AcadPreferencesOpenSave
Dim originalValue As Object
ACADPref = acadDoc.Application.Preferences.OpenSave

originalValue = ACADPref.SaveAsType

MsgBox(originalValue.ToString)

ACADPref.SaveAsType = AcSaveAsType.ac2000_dwg

Exit Sub
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call GetAcadApplication()
Call Ch9_PrintModelSpace()
acadDoc = AcadApp.ActiveDocument
Dim ACADPref As AcadPreferencesOpenSave
ACADPref = acadDoc.Application.Preferences.OpenSave
ACADPref.SaveAsType = AcSaveAsType.ac2000_dwg
acadDoc.Save()

End Sub

Sub Ch9_PrintModelSpace()
' Verify that the active space is model space
If acadDoc.ActiveSpace = AcActiveSpace.acModelSpace Then
acadDoc.MSpace = True
acadDoc.ActiveSpace = AcActiveSpace.acModelSpace
End If

' Set the extents and scale of the plot area
acadDoc.ModelSpace.Layout.PlotType = AcPlotType.acExtents
acadDoc.ModelSpace.Layout.StandardScale = AcPlotScale.acScaleToFit

' Set the number of copies to one
acadDoc.Plot.NumberOfCopies = 1

' Initiate the plot
acadDoc.Plot.PlotToDevice()

End Sub

End Class
0 Likes
Message 8 of 8

Anonymous
Not applicable
Joe,

I ended up using a script file to batch plot multiple dwgs!

Thanks
0 Likes