RunRules & Changing File Names

RunRules & Changing File Names

Joseph.Lasser
Participant Participant
282 Views
4 Replies
Message 1 of 5

RunRules & Changing File Names

Joseph.Lasser
Participant
Participant

Please reference the script below:

iLogicVb.RunRule("AXXXX-TREAD.ipt", "Spreadsheet Link")
'iLogicVb.RunRule("AXXXX-TREAD.ipt", "Document Update")
iLogicVb.RunRule("AXXXX-REF.ipt", "Spreadsheet Link")
'iLogicVb.RunRule("AXXXX-REF.ipt", "Document Update")
'iLogicVb.RunRule("AXXXX-SS01.iam", "Document Update")
'iLogicVb.RunRule("AXXXX-SS02.iam", "Document Update")
ThisDoc.Document.Rebuild()
InventorVb.DocumentUpdate()
'ThisDoc.Save()

This script is taken from an assembly which is meant to run rules in its component files to pull parameters from a spreadsheet and update the components upon opening the assembly.  This assembly is meant to be a template - it will be copied repeatedly and the index in the filename changed.  Each time the index is changed, the assembly component links will need to be resolved.  This is acceptable, however, I do not want it to be necessary to change the iLogic each time.  Is there a way to automatically update the iLogic with the new indexes, perhaps by prompting the user for the index upon opening?  

 

I am inexperienced with iLogic, so there are many capabilities that I am unaware of.  I recognize that the workflow described above is not the most efficient.  If you have any suggestions to improve this template workflow, they would be appreciated.

0 Likes
283 Views
4 Replies
Replies (4)
Message 2 of 5

marcin_otręba
Advisor
Advisor

hi,

for example you can start from:

 

' get active assembly and set it to ass object
Dim
ass As AssemblyDocument = ThisApplication.ActiveDocument
' go trough all assembly reference files and set it to ref_doc object For Each ref_doc As Document In ass.AllReferencedDocuments
' if ref_doc object is part document then run rule If ref_doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then iLogicVb.RunRule(ref_doc.DisplayName, "ruleName") ' change rulename to your rule name
if ref_doc object is assembly document then run rule If ref_doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then iLogicVb.RunRule(ref_doc.DisplayName, "ruleName") ' change rulename to your rule name Next

it will go through all dependent files from this assembly.

 

you can use display name  of ref document to determine if rule should run for example:

' if ref_doc object is part document and name of that part document contains text THREAD- caption sensitive - then run rule 
If
ref_doc.DocumentType = DocumentTypeEnum.kPartDocumentObject And ref_doc.DisplayName.Contains("THREAD") Then iLogicVb.RunRule(ref_doc.DisplayName, "ruleName")

or use property or parameter..

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 3 of 5

Joseph.Lasser
Participant
Participant

Thank you for responding!  Would you be able to add some comments to this script such that I may better understand the syntax and what each line is doing?

0 Likes
Message 4 of 5

marcin_otręba
Advisor
Advisor

@Joseph.Lasser - i updated my original post.

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 5 of 5

WCrihfield
Mentor
Mentor

Hi guys.

@Joseph.Lasser Another little trick that may help in a situation like this would be to 'stabilize' the component names within the model browser tree.  This is done by modifying each occurrence's name to something different than its 'default' value (default value is usually file name, but can also be switched to part number), but is still unique, and still lets you identify which occurrence it is.  When you do this, then when you replace that occurrence with another source file, that action will not change that occurrence's name.  When that is the case, then you can always find that exact occurrence by its name within your iLogic rules, without having to worry about their names changing.  There are multiple forum discussions about this idea / tactic, and some folks have their own personal Blog sites which have good articles about this tactic / strategy.

 

Below is just another example which iterates all referenced documents, similar to @marcin_otręba example, but is laid out a bit different, and includes a couple different methods or techniques.  For example, it is using the 'iLogicVb.Automation.RunRule(document, ruleName) method , instead of the iLogicVb.RunRule(Object, String) method, because it allows us to specify the Document object that we want that rule to target, since we already have access to that object available within the iteration of referenced documents.  And it utilizes a Try...Catch...Finally...End Try statement, to help allow the rule to continue running, even if an error is encountered somewhere along the way.  I tried to include plenty of comments within the code, since you are not that familiar with these things yet.  This is just one possibility among a great many possible ways that a rule like this could be formatted or laid out.  Another idea would be to get the actual internal rule objects within those referenced documents, then use the iLogicVb.Automation.RunRuleDirect(rule As iLogicRule) method to run them.  This just another way to help ensure that the rule being ran is sourced from that exact Document, and effecting that exact Document, as intended.  These extra tactics are sometimes necessary, because when a rule gets ran, and the main assembly is still the 'active' document at that time, to help the rule to target the referenced document, instead of the active assembly document.  The code within each of those internal rules also matters, because they may be trying to access or work with the 'active' document, instead of the Document which that rule is within.  Using specific Document references becomes very important when the rules may get ran 'remotely' like this (while another Document is 'active', or because an event happened).

'capture the current document to a variable
Dim oDoc As Inventor.Document = ThisDoc.Document
'get all referenced documents from all levels to one variable
Dim oRefDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments
'check if there were no referenced documents, and if so exit this rule
If oRefDocs Is Nothing OrElse oRefDocs.Count = 0 Then Return
'get the iLogic Automation object to a variable
Dim oAuto As IiLogicAutomation = iLogicVb.Automation

'start iterating through each referenced document
For Each oRefDoc As Inventor.Document In oRefDocs
	'get 'FullFileName' (full path, file name, & extension) of this document 
	Dim sFFN As String = oRefDoc.FullFileName
	'extract just file name (no path, no extension) from FullFileName
	Dim sFileNameWOExt As String = System.IO.Path.GetFileNameWithoutExtension(sFFN)
	'now check this file name to determine which rules to run on it
	'first check is if it starts with the upper-case letter "A"
	If sFileNameWOExt.StartsWith("A") Then
		'using a Try...Catch...Finally...End Try statement below
		'this will 'try' to do something that might cause an error
		'but if it does cause an error, it will not stop the whole rule
		'and will allow the rule to keep going. (just in case its needed)
		Try 'code to try goes in first section
			'second check what it ends with (remember, no file extensions here)
			If sFileNameWOExt.EndsWith("-TREAD") Then
				'if passed both checks, then run rules on that Document
				'this method lets us specify the actual Document object for the rule to target
				oAuto.RunRule(oRefDoc, "Spreadsheet Link")
				'oAuto.RunRule(oRefDoc, "Document Update")
			ElseIf sFileNameWOExt.EndsWith("-REF") Then
				oAuto.RunRule(oRefDoc, "Spreadsheet Link")
				'oAuto.RunRule(oRefDoc, "Document Update")
			ElseIf sFileNameWOExt.EndsWith("-SS01") Then
				'oAuto.RunRule(oRefDoc, "Document Update")
			ElseIf sFileNameWOExt.EndsWith("-SS02") Then
				'oAuto.RunRule(oRefDoc, "Document Update")
			End If
		Catch ex As Exception
			'This section only runs if 'Try' section caused error
			'the 'ex' variable captures the actual Error Object
			'next line writes info about error to the iLogic Log window
			Logger.Error(ex.ToString)
		End Try 'end of 'EndsWith' check
	End If 'end of 'StartsWith' check
Next 'go to next referenced document, if any
'iteration of referenced documents is done at this point
oDoc.Rebuild2(True)
'oDoc.Save()

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes