Open form when file is to edit mode

Open form when file is to edit mode

patryk_walkusz
Contributor Contributor
662 Views
15 Replies
Message 1 of 16

Open form when file is to edit mode

patryk_walkusz
Contributor
Contributor

Hi

 I have already created a form to change glass parameters. I need help writing code to open this form when the file is in 'edit' mode." Can u help me?

patryk_walkusz_0-1744029521404.png

 

 

0 Likes
Accepted solutions (1)
663 Views
15 Replies
Replies (15)
Message 2 of 16

WCrihfield
Mentor
Mentor

Hi @patryk_walkusz.  With that file visibly open on your screen, make sure you can see the iLogic tab in the side browser area, then go to the 'Rules' sub tab within it, and create a new internal iLogic rule there.  Name it something like "Show Form" (or something more specific if you wish) as you create it.  Then, within that rule, put a line of code like the following:

iLogicForm.Show("Form 1")

...where "Form 1" is replaced by the name of your internal iLogic Form (the form's main title).  Then click the Save button, followed by the Close button on the rule editor dialog, to save and close that rule.

Now, go to the 'Manage' tab in the Inventor ribbon, then to the 'iLogic' panel, and click on the button named 'Event Triggers'.  That will open a small dialog.  Make sure you are on the tab within that dialog named 'This Document'.  Then, find your internal iLogic rule you just made in the list on the left side, and using your left mouse button, click and drag that rule over into the area on the right, and drop just under the event named "After Open Document".  That will cause that rule to be triggered to run whenever that document gets opened.  Then when the rule runs, it will open that Form for you.  Then click on the OK button in that Event Triggers dialog, to save what you just did.  That setting will be saved within that document.  Then make sure to save the document itself, before closing it.

 

Keep in mind however, that placing an instance of that model file into an assembly as a component, will also 'open' that document, even though it may only be opened invisibly, in the background, with no document tab.  This is so that the assembly can reference its model data, and represent that model's geometry within the assembly.  There may be a way to avoid this from happening though.  One thing we can likely do to help avoid this is add an extra line of code at the top of the internal iLogic rule that shows the Form, like the line of code below:

If ThisDoc.Document IsNot ThisApplication.ActiveDocument Then Return

That checks if the document that the rule is saved within (ThisDoc.Document) is the same Document as the application's active document (the one visibly showing on your screen for editing), and if they are not, then exit the rule without letting any further code be read or acted upon.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 16

patryk_walkusz
Contributor
Contributor

This doesn't work as I expected. It always opens the form for no reason when I click Yes or No in Vault. I'm connected with Vault and I think "edit mode" is when I check out a file from Vault, and when I don't check it out, it will not be in edit mode.

I need:when we open a file from Vault and click Yes when it asks us to check it out, it will show my form. But when I click No, it doesn't show my form.

patryk_walkusz_0-1744092482776.png

 

0 Likes
Message 4 of 16

WCrihfield
Mentor
Mentor

OK.  I will take your word for it, because I have not used Vault yet.  We are supposed to be getting into it in the near future though.  The term 'Open' can be a bit misleading, because it is possible for a document to be open, but still not be visibly showing on your screen, and with no document tab.  We can very easily open documents invisibly by code, when we do not need to actually see certain changes being made to it by our code, to reduce processing time and use fewer system resources.

There are also a couple other terms in this general area that you may need to be familiar with, if you are not already.  "Initialize" is one of those terms.  That is when a file that Inventor is seeking for some reason first gets found and a portion of its data read into Inventor's memory, for the purpose of following file references, such as when some other document gets opened in Inventor, and it is referencing that file.  This is similar to a 'partial' open, and is a step that happens before a Document is fully opened, and that document may never get fully opened, depending on the situation and events to follow.  Another term is 'Activate', which essentially means making a Document the 'active' document, which visibly shows it on your screen, ready for direct edits by user interactions.  It is possible for there to many documents open, both visibly (with document tabs), and invisibly (no document tabs), but only one can truly be the 'active' document at any one time.

With that in mind, we can 'listen' for those very specific events to happen, and react to them.  But when relying solely on the simplistic iLogic Event Triggers dialog settings for reacting to events, we are not given that many options for events to use, and not given much control over the timing of our code based reactions.  In order to get access to more events, and more control over those events, we need to utilize the Inventor API's resources, instead of uniquely iLogic resources, which generally requires more advanced coding, and is more complex to manage.  See the following online help links to some related Inventor API events.

ApplicationEvents.OnInitializeDocument

ApplicationEvents.OnOpenDocument  

ApplicationEvents.OnActivateDocument 

ApplicationEvents.OnNewDocument 

ApplicationEvents.OnDeactivateDocument 

ApplicationEvents.OnCloseDocument 

ApplicationEvents.OnTerminateDocument 

...just to mention a few that are available from the ApplicationEvents object, which itself is just one of many such Inventor API objects with events associated with them.  Handling Events like these are generally better managed by Inventor Add-Ins, rather than with normal iLogic rules, but we are able to use them with our rules also, if the rules are well enough written and efficient enough.  But if the code is not written that well, or is not that efficient, it can negatively effect Inventor's performance, depending on the event(s), and what is being done in response to the event(s).

Edit:  It may also help to be aware that...when we open an assembly or a drawing type document, those document types usually always 'reference' other documents & files, because they usually contain representations of those other documents within them.  So, when we open them, all those other documents that they reference also get 'initialized' in the background, invisibly, with no document tabs.  You can notice this taking place if you have Inventor's status bar visibly showing while opening those types of documents, and watch the two numbers on the status bar's right side.  The number on the left is 'total occurrences'.  The number on the right is 'open documents'.  When you open a large assembly, you will see those numbers grow, indicating the effects of opening that assembly, and all the occurrences it contains, and all the documents it had to 'load' into Inventor's memory.  The use of ModelStates can effect those numbers, due to suppression, which unloads some of that type of data from Inventor's session memory, for performance purposes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 16

marcin_otręba
Advisor
Advisor

hi @patryk_walkusz ,  to do it easiest way you can hook up into terminatecomand  event  using addin and check command named Vaultcheckout or Vaultcheckouttop. 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 6 of 16

patryk_walkusz
Contributor
Contributor

Can u more explain Im really bad at Ilogic

0 Likes
Message 7 of 16

marcin_otręba
Advisor
Advisor

ok, i understand... well in this scenario addin is not good idea.

maybe we could make rule wchich will be fired on document open as @WCrihfield suggested with vault state check and if it is not released then your form will be fired ?

 

this is the code to do it:

you must change lines 8,9 and 28 to your enviroment data. If you need help with that give me know.

AddReference "Autodesk.Connectivity.WebServices.dll"
Imports AWS = Autodesk.Connectivity.WebServices
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase
Sub Main()
	Dim released_status As String = "Released"
	Dim global_form_name As String = "copy design"
Dim doc As Document = ThisDoc.Document
Dim mVltCon As VDF.Vault.Currency.Connections.Connection
mVltCon = VB.ConnectionManager.Instance.Connection
Dim stat As String = ""
stat=get_vaultfilestatus(doc.FullFileName, mVltCon)
 logger.Info(stat)
	If stat <> released_status Then iLogicForm.ShowGlobal(global_form_name)
		
	'If stat = "In Progress" Then 
		
	'If stat = "For review" Then 
		

iLogicVb.UpdateWhenDone = True 
 
End Sub
 
Function get_vaultfilestatus(filepath As String, vaultconnection As VDF.Vault.Currency.Connections.Connection) As String
 Dim filepath_to_vault As String ="C:\Vault\"
filepath = filepath.Replace(filepath_to_vault, "$/")
'flip the slashes
filepath = filepath.Replace("\", "/")
 
Dim VaultPaths() As String = New String() {filepath}
 
Dim wsFiels() As AWS.File = vaultconnection.WebServiceManager.DocumentService.FindLatestFilesByPaths(VaultPaths)
 
Dim mFileIt As VDF.Vault.Currency.Entities.FileIteration = New VDF.Vault.Currency.Entities.FileIteration(conn,wsFiels(0))
Dim lifeCycleInfo As VDF.Vault.Currency.Entities.FileLifecycleInfo = mFileIt.LifecycleInfo
 
Return lifeCycleInfo.StateName
Return lifeCycleInfo.StateId
 
End Function

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 8 of 16

marcin_otręba
Advisor
Advisor

oh, sorry this previus one will file even if file is not checked out... you can just use this one instead :

it checks if control checkouttop is not enabled and undocheckout top enabled wchich means that file is checked out.

Sub Main()
	Dim global_form_name As String = "copy design"
Dim doc As Document = ThisDoc.Document
If Not ThisApplication.CommandManager.ControlDefinitions("VaultCheckoutTop").Enabled And ThisApplication.CommandManager.ControlDefinitions("VaultUndoCheckoutTop").Enabled Then iLogicForm.ShowGlobal(global_form_name)
End Sub

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 9 of 16

marcin_otręba
Advisor
Advisor

and lastly to check if file is checked out from vault - and show form if it was you can use this rule;

only changein  line 15 CheckoutUserName <>"" to CheckoutUserName = "your vault user name" 

AddReference "Autodesk.Connectivity.WebServices.dll"
Imports AWS = Autodesk.Connectivity.WebServices
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase
Imports Autodesk.Connectivity.WebServicesTools
Sub Main()
Dim global_form_name As String = "copy design"
Dim doc As Document = ThisDoc.Document
Dim mVltCon As VDF.Vault.Currency.Connections.Connection
mVltCon = VB.ConnectionManager.Instance.Connection
Dim CheckoutUserName As String = ""
CheckoutUserName=get_vaultfilestatus(doc.FullFileName, mVltCon)
If CheckoutUserName <>"" Then iLogicForm.ShowGlobal(global_form_name)
End Sub
 
Function get_vaultfilestatus(filepath As String, vaultconnection As VDF.Vault.Currency.Connections.Connection) As String
 Dim filepath_to_vault As String ="C:\Vault\"
filepath = filepath.Replace(filepath_to_vault, "$/")
'flip the slashes
filepath = filepath.Replace("\", "/")
Dim VaultPaths() As String = New String() {filepath}
Dim mgr As WebServiceManager = vaultconnection.WebServiceManager
Dim filePropDefs As AWS.PropDef() = mgr.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")
Dim checkedOutPropDef As AWS.PropDef = filePropDefs.[Single](Function(n) n.SysName = "CheckoutUserName")
Dim wsFiels() As AWS.File = vaultconnection.WebServiceManager.DocumentService.FindLatestFilesByPaths(VaultPaths)
Dim propValues() As AWS.PropInst = mgr.PropertyService.GetProperties("FILE", New Long() {wsFiels(0).Id}, New Long() {checkedOutPropDef.Id})
Dim CheckoutUserName As String= ""
CheckoutUserName = propValues(0).Val
Return CheckoutUserName

 
End Function

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 10 of 16

patryk_walkusz
Contributor
Contributor
Sub Main()
	Dim global_form_name As String = "copy design"
Dim doc As Document = ThisDoc.Document
If Not ThisApplication.CommandManager.ControlDefinitions("VaultCheckoutTop").Enabled And ThisApplication.CommandManager.ControlDefinitions("VaultUndoCheckoutTop").Enabled Then iLogicForm.Show("GlassEdit")
End Sub


This work almost. He see button but I must click to start this rules. 
I think it work 2 fast when program inventor dont yet load buttons. Myb if we ll give some delay him to check will be reapair the problem.

0 Likes
Message 11 of 16

patryk_walkusz
Contributor
Contributor

In you last code I have error in linie 30:

patryk_walkusz_1-1744191349558.png

patryk_walkusz_2-1744191375853.png



Im not sure i have good location to file voult:

"C:\Program Files\Autodesk\Vault Client 2023"



0 Likes
Message 12 of 16

marcin_otręba
Advisor
Advisor

this need to be workfolder path on your local disc.

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 13 of 16

patryk_walkusz
Contributor
Contributor

I think this is correct :

patryk_walkusz_1-1744194329295.png

 

0 Likes
Message 14 of 16

marcin_otręba
Advisor
Advisor

nope,

from vault click : 

marcin_otrba_0-1744194627863.png

and this will be location wchich will be opened.. but remember to add slash t the end of this path like : C:\Vault\

 

 

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 15 of 16

patryk_walkusz
Contributor
Contributor

Now I can save without problem but still nor working. It show me form when file is in "edit mode" or "no edit mode" when I run him (picure1) and still nor working when I press button to edit mode(picture2) or when I get "edit mode" from Voult(picure3):

 

picture1:

patryk_walkusz_1-1744196670508.png

 

picture2:

patryk_walkusz_0-1744196545264.png

 

picture3:

patryk_walkusz_2-1744196805902.png

 

 

 

0 Likes
Message 16 of 16

patryk_walkusz
Contributor
Contributor
Accepted solution

I added information when program start:

 

patryk_walkusz_0-1744708102408.png

 

Dim result As DialogResult
result = MessageBox.Show("Do you want to open the form for editing?", "Edit Glass - Form", MessageBoxButtons.YesNo)

If result = DialogResult.Yes Then
    ' Kod do otwarcia formularza
    iLogicForm.Show("GlassEdit")
Else
    ' Kod, gdy użytkownik wybierze "Nie"
End If

 Not perfect but works ^^

0 Likes