KnitFeatures.Add Method Failed

KnitFeatures.Add Method Failed

dpvols21
Explorer Explorer
306 Views
3 Replies
Message 1 of 4

KnitFeatures.Add Method Failed

dpvols21
Explorer
Explorer

I am fairly new to incorporating VBA with Inventor (not new to VBA).  Most of the code I've learned has been using snippets found on this forum or other sites when I hit snags...to fit my needs.  However, I've hit a bump with the knitfeatures.add method.  The overall goal I'm trying to accomplish is to move a part in an assembly to a different location and get the minimum distance.  I want to iterate this until I hit a minimum value.  I'm attempting to create a transient surface body for each component I want to measure between.  Each part file is a multi-body part file.  So I am incrementing through each body for that part file and creating a transient surface body and adding that to an object collection.  This works, but when I set my knitfeatures.add to stitch the surface bodies to create a singular surface body on the last line of the code below, I get the .Add Method failed error.  I am in the beginning stages of this process...as I am only working in a single Part file at the moment (not from the assembly) in order to take baby steps and learn.  Any help or insight would be helpful.  I have search for a couple days trying to find a solution (i'm afraid it's simple and I'm just missing it).

 

Private Sub TransBody()
Dim partdoc As PartDocument
Set partdoc = ThisApplication.ActiveDocument

Dim pcDef As PartComponentDefinition, surfbody As SurfaceBodies
Set pcDef = partdoc.ComponentDefinition


Dim oTransBRep As TransientBRep, cnt As Integer
Set oTransBRep = ThisApplication.TransientBRep
cnt = pcDef.SurfaceBodies.Count

Dim sbCollection As ObjectCollection, oTO As TransientObjects
Set oTO = ThisApplication.TransientObjects
Set sbCollection = oTO.CreateObjectCollection

Dim oBody1 As SurfaceBody, i As Integer
For i = 1 To cnt
    Set oBody1 = oTransBRep.Copy(pcDef.SurfaceBodies(i))
    Call sbCollection.Add(oBody1)
    'Call partdoc.ComponentDefinition.Features.NonParametricBaseFeatures.Add(oBody1)
    
Next i

Dim oKnitFeature As KnitFeature
Set oKnitFeature = pcDef.Features.KnitFeatures.Add(sbCollection)

 

 

0 Likes
307 Views
3 Replies
Replies (3)
Message 2 of 4

dpvols21
Explorer
Explorer

I feel like I have something wrong with trying to knit what is in the collection. I'm adding the transient object oBody1 into the collection. And my count variable (cnt) is counting the number of bodies correctly but it is counting surfacebodies which technically shouldn't exist yet in the part file. Wonder if i would be better off tyring to create a transient part file (or solid body), which I haven't even looked to see if that's possible.

0 Likes
Message 3 of 4

Andrii_Humeniuk
Advisor
Advisor

Hi @dpvols21 . If I understand you correctly, you want to combine different bodies in your PartDocument, but you use the KnitFeatures function, which is for WorkSurfaces or WorkPlanes.
To combine different bodies, you need to use the CombineFeatures function. Example code:

Private Sub TransBody()
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument

    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
    
    Dim oBody As SurfaceBody
    Set oBody = oCompDef.SurfaceBodies.Item(1)
    
    Dim oColl As ObjectCollection
    Set oColl = ThisApplication.TransientObjects.CreateObjectCollection
    
    Dim i As Integer
    For i = 2 To oCompDef.SurfaceBodies.Count
        Call oColl.Add(oCompDef.SurfaceBodies.Item(i))
    Next i
    
    Dim oCombine As CombineFeature
    Set oCombine = oCompDef.Features.CombineFeatures.Add(oBody, oColl, PartFeatureOperationEnum.kJoinOperation, True)
End Sub

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 4 of 4

dpvols21
Explorer
Explorer

Thanks Andrii. I will give that a try. I have realized after more research that solid bodies and surface bodies are recognized  the same unless you run a check which was explains my confusion on bodies being added to my collection. 

0 Likes