Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Open each document for view and close inside assembly.

K.TRYFONIDIS
Advocate

Open each document for view and close inside assembly.

K.TRYFONIDIS
Advocate
Advocate

Hello,

 

I am trying to make an external rule that will run only in an assembly document and will open each of the documents inside of it (.iam and .ipt ) view them for 3 sec then save and close, after move to next one.

i am trying to use a code like this one but i don't get it to run.

Dim referencedDocs As DocumentsEnumerator = asmDoc.AllReferencedDocuments
    For Each doc As Document In referencedDocs
        If doc.DocumentType = kPartDocumentObject Or doc.DocumentType = kAssemblyDocumentObject Then
            OpenAndViewDocument(doc)
            Thread.Sleep(5000) ' View each document for 5 seconds
            CloseDocument(doc)

 

0 Likes
Reply
Accepted solutions (1)
479 Views
4 Replies
Replies (4)

Andrii_Humeniuk
Advisor
Advisor

Hi @K.TRYFONIDIS . Try this code:

Sub main()
	Dim oAsmDoc As AssemblyDocument = ThisDoc.FactoryDocument
	Dim oDocs As Documents = ThisApplication.Documents
	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	For Each oRefDoc As Document In oRefDocs
		Dim oDoc As Document = oDocs.Open(oRefDoc.FullDocumentName, True)
		System.Threading.Thread.Sleep(5000)
		If oDoc.IsModifiable Then oDoc.Save()
		oDoc.Close()
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes

K.TRYFONIDIS
Advocate
Advocate

it works pretty good, only problem is that i want to rotate the parts for those 5 sec, and when i run the code its like frozen.

0 Likes

Andrii_Humeniuk
Advisor
Advisor

You cannot use the program while running the code, but you can for example add a 360 degree rotation around the Y axis to the code.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes

K.TRYFONIDIS
Advocate
Advocate
Accepted solution

did a slight change to your code.

it works perfect.

this code is being combined with a trigger ilogic to save custom thumbnails of the part ( you can rotate with spacemouse if you have one )

Open documents code.

Imports System.Threading

Sub Main()
Dim oAsmDoc As AssemblyDocument = ThisDoc.FactoryDocument
Dim oDocs As Documents = ThisApplication.Documents
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
If oRefDocs.Count = 0 Then Exit Sub

For Each oRefDoc As Document In oRefDocs
Dim oDoc As Document = oDocs.Open(oRefDoc.FullDocumentName, True)
Dim startTime As DateTime = DateTime.Now
Do While DateTime.Now.Subtract(startTime).TotalSeconds < 5
' Allow interaction with the model for 5 seconds
ThisApplication.UserInterfaceManager.DoEvents()
Loop

If oDoc.IsModifiable Then oDoc.Save()
oDoc.Close()
Next

Interaction.MsgBox("Success! All referenced documents have been opened, viewed, saved, and closed.", MsgBoxStyle.Information, "iLogic Rule")
End Sub



Code to trigger before save.

Imports System.Drawing
Imports System.Drawing.Imaging
' Rule to run only for IPT and IAM files
Sub MAIN	
    Dim m_Doc As Inventor.Document
    m_Doc = ThisDoc.Document
    
    If m_Doc.DocumentType <> kAssemblyDocumentObject _
        And m_Doc.DocumentType <> kPartDocumentObject Then 
        MessageBox.Show("File is not a model.", "iLogic")
        Return 'exit rule
    End If

    ' Get current color scheme name
    Dim oColorSchemeName As String
    oColorSchemeName = ThisApplication.ActiveColorScheme.Name

    ' Get current color scheme background type
    Dim oBackGroundType As BackgroundTypeEnum
    oBackGroundType = ThisApplication.ColorSchemes.BackgroundType

    ' Change to Deep Blue (Presentation)
    ThisApplication.ColorSchemes.Item("Presentation").Activate

    ' Set to use one color background type
    ThisApplication.ColorSchemes.BackgroundType = BackgroundTypeEnum.kOneColorBackgroundType

    ' Save the active view as an image directly
    Dim m_Camera As Inventor.Camera = ThisApplication.ActiveView.Camera
    Dim m_TO As Inventor.TransientObjects = ThisApplication.TransientObjects
    Dim m_CV As Inventor.View = ThisApplication.ActiveView

    Dim m_PrevMode As Integer = m_CV.DisplayMode
    Dim m_Disp3D As Boolean = ThisApplication.DisplayOptions.Show3DIndicator

    m_CV.DisplayMode = Inventor.DisplayModeEnum.kShadedWithEdgesRendering
    ThisApplication.DisplayOptions.Show3DIndicator = False

    m_CV.Update

    ' Set the image path and name
    Dim oPath As String = "folder to save\IMAGES\"
    Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(m_Doc.FullFileName) ' Get the file name without extension
    Dim oFile As String = oPath & oFileName & ".png"

    ' Save the image
    m_Camera.SaveAsBitmap(oFile, 800, 600, m_TO.CreateColor(255, 255, 255))

    m_CV.DisplayMode = m_PrevMode
    ThisApplication.DisplayOptions.Show3DIndicator = m_Disp3D
    m_CV.Update

    ' Change back to the original color scheme using the name
    ThisApplication.ColorSchemes.Item(oColorSchemeName).Activate

    ' Change back to the original background type
    ThisApplication.ColorSchemes.BackgroundType = oBackGroundType

End Sub