Apply Rule to all parts and subassmblies of an assembly

Apply Rule to all parts and subassmblies of an assembly

b_berghuis
Explorer Explorer
366 Views
3 Replies
Message 1 of 4

Apply Rule to all parts and subassmblies of an assembly

b_berghuis
Explorer
Explorer

Hello Everyone,

 

Im just getting started with iLogic but im having trouble on this. I did alot of searching but couldn't really find what im look for, though i assume this is needed quite often.

 

I am trying to figure out how to apply a rule that I have not just to the top level assembly but to all the parts and subassemblies in the assembly.

 

It could be any rule, but the rule that i have checks if the partnumber is equal to the filename. If not it asks if it should change it to match.

 

oDoc = ThisDoc.FileName(False)
PartNumber = iProperties.Value("Project", "Part Number")

If oDoc = PartNumber Then
	MessageBox.Show("Part number = file name", "Title")
	
Else
		i = MessageBox.Show(("Fix part name?"  & vbCrLf & " " & vbCrLf & "File name =" + oDoc & vbCrLf & "Partnumber =" + PartNumber), "Part number does not equal file name", MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		If i = MsgBoxResult.Yes Then
			iProperties.Value("Project", "Part Number") = oDoc
			ElseIf i = MsgBoxResult.No Then
		End If
End If

 

As I said I'm just getting started so if you have in tips on how to improve this code please let me know.

0 Likes
Accepted solutions (1)
367 Views
3 Replies
Replies (3)
Message 2 of 4

daltonNYAW9
Advocate
Advocate

You would probably want to write a script that searches through 'AllReferencedDocuments' for an assembly that way you have more control over how it works.

A simple option would be to add the rule to the event trigger 'After Open Document' for parts and assemblies since those sub-assemblies/parts will be opened in the background

daltonNYAW9_0-1731338869279.png

 

Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @b_berghuis.  Below is a different iLogic rule I just typed up, based on the process that your original code was doing.  It uses a separate, custom Sub routine to do the primary task, while the 'Main' area of the code calls that custom Sub routing to run for the 'active/current document, then also on all referenced documents (if there are any).  Since that routine includes a user prompt to allow it to continue working, it might not always be a great fit to use in a large assembly, because if that assembly has a ton of referenced documents, it could potentially prompt you a ton of times also.  I opted to stick with primarily Inventor API code for this task, instead of specifically iLogic code, due to all the potential document references that could be involved.  So, the only true iLogic in this code below is the "ThisDoc.Document" code phrase being used in Line 2.

I added comments into this code, to help you follow what is going on.

Sub Main
	'get current document (document type does not matter)
	Dim oDoc As Inventor.Document = ThisDoc.Document
	'run the custom Sub routine on this document
	CompareFileNameWithPartNumber(oDoc)
	'iterate through all referenced documents
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		'run the custom Sub routine on this referenced document
		CompareFileNameWithPartNumber(oRefDoc)
	Next
End Sub

Sub CompareFileNameWithPartNumber(oDoc As Inventor.Document)
	'if this document has not been saved yet, it will not have a file name yet
	If oDoc.FileSaveCounter = 0 Then Return 'one line If statement, no End If needed
	'get this specific document's file name only (no file path, no file extension)
	Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	'get the Inventor.Property object (iProperty) for Part Number (using Index positions)
	Dim oPartNumProp As Inventor.Property = oDoc.PropertySets.Item(3).Item(2)
	'get the value of this iProperty (Part Number), which will be a String
	Dim sPartNumber As String = oPartNumProp.Value
	'compare the two values...if they match, then we're done (nothing to do)
	If sFileName = sPartNumber Then Return 'one line If statement, no End If needed
	'if code reaches here, then those two values did not match
	'prepare the message
	Dim sReport As String = "FileName = " & sFileName & vbCrLf & _
	"Part Number = " & sPartNumber & vbCrLf & _
	"Do you want to fix this?"
	'show the pop-up dialog to ask question
	Dim oAns = MessageBox.Show(sReport, "Fix Part Number?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
	'check answer of question
	If oAns = MsgBoxResult.Yes Then
		'set value of the Part Number iProperty to this Document's file name
		oPartNumProp.Value = sFileName
	End If 'end of answer check
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 4 of 4

b_berghuis
Explorer
Explorer

Hello Wesley,

Thanks for the answer. I like the method of using the first sub as the part that runs through all the documents, and the second sub as the function.

 

After I posted this I kept working on it and got some of the way there but I was missing a couple details. I'll be pulling what you wrote apart to see if I can get mine to work.

 

Thanks again

0 Likes