Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Project Cut Edges of part in sub-assembly via iLogic

0 REPLIES 0
Reply
Message 1 of 1
chewy_robot
186 Views, 0 Replies

Project Cut Edges of part in sub-assembly via iLogic

I have an iLogic rule that I use while in an assembly edit of a part. With the part in edit mode, I run this rule and I can select a face on the part and then it will create a sketch on that face and it projects the cut edges of all parts touching that face. It works pretty well but it will not project the cut edges of a part that is touching the face if the part is in a sub-assembly. For example, maybe I edit part 53 in my U assembly and run this rule. It will detect and project cut edges of parts found within the U assembly, however, if there is a sub-assembly in the U assembly like UQ and one of the parts in UQ is touching the part face, it will not project cut edges of that part. Any ideas on how to also include parts touching the part sketch face if they are in a sub-assembly? Below is my code to test. Run it while editing a part in an assembly

Public Sub Main()
    ' Declare the application object and get the current application
    Dim oApp As Inventor.Application = ThisApplication
    Dim oDoc As Inventor.Document = oApp.ActiveDocument
    Dim oAssDoc As Inventor.AssemblyDocument = oApp.ActiveDocument
    Dim oAssDocDef As Inventor.AssemblyComponentDefinition = oAssDoc.ComponentDefinition
    Dim oFaceProxy As FaceProxy = Nothing
    Dim oSketch As PlanarSketch = Nothing
    
    Try
        ' Prompt user to select a face
        oFaceProxy = oApp.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a face")
        If oFaceProxy Is Nothing Then
            MessageBox.Show("No face was selected.")
            Return
        End If
    Catch ex As Exception
        MessageBox.Show("Error in selecting face: " & ex.Message)
        Return
    End Try
    
    ' Define an empty list to hold occurrences touching the selected face
    Dim oOccurrenceListA As New List(Of ComponentOccurrence)
    
    Try
        ' Iterate through the occurrences in the assembly to find parts touching the selected face
        For Each oOccurrence As ComponentOccurrence In oAssDocDef.Occurrences
            ' Check each occurrence against the selected face to find touching parts
            If oApp.MeasureTools.GetMinimumDistance(oOccurrence, oFaceProxy) < 0.01 AndAlso Not oOccurrence Is oFaceProxy.ContainingOccurrence Then
                oOccurrenceListA.Add(oOccurrence)
            End If
        Next
    Catch ex As Exception
        MessageBox.Show("Error finding touching parts: " & ex.Message)
        Return
    End Try
    
    ' Check if any parts are touching the selected face
    If oOccurrenceListA.Count = 0 Then
        ' If no parts are touching, notify the user and exit the sub
        MessageBox.Show("No parts touching the selected face. No scribe lines will be created.", "No Parts Touching")
        Return
    End If
    
    Try
        ' Set the default sketch name
        Dim defaultSketchName As String = "SCRIBE LINES"
        ' Access the occurrence containing the face
        Dim oOcc As ComponentOccurrence = oFaceProxy.ContainingOccurrence
        ' Access the part document's component definition
        Dim oMainPartDef As PartComponentDefinition = oOcc.Definition
        
        ' Initialize the sketch variable with the unique name
        oSketch = oMainPartDef.Sketches.Add(oFaceProxy.NativeObject)
        oSketch.Name = defaultSketchName
        oSketch.Edit()  ' Start the sketch editing mode
    Catch ex As Exception
        MessageBox.Show("Error initializing sketch: " & ex.Message)
        Return
    End Try
    
    ' Check if the sketch was created successfully
    If oSketch Is Nothing Then
        MessageBox.Show("Failed to create sketch.")
        Return
    End If
    
    'Begin projecting cut edges on sketch
    Try
        ' Start a transaction to project cut edges
        Dim oTrans As Transaction = oApp.TransactionManager.StartTransaction(oDoc, "Project Cut Edges")
        Dim oControlDef As ControlDefinition = oApp.CommandManager.ControlDefinitions.Item("SketchProjectCutEdgesCmd")
        oApp.ActiveDocument.SelectSet.Clear() ' Clear current selection
        For Each oOcc In oOccurrenceListA
            oApp.ActiveDocument.SelectSet.Select(oOcc)
        Next
        oControlDef.Execute() ' Execute the command
        oTrans.End() ' Commit the transaction
    Catch ex As Exception
        MessageBox.Show("Error projecting cut edges: " & ex.Message)
    Finally
        oApp.CommandManager.StopActiveCommand() ' Ensure no command is left active
    End Try

    ' Exit the sketch
    Try
        oSketch.ExitEdit()  ' Exit the sketch editing mode
    Catch ex As Exception
        MessageBox.Show("Error exiting the sketch: " & ex.Message)
    End Try

    ' Final update and clear selection
    oApp.ActiveDocument.Update2(True)
    oApp.ActiveDocument.SelectSet.Clear()
End Sub
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report