iLogic Occurrence.Add or replace with CommandManager.Pick

iLogic Occurrence.Add or replace with CommandManager.Pick

ThomasB44
Mentor Mentor
1,500 Views
4 Replies
Message 1 of 5

iLogic Occurrence.Add or replace with CommandManager.Pick

ThomasB44
Mentor
Mentor

Hi, I have a strange behavior with some of my rules I created years ago with IV2014.

The rule allow user :

  1. to pick some component(s)
  2. Add them to a selection
  3. Then copy these components in pattern following user entries
  4. Or replace components following user entry

But, when adding the collection using the SelectMultiple, the command Occurrences.Add or Replace fails.

The error message :

System.Runtime.InteropServices.COMException (0x80004005): Erreur non spécifiée (Exception de HRESULT : 0x80004005 (E_FAIL))
   à System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   à Inventor.ComponentOccurrences.Add(String FullDocumentName, Matrix Position)
   à ThisRule.Main()
   à Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   à iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

If I add a simple message box command (anywhere), it works well !!! Smiley Indifferent

If user select components first, then run the rule with oSelSet = oDoc.SelectSet, it works well too.

 

What happen here ? Smiley Sad

 

Here an example of code :

Dim oDoc As AssemblyDocument
oDoc = ThisDoc.Document
Dim oOcc As ComponentOccurrence
Dim oOccCol As Inventor.ObjectCollection
oOccCol = ThisApplication.TransientObjects.CreateObjectCollection

Do
	'Ask user for a selection
	oOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a component :")
	If Not oOcc Is  Nothing Then
	'Add component to collection
	oOccCol.Add(oOcc)
	'Set selected component inactive
	oOcc.Enabled = False
	End If
Loop While Not oOcc Is Nothing 
'Add collection to selection
oDoc.SelectSet.SelectMultiple(oOccCol)
oSelSet = oDoc.SelectSet

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oDoc.ComponentDefinition
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim oMatrix As Matrix
oMatrix = oTG.CreateMatrix
Dim Coords(2) As Double
Dim FullFileName As String

For Each oOcc In oSelSet
	'Set selected component active
	oOcc.Enabled = True
	'Get the full file name of selected occurence
	FullFileName = oOcc.Definition.Document.FullFileName
	'Get occurence coordinates
	oMatrix = oOcc.Transformation
	oMatrix.Translation.GetVectorData(Coords)'in cm
	'Copy occurence according to user parameters
	For x = 2 To 4
		'Set new matrix
		'''MessageBox.Show(FullFileName, "Title")
		oMatrix.SetTranslation(oTG.CreateVector( _
		Coords(0) + (x - 1) * 500 / 10, _
		Coords(1), _
		Coords(2)))
		oOcc = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix)
		oOcc.Grounded = True
	Next
Next

Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

0 Likes
1,501 Views
4 Replies
Replies (4)
Message 2 of 5

philip1009
Advisor
Advisor

à Inventor.ComponentOccurrences.Add(String FullDocumentName, Matrix Position)

 

This tells me that it's failing at this line at the bottom of the loop:

oOcc = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix)

If the FullFileName is correct in the message box you have coming up we don't have to worry about that.  Can you insert another message box after doing the matrix translation to see if the resulting Matrix data is correct?

 

I'm not seeing any API that needs to be changed, maybe there's some extra VB work that has to be done before calling that Matrix.

0 Likes
Message 3 of 5

ThomasB44
Mentor
Mentor

Hi @philip1009

I've put a message box after this command line, but the FullFileName and the matrix are correct before and after...


Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

0 Likes
Message 4 of 5

ThomasB44
Mentor
Mentor

If I had a double Try/Catch it will work...

Strangely, the error occurs, as it should not inside the Try/Catch, and the code continue to run.

Try
oOcc = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix)
Catch
	Try
	oOcc = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix)
	Catch
	End Try
End Try

Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

Message 5 of 5

maxim.teleguz
Advocate
Advocate

this deserves more likes. Works in 2020

0 Likes