Autodesk Inventor api ilogic Auto Select Edge for Using constraints

Autodesk Inventor api ilogic Auto Select Edge for Using constraints

elmerblake1500
Observer Observer
1,149 Views
8 Replies
Message 1 of 9

Autodesk Inventor api ilogic Auto Select Edge for Using constraints

elmerblake1500
Observer
Observer

Is there a way to grab an edge from a model to use constraints using ilogic?  I know you can name an edge when you right click and name the entity/geometry but I'd like to  do this with ilogic code.

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

WCrihfield
Mentor
Mentor

Hi @elmerblake1500.  The simplest way to assign names to Face, Edge, or Vertex type objects in a PartDocument by code, is to use the NamedEntities object, and its SetName method.  Below is just a very short and very simple example, which expects that you have just pre-selected one of these manually, then ran this rule.

 

oEntity = ThisDoc.Document.SelectSet.Item(1)
ThisDoc.NamedEntities.SetName(oEntity, "Entity Name")

 

Another way to get that NamedEntities object is like this, which requires you to specify which Document to get them from.

 

Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oDoc)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

BlakeEZJTXL
Participant
Participant

Thanks but I'm more interested in doing this without manually preselecting an edge.  Here's the context of my code so far to illustrate what I'm trying to do 

Dim selectedEdge As Edge = Nothing
'Dim namedEntitiesDict3 As New Dictionary(Of String, Edge)

' Iterate through the edges of the flange part
For Each Bro As Edge In flangePartDef.SurfaceBodies.Item(1).Edges
    ' Get the start and end points of the edge
    Dim startPoint As Point = Bro.StartVertex.Point
    Dim endPoint As Point = Bro.StopVertex.Point

    ' Check if the edge lies on the YZ plane (both start and end points have an X-coordinate of 0)
    If startPoint.X = 0 AndAlso endPoint.X = 0 Then
        ' Save the reference to the selected edge
        selectedEdge = Bro
		MessageBox.Show("The edge is selected bro")
		
        Exit For ' Exit the loop after selecting the first edge parallel to the YZ plane
    End If
Next



If selectedEdge IsNot Nothing Then
'	Dim proxyEdge As Edge = Nothing
'    flangePartDef.CreateGeometryProxy(selectedEdge, proxyEdge)
'     Add the outer Edge To the custom dictionary With the name "outerEdge"
'    selectedEdge.Add
'ThisDoc.NamedEntities.SetName(selectedEdge,"outerEdge")


'     You can access the named Edge later Using namedEntitiesDict("outerEdge")
Else
    MsgBox("Outer edge not found.")
End If

' Close the flange part document
oFlangeDoc.Close(True)  

Basically I'm trying to iterate through all the edges in my surface body then save the edge that aligns to the yz plane to get the edge I'm interested in. Then after it's selected I attempt to name it but it seems there's not a method to do this other then manually selecting it.
0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

Hi @BlakeEZJTXL or @elmerblake1500.  This 'NamedEntities' system will only work in a PartDocument right now, not in an AssemblyDocument.  There is some limited functionality for it in a DrawingDocument, which is used for assigning names to dimensions when created by iLogic code.  Judging by a couple of the commented out lines of code in your post, it seems like you may be trying to find a named Edge within a part type component, while working within an assembly.  If that is the case, then you will need to use the 'iLogicVb.Automation.GetNamedEntities' route, then supply the Document object representing that referenced PartDocument object, because the 'ThisDoc' term will likely get confused with the main assembly.  And by the way, the NamedEntities.Entities property will return a NameValueMap object, which is similar to the Dictionary object, but maybe less strict.  If it has any entries, then the Name of an entry will be the name assigned to an object, then that entry's Value will be the object that that name was assigned to.  One entry for each object, if any.

 

There is a way to assign names to proxy objects within the context of an assembly by code, but it would not be using that 'NamedEntities' resource...it would need to be a custom code solution.  Behind the scenes, the 'NamedEntities' system just uses AttributeSets & Attributes.  Nearly every type of object that can be obtained from within a model has an AttributeSets property.  By default, there are zero AttributeSet objects in it, but we can add some.  Then within the AttributeSet object, we can add Attribute objects.  Each Attribute object has a Name, ValueType, and Value.  The 'NamedEntities' system uses a very specific name for the AttributeSet and Attribute object, that way it can find those specific ones again.  But you can use different names for those objects, as long as you keep track of those names, so you can find them again.  Then you can use the AttributeManager object, and its methods (obtained from Document.AttributeManager property) to find the objects that have been named that way.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

BlakeEZJTXL
Participant
Participant

You are correct although I'm trying to name an edge that hasn't been named yet.  I'm trying to name it so I can use the constraint command to grab the correct edge.  kind of like this 

Constraints.AddInsert("Insert:1", "ø5"" 150# RFSO FLANGE",
                          selectedEdge, "MK1:1", "Edge0",
                          axesOpposed := False, lockRotation := True)

It doesn't like the edge object though like I showed above. 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

OK.  If you will need to use that iLogic IManagedConstraints.AddInsert Method (the 'Constraints' term represents a IManagedConstraints Interface type object) then you would need to use the 'NamedEntities' resource to assign a name to that object Edge the part level before then, otherwise its name will not be recognized by that iLogic method.  However, if you would rather just specify objects directly, instead of assigning names to objects, then specifying the names of those objects, you could use the Inventor API methods, such as AssemblyConstraints.AddInsertConstraint or AssemblyConstraints.AddInsertConstraint2.  You can get the AssemblyConstraints object from AssemblyDocument.ComponentDefinition.Constraints property.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

BlakeEZJTXL
Participant
Participant

Understood I get the rule to work if I preselect two edges like in image 1 it then constrains it image 2 after running this rule I wrote.  

Dim selectSet As SelectSet = ThisApplication.ActiveDocument.SelectSet
'    selectSet.Select(selectedEdge)

    ' Get the number of edges selected
    Dim numSelectedEdges As Integer = selectSet.Count

    If numSelectedEdges < 2 Then
        MsgBox("Please select the second edge.")
    ElseIf numSelectedEdges > 1 Then
        ' Get the two edges from the select set.
        Dim oEdge1 As Edge = selectSet.Item(1)
        Dim oEdge2 As Edge = selectSet.Item(2)

        ' Set a reference to the assembly component definition
        Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition

        ' Create the insert constraint between the parts
        Dim oInsert As InsertConstraint = oAsmCompDef.Constraints.AddInsertConstraint(oEdge1, oEdge2, False, 0)
    Else
        MsgBox("No edge is selected.")
    End If
Else
    MessageBox.Show("No edge is selected.")
End If

 Although that's an extra step I'd like to avoid if I can name the edges somehow instead of having to preselect them that would be ideal for the script I'm working on.image1image1image2 after running preselect ruleimage2 after running preselect rule

0 Likes
Message 8 of 9

m_baczewski
Advocate
Advocate

Hi,

you can assign a name to an edge or plane. Just enter the part, find the edge or plane you're interested in, then you can assign a name.

 

m_baczewski_0-1712228833979.png

Then you can find this edge using iLogic/VB.

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Hi @BlakeEZJTXL.  I understand that you do not want to have to manually select the edges.  You essentially want a way to assign names to specific entities (like a Face, Edge, or vertex), but without manually selecting it, then you want to be able to use that iLogic shortcut snippet to add a constraints between those named entities, by it simply recognizing those names you have assigned to them.

 

The problem is that none of us know your models, your preferences for naming, or which entities should be named what names, like you do.  So one of us would not be able to write a complete working code solution for you to do this, exactly the way you would want, without knowing all that you know about those things.  Generally, the first steps in these types of processes is usually that the same person who will be writing the code for creating the constraints by code, will be the one who manually selects, and manually assigns specific, meaningful names to the entities that they are interested in using for their specific constraints.  Then they will know what names to specify within their code for the constraints.

 

One alternative would be to simply assign a relatively generic name to every entity in the model that can be assigned a name with that 'NamedEntities' system.  Then just familiarize yourself with which names are assigned to which entities before writing the code for the constraints, so you know which names to specify.  Below is one possible example iLogic rule that will attempt to assign a somewhat generic name to all those types of entities.  But beware, this code solution was designed to be used either on a new model (no names assigned yet), or to clear all existing names, then rename all entities again, like a fresh start.  Doing it this way eliminates the possibility of naming any one entity the same as any another entity, and avoids those potential errors.  If it was not done this way, then more robust naming conventions (and the additional code to support their functionality) would be needed to ensure that any entities found without a name in a follow-up run, would not be named the same as any previous entity's name.

 

You will also notice that all of these entities are being assigned names without any manual selection involved, but that also means that specific entities may not be assigned the exact name that you may have wanted.  These names can be changed manually later, if you wanted.  You are likely already aware of this, but just in case, on the iLogic tab (where the Rules, Forms, Global Forms sub tabs are), a new sub tab will appear named 'Entities' after you have assigned names to entities using either that manual system, or this 'NamedEntities' system.  When that 'Entities' tab is activated, you will see the list of named entities, and you can turn their 'tags' on or off from there, as well as rename them from there.

Sub Main
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then Return
	Dim oTrans As Inventor.Transaction
	oTrans = ThisApplication.TransactionManager.StartTransaction(oPDoc, "Assign Name To All Faces, Edges, Vertices - iLogic")
	AssignGenericNames(oPDoc)
	oTrans.End
End Sub

Sub AssignGenericNames(oDoc As Inventor.Document)
	If oDoc Is Nothing Then Return
	If oDoc.IsModifiable = False Then
		Logger.Debug("AssignGenericNames - Supplied Document Is Not Modifiable!")
		Return
	End If
	If (Not TypeOf oDoc Is PartDocument) Then
		Logger.Debug("AssignGenericNames - Supplied Document Is Not A PartDocument!")
		Return
	End If
	Dim oPDoc As PartDocument = oDoc
	Dim oBodies As SurfaceBodies = oPDoc.ComponentDefinition.SurfaceBodies
	If oBodies.Count = 0 Then Return
	Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc)
	'<<< CLEAR ALL EXISTING NAMES, IF ANY (to avoid reusing existing names) >>>
	If oNEs.Entities IsNot Nothing AndAlso oNEs.Entities.Count > 0 Then
		oNEs.Entities.Clear
	End If
	oDoc.Update2(True)
	For Each oBody As SurfaceBody In oBodies
		Dim sBodyName As String = oBody.Name
		Dim oFaces As Faces = oBody.Faces
		If oFaces.Count > 0 Then
			Dim sFaceBaseName As String = sBodyName & "-Face"
			Dim iFace As Integer = 0
			For Each oFace As Face In oFaces
				iFace = iFace + 1
				Dim sNewFaceName As String = sFaceBaseName & iFace.ToString
				Try
					'this will fail if this name is already being used
					oNEs.SetName(oFace, sNewFaceName)
				Catch
					Logger.Error("Error renaming Face to '" & sNewFaceName & "'!")
				End Try
			Next 'oFace
		End If
		Dim oEdges As Edges = oBody.Edges
		If oEdges.Count > 0 Then
			Dim sEdgeBaseName As String = sBodyName & "-Edge"
			Dim iEdge As Integer = 0
			For Each oEdge As Edge In oEdges
				iEdge = iEdge + 1
				Dim sNewEdgeName As String = sEdgeBaseName & iEdge.ToString
				Try
					'this will fail if this name is already being used
					oNEs.SetName(oEdge, sNewEdgeName)
				Catch
					Logger.Error("Error renaming Edge to '" & sNewEdgeName & "'!")
				End Try
			Next 'oEdge
		End If
		Dim oVertices As Vertices = oBody.Vertices
		If oVertices.Count > 0 Then
			Dim sVertexBaseName As String = sBodyName & "-Vertex"
			Dim iVertex As Integer = 0
			For Each oVertex As Vertex In oVertices
				iVertex = iVertex + 1
				Dim sNewVertexName As String = sVertexBaseName & iVertex.ToString
				Try
					'this will fail if this name is already being used
					oNEs.SetName(oVertex, sNewVertexName)
				Catch
					Logger.Error("Error renaming Vertex to '" & sNewVertexName & "'!")
				End Try
			Next 'oVertex
		End If
	Next 'oBody
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes