Mate 2 parts in assembly, one by face name and one by clicking it

Mate 2 parts in assembly, one by face name and one by clicking it

valyyg
Explorer Explorer
710 Views
9 Replies
Message 1 of 10

Mate 2 parts in assembly, one by face name and one by clicking it

valyyg
Explorer
Explorer

I want to run a rule that can add a part to assembly, and automate mate by clicking a face.

I don't know how to read a face name from a part.

I wrote this:

 

Dim componentA = Components.Add("perete_intermediar", "perete_intermediar.ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing)
Dim oFace1 As Face = 
Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick a face. press Esc to cancel")
Dim oAsm As AssemblyDocument = ThisDoc.Document
oAsm.ComponentDefinition.Constraints.AddMateConstraint(oFace1, oFace2, 0)

 

how can i define oFace1 to select edge_jos from perete_intermediar ?

i want to mate edge_jos from perete_intermediar with a selected face by clicking it

 

 

0 Likes
Accepted solutions (1)
711 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @valyyg.  Here is something you may be able to use.  I kept it fairly brief, since that appears to be the theme.  There are 2 main ways to get 'named entities' from a document.  One is by using the iLogic specific 'NamedEntities' interface.  The other is a much longer and more complex bit of code that filters through all the Attributes in the document, looking for the ones with specific names and values.  There are two ways to access the NamedEntities interface...one way is shown above, the other is 'ThisDoc.NamedEntities', but that one will only work on the 'active/local' document, not some other document.

Dim componentA = Components.Add("perete_intermediar", "perete_intermediar.ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing)
Dim oCompADoc As Document = componentA.Occurrence.ReferencedDocumentDescriptor.ReferencedDocument
oNEs = iLogicVb.Automation.GetNamedEntities(oCompADoc)
oEntity = oNEs.TryGetEntity("edge_jos ")
If IsNothing(oEntity) Then Exit Sub
Dim oFace1 As Face = oEntity
Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick a face. press Esc to cancel")
Dim oAsm As AssemblyDocument = ThisDoc.Document
oAsm.ComponentDefinition.Constraints.AddMateConstraint(oFace1, oFace2, 0)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

valyyg
Explorer
Explorer

Thanks!

 

For the moment is not working 😞

2.jpg

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

OK.  You may have an older version of Inventor, where that specific method under the NamedEntities interface did not exist yet.  That's OK, because there are is another method under it you should be able to use.  You can use oNEs.FindEntity("edge_jos ") instead of TryGetEntity().  But that one may throw an error if the object is not found.  There is also a method under the NamedEntities interface called NameExists("EntityName"), which can be used to check if that name can be found, before using the other method which might throw the error if not found.  But I'm not sure which version of Inventor that NameExists method was introduced.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

valyyg
Explorer
Explorer

Still error

valyyg_0-1668795855929.png

 

I use inventor pro 2020

0 Likes
Message 6 of 10

valyyg
Explorer
Explorer

valyyg_0-1668796062125.png

 

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

That error message indicates that the code is encountering the error at the line where it is trying to create the Mate constraint, so that would be the last line.  Here try this version of the code (below).  I added in several bits of code to try to catch the error, or find out why it is failing.

Dim componentA = Components.Add("perete_intermediar", "perete_intermediar.ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing)
Dim oCompADoc As Document = componentA.Occurrence.ReferencedDocumentDescriptor.ReferencedDocument
oNEs = iLogicVb.Automation.GetNamedEntities(oCompADoc)
Dim oEntityName As String = "edge_jos"
Dim oEntity As Object = Nothing
If oNEs.NameExists(oEntityName) = False Then
	MsgBox("Could not find any entity in the document named:  " & oEntityName, vbCritical, "")
	Exit Sub
Else
	Try
		oEntity = oNEs.FindEntity(oEntityName)
	Catch
		MsgBox("Error trying to retrieve named entity from document.", vbCritical, "")
		Exit Sub
	End Try
End If
If IsNothing(oEntity) Then
	MsgBox("No entity was retrieved from the document.", vbCritical, "")
	Exit Sub
End If
Dim oFace1 As Face = oEntity
Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick a face. press Esc to cancel")
Dim oAsm As AssemblyDocument = ThisDoc.Document
Try
	oAsm.ComponentDefinition.Constraints.AddMateConstraint(oFace1, oFace2, 0)
Catch oEx As Exception
	MsgBox("AddMateConstraint method failed" & vbCrLf & _
	oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "")
End Try

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

valyyg
Explorer
Explorer

O think is the same error

valyyg_0-1668798737097.png

 

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Ohhh...  I forgot that the face it is retrieving from the other document, is in the context of that other document, and the assembly constraint needs a reference to that face in the context of the main assembly...so it needs a FaceProxy, instead of a regular Face.  I think I know what it is missing now.  My brain is getting tired today.😪

Here try this:

Dim componentA = Components.Add("perete_intermediar", "perete_intermediar.ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing)
Dim oCompADoc As Document = componentA.Occurrence.ReferencedDocumentDescriptor.ReferencedDocument
oNEs = iLogicVb.Automation.GetNamedEntities(oCompADoc)
Dim oEntityName As String = "edge_jos"
Dim oEntity As Face = Nothing
If oNEs.NameExists(oEntityName) = False Then
	MsgBox("Could not find any entity in the document named:  " & oEntityName, vbCritical, "")
	Exit Sub
Else
	Try
		oEntity = oNEs.FindEntity(oEntityName)
	Catch
		MsgBox("Error trying to retrieve named entity from document.", vbCritical, "")
		Exit Sub
	End Try
End If
If IsNothing(oEntity) Then
	MsgBox("No entity was retrieved from the document.", vbCritical, "")
	Exit Sub
End If
'create the variable to hold the FaceProxy that the method below will get the reference to
Dim oFace1 As FaceProxy = Nothing
'now use this method to set the value to that variable we just created
componentA.Occurrence.CreateGeometryProxy(oEntity, oFace1)
Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick a face. press Esc to cancel")
Dim oAsm As AssemblyDocument = ThisDoc.Document
Try
	oAsm.ComponentDefinition.Constraints.AddMateConstraint(oFace1, oFace2, 0)
Catch oEx As Exception
	MsgBox("AddMateConstraint method failed" & vbCrLf & _
	oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "")
End Try

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 10

valyyg
Explorer
Explorer

Finally it works.
I was starting to go crazy, I didn't understand why such a simple action didn't want to work 🙂
Thank you very much, you are the best.

0 Likes