Open table

Open table

madstrolle
Enthusiast Enthusiast
501 Views
11 Replies
Message 1 of 12

Open table

madstrolle
Enthusiast
Enthusiast

Hi,

 

We use a normal table for our revisions. 

The problem is that people often forget to update the table.

Is there a way to open the table automatically, e.g trigger it to open before save?

 

Thanks in advance😊

 

madstrolle_0-1694171141207.png

 

0 Likes
Accepted solutions (2)
502 Views
11 Replies
Replies (11)
Message 2 of 12

Andrii_Humeniuk
Advisor
Advisor

Hi @madstrolle . Add this code to the trigger "After Save Document".

 

Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oControl As ControlDefinitions = oInvApp.CommandManager.ControlDefinitions
Dim oCtrlDef As ControlDefinition
Dim oTables As RevisionTables = oDDoc.ActiveSheet.RevisionTables
If oTables.Count = 0 Then Exit Sub
oDDoc.SelectSet.Select(oTables(1))
oCtrlDef = oControl("DrawingRevisionTableEditCtxCmd")
oCtrlDef.Execute()
oDDoc.Save()

 

Or this a rule, but this needs to be added to the "After Open Document" trigger.

Class ThisRule
Private oInvApp As Inventor.Application
Private WithEvents oApplicationEvents As ApplicationEvents
Sub main
	If SharedVariable.Exists("ApplicationEvent") Then Exit Sub
	oInvApp = ThisApplication
	oApplicationEvents = oInvApp.ApplicationEvents
	sharedVariable("ApplicationEvent") = oApplicationEvents
End Sub
Private Sub oAPP_onsave(DocumentObject As Document,
                        BeforeOrAfter As EventTimingEnum,
						Context As NameValueMap,
						ByRef HandlingCode As HandlingCodeEnum) _
						Handles oApplicationEvents.OnSaveDocument
	If BeforeOrAfter = EventTimingEnum.kBefore Then
		Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
		Dim oControl As ControlDefinitions = oInvApp.CommandManager.ControlDefinitions
		Dim oCtrlDef As ControlDefinition
		Dim oTables As RevisionTables = oDDoc.ActiveSheet.RevisionTables
		If oTables.Count = 0 Then Exit Sub
		oDDoc.SelectSet.Select(oTables(1))
		oCtrlDef = oControl("DrawingRevisionTableEditCtxCmd")
		oCtrlDef.Execute()
	End If
End Sub
End Class

 

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
Message 3 of 12

madstrolle
Enthusiast
Enthusiast

Hi @Andrii_Humeniuk ,

 

Thank you for your quick responce.

Unfortunately it's not working. Is it because you have made it for "revision table"? 

This is just a normal table, as in the picture.

 

madstrolle_0-1694175530346.png

 

0 Likes
Message 4 of 12

WCrihfield
Mentor
Mentor

Try using either "DrawingTableCmd" or "DrawingTableEditCtxCmd", instead.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 12

Andrii_Humeniuk
Advisor
Advisor

Code 1:

 

Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oControl As ControlDefinitions = oInvApp.CommandManager.ControlDefinitions
Dim oCtrlDef As ControlDefinition
Dim oTables As CustomTables = oDDoc.ActiveSheet.CustomTables
If oTables.Count = 0 Then Exit Sub
oDDoc.SelectSet.Select(oTables(1))
oCtrlDef = oControl("DrawingTableEditCtxCmd")
oCtrlDef.Execute()
oDDoc.Save()

 

Code 2:

Class ThisRule
Private oInvApp As Inventor.Application
Private WithEvents oApplicationEvents As ApplicationEvents
Sub main
	If SharedVariable.Exists("ApplicationEvent") Then Exit Sub
	oInvApp = ThisApplication
	oApplicationEvents = oInvApp.ApplicationEvents
	sharedVariable("ApplicationEvent") = oApplicationEvents
End Sub
Private Sub oAPP_onsave(DocumentObject As Document,
                        BeforeOrAfter As EventTimingEnum,
						Context As NameValueMap,
						ByRef HandlingCode As HandlingCodeEnum) _
						Handles oApplicationEvents.OnSaveDocument
	If BeforeOrAfter = EventTimingEnum.kBefore Then
		Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
		Dim oControl As ControlDefinitions = oInvApp.CommandManager.ControlDefinitions
		Dim oCtrlDef As ControlDefinition
		Dim oTables As CustomTables = oDDoc.ActiveSheet.CustomTables
		If oTables.Count = 0 Then Exit Sub
		oDDoc.SelectSet.Select(oTables(1))
		oCtrlDef = oControl("DrawingTableEditCtxCmd")
		oCtrlDef.Execute()
	End If
End Sub
End Class

 

 

The conditions for triggers are the same as in the previous comment.

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

Message 6 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @madstrolle & @Andrii_Humeniuk.  I you choose to use the 'custom' event handler code route, I would suggest using the DocumentEvents.OnSave Event, instead of the ApplicationEvents.OnSaveDocument Event, just to help contain it to that one document, and help eliminate it the handler once the document is closed.  I just believe it would be more efficient, and easier to handle.  And the DocumentEvents.OnClose Event portion being shown in this example below is most likely not even needed, but I left it in there anyways, for reference.  You will notice that I also made a point to set the HandlingCode to NotHandled, to help ensure that the normal event continues to happen after these interruptions, also as reference.  Carry on. 👍

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
	oDDoc = ThisDoc.Document
	oDocEvents = oDDoc.DocumentEvents
	AddHandler oDocEvents.OnSave, AddressOf oDocEvents_OnSave
	AddHandler oDocEvents.OnClose, AddressOf oDocEvents_OnClose
End Sub

Dim oDDoc As DrawingDocument
Dim oDocEvents As DocumentEvents

Sub oDocEvents_OnSave(oTiming As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	If oTiming = EventTimingEnum.kBefore Then
		Dim oSheets As Inventor.Sheets = oDDoc.Sheets
		Dim oTable As Inventor.CustomTable = Nothing
		For Each oSheet As Inventor.Sheet In oSheets
			Dim oCTables As Inventor.CustomTables = oSheet.CustomTables
			If oCTables.Count = 0 Then Continue For
			oTable = oCTables.Item(1)
			Exit For
		Next
		oDDoc.SelectSet.Clear
		oDDoc.SelectSet.Select(oTable)
		ThisApplication.CommandManager.ControlDefinitions.Item("DrawingTableEditCtxCmd").Execute2(True)
		HandlingCode = HandlingCodeEnum.kEventNotHandled
	End If
End Sub

Sub oDocEvents_OnClose(oTiming As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	If oTiming = EventTimingEnum.kBefore Then
		RemoveHandler oDocEvents.OnSave, AddressOf oDocEvents_OnSave
		HandlingCode = HandlingCodeEnum.kEventNotHandled
	End If
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 7 of 12

madstrolle
Enthusiast
Enthusiast
Seems like code 1 is working.
You wrote that the trigger has to be "after save"
Is it not possible to have it to trigger "before save"?
0 Likes
Message 8 of 12

Andrii_Humeniuk
Advisor
Advisor
It is best to use the 2nd code (larger) and place it in the "After Open Document" trigger. This way, the code will run when the document is launched and only run before the document is saved.

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
Message 9 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

 

Working perfectly!

 

Thanks 👍😀

 

0 Likes
Message 10 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

 

 

I know I said it was working perfectly.

But there is one thing that I have noticed now.

 

If I open an IDW and change something, save it and close it again, it works fine.

But if I change something and save it and make another change and save it again, it opens the table 2 times.

It becomes a bit annoying if you have made changes 10 times, you have to close the table 10 times.

 

It occurs only when you have the same IDW open. So if I close it and open it again it starts from the beginning.

Hope it makes sense😊

 

0 Likes
Message 11 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @madstrolle.  We could make this a 'one shot' routine, instead of a repeating routine, if that would work better for you.  What I mean is, at the end of the code for responding to the OnSave event, we can include the 'RemoveHandler' line of code that you see within the OnClose routine.  That will cause it to only respond that first time, then the event handler will be gone, so it will not respond anymore.

Just copy this line:

RemoveHandler oDocEvents.OnSave, AddressOf oDocEvents_OnSave

and put it just before the 'End If' line near the end of the 'oDocEvents_OnSave' Sub routine.  You can likely get rid of the oDocEvents_OnClose Sub routine too, because since these events are based on that specific document, they will be destroyed when the document is finally closed anyways.  I have a couple of my event handler codes set-up as 'one shot' reactions also, so it is not that uncommon.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

 

Work like a charm 👍

 

Thanks😊

0 Likes