Help. Combine/Divide solids with VBA for Inventor

Help. Combine/Divide solids with VBA for Inventor

Anonymous
Not applicable
1,270 Views
4 Replies
Message 1 of 5

Help. Combine/Divide solids with VBA for Inventor

Anonymous
Not applicable

Hi all,

 

I have a question about the CombineFeature using VBA.

 

I got this solid:Solid1.PNG

 

 

And this other solid:

solid2.PNG

 

 

They are two different solids:

solid12.PNG

 

 

I would like to do the combine feature with vba to get this:solid12expl.PNG

 solid12expl1.PNG

 

 

 

How could I do with a VBA code?

 

Thanks in advance!

0 Likes
Accepted solutions (1)
1,271 Views
4 Replies
Replies (4)
Message 2 of 5

YuhanZhang
Autodesk
Autodesk

Here is a VBA sample code, you should in UI firstly select the base body, and then select the tool body, then run the code:

 

Sub CombineFeatureSample()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oBody1 As SurfaceBody
    Dim oBody2 As SurfaceBody
    
    Set oBody1 = oDoc.SelectSet.Item(1)
    Set oBody2 = oDoc.SelectSet.Item(2)
    
    Dim oToolBodies As ObjectCollection
    Set oToolBodies = ThisApplication.TransientObjects.CreateObjectCollection
    oToolBodies.Add oBody2
    
    Dim oCombine As CombineFeature
    Set oCombine = oDoc.ComponentDefinition.Features.CombineFeatures.Add(oBody1, oToolBodies, kCutOperation, False)
End Sub


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 5

Anonymous
Not applicable

Hi @YuhanZhang,

Thanks in advance. Your code actually works but there is a way to do it automatically without selecting the item manually?

Thanks for your help 😄

0 Likes
Message 4 of 5

YuhanZhang
Autodesk
Autodesk
Accepted solution

That is a sample code that asks you to select the Solid body for it, you can also use any other ways to specify it like if you know the index of the Solid as base solid you can just use below to specify it:

 

Set oBody1 = oDoc.ComponentDefinition.SurfaceBodies (1)

 

Or if you know the name(for example "Solid1") of a Solid to be used as base solid you can use below code to find it:

    Dim oTempBody As SurfaceBody
    For Each oTempBody In oDoc.ComponentDefinition.SurfaceBodies
        If oTempBody.Name = "Solid1" Then
            Set oSolid1 = oTempBody
            Exit For
        End If
    Next

 

Or if you have ever set an attribute(for example "BaseSolid" as its value) to a Solid to indicate it is a base Solid, you can use below code to find it again:

    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    Dim oSolid1 As SurfaceBody
    Dim oAttributeMgr As AttributeManager
    Set oAttributeMgr = oDoc.AttributeManager
    
    Dim oCol As ObjectCollection
    Set oCol = oAttributeMgr.FindObjects(, , "BaseSolid")
    Set oSolid1 = oCol.Item(1)

 

The same way to get the Tool bodies if you know their indices/names/attribute value. Hope this helps.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 5 of 5

Anonymous
Not applicable

Thanks a lot Sr!

It worked good

0 Likes