ilogic to convert circles to holes

ilogic to convert circles to holes

FProcp
Collaborator Collaborator
4,526 Views
72 Replies
Message 1 of 73

ilogic to convert circles to holes

FProcp
Collaborator
Collaborator

Does anyone know of an iLogic code that can convert circles to holes?

 

We receive plate models from others and the holes are drawn as circles and then extruded as through cuts.

 

I would like to be able to convert all circles in the plate (possibly under a certain diameter) to proper holes.

 

Has anyone done this before?

Franco
GMT +08:00
0 Likes
4,527 Views
72 Replies
Replies (72)
Message 61 of 73

GeorgK
Advisor
Advisor

I tried to convert the code to VB.Net. But I get an error in line:

 

Dim drilledCircleGroups = circleGroups.Where(Function(g) g.Count() = 1).Select(Function(g) g.Single()).GroupBy(Function(c) Round(c.Geometry.Radius * 2 * 10, 3))

Can someone help me please?

 

Georg

0 Likes
Message 62 of 73

smilinger
Advisor
Advisor

You need to provide more information about your error, and maybe some more of your code next time.

 

I guess it get error because the Round function? You should use Math.Round instead of Round.

0 Likes
Message 63 of 73

GeorgK
Advisor
Advisor
Dim m_inventorApp As Inventor.Application = Nothing
        m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

        Dim doc As PartDocument = m_inventorApp.ActiveDocument
        Dim comp As PartComponentDefinition = doc.ComponentDefinition

        Dim IsSheetMetal As Boolean = comp.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject

        Dim sketch1 As PlanarSketch = doc.ComponentDefinition.Sketches(1)
        Dim profile As Profile = sketch1.Profiles.AddForSolid(False)
        If IsSheetMetal Then
            Dim faceDef As FaceFeatureDefinition = comp.Features.FaceFeatures.CreateFaceFeatureDefinition(profile)
            faceDef.Direction = PartFeatureExtentDirectionEnum.kNegativeExtentDirection
            comp.Features.FaceFeatures.Add(faceDef)
        Else
            'Change the thickness for non-Sheetmetal parts here.
            Dim thickness = "6 mm"
            Dim extrudeDef = comp.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kNewBodyOperation)
            extrudeDef.SetDistanceExtent(thickness, PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
            comp.Features.ExtrudeFeatures.Add(extrudeDef)
        End If

        'Group circles by position
        Dim circleGroups
        circleGroups = sketch1.SketchCircles.Cast(Of SketchCircle)().GroupBy(Function(c)
                                                                                 Dim point = c.CenterSketchPoint.Geometry
                                                                                 Return Math.Round(point.X, 3) & ", " & Math.Round(point.Y, 3)
                                                                             End Function)

        'Group single circles by diameter
        Dim drilledCircleGroups
        drilledCircleGroups = circleGroups.Where(Function(g)
                                                     Return g.Count() = 1
                                                 End Function).Select(Function(g)
                                                                          Return g.Single()
                                                                      End Function).GroupBy(Function(c)
                                                                                                Return Math.Round(c.Geometry.Radius * 2 * 10, 3)
                                                                                            End Function)

        'Group pairs of circles by diameters
        Dim counterBoreCirclesGroups = circleGroups.Where(Function(g) g.Count() = 2).Select(
    Function(g)
        Dim o = g.OrderBy(Function(c) c.Radius)
        Return New With {.Center = g.First().CenterSketchPoint, .InnerDiameter = Math.Round(o(0).Radius * 2, 3), .OuterDiameter = Math.Round(o(1).Radius * 2, 3)}
    End Function
).GroupBy(Function(item) item.InnerDiameter * 10 & "x" & item.OuterDiameter * 10)


        Dim circleGroup
        For Each circleGroup In drilledCircleGroups
            Dim points As ObjectCollection = m_inventorApp.TransientObjects.CreateObjectCollection()
            For Each Circle In circleGroup
                points.Add(Circle.CenterSketchPoint)
            Next
            Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points)

            Dim depth As Double 'Unit: mm
            'Dim thread As String
            If IsSheetMetal Then depth = comp.Thickness.Value * 10

            'Create drilled holes according to different diameters
            ' Dim hole As HoleFeature
            Dim direction = PartFeatureExtentDirectionEnum.kPositiveExtentDirection

            'Uncomment and change these case statements to adapt to your need
            Dim thread
            Select Case circleGroup.Key 'Diameter, Unit: mm
                Case 6  'Unit: mm
                    depth = 35

                    'Change thread definition here if needed
                    thread = "M6x1"

                    'Create threaded hole
                    Dim tapinfo = comp.Features.HoleFeatures.CreateTapInfo(True, "ISO Metric profile", thread, "6H", True)
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, tapinfo, depth / 10, direction)
                Case 10, 12
                    depth = 50
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key / 10, depth / 10, direction)
                Case 16
                    depth = 75
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key / 10, depth / 10, direction)
                Case Else
                    comp.Features.HoleFeatures.AddDrilledByThroughAllExtent(placementDef, circleGroup.Key / 10, direction)
            End Select
        Next

        Dim circlesGroup
        Dim counterBoreCircles
        For Each circlesGroup In counterBoreCirclesGroups
            Dim points As ObjectCollection = m_inventorApp.TransientObjects.CreateObjectCollection()
            For Each counterBoreCircles In circlesGroup
                points.Add(counterBoreCircles.Center)
            Next
            Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points)

            Dim id As Double = circlesGroup.First().InnerDiameter
            Dim od As Double = circlesGroup.First().OuterDiameter
            Dim depth As Double 'Unit: mm
            Dim sinkDepth As Double 'Unit: mm
            Dim angle As Double = 100   'Unit: degree

            If IsSheetMetal Then depth = comp.Thickness.Value * 10

            'Create countersink or counterbore holes according to different diameters
            ' Dim hole As HoleFeature
            Dim direction = PartFeatureExtentDirectionEnum.kPositiveExtentDirection

            'Uncomment and change these case statements to adapt to your need
            Select Case circlesGroup.Key    '<Hole Diameter>x<CounterSink Diameter>, Unit: mm
                Case "6x14"
                    depth = 40
                    comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, id, depth / 10, direction, od, angle)
                Case "8x20"
                    depth = 50
                    comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, id, depth / 10, direction, od, angle)
                Case "12x35"
                    sinkDepth = 2
                    comp.Features.HoleFeatures.AddSpotFaceByThroughAllExtent(placementDef, id, direction, od, sinkDepth / 10)
                Case Else
                    sinkDepth = 3
                    comp.Features.HoleFeatures.AddCBoreByThroughAllExtent(placementDef, id, direction, od, sinkDepth / 10)
            End Select
        Next
0 Likes
Message 64 of 73

smilinger
Advisor
Advisor

I changed the code for you.

 

Imports Inventor

Module Module1
    Sub Main()
        Dim m_inventorApp As Inventor.Application
        m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

        Dim doc As PartDocument = m_inventorApp.ActiveDocument
        Dim comp As PartComponentDefinition = doc.ComponentDefinition

        Dim IsSheetMetal As Boolean = comp.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject

        Dim sketch1 As PlanarSketch = doc.ComponentDefinition.Sketches(1)
        Dim profile As Profile = sketch1.Profiles.AddForSolid(False)
        If IsSheetMetal Then
            Dim sheetMetalFeatures As SheetMetalFeatures = comp.Features
            Dim faceDef As FaceFeatureDefinition = sheetMetalFeatures.FaceFeatures.CreateFaceFeatureDefinition(profile)
            faceDef.Direction = PartFeatureExtentDirectionEnum.kNegativeExtentDirection
            sheetMetalFeatures.FaceFeatures.Add(faceDef)
        Else
            'Change the thickness for non-Sheetmetal parts here.
            Dim thickness = "6 mm"
            Dim extrudeDef = comp.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kNewBodyOperation)
            extrudeDef.SetDistanceExtent(thickness, PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
            comp.Features.ExtrudeFeatures.Add(extrudeDef)
        End If

        'Group circles by position
        Dim circleGroups = sketch1.SketchCircles.Cast(Of SketchCircle)().GroupBy(Function(c)
                                                                                     Dim point = c.CenterSketchPoint.Geometry
                                                                                     Return Math.Round(point.X, 3) & ", " & Math.Round(point.Y, 3)
                                                                                 End Function)

        'Group single circles by diameter
        Dim drilledCircleGroups = circleGroups.Where(Function(g) g.Count() = 1).Select(Function(g) g.Single()) _
            .GroupBy(Function(c) Math.Round(c.Geometry.Radius * 2 * 10, 3))

        'Group pairs of circles by diameters
        Dim counterBoreCirclesGroups = circleGroups.Where(Function(g) g.Count() = 2).Select(
            Function(g)
                Dim o = g.OrderBy(Function(c) c.Radius)
                Return New With {.Center = g.First().CenterSketchPoint, .InnerDiameter = Math.Round(o(0).Radius * 2, 3), .OuterDiameter = Math.Round(o(1).Radius * 2, 3)}
            End Function).GroupBy(Function(item) item.InnerDiameter * 10 & "x" & item.OuterDiameter * 10)

        For Each circleGroup In drilledCircleGroups
            Dim points As ObjectCollection = m_inventorApp.TransientObjects.CreateObjectCollection()
            For Each Circle In circleGroup
                points.Add(Circle.CenterSketchPoint)
            Next
            Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points)

            Dim depth As Double 'Unit: mm
            Dim thread As String
            Dim direction = PartFeatureExtentDirectionEnum.kPositiveExtentDirection

            If IsSheetMetal Then depth = CType(comp, SheetMetalComponentDefinition).Thickness.Value * 10

            'Create drilled holes according to different diameters
            'Uncomment and change these case statements to adapt to your need
            Select Case circleGroup.Key 'Diameter, Unit: mm
                Case 6 'Unit: mm
                    depth = 35

                    'Change thread definition here if needed
                    thread = "M6x1"

                    'Create threaded hole
                    Dim tapinfo = comp.Features.HoleFeatures.CreateTapInfo(True, "ISO Metric profile", thread, "6H", True)
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, tapinfo, depth / 10, direction)
                Case 10, 12
                    depth = 50
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key / 10, depth / 10, direction)
                Case 16
                    depth = 75
                    comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key / 10, depth / 10, direction)
                Case Else
                    comp.Features.HoleFeatures.AddDrilledByThroughAllExtent(placementDef, circleGroup.Key / 10, direction)
            End Select
        Next

        For Each circlesGroup In counterBoreCirclesGroups
            Dim points As ObjectCollection = m_inventorApp.TransientObjects.CreateObjectCollection()
            For Each counterBoreCircles In circlesGroup
                points.Add(counterBoreCircles.Center)
            Next
            Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points)

            Dim id As Double = circlesGroup.First().InnerDiameter
            Dim od As Double = circlesGroup.First().OuterDiameter
            Dim depth As Double 'Unit: mm
            Dim sinkDepth As Double 'Unit: mm
            Dim angle As Double = 100   'Unit: degree

            If IsSheetMetal Then depth = CType(comp, SheetMetalComponentDefinition).Thickness.Value * 10

            'Create countersink or counterbore holes according to different diameters
            ' Dim hole As HoleFeature
            Dim direction = PartFeatureExtentDirectionEnum.kPositiveExtentDirection

            'Uncomment and change these case statements to adapt to your need
            Select Case circlesGroup.Key '<Hole Diameter>x<CounterSink Diameter>, Unit: mm
                Case "6x14"
                    depth = 40
                    comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, id, depth / 10, direction, od, angle)
                Case "8x20"
                    depth = 50
                    comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, id, depth / 10, direction, od, angle)
                Case "12x35"
                    sinkDepth = 2
                    comp.Features.HoleFeatures.AddSpotFaceByThroughAllExtent(placementDef, id, direction, od, sinkDepth / 10)
                Case Else
                    sinkDepth = 3
                    comp.Features.HoleFeatures.AddCBoreByThroughAllExtent(placementDef, id, direction, od, sinkDepth / 10)
            End Select
        Next
    End Sub
End Module

 

0 Likes
Message 65 of 73

GeorgK
Advisor
Advisor

Thank you for your help. But I get an exception

 

Dim drilledCircleGroups = circleGroups.Where(Function(g) g.Count() = 1).Select(Function(g) g.Single()) _
            .GroupBy(Function(c) Math.Round(c.Geometry.Radius * 2 * 10, 3))

 

System.MissingMemberException
Nachricht = Der öffentliche Member Where für den Typ GroupedEnumerable(Of SketchCircle,Object,SketchCircle) wurde nicht gefunden.

0 Likes
Message 66 of 73

smilinger
Advisor
Advisor

Well, that's strange. Did you use exactly my code above?

 

Which version of Visual Studio are you using? You might also need to check you project settings.

0 Likes
Message 67 of 73

GeorgK
Advisor
Advisor

I use your code with Visual Studio 2017. The project is fine. All other code work perfect.

0 Likes
Message 68 of 73

smilinger
Advisor
Advisor

Sorry I have no clue why, it works for me. I am also using VS2017.

 

Besides, why you decided to run it as a standalone app? it is slower then run it inside inventor, you can just save the ilogic code as external rules, and run it for all the parts needed.

0 Likes
Message 69 of 73

GeorgK
Advisor
Advisor

Is it possible that you post your VS 2017 project? I run it in an external program to test the code. Normally I use it in an add-in with a nice button. It's easier to have an add-in as a lot of iLogic files.

0 Likes
Message 71 of 73

GeorgK
Advisor
Advisor

Hello @smilinger,

 

thank's for sharing your code. I use Option Strict. I get the following errors:

 

"CenterSketchPoint" is not a member from "Circle"
"Circle" is an interface type and can not be used as an expression
"circlesGroup" was not declared
"counterBoreCircles" was not declared

 

The iLogic code works perfect.

0 Likes
Message 72 of 73

smilinger
Advisor
Advisor

Then try turn Option Strict off, and turn Option Infer on.

 

Or leave Option Strict on, but turn Option Infer on, add some type casts as needed. For LINQ code, Option Infer is a must, otherwise it will be awkward to declare anonymous types.

0 Likes
Message 73 of 73

GeorgK
Advisor
Advisor

Yes. That's the problem with the declaration of anonymous types. I use the iLogic code and run it from my add-in.

 

@smilinger Thank you very much.

0 Likes