Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: Michiel.Valcke

There is actually another way to do this which will preserve all your dimensions and such.  I'm not sure why I did not mention this in any of my earlier posts here.  It seems like I have shown this method in a few other places in the forum before.  Anyways, it uses the basic Copy/Paste system, combined with simulating some user inter face actions, such as selecting the active sheet, executing a command (copy), unselecting that sheet, then executing another command (paste).  It includes some Inventor API code, but is a mix of using API and simulating user interface actions, so it will only work when that drawing is currently the 'active' document (currently fully visible on your screen at that moment).  I even took it another step further and turned it into a separate Function routine, which will then retrieve and return the newly created copy of the active sheet for you.  It is still not 'perfect', but pretty close.  Of course it could still be developed further, such as adding the option to rename the new sheet, and/or repositioning the new sheet to a specific location, instead of just at the end of all existing sheets.  As it is right now, the name of the new sheet will always start with "Copy of ", then the name of the original sheet, but with the new index number at the very end, because we can not control that index number.  Also, if the active sheet (when you run the rule) contains views which have 'parent' views on another sheet, then it will show a small dialog telling you that the parent view must be copied along with the child view, due to the child view being dependent on the parent view, with only an OK button, so it will not copy that sheet.

 

Below is that code example:

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	Dim oNewCopySheet As Inventor.Sheet = CopySheet(oSheet)
	Logger.Info("New Sheet's Name = " & oNewCopySheet.Name)
End Sub

Function CopySheet(oSheet As Inventor.Sheet) As Inventor.Sheet
	If oSheet Is Nothing Then Return Nothing
	Dim oDDoc As DrawingDocument = oSheet.Parent
	Dim oInvApp As Inventor.Application = oSheet.Application
	If oDDoc.Views.Count = 0 OrElse oDDoc IsNot oInvApp.ActiveDocument Then
		Try 'it must be the 'active' document (visibly showing on your screen)
			oDDoc = oInvApp.Documents.Open(oDDoc.FullDocumentName, True)
			oDDoc.Activate()
		Catch : End Try
	End If
	Dim oCmdMgr As CommandManager = oInvApp.CommandManager
	Dim oCDs As ControlDefinitions = oCmdMgr.ControlDefinitions
	oCmdMgr.DoSelect(oSheet)
	oCDs.Item("AppCopyCmd").Execute()
	oCmdMgr.DoUnSelect(oSheet)
	oCDs.Item("AppPasteCmd").Execute()
	Dim oNewSheet As Inventor.Sheet = oDDoc.Sheets.Item(oDDoc.Sheets.Count)
	Return oNewSheet
End Function

One possible further developed version of that custom Function might look something like this:

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	
	'<<< if just copying one sheet, expect one Sheet to get returned >>>
	'Dim oNewSheet As Inventor.Sheet = CopySheet(oSheet, 1)
	'Logger.Info("New Sheet's Name = " & oNewSheet.Name)
	
	'OR
	'<<< if creating multiple copies, expect an array of Sheet to be returned >>>
	Dim oNewCopies() As Inventor.Sheet = CopySheet(oSheet, 2, "Sheet 3", "Sheet 4")
	
	Logger.Info("New Sheet Names:")
	For Each oNewCopy In oNewCopies
		Logger.Info(oNewCopy.Name)
	Next
End Sub

Function CopySheet(ByVal oSheet As Inventor.Sheet, _
	iNumberOfCopies As Integer, _
	ByVal ParamArray NewSheetNames() As String) As Object
	If oSheet Is Nothing Then Return Nothing
	If iNumberOfCopies < 1 Then Return Nothing
	Dim bMultipleCopies As Boolean = (iNumberOfCopies > 1)
	Dim bNamesProvided As Boolean = ((NewSheetNames IsNot Nothing) AndAlso (NewSheetNames.Length > 0))
	Dim oDDoc As DrawingDocument = oSheet.Parent
	Dim oInvApp As Inventor.Application = oSheet.Application
	If oDDoc.Views.Count = 0 OrElse oDDoc IsNot oInvApp.ActiveDocument Then
		Try 'it must be the 'active' document (visibly showing on your screen)
			oDDoc = oInvApp.Documents.Open(oDDoc.FullDocumentName, True)
			oDDoc.Activate()
		Catch : End Try
	End If
	Dim oCmdMgr As CommandManager = oInvApp.CommandManager
	Dim oCDs As ControlDefinitions = oCmdMgr.ControlDefinitions
	Dim oCopyCmd As ControlDefinition = oCDs.Item("AppCopyCmd")
	Dim oPasteCmd As ControlDefinition = oCDs.Item("AppPasteCmd")
	Dim oNewSheet As Inventor.Sheet = Nothing
	Dim oNewSheets As List(Of Inventor.Sheet)
	If bMultipleCopies Then oNewSheets = New List(Of Inventor.Sheet) 'initialize it
	For i As Integer = 1 To iNumberOfCopies
		oCmdMgr.DoSelect(oSheet)
		oCopyCmd.Execute()
		oCmdMgr.DoUnSelect(oSheet)
		oPasteCmd.Execute()
		oNewSheet = oDDoc.Sheets.Item(oDDoc.Sheets.Count)
		If bNamesProvided Then
			Try
				oNewSheet.Name = NewSheetNames(i - 1)
			Catch e As Exception
				Logger.Error(e.ToString)
			End Try
		End If
		If bMultipleCopies Then oNewSheets.Add(oNewSheet)
	Next
	If bMultipleCopies Then Return oNewSheets.ToArray
	Return oNewSheet
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) :thumbs_up:.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)