iLogic Code to export Sheets to IDWS

iLogic Code to export Sheets to IDWS

rossPJ9W7
Participant Participant
239 Views
12 Replies
Message 1 of 13

iLogic Code to export Sheets to IDWS

rossPJ9W7
Participant
Participant

I need help with this code. The goal is to export all the sheets in a idw file to seperate idws with the sheet name as the file name. The code works. All the sheets are saved in a new folder created called " Sheets". All new idw files are named with the sheet name. 

Issue: The new idws dont open. 

From my understanding the way the code needs to work is, add a number on the sheet names to be a counter. Then deleted all sheets except one and save as new idw file. Then go to the next counter number and repeat.

 

 

 

'------start of iLogic-------

oPath = ThisDoc.Path

oFileName = ThisDoc.FileName(False) 'without extension

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _

("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

oDocument = ThisApplication.ActiveDocument

oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

 

'Define the drawing

Dim oDrawing As DrawingDocument

oDrawing = ThisDoc.Document

 

Dim oSheet As Sheet

Dim lPos As Long

Dim rPos As Long

Dim sLen As Long

Dim sSheetName As String

Dim iSheetNumber As Integer

 

'step through each drawing sheet

For Each oSheet In oDrawing.Sheets

 

'find the seperator in the sheet name:number

lPos = InStr(oSheet.Name, ":")

'find the number of characters in the sheet name

sLen = Len(oSheet.Name)

'find the sheet name

sSheetName = Left(oSheet.Name, lPos -1)

'find the sheet number

iSheetNumber = Right(oSheet.Name, sLen -lPos)

 

 

'get SHEET target folder path

oFolder = Left(oPath, InStrRev(oPath, "\")) & "Sheets"

 

'Check for the PDF folder and create it if it does not exist

If Not System.IO.Directory.Exists(oFolder) Then

    System.IO.Directory.CreateDirectory(oFolder)

End If

 

 

'Set the SHEET target file name

oDataMedium.FileName = oFolder & "\" & oFileName & " " & sSheetName &  ".idw"

 

'Publish document

oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

Next

'------end of iLogic-------

0 Likes
Accepted solutions (1)
240 Views
12 Replies
Replies (12)
Message 2 of 13

WCrihfield
Mentor
Mentor
Accepted solution

Hi @rossPJ9W7.  That code example seems to be trying to export the drawing as a PDF file, not as an IDW file.  No translator add-in is required to create copies of a regular Inventor drawing.  For that you can just use the regular SaveAs method.  There are two ways to use SaveAs method, depending on what you specify for the optional, second input parameter it is asking for named 'SaveCopyAs'.  If you specify False, then it will create the new file, and will also create a 'new' Document associated with that new file, that will remain open.  If you specify True, then it will just create the new file in the background, without opening that new copy, which leaves the original document open/active.

Below is an alternate example iLogic rule that you can play around with.  I tried to include a lot of comments, so that you could follow everything that it is doing along the way.

Sub Main
	'get the Inventor Application to a variable
	Dim oInvApp As Inventor.Application = ThisApplication
	'get the main Documents object of the application, for use later
	Dim oDocs As Inventor.Documents = oInvApp.Documents
	'get the 'active' Document, and try to 'Cast' it to the DrawingDocument Type
	'if this fails, then no value will be assigned to the variable
	'but no error will happen either
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	'check if the variable got assigned a value...if not, then exit rule
	If oDDoc Is Nothing Then Return
	'get the main Sheets collection of the 'active' drawing
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	'if only 1 sheet, then exit rule, because noting to do
	If oSheets.Count = 1 Then Return
	'record currently 'active' Sheet, to reset to active at the end
	Dim oASheet As Inventor.Sheet = oDDoc.ActiveSheet
	'get the full path of the active drawing, without final directory separator character
	Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
	'specify name of sub folder to put new drawing documents into for each sheet
	Dim sSubFolder As String = "Sheets"
	'combine main Path with sub folder name, to make new path
	'adds the directory separator character in for us, automatically
	Dim sNewPath As String = System.IO.Path.Combine(sPath, sSubFolder)
	'create the sub directory, if it does not already exist
	If Not System.IO.Directory.Exists(sNewPath) Then
		System.IO.Directory.CreateDirectory(sNewPath)
	End If
	'not using 'For Each' on purpose, to maintain proper order 
	For iSheetIndex As Integer = 1 To oSheets.Count
		'get the Sheet at this index
		Dim oSheet As Inventor.Sheet = oSheets.Item(iSheetIndex)
		'make this sheet the active sheet
		oSheet.Activate()
		Dim sSheetName As String = oSheet.Name
		If sSheetName.Contains(":") Then
			'Split Sheet name at colon character to create Array of Strings
			'then just get the first String in that Array, as the sheet's name
			'expecting only one colon character in the name, just before sheet number
			sSheetName = sSheetName.Split(":").First()
		End If
		'inserting a single space between sheet name and its Index number, for clarity
		sSheetName = sSheetName & " " & iSheetIndex.ToString()
		'combine full path with new file name and file extension, as value of new variable
		Dim sNewDrawingFFN As String = System.IO.Path.Combine(sNewPath, sSheetName & ".idw")
		'save copy of this drawing to new file, in background (new copy not open yet)
		oDDoc.SaveAs(sNewDrawingFFN, True)
		'open that new drawing file (invisibly), so we can modify it
		Dim oNewDDoc As DrawingDocument = oDocs.Open(sNewDrawingFFN, False)
		'get Sheets collection of new drawing to a variable
		Dim oNewDocSheets As Inventor.Sheets = oNewDDoc.Sheets
		'get the Sheet at same Index in new drawing (the one to keep)
		Dim oThisNewDocSheet As Inventor.Sheet = oNewDocSheets.Item(iSheetIndex)
		'delete all other sheets in new drawing besides this sheet
		For Each oNewDocSheet As Inventor.Sheet In oNewDocSheets
			If Not oNewDocSheet Is oThisNewDocSheet Then
				Try
					'Optional 'RetainDependentViews' is False by default
					oNewDocSheet.Delete() 
				Catch
					Logger.Error("Error deleting Sheet named:  " & oNewDocSheet.Name)
				End Try
			End If
		Next
		'save new drawing document again, after changes
		oNewDDoc.Save()
		'next line only valid when not visibly opened, and not being referenced
		oNewDDoc.ReleaseReference()
		'if visibly opened, then use oNewDDoc.Close() method instead
	Next 'oSheet
	'activate originally active sheet again
	oASheet.Activate()
	'only clear out all Documents that are 'unreferenced' in Inventor's memory
	'used after ReleaseReference, to clean-up
	oDocs.CloseAll(True)
End Sub

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 13

hollypapp65
Advocate
Advocate

Your code is saving PDF file with .idw extension.

Do you want idw or pdf?

0 Likes
Message 4 of 13

rossPJ9W7
Participant
Participant

.idw

0 Likes
Message 5 of 13

rossPJ9W7
Participant
Participant

How would i removed the counting integer at the end of the file name? 

iSheetIndex.ToString()
0 Likes
Message 6 of 13

WCrihfield
Mentor
Mentor

In this block of code:

		Dim sSheetName As String = oSheet.Name
		If sSheetName.Contains(":") Then
			'Split Sheet name at colon character to create Array of Strings
			'then just get the first String in that Array, as the sheet's name
			'expecting only one colon character in the name, just before sheet number
			sSheetName = sSheetName.Split(":").First()
		End If
		'inserting a single space between sheet name and its Index number, for clarity
		sSheetName = sSheetName & " " & iSheetIndex.ToString()
		'combine full path with new file name and file extension, as value of new variable
		Dim sNewDrawingFFN As String = System.IO.Path.Combine(sNewPath, sSheetName & ".idw")

...you could just delete the following lines of code:

		'inserting a single space between sheet name and its Index number, for clarity
		sSheetName = sSheetName & " " & iSheetIndex.ToString()

...or replace the [& " " & iSheetIndex.ToString()] portion of that line of code of it with something else.

Just getting rid of that sheet index number would only work if each sheet's name, before that index number, was unique though.  If the sheet names are not unique, then you would have to do something else to make sure the names of each of the new drawing files will be unique, otherwise it will likely throw errors, or just overwrite existing drawing files.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 13

rossPJ9W7
Participant
Participant

Thank you

0 Likes
Message 8 of 13

rossPJ9W7
Participant
Participant

Im getting this error promt running this code. Can anyone help?

 

 

Sub Main
	'get the Inventor Application to a variable
	Dim oInvApp As Inventor.Application = ThisApplication
	'get the main Documents object of the application, for use later
	Dim oDocs As Inventor.Documents = oInvApp.Documents
	'get the 'active' Document, and try to 'Cast' it to the DrawingDocument Type
	'if this fails, then no value will be assigned to the variable
	'but no error will happen either
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	'check if the variable got assigned a value...if not, then exit rule
	If oDDoc Is Nothing Then Return
	'get the main Sheets collection of the 'active' drawing
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	'if only 1 sheet, then exit rule, because noting to do
	If oSheets.Count = 1 Then Return
	'record currently 'active' Sheet, to reset to active at the end
	Dim oASheet As Inventor.Sheet = oDDoc.ActiveSheet
	'get the full path of the active drawing, without final directory separator character
	Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
	'specify name of sub folder to put new drawing documents into for each sheet
	Dim sSubFolder As String = "Sheets"
	'combine main Path with sub folder name, to make new path
	'adds the directory separator character in for us, automatically
	Dim sNewPath As String = System.IO.Path.Combine(sPath, sSubFolder)
	'create the sub directory, if it does not already exist
	If Not System.IO.Directory.Exists(sNewPath) Then
		System.IO.Directory.CreateDirectory(sNewPath)
	End If
	'not using 'For Each' on purpose, to maintain proper order 
	For iSheetIndex As Integer = 1 To oSheets.Count
		'get the Sheet at this index
		Dim oSheet As Inventor.Sheet = oSheets.Item(iSheetIndex)
		'make this sheet the active sheet
		oSheet.Activate()
		Dim sSheetName As String = oSheet.Name
		If sSheetName.Contains(":") Then
			'Split Sheet name at colon character to create Array of Strings
			'then just get the first String in that Array, as the sheet's name
			'expecting only one colon character in the name, just before sheet number
			sSheetName = sSheetName.Split(":").First()
		End If
		'inserting a single space between sheet name and its Index number, for clarity
		'sSheetName = sSheetName & " " & iSheetIndex.ToString()
		'combine full path with new file name and file extension, as value of new variable
		Dim sNewDrawingFFN As String = System.IO.Path.Combine(sNewPath, sSheetName & ".idw")
		'save copy of this drawing to new file, in background (new copy not open yet)
		oDDoc.SaveAs(sNewDrawingFFN , True)
		'open that new drawing file (invisibly), so we can modify it
		Dim oNewDDoc As DrawingDocument = oDocs.Open(sNewDrawingFFN, False)
		'get Sheets collection of new drawing to a variable
		Dim oNewDocSheets As Inventor.Sheets = oNewDDoc.Sheets
		'get the Sheet at same Index in new drawing (the one to keep)
		Dim oThisNewDocSheet As Inventor.Sheet = oNewDocSheets.Item(iSheetIndex)
		'delete all other sheets in new drawing besides this sheet
		For Each oNewDocSheet As Inventor.Sheet In oNewDocSheets
			If Not oNewDocSheet Is oThisNewDocSheet Then
				Try
					'Optional 'RetainDependentViews' is False by default
					oNewDocSheet.Delete() 
				Catch
					Logger.Error("Error deleting Sheet named:  " & oNewDocSheet.Name)
				End Try
			End If
		Next
		'save new drawing document again, after changes
		oNewDDoc.Save()
		'next line only valid when not visibly opened, and not being referenced
		oNewDDoc.ReleaseReference()
		'if visibly opened, then use oNewDDoc.Close() method instead
	Next 'oSheet
	'activate originally active sheet again
	oASheet.Activate()
	'only clear out all Documents that are 'unreferenced' in Inventor's memory
	'used after ReleaseReference, to clean-up
	oDocs.CloseAll(True)
End Sub

Screenshot 2025-08-01 072613.png
0 Likes
Message 9 of 13

WCrihfield
Mentor
Mentor

It looks like Line 47 in that code example is where it is using the 'SaveAs' method.  The error means that one of the two 'arguments' (inputs) supplied is invalid, but does not say why it is invalid.  It is not possible for the second input to be invalid, because it is just a Boolean, so it must be the first input...the full file name of the new drawing file it is supposed to create.  My guess is that either a file with the same name already exists in the destination folder, or there may be one or more invalid characters in the file path or file name somewhere.  Since each new file is being named after each sheet, then the names of the sheets become critical.  No two sheets can have the same name, and none of the names can contain any characters that would be invalid to use in an Inventor file name.  There is a built-in utility that you could try out for that type of situation, but I do not recall using it myself yet.  It is the FileManager.IsFileNameValid method.  To use it, it seems like the first input would be the String variable containing the full path, file name, and file extension of the file that you are planning on creating.  Then you would have declared another String type variable ahead of calling this method, but does not have a value yet, and supply that as the second input.  Then, if anything was wrong with the first input String value, it would set the value of the second String variable to an 'appropriate or fixed' value.  But the method itself is a Function with a Boolean value that we can check, which would be the answer to the 'is this file name valid' question.  If it returns False, then check the value of the second variable, otherwise there was nothing wrong with the first input.  You can reach that FileManager object through the Application.FileManager property (where 'Application' is represented by the 'ThisApplication' rule variable).

By the way, when posting an error message, it is usually best to post either both tabs of that error dialog, or just the 'More Info' tab of it, because that side contains...more information, and often includes the 'method' or 'property' that was being accessed when the exception occurred, helping to more quickly find where in the code the error happened.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 13

rossPJ9W7
Participant
Participant

It works for the first 4 sheets then crashes

0 Likes
Message 11 of 13

WCrihfield
Mentor
Mentor

Since this rule is potentially creating, then using a sub directory that is simply named "Sheets", for saving all the new drawing files into, there is likely a high probability that there will be multiple 'attempts' to put drawings into it that have the same file name.  For example if there are multiple drawings in the same directory that the 'active' drawing is in, and a rule like this was used on all of the drawings in that same directory, they would all be trying to put their 'sheets' versions of their drawing files into that same sub folder.  It may be a better idea to make that sub folder a more specific name, such as including a portion of the drawing's file name or part number as the first part of that sub directory's name, instead of just 'Sheets'.  That way each drawing would have its own custom sub directory for just the new files made from its own sheets.  Helps eliminate the possibility of files with duplicate file names.

Originally, our 'Project' file settings were set to not allow any files within our project workspace to have the same file name as another file in that space.  But now that we got Vault, we found out that we really had tons of 'duplicates' (files with same file name in the project space), which is a real pain to deal with.

Can you please confirm that none of the new files it that the code is trying to create are named the same as any files that already exist in that folder, or in that same project space, or even files that were created earlier in the same code process?  Just trying to establish a 'process of elimination', and check specific possibilities off the list, to narrow it down.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 13

rossPJ9W7
Participant
Participant

All the sheets have different names. I tried running again. The first sheet gets exported the the newly created folder " Sheets ", then inventor crashes.

0 Likes
Message 13 of 13

WCrihfield
Mentor
Mentor

OK, thanks for clarifying.  If Inventor truly crashed, not just another error, then there may be something else going on that I would not be able to diagnose over the forum.

So, that last time you ran it, did you clear out all of the pre-existing files in that sub folder, before running it?  If not, then the code may have been trying to create a new file in that sub folder when there was already a file in that folder with the same name, from the last time you ran the code, and if so, that may have been why it encountered an error (but certainly should not have caused Inventor to crash).  Was there any error shown before the crash?  When Inventor crashes start to happen, it may be time to ask the folks at Autodesk Tech Support about it.  They would likely want to get a copy of the files involved, and the exact actions and/or processes being used at the time, so that they can attempt to reproduce the error or crash, and if they can, they may be able to fix, and/or give some advice about how to avoid it until they can further investigate it.  Maybe others here on this forum will chime in also, or someone from Autodesk who frequent these interactions.  I'm running out of ideas, and relatively busy with a separate set of issues where I work.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes