- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to programmatically mate parts using VB.NET. All of the examples I have found involve the user selecting the faces or other elements that want to be mated as part of the program. I am hoping to instead do it fully programmatically but I don't see any examples of how this can be done. I have tried using the "Assign Name" function and then referencing that later on, but I don't see the AttributeSet show up in the assembly under the component occurrence, only in the part file itself. Does anyone have a recommendation for how I can name a few faces for a part and then programmatically mate them and whatnot inside of an assembly? Thank you in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here's some quickie code using 'NamedEntities', you may find useful in this situation. It's likely much easier to use the 'NamedEntities' than it is to search through all the Attributes to find the right one.
'oDoc can be either the active document,
'or the Document of a ComponentOccurrence in an assembly,
'or the oRefDoc of the assembly's AllReferencedDocuments.
Dim oNEntities As NamedEntities = iLogicVb.Automation.GetNamedEntities(oDoc)
Dim oMap As NameValueMap = oNEntities.Entities
'You can define these objects as whatever type of object they really are instead of just Object
Dim oObj1 As Object
Try
oObj1 = oNEntities.FindEntity("Name")
Catch
MsgBox("Coundn't find an entity in that document with that name.")
End Try
Dim oName As String = oNEntities.GetName(oEntity)
If oNEntities.NameExists("Name") Then
MsgBox("That names exists.")
End If
oNEntities.SetName(oEntity, "Name")
'This is the same as FindEntity, but can return Nothing instead of an Exception.
Dim oObj2 As Object = oNEntities.TryGetEntity("Name")
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @Anonymous
From what I can tell, you can only name faces in parts, not assemblies. I guess because the faces doesn't really exist in the assembly. Whenever you work with a face on assembly level Inventor actually creates a proxy for that face through the occurrence. So we have to find the named face in the part, then create a proxy for it and then use it in the Mate constraint.
See example below. I've placed two occurrences of the same part (Part5). So the occurrences are called Part5:1 and Part5:2. I use the document from one of the occurrences to get the Face in the part named "Mate1". Then I create proxies for that face for each part.
I then use these proxies to constrain the faces together ![]()
Sub Main Dim oAsm As AssemblyDocument = ThisDoc.Document Dim oAsmDef As AssemblyComponentDefinition = oAsm.ComponentDefinition 'Get the occurrences Dim oOcc1 As ComponentOccurrence = oAsmDef.Occurrences.ItemByName("Part5:1") Dim oOcc2 As ComponentOccurrence = oAsmDef.Occurrences.ItemByName("Part5:2") 'Find the named face (object in part document) Dim oFace As Face = GetNamedEntity(oOcc1.Definition.Document, "Mate1") 'Create Proxies for that face through each of the occurrences. Dim oFaceProx1 As FaceProxy Dim oFaceProx2 As FaceProxy oOcc1.CreateGeometryProxy(oFace, oFaceProx1) oOcc2.CreateGeometryProxy(oFace, oFaceProx2) 'Create a mate constraint oAsmDef.Constraints.AddMateConstraint(oFaceProx1, oFaceProx2, 0) End Sub Public Function GetNamedEntity(doc As Inventor.Document, name As String) As Object Dim attribMgr As AttributeManager = doc.AttributeManager Dim objsFound As ObjectCollection objsFound = attribMgr.FindObjects("iLogicEntityNameSet", "iLogicEntityName", name) If objsFound.Count > 0 Then Return(objsFound.Item(1)) Else Return(Nothing) End If End Function
I'll also attach the assembly with this post (Inv 2020).
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thank you both for the support and the quick responses. @JhoelForshav provided the exact code I needed to resolve my issue and a great explanation. Thank you very much.