Problem placing of parts and constraints via api

Problem placing of parts and constraints via api

COLINBRUSCHI
Enthusiast Enthusiast
594 Views
6 Replies
Message 1 of 7

Problem placing of parts and constraints via api

COLINBRUSCHI
Enthusiast
Enthusiast

Hi All,

 

Below is some extract code, a user selects some edges via the commandmanager.pick method, these edge objects are stored in an object collection. A for loop indexes through the edges and places and constrains parts to these edges. The code will inconsistently manage to insert a part and constrain, sometimes failing to insert, sometimes inserting but then failing to constrain.

 

When it fails to insert a part, "operation aborted" followed by Exception from HRESULT: 0x80004005 (E_FAIL)

When it fails to constrain, Exception from HRESULT: 0x80004005 (E_FAIL)

 

Any ideas?

 

 

Dim assemDoc As AssemblyDocument = g_inventorApplication.ActiveDocument                                                 Dim oAsmCompDef As AssemblyComponentDefinition = assemDoc.ComponentDefinition   
Dim oCylEdgeA As Edge
Dim userSelection As ObjectCollection = g_inventorApplication.TransientObjects.CreateObjectCollection()
Dim oOcc1 As ComponentOccurrence
Dim oInsert1 As InsertConstraint

While True

oCylEdgeA = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeCircularFilter, "Select Hole(s), ESC to Finish")

                If oCylEdgeA Is Nothing Then

                    If userSelection.Count = 0 Then
                      
                        Show()
                        Refresh()

                        Exit Sub

                    End If

                    Exit While

                Else

                    ''''' Add selected edges to collection
                    userSelection.Add(oCylEdgeA)
                   
                End If

            End While


For Each oCylEdge1 as edge In userSelection

oOcc1 = oAsmCompDef.Occurrences.Add(sFile1, oMatrix) 

                Dim i = 1

                For Each iMateDefinition In oOcc1.iMateDefinitions

                    If FeatureType = True Then  ' non csk
                        If iMateDefinition.Name = "Insert In1" Then
                            iMate1 = oOcc1.iMateDefinitions.Item(i)
                            Exit For
                        End If
                    Else 'csk
                        If iMateDefinition.Name = "Align1" Then
                            iMate1 = oOcc1.iMateDefinitions.Item(i)
                            Exit For
                        End If
                    End If
                    i = i + 1
                Next



oInsert1 = oAsmCompDef.Constraints.AddInsertConstraint(oCylEdge1, iMate1, True, 0)


next

 

 

0 Likes
Accepted solutions (1)
595 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Is this just one portion of a larger iLogic rule?

Where is the Type & Value of "sFile1" set?

Where is the Type & Value of "oMatrix" set?

Where is "FeatureType" defined? (Used in  "If FeatureType = True Then" line of code.)

What are "Show()" and "Refresh()" doing?

I wasn't aware that the second 'Entity' in a AddInsertConstraint could be an iMateDefinition.

Are you sure this line is working?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

COLINBRUSCHI
Enthusiast
Enthusiast

Yes I've just extracted code for brevity.

 

sFile1 is assigned a string path and mMatrix is defined with this:

 

Dim oTG = g_inventorApplication.TransientGeometry
Dim oMatrix = oTG.CreateMatrix
oMatrix.SetTranslation(oTG.CreateVector(X, Y, Z))

 

Featuretype is boolean and defines whether it should be an edge or face feature to constrain to. 

 

Show() & Refresh() are making the form visible (it's an addin)

 

Yep, addinsertconstraint works an imate. I'm adapting this code from my program which inserts & constrains one set of parts at a time, this is just allowing for a selection set upfront, and should then constrain the parts in the same way

 

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Without knowing the full extent of the surrounding code, I inserted some Dim's and Sub's to get this section of code to not throw errors when I save it.  But here's my re-formatted version of your code.  See if this helps to eliminate any of the errors you are getting.

 

Sub Main
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition

	Dim oCylEdgeA As Edge
	Dim oObjCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

	While True
		oCylEdgeA = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeCircularFilter, "Select Hole(s), ESC to Finish")
		If oCylEdgeA Is Nothing Then
			If oObjCol.Count = 0 Then
				Show()
				Refresh()
				Exit Sub
			End If
			Exit While
		Else
			'Add selected edges to collection
			oObjCol.Add(oCylEdgeA)
		End If
	End While
	
	Dim oFeatureType As Boolean
	Dim oOcc1 As ComponentOccurrence
	Dim oiMate1 As iMateDefinition
	Dim oInsert1 As InsertConstraint
	For Each oCylEdge1 As Edge In oObjCol
		oOcc1 = oADef.Occurrences.Add(sFile1, oMatrix) 
		Dim i = 1
		For Each iMateDefinition In oOcc1.iMateDefinitions
			If oFeatureType = True Then  ' non csk
				If iMateDefinition.Name = "Insert In1" Then
					oiMate1 = oOcc1.iMateDefinitions.Item(i)
				End If
			Else 'csk
				If iMateDefinition.Name = "Align1" Then
					oiMate1 = oOcc1.iMateDefinitions.Item(i)
					Exit For
				End If
			End If
			i = i + 1
		Next
		oInsert1 = oADef.Constraints.AddInsertConstraint(oCylEdge1, oiMate1, True, 0)
	Next
End Sub

Public Sub Show()
	'Show Form code here
End Sub

Public Sub Refresh()
	'Refresh Form code here
End Sub

 

I hope this helps.
If this solves your problem, or answers your questions, please click 'Accept As Solution".
Or, if this helps you reach your goal, please click 'LIKES" 👍.

 

Also, if you're interested, here are a few of the 'Ideas' I'd like to get implemented.
If you agree with any of them, please vote for them.

  • MessageBox, InputBox, and InputListBox Size & Format Options 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

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

COLINBRUSCHI
Enthusiast
Enthusiast

Thanks, thats pretty much my exact code, it runs fine until a second or third part is inserted. I suspect it might be trying to constrain the part before its been inserted into the assembly. Is there a way to test for when a part has been loaded into the assembly?

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

It's going to be difficult to diagnose that problem, without having the whole thing to experiment with.

Since this code isn't actually inserting any components, I just have to assume the components are already present within the main assembly when it gets ran; and that those iMateDefinitions are already defined and present.

Perhaps you need another 'ElseIf' section within your loop which searches for the named iMateDefinitions, that gives another option, in case it doesn't find either one of them.

I have to assume that either one of those iMateDefinitions will work with your selection.

As for the timing issue...unfortunately, I haven't done enough with 'Events' yet to be much help with that question.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

COLINBRUSCHI
Enthusiast
Enthusiast
Accepted solution

Thanks for the help. It turned out to be a timing related bug with command manager pick method and the ESC keydown and keyup events. If using a for - to loop instances of selection can be skipped, in my case the exiting of the while loop caused a null selection or some other strange selection, giving an exception later on. The solution is to create a pause  while allowing for continuation of other events with do events() , thanks to Brian Ekins

 

https://forums.autodesk.com/t5/inventor-customization/commandmanager-pick-bug-with-esc-key/td-p/5337...

https://ekinssolutions.com/selecting-multiple-entities-using-pick-in-inventor/

0 Likes