Selecting Edge for Chamfer in VBA (VB.net)

Anonymous

Selecting Edge for Chamfer in VBA (VB.net)

Anonymous
Not applicable

I am struggling to select a single edge of a cylindrical tube with a key to chamfer. The top edge (the face the key is closest to) needs to be chamfered as does the exterior edge of the key closest to the top face (circled in picture.)  I have a decent grasp of how to chamfer an edge once selected, but I cannot figure out how to select only a single edge. I'm coding in VB.net as a standalone application, but VBA is not a problem for me to translate.Chamfer Edges.PNG

 

Thanks,

Chris

 

'Allows Inventor assets
Imports Inventor
Imports System



'Creates a structure for variables
Structure Data
    Dim KeyHeight As Double
    Dim KeyWidth As Double
    Dim OD As Double
    Dim ID As Double
    Dim ConnectorDepth As Double
End Structure


'Establishes the form to open Inventor and draw connector
Public Class ShellExtrude

    Dim m_inventorApp As Inventor.Application = Nothing

    Private Sub ShellExtrude_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Try
                m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
            Catch ex As Exception
                Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
                m_inventorApp = System.Activator.CreateInstance(inventorAppType)
                m_inventorApp.Visible = True
            End Try
        Catch
            System.Windows.Forms.MessageBox.Show("Error: couldn't create Inventor instance")
        End Try


        Dim oPartDoc As PartDocument = m_inventorApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, m_inventorApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTG As TransientGeometry = m_inventorApp.TransientGeometry

        'Open the file.
        'Dim FileName As String = "C:\Users\Chris\Desktop\text.txt"

        Dim PartData() As Data
        Dim PartCount As Long
        PartCount = 0

        ' Open the file
        'FileOpen(1, "C:\Users\Christian's PC\Desktop\Connector C++\Test Connector\data.csv", OpenMode.Input)
        FileOpen(1, "C:\Users\Chris\Desktop\data.csv", OpenMode.Input)

        ' Read through the file until we reach the end.
        Do While Not EOF(1)
            ' Read the next line from the file.

            Dim strLine = LineInput(1)


            strLine = Trim$(strLine)

            If strLine <> "" Then
                ' Break the line up, using commas as the delimiter.
                Dim astrPieces() As String
                astrPieces = Split(strLine, ",")

                If UBound(astrPieces) <> 4 Then
                    MessageBox.Show("Invalid line in the file: " & strLine)
                    Exit Sub
                End If

                ' Increase the size of the array of part data.
                PartCount = PartCount + 1
                ReDim Preserve PartData(PartCount - 1)

                ' Save the data in the part data array.
                PartData(PartCount - 1).KeyHeight = astrPieces(0)
                PartData(PartCount - 1).KeyWidth = astrPieces(1)
                PartData(PartCount - 1).ID = astrPieces(2)
                PartData(PartCount - 1).OD = astrPieces(3)
                PartData(PartCount - 1).ConnectorDepth = astrPieces(4)


                'MessageBox.Show(astrPieces(0) & astrPieces(1) & astrPieces(2) & astrPieces(3)



                'Create a sketch on the origin plane XY
                Dim oSketch As PlanarSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

                'Sketch the outer diameter circle on the sketch
                Dim oOD As SketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), astrPieces(3) / 2)

                'Sketch the inner diamter circle on the sketch
                Dim oID As SketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), astrPieces(2) / 2)

                'Create a profile.
                Dim oShellProfile As Profile = oSketch.Profiles.AddForSolid

                'Determine which profile to extrude
                Dim oProfPath As ProfilePath
                For Each oProfPath In oShellProfile
                    If oProfPath.Item(1).SketchEntity Is oOD Then
                        oProfPath.AddsMaterial = False
                    ElseIf oProfPath.Item(1).SketchEntity Is oID Then
                        oProfPath.AddsMaterial = False
                    End If
                Next

                'Extrude profile
                Dim oExtOD As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oShellProfile, PartFeatureOperationEnum.kJoinOperation)
                Call oExtOD.SetDistanceExtent(astrPieces(4), PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
                Dim oExtrude As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtOD)

                'Create work plane offset from XY plane
                Dim WorkPlane1 As WorkPlane = oCompDef.WorkPlanes.AddByPlaneAndOffset(oCompDef.WorkPlanes(3), (-1.3716))

                'Create sketch on offset plane
                Dim oSketch2 As PlanarSketch = oCompDef.Sketches.Add(WorkPlane1)

                WorkPlane1.Visible = False

                'Create lines to form the key
                Dim oKeyLines(0 To 3) As SketchLine
                oKeyLines(0) = oSketch2.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(-astrPieces(1) / 2, 0.95 * astrPieces(3) / 2), oTG.CreatePoint2d(-astrPieces(1) / 2, astrPieces(0)))
                oKeyLines(1) = oSketch2.SketchLines.AddByTwoPoints(oKeyLines(0).EndSketchPoint, oTG.CreatePoint2d(astrPieces(1) / 2, astrPieces(0)))
                oKeyLines(2) = oSketch2.SketchLines.AddByTwoPoints(oKeyLines(1).EndSketchPoint, oTG.CreatePoint2d(astrPieces(1) / 2, 0.95 * astrPieces(3) / 2))
                oKeyLines(3) = oSketch2.SketchLines.AddByTwoPoints(oKeyLines(2).EndSketchPoint, oKeyLines(0).StartSketchPoint)

                'Create profile for the key extrusion
                Dim oKeyProfile As Profile = oSketch2.Profiles.AddForSolid

                'Extrude key
                Dim oExtKey As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oKeyProfile, PartFeatureOperationEnum.kJoinOperation)
                Call oExtKey.SetDistanceExtent(1.2192, PartFeatureExtentDirectionEnum.kPositiveExtentDirection)
                Dim oExtrude2 As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtKey)

                'Create sketch on XY plane to cut away part of key
                Dim oSketch3 As PlanarSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

                'Circle to be removed
                Dim oKeyRadius As SketchCircle = oSketch3.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), astrPieces(0))
                Dim oKeyRadiusCut As SketchCircle = oSketch3.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), astrPieces(0) * 1.05)

                'Profile for cut
                Dim oKeyRadiusCutaway As Profile = oSketch3.Profiles.AddForSolid

                'Select which to cut
                Dim oProfPath2 As ProfilePath
                For Each oProfPath2 In oKeyRadiusCutaway
                    If oProfPath2.Item(1).SketchEntity Is oKeyRadius Then
                        oProfPath2.AddsMaterial = False
                    ElseIf oProfPath2.Item(1).SketchEntity Is oKeyRadiusCut Then
                        oProfPath2.AddsMaterial = False
                    End If
                Next

                'Cut away part of key
                Dim oKeyCutaway As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oKeyRadiusCutaway, PartFeatureOperationEnum.kCutOperation)
                Call oKeyCutaway.SetThroughAllExtent(PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
                Dim oExtrude3 As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oKeyCutaway)

                'Set up edge collection for chamfer
                Dim oEdges As EdgeCollection = m_inventorApp.TransientObjects.CreateEdgeCollection

                Dim oEdge As Edge
                For Each oEdge In oExtrude.StartFaces.Item(1).Edges
                    oEdges.Add(oEdge)
                Next

                'Chamfer top edge
                Dim oKeyChamfer As ChamferFeature = oCompDef.Features.ChamferFeatures.AddUsingDistance(oEdges, 0.0254)



                oSketch3.Visible = False
                End If
                Close()

        Loop

    End Sub
End Class
0 Likes
Reply
Accepted solutions (1)
1,278 Views
2 Replies
Replies (2)

YuhanZhang
Autodesk
Autodesk
Accepted solution

So seems you want to just add the exterior edge on the top face to create chamfer feature, that in your below code you add the two edges:

 

Dim oEdge As Edge
For Each oEdge In oExtrude.StartFaces.Item(1).Edges
        oEdges.Add(oEdge)
Next

Now  you can compare the radius of the two circular edges and find the exterior edge, so you can use below code instead:

 

 

    Dim oEdge As Edge, oExteriorEdge As Edge, dRadius As Double: dRadius = 0
    For Each oEdge In oExtrude.StartFaces.Item(1).Edges
        
        
        If dRadius = 0 Then
            dRadius = oEdge.Geometry.Radius
            oExteriorEdge = oEdge
        Else
            If dRadius < oEdge.Geometry.Radius Then
                oExteriorEdge = oEdge
            End If
            oEdges.Add (oExteriorEdge)
        End If
    Next

 

 

Hope it 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.

Anonymous
Not applicable

Thank you, very helpful. I should be able to adapt this for the rest of my project as well. Fillets should also work in the same fashion?

0 Likes