ilogic to convert circles to holes

ilogic to convert circles to holes

FProcp
Collaborator Collaborator
4,462 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,463 Views
72 Replies
Replies (72)
Message 41 of 73

FProcp
Collaborator
Collaborator

Thank you so much. This works perfectly Smiley Happy

Please keep me informed on your feasibility study.

Franco
GMT +08:00
0 Likes
Message 42 of 73

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @FProcp,

 

Its working with Part document and posted the iLogic code as well.

 

Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Dim oSK As PlanarSketch
oSK = oDoc.ComponentDefinition.Sketches(1)
Dim objCol As ObjectCollection
objCol = ThisApplication.TransientObjects.CreateObjectCollection

For Each oskarc In oSK.SketchArcs
    objCol.Add(oskarc)
Next
For Each oskline In oSK.SketchLines
    objCol.Add (oskline)
Next

Dim oRadiusList As ArrayList = New ArrayList 
Dim oCircle As SketchCircle
For Each oCircle In oSK.SketchCircles	
	If oRadiusList.Contains(Math.Round(oCircle.Radius, 3)) = False Then		
		oRadiusList.Add(Math.Round(oCircle.Radius, 3))
	End If	
Next

Dim oCircleCol(oRadiusList.Count) As ObjectCollection
For i = 0 To oRadiusList.Count 
    oCircleCol(i) = ThisApplication.TransientObjects.CreateObjectCollection
Next

For j = 0 To oRadiusList.Count - 1
	For Each oCircle In oSK.SketchCircles 
		If Math.Round(oCircle.Radius, 3).ToString = oRadiusList.Item(j).ToString Then
			oCircleCol(j).Add(oCircle)
		End If
	Next
Next 

Dim oP1 As Profile
oP1 = oSK.Profiles.AddForSolid(False, objCol)
oD1 = oDoc.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oP1, kNewBodyOperation)
oD1.SetDistanceExtent("6 mm", kSymmetricExtentDirection)
oF2 = oDoc.ComponentDefinition.Features.ExtrudeFeatures.Add(oD1)


'Create hole feature for the rest center point of the circles
Dim oTempCol As ObjectCollection
oTempCol = ThisApplication.TransientObjects.CreateObjectCollection
Dim oPD As HolePlacementDefinition
For i = 0 To oRadiusList.Count - 1
    oTempCol.Clear
    Dim dDiameter As Double
    For Each oCircle In oCircleCol(i)
        oTempCol.Add (oCircle.CenterSketchPoint)
    Next
	
    dDiameter = oCircleCol(i).Item(1).Radius * 2
	dDiameter = Math.Round(dDiameter, 1)
    oPD = oDoc.ComponentDefinition.Features.HoleFeatures.CreateSketchPlacementDefinition(oTempCol)
    Dim oHole As HoleFeature
	oHole = oDoc.ComponentDefinition.Features.HoleFeatures.AddDrilledByThroughAllExtent(oPD, dDiameter, kSymmetricExtentDirection)		
	dDiameter = dDiameter * 10
	oHole.Name = "HoleD" + dDiameter.ToString
Next

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 43 of 73

GeorgK
Advisor
Advisor

Hello Chandra,

 

thats a realy great tool. Is it possible to check for several diameter with the same origin and use only the inner diameter?

 

Hole.png

 

Thank you

 

Georg

0 Likes
Message 44 of 73

FProcp
Collaborator
Collaborator
We sometimes have drawings with two holes located at the same place, one is larger than the other. These represent counterbored or countersunk holes.
It would be good if it could turn double holes into counterbored holes of any depth. I could then go back and edit them later.
There usually aren't many of those so it's a minor issue.
Franco
GMT +08:00
0 Likes
Message 45 of 73

GeorgK
Advisor
Advisor

The counterbored holes could use the standard hole depth.

0 Likes
Message 46 of 73

Anonymous
Not applicable

Can you try this for me Chandra? I can't seem to get this to work. It adds an extrusion instead of a hole? Simple piece of 10mm plate with two extruded circular features

 

Thanks for your time with this....

0 Likes
Message 47 of 73

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Anonymous,

 

To work with below iLogic code for part document, only sketch is enough. The modified part is attached with this post.

 

Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Dim oSK As PlanarSketch
oSK = oDoc.ComponentDefinition.Sketches(1)
Dim objCol As ObjectCollection
objCol = ThisApplication.TransientObjects.CreateObjectCollection

For Each oskarc In oSK.SketchArcs
    objCol.Add(oskarc)
Next
For Each oskline In oSK.SketchLines
    objCol.Add (oskline)
Next

Dim oRadiusList As ArrayList = New ArrayList 
Dim oCircle As SketchCircle
For Each oCircle In oSK.SketchCircles	
	If oRadiusList.Contains(Math.Round(oCircle.Radius, 3)) = False Then		
		oRadiusList.Add(Math.Round(oCircle.Radius, 3))
	End If	
Next

Dim oCircleCol(oRadiusList.Count) As ObjectCollection
For i = 0 To oRadiusList.Count 
    oCircleCol(i) = ThisApplication.TransientObjects.CreateObjectCollection
Next

For j = 0 To oRadiusList.Count - 1
	For Each oCircle In oSK.SketchCircles 
		If Math.Round(oCircle.Radius, 3).ToString = oRadiusList.Item(j).ToString Then
			oCircleCol(j).Add(oCircle)
		End If
	Next
Next 

Dim oP1 As Profile
oP1 = oSK.Profiles.AddForSolid(False, objCol)
oD1 = oDoc.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oP1, kNewBodyOperation)
oD1.SetDistanceExtent("6 mm", kSymmetricExtentDirection)
oF2 = oDoc.ComponentDefinition.Features.ExtrudeFeatures.Add(oD1)


'Create hole feature for the rest center point of the circles
Dim oTempCol As ObjectCollection
oTempCol = ThisApplication.TransientObjects.CreateObjectCollection
Dim oPD As HolePlacementDefinition
For i = 0 To oRadiusList.Count - 1
    oTempCol.Clear
    Dim dDiameter As Double
    For Each oCircle In oCircleCol(i)
        oTempCol.Add (oCircle.CenterSketchPoint)
    Next
	
    dDiameter = oCircleCol(i).Item(1).Radius * 2
	dDiameter = Math.Round(dDiameter, 2)
    oPD = oDoc.ComponentDefinition.Features.HoleFeatures.CreateSketchPlacementDefinition(oTempCol)
    Dim oHole As HoleFeature
	oHole = oDoc.ComponentDefinition.Features.HoleFeatures.AddDrilledByThroughAllExtent(oPD, dDiameter, kSymmetricExtentDirection)		
	dDiameter = dDiameter * 10
	oHole.Name = "HoleD" + dDiameter.ToString
Next

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 48 of 73

Anonymous
Not applicable

thanks Chandra

 

I am on v2017 so cant open. Is it a part with only one sketch or multiple sketches... ie one sketch for the plate , one sketch for a hole and another sketch for 2 holes?

 

or is it all in one sketch...or something else?

 

 

0 Likes
Message 49 of 73

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Everything should be in one sketch.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 50 of 73

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

is it possible to add the countersunk hole function?

 

Thank you very much

 

Georg

0 Likes
Message 51 of 73

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

To create Countersunk hole, Collecting a concentric circles from sketch looks challenging task.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 52 of 73

FProcp
Collaborator
Collaborator
Of course it's possible. It's just a matter of investing the time in working out a method.
I hope Chandra will take on this challenge.
Franco
GMT +08:00
0 Likes
Message 54 of 73

smilinger
Advisor
Advisor

CounterSink or CounterBore holes are also doable, just a little complicated:

 

Imports System.Linq
Imports Inventor

Dim doc As PartDocument = ThisDoc.Document
Dim comp As PartComponentDefinition = doc.ComponentDefinition

'Assuming the 2nd sketch is for holes Dim sketch1 As PlanarSketch = doc.ComponentDefinition.Sketches(2) 'Group circles by position Dim circleGroups = sketch1.SketchCircles.Cast(Of SketchCircle)().GroupBy( Function(c) Dim point = c.CenterSketchPoint.Geometry Return Round(point.X, 3) & ", " & 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) Round(c.Geometry.Radius * 2, 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 = Round(o(0).Radius*2, 3), .OuterDiameter = 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 = ThisApplication.TransientObjects.CreateObjectCollection() For Each Circle In circleGroup points.Add(Circle.CenterSketchPoint) Next Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points) 'Create drilled holes according to different diameters If circleGroup.Key = 1 Then comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key, 5, PartFeatureExtentDirectionEnum.kPositiveExtentDirection) Else If circleGroup.Key = 1.6 Then comp.Features.HoleFeatures.AddDrilledByDistanceExtent(placementDef, circleGroup.Key, 7.2, PartFeatureExtentDirectionEnum.kPositiveExtentDirection) Else comp.Features.HoleFeatures.AddDrilledByThroughAllExtent(placementDef, circleGroup.Key, PartFeatureExtentDirectionEnum.kPositiveExtentDirection) End If Next For Each circlesGroup In counterBoreCirclesGroups Dim points As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection() For Each counterBoreCircles In circlesGroup points.Add(counterBoreCircles.Center) Next Dim placementDef As HolePlacementDefinition = comp.Features.HoleFeatures.CreateSketchPlacementDefinition(points) 'Create countersink or counterbore holes according to different diameters If circlesGroup.Key = "6x14" Then comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, circlesGroup.First().InnerDiameter, 8, PartFeatureExtentDirectionEnum.kPositiveExtentDirection, circlesGroup.First().OuterDiameter, 90) Else If circlesGroup.Key = "8x20" Then comp.Features.HoleFeatures.AddCSinkByDistanceExtent(placementDef, circlesGroup.First().InnerDiameter, 12, PartFeatureExtentDirectionEnum.kPositiveExtentDirection, circlesGroup.First().OuterDiameter, 90) Else comp.Features.HoleFeatures.AddCBoreByThroughAllExtent(placementDef, circlesGroup.First().InnerDiameter, PartFeatureExtentDirectionEnum.kPositiveExtentDirection, circlesGroup.First().OuterDiameter, 0.2) End If Next
0 Likes
Message 55 of 73

GeorgK
Advisor
Advisor

I think it is better to write an add-in an store the data in DataTables. It's much easier to sort and filter the data.

0 Likes
Message 56 of 73

FProcp
Collaborator
Collaborator

One of the issues with Chandra's routine is that it extrudes the plate up (towards you) and it should extrude down (away from you).

If you are looking at the top view of the plate, you would see the counter-bored or counter-sunk holes on the top plane of the plate and going down into the plate.

To convert some of the holes to countersunk holes I currently have to go to the first face extrusion and switch it to extrude in the opposite direction and then edit some holes to become countersunk.

It would be nicer if it did this originally.

Franco
GMT +08:00
0 Likes
Message 57 of 73

smilinger
Advisor
Advisor
Accepted solution

This code should suffice in most cases, including sheet metal, countersink or counterbore holes:

 

Imports System.Linq
Imports Inventor
Dim doc As PartDocument = ThisDoc.Document 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 = sketch1.SketchCircles.Cast(Of SketchCircle)().GroupBy( Function(c) Dim point = c.CenterSketchPoint.Geometry Return Round(point.X, 3) & ", " & 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) 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 = Round(o(0).Radius*2, 3), .OuterDiameter = 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 = ThisApplication.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 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 = ThisApplication.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
Message 58 of 73

FProcp
Collaborator
Collaborator

Hi

 

How difficult would it be to place the hole diameter in the name of the hole feature to one decimal place?

For example re-name a Ø14.3 hole to "HoleD14.3".

This would make this code perfect Smiley Happy

Franco
GMT +08:00
0 Likes
Message 59 of 73

smilinger
Advisor
Advisor
Accepted solution

Can be complicated for countersink holes, I suggest turn on the inventor option to show the infomation automatically.

 

2018-01-19_22-22-35.png

0 Likes
Message 60 of 73

FProcp
Collaborator
Collaborator

Yes, this is excellent Smiley Wink

Franco
GMT +08:00
0 Likes