Document.Close doesn't work.

Document.Close doesn't work.

aries.1482
Enthusiast Enthusiast
675 Views
2 Replies
Message 1 of 3

Document.Close doesn't work.

aries.1482
Enthusiast
Enthusiast

Hi, I have an ilogic start from active document (source document) to do:

- Load idw file from a list (csv or txt file) (new document)

- Check if there a titleblock "VMC-TP" in new document, delete both reference and definition

- Copy titleblock with same name "VMC-TP" from source document and add to new document

- Save and close new document

- If there is'nt titleblock "VMC-TP" in new document, just close it.

Everything work fine if "VMC-TP" exist, but if not the document still active and doesn't close

Plz help me, thanks!

(See the attach file)

0 Likes
Accepted solutions (1)
676 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @aries.1482.  There are multiple possibilities for failure there, but the main thing that I suggest is narrowing the amount of code you put into your Try block of code to the minimum needed for each potentially error prone line of code.  The main two places that may need this are where you are attempting to retrieve the title block definition from the source, and from the destination.  That leaves your Save & Close lines outside, where they will be obeyed either way.  I also moved the block of code for getting the source title block definition up above the loop, because you only need to retrieve it once.

Try this edited version of your code:

Imports System.Windows.Forms
Sub Main
	'Set active drawing as source drawing
	Dim oSourceDocument As DrawingDocument
	oSourceDocument = ThisApplication.ActiveDocument

	'Get the source title block definition.
	Dim oSourceTitleBlockDef As TitleBlockDefinition
	Try
		oSourceTitleBlockDef = oSourceDocument.TitleBlockDefinitions.Item("VMC-TP")
	Catch oEx As Exception
		Logger.Error("The specified TitleBlockDefinition was not found in the 'Source'.  Exiting rule.  {0}", oEx.Message)
		'MsgBox("The specified TitleBlockDefinition was not found in the 'Source'.  Exiting rule.", vbExclamation, "iLogic")
		Exit Sub
	End Try

	'Open the destination drawing to copy the title block into.
	Dim idwPaths = LoadIdwPaths()
	Logger.Debug("Files to replace titleblock:" & vbCrLf & String.Join(vbCrLf, idwPaths))

	For Each idwPath As String In idwPaths
		If Not System.IO.File.Exists(idwPath) Then 
			Logger.Warn("File not found{0}{1}", vbCrLf, idwPath)
			Continue For
		End If
		Dim oNewDocument As DrawingDocument = ThisApplication.Documents.Open(idwPath)

		'Check if exist titleblock named VMC-TP
		Dim ExistTitleBlock As TitleBlockDefinition
		Try
			ExistTitleBlock = oNewDocument.TitleBlockDefinitions("VMC-TP")
		Catch oEx As Exception
			Logger.Error("Specified TitleBlockDefinition not found in 'Destination'.  {0}", oEx.Message)
			'MsgBox("Specified TitleBlockDefinition not found in 'Destination'. Skipping to next.", vbInformation, "iLogic")
			Continue For
		End Try
		
		If ExistTitleBlock.IsReferenced = True Then
			Dim oSheet As Sheet
			'Delete if exist titleblock named VMC-TP
			For Each oSheet In oNewDocument.Sheets
				If oSheet.TitleBlock.Definition Is ExistTitleBlock Then
					oSheet.TitleBlock.Delete
				End If
			Next
			ExistTitleBlock.Delete
			
			'Get the new title block definition.
			Dim oNewTitleBlockDef As TitleBlockDefinition
			oNewTitleBlockDef = oSourceTitleBlockDef.CopyTo(oNewDocument)
		
			'Add new title block reference
			For Each oSheet In oNewDocument.Sheets
				oSheet.Activate
				Call oSheet.AddTitleBlock(oNewTitleBlockDef)
			Next
			oNewDocument.Save
			oNewDocument.Close
		Else
			oNewDocument.Close
		End If
	Next
End Sub

Function LoadIdwPaths() As String()
    Dim openFileDialog As New OpenFileDialog()
    openFileDialog.Filter = "All files|*.*|Csv files|*.csv|Text files|*.txt"
    openFileDialog.FilterIndex = 2
    If openFileDialog.ShowDialog <> DialogResult.OK Then Return New String() {}
    Dim fileName = openFileDialog.FileName
    'Select appropriate encoding, 
    'especially when you use some non-ASCII chars in path and file names
    Dim encoding = System.Text.Encoding.UTF8
    Dim lines = System.IO.File.ReadAllLines(fileName, encoding)
    Return lines
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

aries.1482
Enthusiast
Enthusiast

Thank you, it works ok. I will try with more files to make sure there is no issue.

0 Likes