Circular pattern iLogic / VBA - Help required

Circular pattern iLogic / VBA - Help required

nsatheesan34D64
Advocate Advocate
1,021 Views
12 Replies
Message 1 of 13

Circular pattern iLogic / VBA - Help required

nsatheesan34D64
Advocate
Advocate

Hi all,

 

Need your help with automation. I need to create a pattern like this wrapped around cylinder. 

Later, holes need to be cut at all locations. The snippets in iLogic ipt environment is resulting in error as it is not an assembly file. Could anyone please help

 

Capture.PNG

0 Likes
Accepted solutions (3)
1,022 Views
12 Replies
Replies (12)
Message 2 of 13

aelqabbany
Advocate
Advocate

Hi @nsatheesan34D64 ,

 

Give this iLogic script a shot. Let me know if you run into any issues:

 

 

'Check that script is being run in a part document
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MessageBox.Show("Please re-run this script in a part document.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End If

'Set the part document variables
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition

'Prompt user to select cylinderical face
Dim oCylinderFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select cylinder face")

'Prompt user to select the plane that the first hole will be perpendicular to 
Dim oRefPlane = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPlanarEntities, "Select the plane that you want the first hole to be perpendicular to")

'Get the cylinder mathematical object
Dim oCylinder As Cylinder = oCylinderFace.Geometry

'Get the cylinder radius
Dim oCylinderRadius As Double = oCylinder.Radius

'Generate the cylinder line
Dim oCylinderAxis As WorkAxis = oCompDef.WorkAxes.AddByRevolvedFace(oCylinderFace)
Try
	oCompDef.WorkAxes.Item("CylinderAxis").Delete(False)
Catch
End Try
oCylinderAxis.Name = "CylinderAxis"
oCylinderAxis.Visible = False

'Get user input
Dim HoleDiameter As Double = InputBox("Enter hole diameters", "Enter hole diameters as a decimal in cm", 1)
Dim HoleDepth As Double = InputBox("Enter hole depth as a decimal in cm. If you want it to go all the way througm enter a value way larger than the cylinder diameter", "Enter hole depth", 1)
Dim NumOfHoles As Integer = InputBox("Enter the number of holes", "Enter the number of holes as a whole number", 4)
Dim AngleBetweenHoles As Double = InputBox("Enter angle between holes", "Enter angle between holes in degrees", 90)
Dim HeightDifference As Double = InputBox("Enter height difference between holes in cm", "Enter height difference between holes in decimals", 5)

'Arraylist to hold the workplanes
Dim HoleWorkPlanes As New ArrayList

'Set the initial plane to work off of
Try
	Dim InitialPlane As WorkPlane = oCompDef.WorkPlanes.AddByPlaneAndTangent(oRefPlane, oCylinderFace, oCompDef.WorkPoints(1).Point)
	HoleWorkPlanes.Add(InitialPlane)
	Try
		oCompDef.WorkPlanes.Item("InitialPlane").Delete(False)
	Catch
	End Try
	InitialPlane.Name = "InitialPlane"
	InitialPlane.Visible = False
Catch
	MessageBox.Show("Please re-run this script and select a plane that can be tangent to the cylinder face.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
End Try
	

For i = 0 To NumOfHoles - 1
	'Generate the planes to hold the sketches
	Dim wPlaneAngleInRads As Double = AngleBetweenHoles * PI / 180
	Dim TempPlane As WorkPlane = oCompDef.WorkPlanes.AddByLinePlaneAndAngle(oCylinderAxis, HoleWorkPlanes(i), wPlaneAngleInRads, True)
	Dim HolePlane As WorkPlane = oCompDef.WorkPlanes.AddByPlaneAndOffset(TempPlane, oCylinderRadius)
	HolePlane.Visible = False
	HolePlane.Name = "HolePlane" & Str(i + 1)
	HoleWorkPlanes.Add(HolePlane)
	
	'Draw the sketches with circles
	Dim HoleSketch As PlanarSketch = oCompDef.Sketches.Add(HolePlane)
	Dim Hole2DCenter As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(i * HeightDifference,0)
	HoleSketch.SketchCircles.AddByCenterRadius(Hole2DCenter,HoleDiameter/2)
	HoleSketch.Name = "HoleSketch" & Str(i + 1)
	
	'Extrude the holes
	Dim oProfile As Profile = HoleSketch.Profiles.AddForSolid
	Dim HoleCut As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
	HoleCut.SetDistanceExtent(HoleDepth, kNegativeExtentDirection)
	Dim Hole As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(HoleCut)
	Hole.Name = "Hole " & Str(i + 1)
Next

 

 

This is what the end result looked like for me:

HolesOnCylinder.png

 

0 Likes
Message 3 of 13

nsatheesan34D64
Advocate
Advocate

Hi @aelqabbany  Thank you for the code. Its something which is inline with what i wanted. But i have two issues

1) The start point of hole is not in the location indended. I have a point, which i would like to be beginning of pattern.

2) Each row has to have 40nos of holes in pattern. For eg: bottom row, 40 holes ; row above - slightly offset - 40 holes etc.

 

Could you please help.

 

 

0 Likes
Message 4 of 13

aelqabbany
Advocate
Advocate
Could you upload a non-confidential part, as well as a screenshot of what you're trying to do?
I want to be sure that I completely understand the requirements.
0 Likes
Message 5 of 13

nsatheesan34D64
Advocate
Advocate

Hi @aelqabbany pls find attached a sample part. hope it will be clear now.

 

also is there any code to revolve/extrude parts of a sketch as individual solids?

0 Likes
Message 6 of 13

aelqabbany
Advocate
Advocate
Accepted solution

Hi @nsatheesan34D64,

 

Give this a shot. The circular feature is bugged in Inventor 2020, so I developed and tested this in Inventor 2022.

I've placed a screencast here to show you how the script works.

 

'Check that script is being run in a part document
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MessageBox.Show("Please re-run this script in a part document.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End If

'Set the part document variables
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition

'Prompt user to select cylinderical face
Dim oCylinderFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select cylinder face")

'Prompt user to select the starting point
Dim StartingPoint As SketchPoint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select starting point")
Dim StartingPointSketch As PlanarSketch = StartingPoint.Parent
Dim oRefPlane As WorkPlane = StartingPointSketch.PlanarEntity

StartingPointOffset = ThisApplication.MeasureTools.GetMinimumDistance(StartingPointSketch.OriginPointGeometry, StartingPoint.Geometry3d)
'MessageBox.Show(StartingPointOffset)

'Get the cylinder mathematical object
Dim oCylinder As Cylinder = oCylinderFace.Geometry

'Get the cylinder radius
Dim oCylinderRadius As Double = oCylinder.Radius

'Generate the cylinder line
Dim oCylinderAxis As WorkAxis = oCompDef.WorkAxes.AddByRevolvedFace(oCylinderFace)
Try
	oCompDef.WorkAxes.Item("CylinderAxis").Delete(False)
Catch
End Try
oCylinderAxis.Name = "CylinderAxis"
oCylinderAxis.Visible = False

'Get user input
Dim HoleDiameter As Double = InputBox("Enter hole diameters", "Enter hole diameters as a decimal", 1)
Dim HoleDepth As Double = InputBox("Enter hole depth as a decimal. If you want it to go all the way througm enter a value way larger than the cylinder diameter", "Enter hole depth", 1)
Dim NumOfHoles As Integer = InputBox("Enter the number of rows", "Enter the number of rows as a whole number", 4)
Dim AngleBetweenHoles As Double = InputBox("Enter angle offset between rows", "Enter angle offset between rows", 4.7)
Dim HeightDifference As Double = InputBox("Enter height difference between rows", "Enter height difference between rows in decimals", 1)
Dim HolesPerRow As Integer = InputBox("Enter the number of holes for each row", "Enter the number of holes for each row as a whole number", 40)

'Arraylist to hold the workplanes
Dim HoleWorkPlanes As New ArrayList

'Set the initial plane to work off of
Try
	Dim InitialPlane As WorkPlane = oCompDef.WorkPlanes.AddByPlaneAndTangent(oRefPlane, oCylinderFace, oCompDef.WorkPoints(1).Point)
	HoleWorkPlanes.Add(InitialPlane)
	Try
		oCompDef.WorkPlanes.Item("InitialPlane").Delete(False)
	Catch
	End Try
	InitialPlane.Name = "InitialPlane"
	InitialPlane.Visible = False
Catch
	MessageBox.Show("Please re-run this script and select a plane that can be tangent to the cylinder face.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
End Try
	


For i = 0 To NumOfHoles - 1
	Dim HoleCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection	
	'Generate the planes to hold the sketches
	Dim wPlaneAngleInRads As Double = AngleBetweenHoles * PI / 180
	Dim TempPlane As WorkPlane = oCompDef.WorkPlanes.AddByLinePlaneAndAngle(oCylinderAxis, HoleWorkPlanes(i), wPlaneAngleInRads, True)
	Dim HolePlane As WorkPlane = oCompDef.WorkPlanes.AddByPlaneAndOffset(TempPlane, oCylinderRadius)
	HolePlane.Visible = False
	HolePlane.Name = "HolePlane" & Str(i + 1)
	HoleWorkPlanes.Add(HolePlane)
	
	'Draw the sketches with circles
	Dim HoleSketch As PlanarSketch = oCompDef.Sketches.Add(HolePlane)
	Dim Hole2DCenter As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(i * HeightDifference + StartingPointOffset,0)
	HoleSketch.SketchCircles.AddByCenterRadius(Hole2DCenter,HoleDiameter/2)
	HoleSketch.Name = "HoleSketch" & Str(i + 1)
	
	'Extrude the holes
	Dim oProfile As Profile = HoleSketch.Profiles.AddForSolid
	Dim HoleCut As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
	HoleCut.SetDistanceExtent(HoleDepth, kNegativeExtentDirection)
	Dim Hole As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(HoleCut)
	Hole.Name = "Hole " & Str(i + 1)
	HoleCol.Add(Hole)
	
	'Make circular pattern on each row
	Dim HolesPerRowDef As CircularPatternFeatureDefinition = oCompDef.Features.CircularPatternFeatures.CreateDefinition(HoleCol,oCylinderAxis,True,HolesPerRow,"360 deg",True)
	Dim HolesCircularPattern As CircularPatternFeature = oCompDef.Features.CircularPatternFeatures.AddByDefinition(HolesPerRowDef)
	HolesCircularPattern.Name = "HolePattern" & Str(i + 1)

	HoleCol.Clear
Next
	

 

0 Likes
Message 7 of 13

nsatheesan34D64
Advocate
Advocate
Accepted solution
Hi.. I am using Inventor 2021. The issue is I am not able to select the work point to start running the remaining portion of the program. Is it due to any version mismatch?
0 Likes
Message 8 of 13

aelqabbany
Advocate
Advocate

You need to select a sketch point, not a. workpoint. Please watch the screencast that I link to in my prior post, to see how to set one up.

0 Likes
Message 9 of 13

nsatheesan34D64
Advocate
Advocate

Hi @aelqabbany The code is good. but I am finding it that, for some reason, the pattern is coming almost centre of the cylinder, not at bottom. This messagebox, 

MessageBox.Show(StartingPointOffset)

 is returning 0.

 

Also, I am beginning with Inventor iLogic and VBA programming. could you please tell me where i can find more resources, code sample or perhaps a book? Thanks a lot. sincerely appreciate your help.  

0 Likes
Message 10 of 13

aelqabbany
Advocate
Advocate
Accepted solution

Hi @nsatheesan34D64, at this point I would need you to send me the actual model that you are working off of, so I can see exactly how your cylinder and starting point are oriented. Hopefully someone more knowledgeable can pitch in.

 

I learned iLogic the same way that you are - by posting questions on the forums and trying to understand the answers. I wish that there was a comprehensive A-Z guide on learning iLogic, but I have yet to find one. Once again, hopefully someone more knowledgeable can pitch in.

 

@WCrihfield @JelteDeJong @Michael.Navara @Ralf_Krieg @A.Acheson 

0 Likes
Message 11 of 13

A.Acheson
Mentor
Mentor

@nsatheesan34D64  I agree with @aelqabbany  you will need to provide the ipt model you are working with which will do one of two things figure out how you have manually drawn this and then this can be translated to the ilogic version. Also supply images and or a video of how you have implemented this.  I have no great experience with feature creation using API as my experience is self thought like @aelqabbany so cannot offer too much advice on its interaction other than pouring hours into what each line is doing. 

 

I would suggest for vba samples to started with the API help that comes with the software. If you get lucky you can find a sample and then dissect its operation.  You will need to manually created the part then replicate the feature creation through API, looking then to search the help documents to find the objects, methods and properties required. There is no fast tracking this operation. If you get lucky you might find a sample that someone has debugged on this forum in vb.net or in this case that @aelqabbany  has generously put together.

AAcheson_0-1641254791869.png

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 12 of 13

nsatheesan34D64
Advocate
Advocate

@A.Acheson @aelqabbany Thank you guys. I figured it out. The problem was in the way I created the sketch point. When I create a surface tangent to cylinder & create sketch point [as shown by @aelqabbany ] in his screengrab, the code works perfectly. Thanks again. I will go through the API and the forum. Thanks a lot @aelqabbany Much appreciated.

Message 13 of 13

aelqabbany
Advocate
Advocate
You're very welcome
0 Likes