Macro for creating a sweep from existing sketches Fails

Macro for creating a sweep from existing sketches Fails

Anonymous
Not applicable
1,017 Views
3 Replies
Message 1 of 4

Macro for creating a sweep from existing sketches Fails

Anonymous
Not applicable

Hey all!

 

I am having some trouble creating a sweep feature through the API.

Here's the skinny-

 

I have a sketch that define the end of a tube, and then a series of connected sketches that define the route of the tube on separate sketches and different planes (But they all are connected end to end). Currently, I manually create the sweep just fine, but I am trying to automate some of this.

 

With either of the sweep feature adding methods (one is commented out) I keep getting this error-

 

Run-Time Error, method 'add' of object 'sweepfeatures' failed

 

I tried to follow along with these to no avail

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2018/ENU/Inventor-API/files/Pr...

 

http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-20388027-7B42-4645-AE3D-F9528AB24EE4

 

Here is my code (the extrude stuff at the end works, it was just to see if my profile was usable)

 

Public Sub CreateSweep()

 

Dim PartDoc As PartDocument
Set PartDoc = ThisApplication.ActiveDocument

Dim PartDef As PartComponentDefinition
Set PartDef = PartDoc.ComponentDefinition

Dim oFeatures As Object
Set oFeatures = PartDef.Features

Dim oUserParams As UserParameters 'declare
Set oUserParams = PartDef.Parameters.UserParameters 'set to user params collection object

Dim NumBends As Integer 'set to desired parameter
NumBends = oUserParams.Item("NumBends").Value

'staggered bc of the sketch order
Dim lastSk As Integer
lastSk = NumBends + 2

PartDef.Sketches.Item(lastSk).SetEndOfPart (True)

'using first bend sketch to create path, all sketches before EOP should be included (in theory)
Dim oPath As Path
Set oPath = oFeatures.CreatePath(PartDef.Sketches.Item(3).SketchLines.Item(1))
'Set oPath = PartDef.Sketches.CreatePath(PartDef.Sketches.Item(3).SketchLines.Item(1))
'Set oPath = SweepDefinition.Path(PartDef.Sketches.Item(3))

'setting the end of tube sketch as the sweep profile
'need to somehow select the area between the two concentric circles only
Dim oProfile As Profile
Set oProfile = PartDef.Sketches.Item(1).Profiles.AddForSolid
'Set oProfile = PartDef.Sketches.Item(1)
'Set oProfile = SweepDefinition.Profile(PartDef.Sketches.Item(1).SketchLines.Item(1))

'attempt to add definition based on tube end profile and bend sketches
Dim oSweepDef As SweepDefinition
Set oSweepDef = oFeatures.SweepFeatures.CreateSweepDefinition(kPathSweepType, oProfile, oPath, PartFeatureOperationEnum.kJoinOperation)

'failed attempts to create the sweep
Dim oSweep As SweepFeature
Set oSweep = oFeatures.SweepFeatures.Add(oSweepDef)
'Set oSweep = oFeatures.SweepFeatures.AddUsingPath(oProfile, oPath, kJoinOperation)

'verifying sanity with an extrude
'Dim extrudeDef As ExtrudeDefinition
'Set extrudeDef = oFeatures.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
'Call extrudeDef.SetDistanceExtent(5, kPositiveExtentDirection)
'Dim extrude As ExtrudeFeature
'Set extrude = oFeatures.ExtrudeFeatures.Add(extrudeDef)

 

End Sub

 

TL:DR

Sweep feature fails. Help a brother out?

 

Thanks for looking!

D.M.

0 Likes
Accepted solutions (1)
1,018 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

Having the ipt would be a big help since the problem could be geometry based and it's also difficult to understand the code without the model.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

Anonymous
Not applicable
Accepted solution

Thanks for the input @BrianEkins, I didnt realize what it sounded like from someone else's perspective.

 

I think it was just syntax issues, since I got the create sweep feature to work by changing the way I defined things. This code works as long as there is a user parameter called "NumBends" -

 

Public Sub CreateSweepYBC()
   
    'get active document
    Dim PartDoc As PartDocument
    Set PartDoc = ThisApplication.ActiveDocument
    
    'get component definition collection object of active part
    Dim PartDef As PartComponentDefinition
    Set PartDef = PartDoc.ComponentDefinition
    
    'get features object
    Dim oFeatures As Object
    Set oFeatures = PartDef.Features
    
    'get user parameters collection object
    Dim oUserParams As UserParameters
    Set oUserParams = PartDef.Parameters.UserParameters
    
    'SetEndOfPart Argument to desired parameter
    'staggered bc of sketch order
    Dim NumBends As Integer
    NumBends = oUserParams.Item("NumBends").Value + 2
               
    'Move EOP
    PartDef.Sketches.Item(NumBends).SetEndOfPart (True)
    
     'find sketchcurve object to set as argument for create path
    Dim swCurve As SketchLine
    Set swCurve = PartDef.Sketches.Item(2).SketchLines.Item(1)
    
    'using first bend sketch to create path, all sketches before EOP should be included (in theory)
    Dim oPath As Path
    Set oPath = oFeatures.CreatePath(swCurve)
        
    'setting the end of tube sketch as the sweep profile
    'selects the area between the two concentric circles with a boolean [combine = true]
    Dim oProfile As Profile
    Set oProfile = PartDef.Sketches.Item(1).Profiles.AddForSolid(True)
          
    'Create the sweep
    Dim oSweep As SweepFeature
    Set oSweep = oFeatures.SweepFeatures.AddUsingPath(oProfile, oPath, kJoinOperation)

End Sub

 

0 Likes
Message 4 of 4

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

It is nice to hear that code is working for you.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes