Object reference error when activating a drawing

Object reference error when activating a drawing

catherine28122001
Observer Observer
514 Views
7 Replies
Message 1 of 8

Object reference error when activating a drawing

catherine28122001
Observer
Observer

I wrote some code to make drawings of a part file using a template but I am running into this error: "Object reference not set to an instance of an object." when I try to activate a sheet of the DrawingDocument. Below is my code: 

 

' Get the active part document
Dim partDoc As PartDocument
partDoc = ThisApplication.ActiveDocument

' Get the part file name without extension
Dim partFileName As String
partFileName = System.IO.Path.GetFileNameWithoutExtension(partDoc.FullFileName)

' Get the folder path of the part
Dim partFolderPath As String
partFolderPath = System.IO.Path.GetDirectoryName(partDoc.FullFileName)

' Specify the path to your drawing template file
Dim sortTemplatePath As String
sortTemplatePath = "C:\Users\Public\Documents\Autodesk\Inventor 2023\Templates\en-US\Standard.dwg"

' Create the drawing file name by appending ".idw"
Dim sortFileName As String
sortFileName = System.IO.Path.Combine(partFolderPath, partFileName & "-DRAWING.dwg")

' Get the active drawing document
Dim sortDwg As DrawingDocument

' Check if the drawing already exists
If System.IO.File.Exists(sortFileName) Then
    ' If the drawing exists, open it
	ThisDoc.Launch(sortFileName & DWGType)
	
Else
    ' If the drawing doesn't exist, create a new drawing from the template
    sortDwg = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,sortTemplatePath,True)
	'sortDwg = ThisApplication.Documents.Open(sortTemplatePath, False)

    ' Save the drawing with a new name
    sortDwg.SaveAs(sortFileName, False)
	ThisDoc.Launch(sortFileName & DWGType)
	
End If

sortDwg.Activate() 'This is the line that gives me the error
Dim oSheet As Sheet = sortDwg.Sheets.Item(1)

 Any help would be appreciated! Thank you!

0 Likes
515 Views
7 Replies
Replies (7)
Message 2 of 8

A.Acheson
Mentor
Mentor

Hi @catherine28122001 

 

I am not seeing that specific error. Do you know what line it occurs on? Post the image of the more info tab. I do see a missing string variable. If you use Option Explicit On in the header it will flag any variables that have been incorrectly referenced. 

AAcheson_0-1718211666310.png

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor

Hi @catherine28122001.  I see the following line of code in 2 places:

ThisDoc.Launch(sortFileName & DWGType)

...but I do not see where that 'DWGType' variable was created, or where a value was set to it.

Also, maybe try using ThisApplication.Documents.Open() method to open those drawings, instead of ThisDoc.Launch.  Just a suggestion.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 8

Frederick_Law
Mentor
Mentor
Dim sortDwg As DrawingDocument

' Check if the drawing already exists
If System.IO.File.Exists(sortFileName) Then
    ' If the drawing exists, open it
	ThisDoc.Launch(sortFileName & DWGType)

'sortDwg not assigned any value
0 Likes
Message 5 of 8

catherine28122001
Observer
Observer

I removed it but am still getting the same error on the same line: line 40

 

' Get the active part document
Dim partDoc As PartDocument
partDoc = ThisApplication.ActiveDocument

' Get the part file name without extension
Dim partFileName As String
partFileName = System.IO.Path.GetFileNameWithoutExtension(partDoc.FullFileName)

' Get the folder path of the part
Dim partFolderPath As String
partFolderPath = System.IO.Path.GetDirectoryName(partDoc.FullFileName)

' Specify the path to your drawing template file
Dim sortTemplatePath As String
sortTemplatePath = "C:\Users\Public\Documents\Autodesk\Inventor 2023\Templates\en-US\Standard.dwg"

' Create the drawing file name by appending ".idw"
Dim sortFileName As String
sortFileName = System.IO.Path.Combine(partFolderPath, partFileName & "-DRAWING.dwg")

' Get the active drawing document
Dim sortDwg As DrawingDocument

' Check if the drawing already exists
If System.IO.File.Exists(sortFileName) Then
    ' If the drawing exists, open it
	ThisDoc.Launch(sortFileName & DWGType)
	
Else
    ' If the drawing doesn't exist, create a new drawing from the template
    sortDwg = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,sortTemplatePath,True)
	'sortDwg = ThisApplication.Documents.Open(sortTemplatePath, False)

    ' Save the drawing with a new name
    sortDwg.SaveAs(sortFileName, False)
	ThisDoc.Launch(sortFileName)
	
End If

sortDwg.Activate() 'This is the line that gives me the error
Dim oSheet As Sheet = sortDwg.Sheets.Item(1)
0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

Line 40 is to activate the drawing. This isn't always needed if you have the correct object open previously. 

 

I would use this line

"ThisApplication.Documents.Open()" as Wesley has already suggested and just supply the filepath. If the file is already open you can use the active document to get the drawing object and do the next operation with it.

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

catherine28122001
Observer
Observer

How do I get the drawing object? I found the segment of code below online and they activate, call out the sheets, and place the drawing. This code worked perfectly to put in the part but it doesn't load the template for some reason. The part just loads onto a blank sheet. 

 

Dim oDoc As Document = ThisDoc.Document  ' Part Document or Assembly Document

Dim oSep As Double = 2

'Create a drawing from the drawing template
Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter =  "Inventor Files (*.idw)|*.idw"
oFileDlg.DialogTitle = "Select Drawing Template"
oFileDlg.InitialDirectory = ThisApplication.FileOptions.TemplatesPath
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
	Exit Sub
ElseIf oFileDlg.FileName <> "" Then
MasterFile = oFileDlg.FileName
End If

Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,templateFile,True)

oDrawingDoc.Activate()
Dim oSheet As Sheet = oDrawingDoc.Sheets.Item(1)

Dim oCentralPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width/2, oSheet.Height/2)

Dim oView As DrawingView
oView = oSheet.DrawingViews.AddBaseView(oDoc,oCentralPoint, 1, ViewOrientationTypeEnum.kBottomViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)

Dim V1Point2D As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X + 10, oView.Center.Y)
Dim V2Point2D As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Center.Y - 10)
Dim V3Point2D As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X + 10, oView.Center.Y - 10)

Dim oView1 As DrawingView = oSheet.DrawingViews.AddProjectedView(oView, V1Point2D, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
Dim oView2 As DrawingView = oSheet.DrawingViews.AddProjectedView(oView, V2Point2D, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
Dim oView3 As DrawingView = oSheet.DrawingViews.AddProjectedView(oView, V3Point2D, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)

oView1.Position= ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X + oView.Width / 2 + oSep + oView1.Width / 2, oView1.Center.Y)
oView2.Position = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Center.Y - oView.Height / 2 - oSep - oView2.Height / 2)

oView3.Position = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X + oView.Width / 2 + oSep + oView3.Width / 2, _
oView.Center.Y - oView.Height / 2 - oSep - oView3.Height / 2)
oView3.ViewStyle = oView.ViewStyle 

 

0 Likes
Message 8 of 8

A.Acheson
Mentor
Mentor

The drawing object you can get using the add or open from the document collection see help page here.

I would suggest to review the help page as it gives additional information  on what arguments are optional and what each one does.

Syntax

Documents.AddDocumentType As DocumentTypeEnum, [TemplateFileName] As String, [CreateVisible] As Boolean ) As Document

 

 

Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,templateFile,True)

 

 

Or

Syntax

Documents.OpenFullDocumentName As String, [OpenVisible] As Boolean ) As Document

 

 

Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Open("fullfilname",True)

 

 

 

 

In your new sample case I don't see you declaring and setting the filepath for the template file so this will be a null value. If you look at the add method the path is optional and if left empty what happens is an internal template is chosen and your customized one. This error can  either spotted  doing a code review and checking the path of each variable or  y using option Explicit On which h does a pre-screening for undeclared variables which can catch these errors before the code runs. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes