create lines and tangent arcs in iLogic

create lines and tangent arcs in iLogic

miechh
Collaborator Collaborator
827 Views
7 Replies
Message 1 of 8

create lines and tangent arcs in iLogic

miechh
Collaborator
Collaborator

Hello all, hope you are doing well.

 

I have this challenge in which I want to draw a 2d sketch in a part with the use of iLogic. The sketch consists of 1 straight horizontal line, starting let's say, on X0, Y 100. Coincident with the endpoint of and tangent to that line follows a 2d Arc, followed by another 2d Arc, tangent to the previous arc. Then this combination of line and arcs must be offset to the outside, and finally closed at both ends. See picture below.

 

I've done quite some iLogic coding in the recent past, but never used iLogic to actually draw sketches/geometry. So my question is, where do I start? What's the best approach to draw a sketch like this? Is iLogic the best choice to do this, or should I revert to VBA?

 

Thanks for your support!

2d Line-arc-arc.png

 

 


Product Design Suite 2024
Inventor 2024 (v 28.20.27200.0000), Vault Basic 2024
Fusion 360
HP Workstation Z4
Intel Xeon 3.4GHz
32GB RAM
Windows 10 Professional (64bit)
0 Likes
828 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @miechh.  Which coding platform you should use is mostly a matter of opinion/preference, but I would advise you to stick with iLogic (which uses vb.net), instead of VBA, because it is a newer system, code can be much more condensed, easier error handling, and avoids the current security problems related to VBA.

Writing code for drawing sketches not only just 'sucks' in general, but is extremely custom, and not easily reusable by others.  You are probably not getting many responses because not many folks want to volunteer for writing that type or code for others.  We don't even like writing our own code for creating sketch geometry from scratch, if we can avoid it any other possible way, especially if it is complex (not just simple rectangles/circles).

I started a little something for you, as an example, but it is not perfect yet by a long shot, and does not include any dimensional constraints yet.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisDoc.Document
	Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
	Dim oXZPlane As WorkPlane = oPDef.WorkPlanes.Item(2)
	Dim oSketch As PlanarSketch = oPDef.Sketches.Add(oXZPlane)
	'eliminating possible projected origin point (there is a setting to include this)
	If oSketch.SketchPoints.Count > 0 Then oSketch.SketchPoints.Item(1).Delete
	Dim oSLs As SketchLines = oSketch.SketchLines
	Dim oSAs As SketchArcs = oSketch.SketchArcs
	Dim oGCs As GeometricConstraints = oSketch.GeometricConstraints
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oInnerGeom As ObjectCollection = oTO.CreateObjectCollection
	Dim oP1 As Point2d = oTG.CreatePoint2d(0, 100)
	Dim oP2 As Point2d = oTG.CreatePoint2d(33, 100)
	Dim oSL1 As SketchLine = oSLs.AddByTwoPoints(oP1, oP2)
	oGCs.AddHorizontal(oSL1)
	oInnerGeom.Add(oSL1)
	'arcs naturally draw in counter-clockwise, from start to end, so keep that fact in mind
	Dim oCp1 As Point2d = oTG.CreatePoint2d(33, 66)
	Dim oP3 As Point2d = oTG.CreatePoint2d(50, 75)
	Dim oSA1 As SketchArc = oSAs.AddByCenterStartEndPoint(oCp1, oSL1.EndSketchPoint, oP3, False)
	Try : oGCs.AddCoincident(oSL1.EndSketchPoint, oSA1.StartSketchPoint) : Catch : End Try
	oGCs.AddTangent(oSL1, oSA1)
	oInnerGeom.Add(oSA1)
	Dim oCp2 As Point2d = oTG.CreatePoint2d(-50, 0)
	Dim oP4 As Point2d = oTG.CreatePoint2d(100, 0)
	Dim oSA2 As SketchArc = oSAs.AddByCenterStartEndPoint(oCp2, oSA1.StartSketchPoint, oP4, False)
	Try : oGCs.AddCoincident(oSA1.StartSketchPoint, oSA2.StartSketchPoint) : Catch : End Try
	oGCs.AddTangent(oSA1, oSA2)
	oInnerGeom.Add(oSA2)
	Dim oOffsetGeom As SketchEntitiesEnumerator
	Try
		oOffsetGeom = oSketch.OffsetSketchEntitiesUsingDistance(oInnerGeom, 5, True, True, True)
	Catch
		MsgBox("Offset method failed.", vbExclamation,"")
	End Try
	
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

miechh
Collaborator
Collaborator
Thanks for your reply WCrihfield. I've put this in the 'freezer' and going for another, simpler approach for now. I will try your code someday.

Product Design Suite 2024
Inventor 2024 (v 28.20.27200.0000), Vault Basic 2024
Fusion 360
HP Workstation Z4
Intel Xeon 3.4GHz
32GB RAM
Windows 10 Professional (64bit)
0 Likes
Message 4 of 8

tbillsKGJL5
Participant
Participant

Does anyone know how to get the coordinates in the point callouts like this

oTG.CreatePoint2d(-50, 0)

to be expressed in Inches directly? I'm willing to multiply all of the sketch coordinates in my code by 2.54, but I really don't want to if I don't have to.

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Hi @tbillsKGJL5.  I am not sure I understand what you want here.  Are you saying that you already have an existing sketch, and now you want to run some code on that sketch that will find each SketchPoint in that sketch, and report its coordinates in inches?  Or are you trying to create a sketch, but want to be able to specify coordinates in inches as you write the code?  Please explain the 'whole' situation in more detail, so we can fully understand what you are trying to do, and be able to help you better.

 

There are such things as 'document units geometry', but they are unique to the iLogic add-in (not Inventor API or VBA).  Those object can be created starting from the 'ThisDoc.Geometry' phrase, where that property returns an IGeometry Interface object.  That object has about 13 methods for creating geometry using document units.  However, the objects you get as the result of those methods are different objects than the ones you get from the normal Inventor API TransientGeometry object and its methods.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

Curtis_Waguespack
Consultant
Consultant

@tbillsKGJL5 

 

There are some API samples that might be of interest:

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=Sketch_SketchLines_Sample

 

As for inputting in inches, Inventor's internal units is centimeters, all inputs will need to be translated.

Here is an example taken from this link that uses the document's units to create a conversion factor, to convert the point coordinates from the internal cm to the document's units ( Tools > Documents Settings > Units tab)

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Dim oDoc As PartDocument = ThisDoc.Document

' a reference to the active sketch.
Dim oSketch As PlanarSketch
oSketch = oDoc.ComponentDefinition.Sketches.Item("Sketch1")
oSketch.Edit

'set a reference to the transient geometry collection.
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create a new transaction to wrap the construction of the three lines
' set into a single undo.
Dim oTransaction As Transaction
oTransaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Create Triangle Sample")

'*** Define Units Of Measure Conversion       
Dim oUOM As UnitsOfMeasure
oUOM = oDoc.UnitsOfMeasure
oInvUnits = UnitsTypeEnum.kCentimeterLengthUnits
oConversion = oUOM.ConvertUnits(1, oUOM.LengthUnits, oInvUnits)

'input points
Dim Pt1 As Point2d = oTG.CreatePoint2d(0, 0)
Dim Pt2 As Point2d = oTG.CreatePoint2d(4 * oConversion, 0)
Dim Pt3 As Point2d = oTG.CreatePoint2d(2 * oConversion, 3 * oConversion)


' Create the first line of the triangle. This uses two transient points as
' input to definethe coordinates of the ends of the line. Since a transient
' point is input a sketch point is automatically created at that location and
' the line is attached to it.
Dim oLines(0 To 2) As SketchLine
oLines(0) = oSketch.SketchLines.AddByTwoPoints(Pt1, Pt2)

' Create a sketch line that is connected to the sketch point the previous lines
' end point is connected to. This will automatically create the constraint to
' tie the new line to the sketch point the previous line is also connected to.
' This will result in the the two lines being connected since they're both tied
' to the same sketch point.
oLines(1) = oSketch.SketchLines.AddByTwoPoints(oLines(0).EndSketchPoint, Pt3)

' Create a third line and connect it to the start point of the first line and the
' end point of the second line. This will result in a connected triangle.
oLines(2) = oSketch.SketchLines.AddByTwoPoints(oLines(1).EndSketchPoint, oLines(0).StartSketchPoint)

' End the transaction
oTransaction.End

iLogicVb.UpdateWhenDone = True

 

 

 

EESignature

0 Likes
Message 7 of 8

tbillsKGJL5
Participant
Participant

I appreciate the effort, but I think I've found another way to accomplish what I'm looking for, rather than hard-coding a sketch & its geometry every time I need a Cut Extrusion feature at the assembly level. I didn't realize that participation in a feature was an aspect of each component in an assembly.

 

Unrelated, but that Transaction creation code is extremely helpful for other parts of our workflow; Thank you very much for including that!

0 Likes
Message 8 of 8

tbillsKGJL5
Participant
Participant

I'm sorry; I should've given this a bit more thought as to how it related to my use case before asking this.

I think I found a workaround at a higher level that makes sketch manipulation by iLogic unnecessary.

 

Still, I appreciate your help in pointing me in the right direction!

0 Likes