(Need help) Traversing open tabs with iLogic

(Need help) Traversing open tabs with iLogic

Mac_W
Contributor Contributor
535 Views
10 Replies
Message 1 of 11

(Need help) Traversing open tabs with iLogic

Mac_W
Contributor
Contributor

I am trying to make a program that prints all the open tabs in inventor from left to right. My program is failing to print duplicate tabs. Any help or advice would be greatly appreciated.

printAll program:

 

Sub main()
	Dim i As Integer = 1
	For Each oDrwDoc In ThisApplication.Documents
		If oDrwDoc.DocumentType = kDrawingDocumentObject Then
			
			Dim oDrgPrintMgr As DrawingPrintManager
			oDrgPrintMgr = oDrwDoc.PrintManager
			
			'Set the paper size , scale and orientation
			oDrgPrintMgr.ScaleMode = kCustomScale
			oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
			
			'oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSizeLedger
			'oDrgPrintMgr.PaperSize = 14338
			
			oDrgPrintMgr.PrintRange = kPrintCurrentSheet
			'oDrgPrintMgr.Orientation = kLandscapeOrientation
			
			oDrgPrintMgr.AllColorsAsBlack = True
			oDrgPrintMgr.SubmitPrint
			
		End If
	Next
End Sub

 



I made a separate program that opens all drawings of parts and sub assemblies found in a parts list of a top level assembly and its sub assemblies parts lists. In my example we have a LH and RH sub assembly which both contain an identical part. My "openAll" program checks to see if a drawing is already open and if so, it creates a new window of that drawing. 

 

TABS.png

Snippet from openAll program that makes new windows of duplicate drawings:

 

'we need to check if drawing is already open and if it is not then open it
'if already open then we call CopyTab()

'source: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-check-if-drawing-file-is-open/td-p/10243425
Sub DupeCheck()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	TopLVL = RuleArguments("Og")
	ThisName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
	'MsgBox(ThisName & " compared to " & TopLVL)
	For Each Doc In ThisApplication.Documents 'check if there is an open IDW with matching doc name
		Dim oDC As Document = Doc
		If oDC.DocumentType = DocumentTypeEnum.kDrawingDocumentObject And System.IO.Path.GetFileNameWithoutExtension(oDC.FullDocumentName) = ThisName And TopLVL <> ThisName
			oDC.Activate
			Call CopyTab() 'copy IDW window to new window
			If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'if duped doc is an assy we get recursive
				oDoc.Close(True) 'close the reference doc
				Call Main()
			Else 
				oDoc.Close(True) 'close the reference doc
			End If
			Exit Sub
		End If
	Next

	If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'If active doc=Iam, do recursion
		iLogicVb.RunExternalRule("C:\Users\Mw\Desktop\Inventor Macros\OpenVaultIDW") 'opens matching IDW and closes refDoc
		If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Call PartsListCheck() 'check if assy drawing has parts list
			
		Else If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'MsgBox("close iam")
			ThisApplication.ActiveDocument.Close(True)
		End If
	Else If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		iLogicVb.RunExternalRule("C:\Users\Mw\Desktop\Inventor Macros\OpenVaultIDW") 'opens matching IDW and closes refDoc
		If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			'MsgBox("close ipt")
			ThisApplication.ActiveDocument.Close(True)
		End If
	End If
 
End Sub

'copy IDW window to new window
'source: https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-DB8C7D03-9B57-403B-885F-2F2180CBD45A
Sub CopyTab()
	'MsgBox("CopyTab Called")
    Dim oDoc2 As Document = ThisApplication.ActiveDocument
    
    Dim oView1 As View
    oView1 = oDoc2.Views(1)
    
    Dim oViewTab1 As ViewTab
    oViewTab1 = oView1.ViewTab
        
    Dim oView2 As View
    oView2 = oDoc2.Views.Add
    
End Sub

 

 

0 Likes
536 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

Hi @Mac_W.  There are a couple different ways to modify that first code that might help out in this situation.  The simplest change would be to add 1 more step after the Application.Documents to get the VisibleDocuments only.  That will avoid any that are open, but not currently visible (that is possible).  The alternate route could be to loop through ViewTabs, get the view for that tab, get the Document for that tab, check its type, and so on.  I believe only visible documents will have view tabs.  The other minor change I would recommend is adding in the Enum name before the Enum variation, when using the variation's name, instead of its Integer value, when doing it in iLogic (vs VBA).  Below is just a copy of your original code that I made a few minor changes to.

Sub main()
	Dim i As Integer = 1
'	Dim oVTs As ViewTabsEnumerator = ThisApplication.ActiveViewFrame.ViewTabs
'	For Each oVT As ViewTab In oVTs
'		Dim oView As Inventor.View = oVT.View
'		Dim oViewDoc As Document = oView.Document
'		If oViewDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
	Dim oVDocs As DocumentsEnumerator = ThisApplication.Documents.VisibleDocuments
	For Each oDrwDoc In oVDocs
		If oDrwDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Dim oDrgPrintMgr As DrawingPrintManager = oDrwDoc.PrintManager
			oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
			'oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSizeLedger
			'oDrgPrintMgr.PaperSize = 14338
			oDrgPrintMgr.PrintRange = PrintRangeEnum.kPrintCurrentSheet
			oDrgPrintMgr.Orientation = PrintOrientationEnum.kLandscapeOrientation
			oDrgPrintMgr.AllColorsAsBlack = True
			oDrgPrintMgr.SubmitPrint
		End If
	Next
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)

Message 3 of 11

WCrihfield
Mentor
Mentor

Hi @Mac_W.  By the way, it might be more efficient to create a shared Dictionary(Of DrawingDocument, Integer) type variable near the beginning, then when a new drawing is encountered in the search, add an entry into the Dictionary for it, with the Integer value of 1 (quantity of that drawing to print so far).  Then if that same drawing is encountered again, get the existing Dictionary Entry for it, and add 1 to its quantity value.  When done with the search, loop through that dictionary and print each drawing the number of times specified by its quantity value.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 11

Mac_W
Contributor
Contributor

Hi @WCrihfield thank you for your reply. I don't think the duplicated windows are considered documents. I tried modifying the code you provided to save paper while testing. The modified code below prints the number of tabs open but when trying to print the tabs related display name it just prints the active doc display name. 

Sub main()

	Dim oVTs As ViewTabsEnumerator = ThisApplication.ActiveViewFrame.ViewTabs
	
	MsgBox(oVTs.Count)

	For Each oVT As ViewTab In oVTs
		
		Dim oView As Inventor.View = oVT.View
		Dim oViewDoc As Document = oView.Document
		MsgBox(oViewDoc.DisplayName)

	Next
			
End Sub

 

0 Likes
Message 5 of 11

WCrihfield
Mentor
Mentor

Are you are saying that it just a message containing the name of the currently active tab multiple times?  If so, that seems odd to me.  I just copied your code to a new internal rule and ran it (while only having 2 tabs open), and it showed a message containing the quantity 2, then message boxes containing exactly what was showing on the first tab, then exactly what it shows on the second tab, as expected.  I am using Inventor Pro 2024.0.1 at the moment.

Edit:  Try changing DisplayName to something more specific like FullDocumentName.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 11

Mac_W
Contributor
Contributor

Correct, it will message the number of tabs open and then for the number of tabs open it will message the active documents display name. 

For example lets say I have "A.idw" and "B.idw" open. If I run my rule it will message "2" and then 2 messages containing the display name of whichever doc is active. So if "A.idw" is the active doc it messages "2", then "A.idw", and then "A.idw".

I am using Inventor Pro 2022 64-Bit Build: 153

0 Likes
Message 7 of 11

Mac_W
Contributor
Contributor

I also found this thread where you supplied some different code that I tried modifying and it does message each display name but if there are duplicated windows then the order doesn't match.
EX:
     Open tabs: "A.idw:1", "B.idw", "A.idw:2"
     Output: "2", "A:1", "A:2", "B"

Sub main()

    Dim oVTs As ViewsEnumerator = ThisApplication.Views

	MsgBox(oVTs.Count)

    For Each oView As Inventor.View In oVTs
        Dim oViewDoc As String = oView.Caption
        MsgBox(oViewDoc)
    Next

End Sub

 

 

0 Likes
Message 8 of 11

WCrihfield
Mentor
Mentor

I am honestly not sure how to retrieve those tabs in the exact same order that they are shown on your screen.  I only know that usually their Index number goes by when they were created, with 1 being the oldest (created first or survived longest), then larges number being the last one created, or newest one remaining after newer ones removed.  That does not account for when you move them around on the screen.  Maybe if you use the ViewTabGroup it they will be in the order you want, but not sure about that either.

Heading out now, so have a great weekend.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 11

Mac_W
Contributor
Contributor

@WCrihfield I appreciate your help and feedback. I think making a new window for duplicated drawings would be nice if it was seen as its own document/had its own tab properties. I made a workaround; instead of making a new window for duplicate drawings, I am saving those drawings as "copies".  I replaced my CopyTab() sub with the one below and it seems to work well enough, albeit slower. Thanks again!

 

Sub saveAsCopyandOpen()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	fPath = "C:\Users\Mw\Desktop\Prints\temp\"
	Dim fName As String = oDoc.FullDocumentName
	Dim fNp As Integer = InStrRev(fName, "\", -1)
	fName = Right(fName, Len(fName)-fNp)
	Dim tCount As Integer = Directory.GetFiles(fPath, "*").Count + 1
	Dim newDoc As String = fPath & "Copy" & tCount & " " & fName
	oDoc.SaveAs(newDoc, True)
	ThisApplication.Documents.Open(newDoc)

End Sub

 

  

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

Hi @Mac_W.  I'm glad to hear that you have found a process that seems to be working for you.  However, I just wanted to reiterate the suggestion I made earlier about using something like a List(Of DrawingDocument) or Dictionary(Of DrawingDocument, Integer) or List(Of Object()) type collections to keep track of all the drawings you want to print, and in what order, and how many times to print them.  It certainly sounds to me like it would be much easier to manage, and potentially require a lot less processing than creating a bunch of document tabs across multiple windows, then trying to go back and print them all in the same order they are on your screen later.  Did you know that you can 'send' and 'receive' things like List or Dictionary through the RuleArguments tool (uses a NameValueMap).  And you can also set them as the value of a SharedVariable, then retrieve them in another rule from either the RuleArguments or a SharedVariable and use them as needed.  You can also send/receive a Document object either of those two ways, just in case you were not aware.  If you put those documents (and maybe an Integer representing how many times to print it) into a List or Dictionary as you find them, you can then rearrange them later if needed before you iterate through that List or Dictionary and print them.  The List object (and the Dictionary) maintain the order you specify, and can be sorted, if needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 11

Mac_W
Contributor
Contributor

@WCrihfield Thank you for emphasizing how useful SharedVariable, RuleArguments, and dictionaries can be. I have been meaning to learn more about utilizing dictionaries and would like to work towards that soon. I've been really busy lately and I already had this sub built for exporting drawings as PDFs so I repurposed that as a quick fix. I also posted a feedback form regarding the discrepancy in our results with the "for each ViewTab rule". If I hear any news about that I'll post it here. Thanks again for all the help, advice, and documentation!

0 Likes