iLogic. Inventor 2020 Professional

iLogic. Inventor 2020 Professional

john_combo
Participant Participant
580 Views
7 Replies
Message 1 of 8

iLogic. Inventor 2020 Professional

john_combo
Participant
Participant

Hi everyone!

I’m using Inventor 2020 Pro. Right now I’m trying to write a script to automate part of my workflow.

In a folder I have several files that contain flat parts and follow a similar naming pattern.

In a loop I:

  • take each file,

  • open it,

  • get all faces of the flat part,

  • find the face with the largest area,

  • and try to export that selected face as a DXF.

I’m able to find the face with the maximum area in code, but after that I can’t get the export function to work with that face.

Here’s my code.

...
Dim partFiles As String() = Directory.GetFiles(sourceFolder, "*.ipt", SearchOption.TopDirectoryOnly)
...

Try
    Dim compDef As Inventor.PartComponentDefinition = partDoc.ComponentDefinition
    Dim maxFace As Inventor.Face = Nothing
    Dim maxArea As Double = -1.0

    For Each body As Inventor.SurfaceBody In compDef.SurfaceBodies
        For Each face As Inventor.Face In body.Faces
            If face.SurfaceType = Inventor.SurfaceTypeEnum.kPlaneSurface Then
                Dim evaluator As Inventor.SurfaceEvaluator = face.Evaluator
                Dim area As Double = evaluator.Area
                If area > maxArea Then
                    maxArea = area
                    maxFace = face
                End If
            End If
        Next
    Next

    If maxFace Is Nothing Then
        Return "[Skip] " & System.IO.Path.GetFileName(partPath) & " — no flat faces found"
    End If

    Dim selectSet As Inventor.SelectSet = partDoc.SelectSet
    selectSet.Clear()
    selectSet.Select(maxFace)

    Dim dxfPath As String = System.IO.Path.Combine(dxfFolder, System.IO.Path.GetFileNameWithoutExtension(partPath) & ".dxf")
    Dim exportStatus As String = ExportSelectedFaceViaUI(dxfPath)
    Return "[OK] " & System.IO.Path.GetFileName(partPath) & " → " & exportStatus
Finally
    If Not isAlreadyOpen AndAlso partDoc IsNot Nothing Then
        partDoc.Close(False)
    ElseIf partDoc IsNot Nothing Then
        partDoc.Activate()
    End If
End Try


Maybe someone more experienced can take a look and point out what I’m doing wrong. I realize it might look like total nonsense, but I really need some guidance.

I can’t get the face export to DXF working — the files just never show up in the target folder. I double-checked that the script returns the correct area for the largest face, so it is finding that face and selecting it. (From what I’ve read, programmatically selecting a face doesn’t necessarily produce any visible selection highlight in the UI.)

 

My guess is that in the Inventor 2020 Pro API there may not be a way to export a selected face directly.

Thanks for taking the time to read my question.

0 Likes
Accepted solutions (1)
581 Views
7 Replies
Replies (7)
Message 2 of 8

john_combo
Participant
Participant

I can do this manually without any issues. But I seriously doubt I can keep doing it when I’ve got 200 files.

0 Likes
Message 3 of 8

john_combo
Participant
Participant

Right now I’m trying a different approach: I select the face with the largest area, create a sketch on it by projecting the geometry, and then I try to export that sketch as a DXF. I’ll share the results a bit later.

0 Likes
Message 4 of 8

kacper.suchomski
Mentor
Mentor

Hi

Are these files sheet metal parts?


Kacper Suchomski

EESignature


YouTube - Inventor tutorials | LinkedIn | Instagram

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 5 of 8

john_combo
Participant
Participant

No — it’s just a regular flat part. It’s not sheet metal.

0 Likes
Message 6 of 8

jnowel
Collaborator
Collaborator
Accepted solution
0 Likes
Message 7 of 8

john_combo
Participant
Participant

Thanks for your reply — I’ll take a short break to verify the info and then I’ll come back here with an update.

0 Likes
Message 8 of 8

john_combo
Participant
Participant

Thanks — your answer really helped me! I was able to figure it out and fix the mistakes in my code. Now I’m getting the result I expected — awesome!

' Selecting the largest face
Dim selectSet As Inventor.SelectSet = partDoc.SelectSet
selectSet.Clear()
selectSet.Select(maxFace)

' Export via the GeomToDXFCommand
Dim dxfPath As String = System.IO.Path.Combine(dxfFolder, System.IO.Path.GetFileNameWithoutExtension(partPath) & ".dxf")
Dim exportStatus As String = ExportFaceViaGeomToDXF(dxfPath, dxfFolder)

' New export function
Private Function ExportFaceViaGeomToDXF(dxfPath As String, dxfFolder As String) As String
    Dim cmdMgr As Inventor.CommandManager = ThisApplication.CommandManager
    Dim exportDef As Inventor.ControlDefinition = Nothing

    Try
        exportDef = cmdMgr.ControlDefinitions.Item("GeomToDXFCommand")
    Catch ex As System.Exception
        Dim listPath As String = System.IO.Path.Combine(dxfFolder, "_command_names.txt")
        WriteControlDefinitionsList(listPath)
        Return "GeomToDXFCommand not found, commands: " & listPath
    End Try

    Try
        cmdMgr.PostPrivateEvent(Inventor.PrivateEventTypeEnum.kFileNameEvent, dxfPath)
        exportDef.Execute()
    Catch ex As System.Exception
        Return "Error of GeomToDXFCommand: " & ex.Message
    End Try

    Return "GeomToDXFCommand started (file: " & dxfPath & ")"
End Function
0 Likes