Sort Edges with Distance to CenterPoint Inventor API

Sort Edges with Distance to CenterPoint Inventor API

florian_wenzel
Advocate Advocate
212 Views
1 Reply
Message 1 of 2

Sort Edges with Distance to CenterPoint Inventor API

florian_wenzel
Advocate
Advocate

Hi,

Inventor 2022

API VB.NET Visual Studio

 

i try to Sort Edges with Shortest Distance to CeneterPoint (0,0,0)

My Goal is to Create SketchLines to Connect all Edges in This Way:

florian_wenzel_0-1665424147624.png

 

 

florian_wenzel_0-1665415691799.png

 

The Edges  are with Length between 81.0 to 81.3mm

florian_wenzel_1-1665415792465.png

florian_wenzel_2-1665415808529.png

florian_wenzel_5-1665415948027.png

 

Is there a way to sort the Edges with Shortest Distance to CenterPoint(0,0,0)

and then Connect each Bottom Vertex from each Edge with each other.

To get the Result like in Sketch1 ?

 

This is my Code:

Still in Progess

 

 

Imports System.Runtime.InteropServices
Imports Inventor
Imports Microsoft.Win32
Imports System.Collections.Generic
Imports System.Linq
Imports System.IO
Imports Autodesk.iLogic.Automation
Imports Autodesk.iLogic.Interfaces

Module CommandFunctionButton_16


    Public Sub CommandFunctionfweButton_16()

        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry


        Dim oFace As Face = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a Face")

        Dim oEdge As Edge
        Dim oCurveEval As CurveEvaluator
        Dim oMin, oMax, oLength As Double

        For Each oEdge In oFace.Edges
            oCurveEval = oEdge.Evaluator
            Call oCurveEval.GetParamExtents(oMin, oMax)
            Call oCurveEval.GetParamAtLength(oMin, oMax, oLength)

        Next



    End Sub

End Module

 

 

 

 

Thanks for any Suggestion.

 

 

0 Likes
Accepted solutions (1)
213 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor
Accepted solution

You can use two approaches. Below is both of them with appropriate test methods (at the beginning of the code block). I don't know which one is better for your needs.

 

1. PartComponentDefinition.FindUsingVector - This function returns all entities along the line from origin

Sub FindEdgesInSortedOrderTest()
    Dim pickFromEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick FROM edge")
    Dim pickToEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick TO edge")

    Dim fromEdge As Edge = pickFromEdge
    Dim toEdge As Edge = pickToEdge

    Dim fromPoint = fromEdge.PointOnEdge
    Dim toPoint = toEdge.PointOnEdge


    Dim sortedEdges = FindEdgesInSortedOrder(fromPoint, toPoint)

    'Select edges one-by-one in sorted order
    Dim part As PartDocument = ThisDoc.Document
    part.SelectSet.Clear()
    For Each edge As Edge In sortedEdges
        part.SelectSet.Select(edge)
        MsgBox("Next")
    Next

End Sub

Function FindEdgesInSortedOrder(fromPoint As Point, toPoint As Point) As List(Of Edge)
    Dim part As PartDocument = ThisDoc.Document

    Dim origin As Point = fromPoint
    Dim direction As UnitVector = fromPoint.VectorTo(toPoint).AsUnitVector()
    Dim tol As Double = 0.01 '[mm]
    Dim findUsingVector As ObjectsEnumerator = part.ComponentDefinition.FindUsingVector(origin, direction, {SelectionFilterEnum.kPartEdgeFilter}, True, tol)

    Return findUsingVector.OfType(Of Edge).ToList()
End Function

 

2. You can really sort the list of edges by distance from given point

Sub SortEdgesByDistanceFromPointTest()

    'Pick edges in any order
    Dim pick As Object
    Dim edges As New List(Of Edge)
    Do
        pick = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick edge - ESC to continue")
        If pick Is Nothing Then Exit Do
        edges.Add(pick)
    Loop

    'Sort edges by distance from origin
    Dim origin As Point = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)
    Dim sortedEdges = SortEdgesByDistanceFromPoint(edges, origin)

    'Select edges one-by-one in sorted order
    Dim part As PartDocument = ThisDoc.Document
    part.SelectSet.Clear()
    For Each edge As Edge In sortedEdges
        part.SelectSet.Select(edge)
        MsgBox("Next")
    Next
End Sub
Function SortEdgesByDistanceFromPoint(edges As List(Of Edge), origin As Point) As List(Of Edge)

    Dim edgesWithDistances As List(Of EdgeWithDistance) = New List(Of EdgeWithDistance)
    For Each edge As Edge In edges
        Dim distance = ThisApplication.MeasureTools.GetMinimumDistance(edge, origin)
        edgesWithDistances.Add(New EdgeWithDistance(edge, distance))
    Next
    'edgesWithDistances.OrderBy(Function(e) e.Distance)
    edgesWithDistances.Sort(Function(e1, e2) e1.Distance.CompareTo(e2.Distance))

    Return edgesWithDistances.Select(Function(e) e.Edge).ToList()
End Function

Class EdgeWithDistance
    Public Property Edge As Edge
    Public Property Distance As Double

    Public Sub New(edge As Edge, distance As Double)
        Me.Edge = edge
        Me.Distance = distance
    End Sub

End Class