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

iLogic - Iterate parts with button

ChristianAndersenIsmyname
Advocate

iLogic - Iterate parts with button

ChristianAndersenIsmyname
Advocate
Advocate

Hello,

In a assembly, I want a Form window similar to my image below.

ChristianAndersenIsmyname_1-1596116160381.png

 

I've found multiple codes that lets you iterate all parts and change all properties to a fixed value.

This forum post foe example have a code that does that.

 

However, I want to manually cycle through when I press "Next Part" button as shown in the picture above.

 

What would be the best approach to make a code that does this?

Should the "Next Part" button Apply changes to the properties, or would you have the "Write" button at the bottom to apply the changes, while next part only cycles through the parts?

 

Thanks for any answer.

 

0 Likes
Reply
454 Views
5 Replies
Replies (5)

WCrihfield
Mentor
Mentor
  • Is this form an iLogic form, and not a Windows.Forms.Form?
  • If this for is an iLogic form, is it stored locally within the assembly document, or is it a Global Form?
  • If it is a VBA UserForm, is its module within the assembly document's project, or within the main default application project.
  • Do you only want to cycle through the PartDocuments that are within the active assembly, or do you want to cycle through sub-assemblies too.
  • If you want to cycle through sub-assemblies, do you also want to cycle through each part within each level of each sub-assembly. How many layers deep do you want to go within the sub-assemblies?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

I'm just assuming this is a global iLogic form with for the following attempt at a solution.

I have written an iLogic code that I'm hoping will do what you want it to.

Because an iLogic form is always acting on whatever document is "Active" at the time it is launched, I couldn't identify the assembly as ThisDoc.Document or ThisApplication.ActiveDocument, because we want the 'active' document to be one of the documents referenced within the assembly, when it is launched (at least for the second time it is launched).

As you said before, this is likely new ground I'm attempting to cover here, so bare with me.

 

The first section of the code attempts to make sure it is referencing the correct assembly file.

If it is the first time you run the rule, it will likely ask you about the assembly file, but after the first run, it should have saved the assembly's file name to a SharedVariable, for use the next time.

If the shared variable isn't found, it will first ask you if the assembly document is the active document.

If so, it sets oADoc to the active document, but if not, it proceeds to show you the Open file dialog box for you to select it.

Once the assembly file if found and the oADoc variable's value is set correctly, it proceeds to the Cycle portion.

 

The cycle portion attempts to activate the next referenced document within the assembly, using another SharedVariable to store & retrieve an Integer which specifies which document it worked with last time.

 

The last section of the code launches the form.

It should wait for you to do what you want to do within that form.

Then, when the form is closed, the code attempts to determine how the form was closed.

If it was closed due to you clicking on the specific rule button, it will proceed with the rest of the rule, to cycle to the next reference document within the assembly.

If it wasn't closed by that rule button, it simply exits the code.

 

It's the end of the day here, and it's still untested, but go ahead and give it a try:

 

'[ Get the main assembly
GetAssembly :
Dim oADoc As AssemblyDocument
Dim oAsmName As String
Dim oFileDlg As Inventor.FileDialog = Nothing
If SharedVariable.Exists("FullDocumentName") Then
	oAsmName = SharedVariable.Value("FullDocumentName")
	Try
		oADoc = ThisApplication.Documents.ItemByName(oAsmName)
	Catch
		MsgBox("Main assembly was not open." & vbCrLf & _
		"Attempting to open it now.",vbOKOnly,"ASSEMBLY NOT OPEN")
		oADoc = ThisApplication.Documents.Open(oAsmName)
	End Try
Else
	oActive = MsgBox("Is the assembly the active document?", vbYesNo + vbQuestion, "ACTIVE ASSEMBLY?")
	If oActive = vbYes Then
		oADoc = ThisApplication.ActiveDocument
		GoTo Cycle
	Else
		GoTo FileDialog
	End If
End If

FileDialog :
Try
	'Set value for oAsmName
	'This can be left manual, or as an InputBox, or you can create a FileOpen dialog box here.
	iLogicVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
	oFileDlg.DialogTitle = "Select the assembly file"
	oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDlg.CancelError = True
	oFileDlg.ShowOpen()
	If Err.Number <> 0 Then
		MsgBox("File not chosen.", vbOKOnly, "Dialog Cancellation")
	ElseIf oFileDlg.FileName <> "" Then
		oAsmName = oFileDlg.FileName
		Try
			oADoc = ThisApplication.Documents.ItemByName(oAsmName)
		Catch
			MsgBox("Main assembly was not open." & vbCrLf & _
			"Attempting to open it now.",vbOKOnly,"ASSEMBLY NOT OPEN")
			oADoc = ThisApplication.Documents.Open(oAsmName)
		End Try
	End If
Catch oEx As Exception
	MsgBox("Could not find file name of the main assembly. Exiting.", vbOKOnly, "NO ASSEMBLY FILE NAME")
End Try
']

'[ Cycle to the next referenced document within the assembly and activate it
Cycle :
SharedVariable.Value("FullDocumentName") = oADoc.FullDocumentName
Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
Dim oIndex As Integer
Dim oLastOne As Integer
'Check SharedVariable for Index from last time
If SharedVariable.Exists("Index") Then
	oLastOne = SharedVariable.Value("Index")
	oIndex = oLastOne + 1
Else
	oIndex = 1
End If
Try
	Dim oRefDoc As Document = oRefDocs.Item(oIndex)
	If oRefDoc.Open = False Then
		ThisApplication.Documents.Open(oRefDoc.FullDocumentName, False)
	End If
	oRefDoc.Activate
	SharedVariable.Value("Index") = oIndex
	GoTo ShowForm
Catch oEx As Exception
	MsgBox("You've reached the end of the referenced documents. Exiting.", vbOKOnly, "NO NEXT PART")
	Return
End Try
']

'[  Show the form, hopefully using the activated referenced document
ShowForm :
Dim oFRV As FormReturnValue = iLogicForm.ShowGlobal("Check iProperties")
Select Case oFRV.Result
	Case FormResult.Cancel
		Return
	Case FormResult.Close
		Return
	Case FormResult.Done
		Return
	Case FormResult.None
		Return
	Case FormResult.OK
		Return
	Case FormResult.RuleButtonApplyAndClose
		If oFRV.RuleName = iLogicVb.RuleName Then
			'The button that closed the form was the one to run this rule
			GoTo Cycle
		End If
	Case FormResult.RuleButtonClose
		If oFRV.RuleName = iLogicVb.RuleName Then
			'The button that closed the form was the one to run this rule
			GoTo Cycle
		End If
End Select
']

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE" :thumbs_up:.

Also, when you have time, please review & vote for these 'Ideas' I'd like to get implemented.

  • Add more capabilities to the 'Customize' dialog box (exe. Add Tab & Add Panel) Click Here
  • Constrain & Dimension Images In Assembly Sketches & Drawing Sketches (TitleBlocks & SketchedSymbols) Click Here
  • Save Section View Status In DesignViewRepresentation (So It Can Be Used In The Drawing) Click Here
  • Add SolidBodies Folder In iLogic Rule Editor Model Tab Click Here
  • Convert All Views To Raster Before Autosave Stores To 'OldVersions' Folder Click Here
  • SetDesignViewRepresentation - Fix limitations for DrawingView of a Part Click Here
  • Create DocumentSubTypeEnum Click Here
  • Add kRevisionTag or kDrawingRevisionTag to ObjectTypeEnum Click Here

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

ChristianAndersenIsmyname
Advocate
Advocate
  • This is a iLogic form, but im open for suggestions
  • I assume the code should be written in Assembly, and not global. Global (external) rules can not have «apply, close, run code» setting In forms?
  • I dont want this to be VBA, as VBA seems a lot slower to open excel than iLogic. But VBA is better than nothing.
  • Sub-assemblys iProperties are irrelevant, so parts within sub-assembly would be best.
0 Likes

ChristianAndersenIsmyname
Advocate
Advocate

Holy, you’re fast at writing codes.

 

I will have to test this tomorrow when I get back to work, but from what you explained, this looks very promising.

0 Likes

ChristianAndersenIsmyname
Advocate
Advocate

I'm not sure what your code did, or If I just don't understand how to use it.

However, I think maybe your code is a bit too complicated for its use? Maybe I'm wrong.

 

I've looked a bit around and found out that this short code does almost what I need it to do.

 

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oOccurrence As ComponentOccurrence

For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef) 
	
iProperties.Value("Custom", "Temp PartNum") = iProperties.Value(oOccurrence.Name, "Project", "Part Number")

Next

 

Now I just need the 'Next' at the end to wait for user input istead of being automatic. I guess this is what you did with following code:

 

Dim oIndex As Integer
Dim oLastOne As Integer
If
	oIndex = oLastOne + 1
Else
	oIndex = 1

 

 

With the code first code in this post, I have a custom property called "Temp PartNum", which will tell the user which part is currently active for edits.

Skjermbilde.PNG

 

If I get this "next part" (the code above) button to work, all others iProperties I will be able to fix myself.

0 Likes