Failed to create sweep with Ilogic

Failed to create sweep with Ilogic

jaconoorland45
Contributor Contributor
205 Views
2 Replies
Message 1 of 3

Failed to create sweep with Ilogic

jaconoorland45
Contributor
Contributor

I have Inventor 2023. 

This is my path to be sweeped. The path is a 3D sketch. When i use the UI it works just fine.

jaconoorland45_0-1742465497867.png

Now my code always fails to make a sweeppath. It fails at line 22, because it can't create a path. I already tried filtering out everything exept the usable lines so it just uses the 3dArcs and 3D lines. I'm not an Ilogic expert. Can someone help me?

 

'=========== Begin iLogic Rule ===========

Dim oDoc As PartDocument = ThisDoc.Document
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition

' Create the sweep profile from a 2D sketch named "ProfileSketch".
Dim oSweepProfile As Profile = oCompDef.Sketches.Item("ProfileSketch").Profiles.AddForSolid()

' Retrieve the 3D sketch containing the sweep path curves.
Dim oSweepPathSketch As Sketch3D = oCompDef.Sketches3D.Item("PathSketch")

' Create an ObjectCollection and add all SketchLine3D and SketchArc3D entities.
Dim oColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection()
Dim oEnt As Object
For Each oEnt In oSweepPathSketch.SketchEntities3D
    If TypeOf oEnt Is Inventor.SketchLine3D Or TypeOf oEnt Is Inventor.SketchArc3D Then
        oColl.Add(oEnt)
    End If
Next

' Create a continuous path from the collected entities.
Dim oSweepPath As Path = oCompDef.Features.CreatePath(oColl)

' Create the sweep feature using the profile and the sweep path.
Dim oSweepFeatures As SweepFeatures = oCompDef.Features.SweepFeatures
Dim oSweepDef As SweepDefinition = oSweepFeatures.CreateSweepDefinition( _
                            SweepTypeEnum.kPathSweepType, _
                            oSweepProfile, _
                            oSweepPath, _
                            PartFeatureOperationEnum.kNewBodyOperation)
Dim oSweep As SweepFeature = oSweepFeatures.Add(oSweepDef)

'=========== End iLogic Rule ===========

 

This will be part of an automated workflow so the manually making the sweep is not an option.

 

In advance: thank you for helping me!

0 Likes
Accepted solutions (1)
206 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @jaconoorland45.  One thing I see is in the area of Line 14 through Line 19, where you are adding potentially multiple entities into an ObjectCollection.  Then you are specifying that ObjectCollection as the input into the PartFeatures.CreatePath method.  That specific method only wants you to specify a single sketch entity as input, then it will get all other sketch entities that are connected to that entity, end to end, or start to start, or end to start.  However, there is another similar method (PartFeatures.CreateSpecifiedPath), which does want you to input an ObjectCollection of all the sketch entities that you want to be included in the path.  But when using that one, all those entities that you specify must all be connected to each other, or it will fail.  So, you will need to change your code to adapt to the requirements of one of those methods, or the other.  The simplest change would likely be to change the method you are using on Line 22 to the one named CreateSpecifiedPath instead of the one named CreatePath.  That would require the least changes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

jaconoorland45
Contributor
Contributor
Accepted solution

Hi,

 

Your answer didn't resolve my problem but it helped me find the problem with my code! Now i select just one line of the 3d sketch and it works!! You are a great help. 

 

Working code:

Sub Main()
    ' Get the active part document and component definition.
    Dim oPartDoc As PartDocument = ThisDoc.Document
    Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition

    ' Retrieve the 2D sketch containing the profile.
    Dim oSketch As Sketch = Nothing
    On Error Resume Next
    oSketch = oCompDef.Sketches.Item("ProfileSketch")
    On Error GoTo 0
    If oSketch Is Nothing Then
        MessageBox.Show("ProfileSketch not found. Please check the sketch name.")
        Exit Sub
    End If

    ' Create a profile from the 2D sketch.
    Dim oProfile As Profile = Nothing
    On Error Resume Next
    oProfile = oSketch.Profiles.AddForSolid()
    If Err.Number <> 0 Then
        MessageBox.Show("Failed to create profile: " & Err.Description)
        Exit Sub
    End If
    On Error GoTo 0

    ' Retrieve the 3D sketch containing the path line.
    Dim oSketch3D As Sketch3D = Nothing
    On Error Resume Next
    oSketch3D = oCompDef.Sketches3D.Item("PathSketch")
    On Error GoTo 0
    If oSketch3D Is Nothing Then
        MessageBox.Show("PathSketch not found. Please check the sketch name.")
        Exit Sub
    End If

    ' Select a single line from the 3D sketch.
    Dim oLine3D As SketchLine3D = Nothing
    On Error Resume Next
    oLine3D = oSketch3D.SketchLines3D.Item(1)
    On Error GoTo 0
    If oLine3D Is Nothing Then
        MessageBox.Show("No line found in the 3D sketch.")
        Exit Sub
    End If

    ' Create a path using the selected 3D line.
    Dim oPath As Path = Nothing
    On Error Resume Next
    oPath = oCompDef.Features.CreatePath(oLine3D)
    If Err.Number <> 0 Then
        MessageBox.Show("Failed to create path: " & Err.Description)
        Exit Sub
    End If
    On Error GoTo 0

    ' Create the sweep feature along the path using the profile.
    Dim oSweep As SweepFeature = Nothing
    On Error Resume Next
    oSweep = oCompDef.Features.SweepFeatures.AddUsingPath(oProfile, oPath, PartFeatureOperationEnum.kJoinOperation)
    If Err.Number <> 0 Then
        MessageBox.Show("Failed to create sweep feature: " & Err.Description)
        Exit Sub
    End If
    On Error GoTo 0

    MessageBox.Show("Sweep feature created successfully!")
End Sub

 

0 Likes