Mating with .NET and Named Faces

Mating with .NET and Named Faces

Anonymous
Not applicable
728 Views
8 Replies
Message 1 of 9

Mating with .NET and Named Faces

Anonymous
Not applicable

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. 

0 Likes
Accepted solutions (1)
729 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

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).

0 Likes
Message 4 of 9

Anonymous
Not applicable

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. 

Message 5 of 9

J_Dumont
Advocate
Advocate

Hi @JhoelForshav 

Thank you for providing this code. I am trying to use it, but my code is failing at CreateGeometryProxy.

Any help would be greatly appreciated.

I'm using Inventor 2024 and VS 2022

 

Below is my Subroutine. I have commented out many lines until I can troubleshoot the issue.

    Public Sub RemoveableCores()
        ' Define component file paths
        Dim AssyPathName As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
        Dim asmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition

        ' Define component file paths
        Dim compPath1 As String = AssyPathName + "\" + "iCoreStrap.ipt"

        ' Add components to assembly
        Dim occ1 As ComponentOccurrence = asmCompDef.Occurrences.Add(compPath1, g_inventorApplication.TransientGeometry.CreateMatrix())
        Dim occ2 As ComponentOccurrence = asmCompDef.Occurrences.ItemByName("Frame S2")
        Dim occ3 As ComponentOccurrence = asmCompDef.Occurrences.ItemByName("Frame L1")

        ' Get named faces for Strap
        Dim Strap_face1 As Face = GetNamedFace(occ1.Definition.Document, "LeftFace1")
        'Dim Strap_face2 As Face = GetNamedFace(occ1.Definition.Document, "TopFace")
        'Dim Strap_face3 As Face = GetNamedFace(occ1.Definition.Document, "FrontFace")
        Dim Strap_FaceProx1 As FaceProxy
        'Dim oStrap_FaceProx2 As FaceProxy
        'Dim oStrap_FaceProx3 As FaceProxy
        occ1.CreateGeometryProxy(Strap_face1, Strap_FaceProx1)
        'occ1.CreateGeometryProxy(Strap_face2, oStrap_FaceProx2)
        'occ1.CreateGeometryProxy(Strap_face3, oStrap_FaceProx3)

        ' Get named faces for Frame S2
        Dim FrameS2_face1 As Face = GetNamedFace(occ2.Definition.Document, "BShortInsideFace")
        Dim FrameS2_Prox1 As FaceProxy
        'occ2.CreateGeometryProxy(FrameS2_face1, FrameS2_Prox1)

        ' Get named faces for Frame L1
        'Dim FrameL1_face1 As Face = GetNamedFace(occ3.Definition.Document, "BLongBottomFace")
        'Dim FrameL1_face2 As Face = GetNamedFace(occ3.Definition.Document, "Face0")

        'Constrain strap to Frame S2
        asmCompDef.Constraints.AddMateConstraint(Strap_FaceProx1, FrameS2_Prox1, 0) ' InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference)
        'asmCompDef.Constraints.AddMateConstraint(Strap_face2, FrameL1_face1, 0) ', InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference)
        'asmCompDef.Constraints.AddMateConstraint(Strap_face3, FrameL1_face2, 0) ', InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference)

    End Sub

Here is function for getting the named faces.

    ' Function to get a named face from a component occurrence
    Function GetNamedFace(occ As Inventor.Document, faceName As String) As Face

        Dim attribMgr As AttributeManager = occ.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

 

Here is the error I get.

J_Dumont_0-1739642701591.png

 

The following images show the named entities in the first two part files.

J_Dumont_1-1739642811612.png

J_Dumont_3-1739643286425.png

 

 

 

 

 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

Hi @J_Dumont Sorry if I'm butting in here, but on Line 15 of the code you posted above,

Dim Strap_face1 As Face = GetNamedFace(occ1.Definition.Document, "LeftFace1")

...where you are specifying the name of the face for it to find, you specified "LeftFace1", but in the first image of named entities you posted, there is a 'similarly' named entity which does not have "1" at the end of its name.  So, if Line 15 was not able to set a value to your variable named "Strap_face1", then Line 21 where you have the following line of code:

occ1.CreateGeometryProxy(Strap_face1, Strap_FaceProx1)

would throw an error, as it seems it did here, due to an 'argument' (input) related problem.  So, this error may just be due to one little typing mistake or misspelling.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

J_Dumont
Advocate
Advocate

Hi @WCrihfield 

Thanks for the reply.

Great catch. I did notice that earlier, but unfortunately, I pasted the wrong code in the editor.

 

Here's my updated code and the error that I get.

    Public Sub RemoveableCores()

        ' Assembly file path
        Dim AssyPathName As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
        Dim asmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition

        ' Define component file paths
        Dim compPath1 As String = AssyPathName + "\" + "iCoreStrap.ipt"

        ' Add components to assembly
        Dim occ1 As ComponentOccurrence = asmCompDef.Occurrences.Add(compPath1, g_inventorApplication.TransientGeometry.CreateMatrix())
        Dim occ2 As ComponentOccurrence = asmCompDef.Occurrences.ItemByName("Frame S2")

        ' Get named faces for Strap
        Dim Strap_face1 As Face = GetNamedFace11(occ1.Definition.Document, "LeftFace")

        'Set Proxy faces for strap
        Dim Strap_FaceProx1 As FaceProxy
        occ1.CreateGeometryProxy(Strap_face1, Strap_FaceProx1)

        ' Get named faces for Frame S2
        Dim FrameS2_face1 As Face = GetNamedFace11(occ2.Definition.Document, "BShortInsideFace")
        Dim FrameS2_Prox1 As FaceProxy
        occ2.CreateGeometryProxy(FrameS2_face1, FrameS2_Prox1)

        'Constrain strap to Frame S2
        asmCompDef.Constraints.AddMateConstraint(Strap_FaceProx1, FrameS2_Prox1, 0) ' InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference)

    End Sub

    ' Function to get a named face from a component occurrence
    Function GetNamedFace11(occ As Inventor.Document, faceName As String) As Face

        Dim attribMgr As AttributeManager = occ.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

J_Dumont_0-1739806087679.png

 

 

 

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

OK.  Well since it it still an ArgumentException for the same line of code, and the second argument is just an empty variable for it to assign a value to, then there must still be something wrong with the first argument (the "Stap_face1" variable), and I can only think of two possibilities.  Either it still did not find that named Face for some reason, of the thing it found was not a Face type object, so it can not Cast something that is not a Face (Vertex or Edge for example) to a FaceProxy.  But if the second thing was the case, it seems like it would have been a Cast related error (more specific), and there probably would have been an error where that variable's value got set, before it even got to that point.  So, you may need to include some extra lines of code that check if that variable got a Value assigned to it after that 'GetNamedFace11' method was called.  That method does have the ability/option to return 'Nothing', so it's always good practice to check things out after calling it.

 

Edit:  Wait...I see it now.  It is within the 'GetNamedFace11' Function.  Its 'input parameter' is named "faceName", but then a few lines into it, you are checking "Name" variable instead.  The variable names are spelled differently.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

J_Dumont
Advocate
Advocate

AHH, darn, I missed that. It takes a village..lol

 

Thank you so much!!!

0 Likes