Trying to create a sketch with corner radius's, I could use some help

Trying to create a sketch with corner radius's, I could use some help

chris
Advisor Advisor
238 Views
4 Replies
Message 1 of 5

Trying to create a sketch with corner radius's, I could use some help

chris
Advisor
Advisor

I would like to create a sketch on the YX plane, where I have a line drawn at a length defined by a parameter called "Length" that follows the "X" axis and is centered on the origin. At each end of that line drawn along the X axis I'd like another line drawn in the negative Y direction, at a length determined by the parameter "Length2", remember the start of that line is also the end point of the first line. Further more I'd like the point where each line meets the first line to have a radius that is a percentage of the "length2", that percentage will be controlled by the parameter "Radius"

 

This seems like it might be an easy task, but getting the radius to work is proving to be very difficult.

I have the rule working that creates the below "3" lines, (no raidus)

 

 

chris_0-1751037860381.png

 

Sub Main()
' Simple iLogic code to draw 3 lines forming a U-shape
' No radius functionality - just basic lines

' Ensure parameters exist with default values
If Not ParameterExists("Length") Then
Parameter("Length") = 10.0 ' Default 10 cm
End If

If Not ParameterExists("Length2") Then
Parameter("Length2") = 5.0 ' Default 5 cm
End If

' Get the active part document
Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument

' Find or create the sketch on XY plane
Dim oSketch As PlanarSketch = Nothing

' Look for existing sketch
For i As Integer = 1 To oPartDoc.ComponentDefinition.Sketches.Count
If oPartDoc.ComponentDefinition.Sketches.Item(i).Name = "ParametricSketch" Then
oSketch = oPartDoc.ComponentDefinition.Sketches.Item(i)
Exit For
End If
Next

' Create new sketch if not found
If oSketch Is Nothing Then
Dim oWorkPlane As WorkPlane = oPartDoc.ComponentDefinition.WorkPlanes.Item(3) ' XY Plane
oSketch = oPartDoc.ComponentDefinition.Sketches.Add(oWorkPlane)
oSketch.Name = "ParametricSketch"
End If

' Get parameter values
Dim dLength As Double = Parameter("Length")
Dim dLength2 As Double = Parameter("Length2")
Dim dHalfLength As Double = dLength / 2

' Start editing the sketch
oSketch.Edit()

' Delete existing lines if any
For i As Integer = oSketch.SketchLines.Count To 1 Step -1
oSketch.SketchLines.Item(i).Delete()
Next

' Create transient geometry object for points
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

' Define the 4 corner points of the U-shape
Dim ptLeftBottom As Point2d = oTG.CreatePoint2d(-dHalfLength, -dLength2) ' Left bottom
Dim ptLeftTop As Point2d = oTG.CreatePoint2d(-dHalfLength, 0) ' Left top (at origin Y)
Dim ptRightTop As Point2d = oTG.CreatePoint2d(dHalfLength, 0) ' Right top (at origin Y)
Dim ptRightBottom As Point2d = oTG.CreatePoint2d(dHalfLength, -dLength2) ' Right bottom

' Create the 3 lines
Dim lineLeft As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptLeftBottom, ptLeftTop) ' Left vertical line
Dim lineHorizontal As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptLeftTop, ptRightTop) ' Horizontal line (centered on origin)
Dim lineRight As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptRightTop, ptRightBottom) ' Right vertical line

' Exit sketch edit mode
oSketch.ExitEdit()

' Update the part
oPartDoc.Update()

End Sub

' Helper function to check if parameter exists
Function ParameterExists(paramName As String) As Boolean
Try
Dim testParam = Parameter(paramName)
Return True
Catch
Return False
End Try
End Function

 

0 Likes
Accepted solutions (1)
239 Views
4 Replies
Replies (4)
Message 2 of 5

chris
Advisor
Advisor

I'm trying to mimic this that I built in html: 

https://www.cruisercarry.com/WebApps/USketch.html

 

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi Chris.  Looks like you just needed to add the Radius value into the code, then use the SketchArcs.AddByFillet method a couple times.  That method can be a little odd, but I got it working locally before posting it.  The first 3 things are pretty normal, but the last two can throw you off.  I tried the SketchLine.StartSketchPoint of one, then the similar end sketch point of the other, where the two meet at first, but that would not work.  Then I got the Geometry (a 2d line segment) of the two sketch lines involved, and used its mid point, for the last two inputs.  I also had to make a mathematical adjustment, because the Radius value, if supplied as a Double, is expecting centimeters.  I just added '* 2.54' in there to fix that.

Try this edited version:

Sub Main
	' Simple iLogic code to draw 3 lines forming a U-shape
	' No radius functionality - just basic lines

	' Ensure parameters exist with default values
	If Not ParameterExists("Length") Then
		Parameter("Length") = 10.0 ' Default 10 cm
	End If

	If Not ParameterExists("Length2") Then
		Parameter("Length2") = 5.0 ' Default 5 cm
	End If

	' Get the active part document
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument

	' Find or create the sketch on XY plane
	Dim oSketch As PlanarSketch = Nothing

	' Look for existing sketch
	For i As Integer = 1 To oPartDoc.ComponentDefinition.Sketches.Count
		If oPartDoc.ComponentDefinition.Sketches.Item(i).Name = "ParametricSketch" Then
			oSketch = oPartDoc.ComponentDefinition.Sketches.Item(i)
			Exit For
		End If
	Next

	' Create new sketch if not found
	If oSketch Is Nothing Then
		Dim oWorkPlane As WorkPlane = oPartDoc.ComponentDefinition.WorkPlanes.Item(3) ' XY Plane
		oSketch = oPartDoc.ComponentDefinition.Sketches.Add(oWorkPlane)
		oSketch.Name = "ParametricSketch"
	End If

	' Get parameter values
	Dim dLength As Double = Parameter("Length")
	Dim dLength2 As Double = Parameter("Length2")
	Dim dRadius As Double = Parameter("Radius") * 2.54 'convert in to cm
	Dim dHalfLength As Double = dLength / 2

	' Start editing the sketch
	oSketch.Edit()

	' Delete existing lines if any
	For i As Integer = oSketch.SketchLines.Count To 1 Step -1
		oSketch.SketchLines.Item(i).Delete()
	Next

	' Create transient geometry object for points
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

	' Define the 4 corner points of the U-shape
	Dim ptLeftBottom As Point2d = oTG.CreatePoint2d(-dHalfLength, -dLength2) ' Left bottom
	Dim ptLeftTop As Point2d = oTG.CreatePoint2d(-dHalfLength, 0) ' Left top (at origin Y)
	Dim ptRightTop As Point2d = oTG.CreatePoint2d(dHalfLength, 0) ' Right top (at origin Y)
	Dim ptRightBottom As Point2d = oTG.CreatePoint2d(dHalfLength, -dLength2) ' Right bottom

	' Create the 3 lines
	Dim lineLeft As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptLeftBottom, ptLeftTop) ' Left vertical line
	Dim lineHorizontal As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptLeftTop, ptRightTop) ' Horizontal line (centered on origin)
	Dim lineRight As SketchLine = oSketch.SketchLines.AddByTwoPoints(ptRightTop, ptRightBottom) ' Right vertical line
	
	Dim oFillet1 As SketchArc = oSketch.SketchArcs.AddByFillet(lineLeft, lineHorizontal, dRadius, lineLeft.Geometry.MidPoint, lineHorizontal.Geometry.MidPoint)
	Dim oFillet2 As SketchArc = oSketch.SketchArcs.AddByFillet(lineHorizontal, lineRight, dRadius, lineHorizontal.Geometry.MidPoint, lineRight.Geometry.MidPoint)
	
	' Exit sketch edit mode
	oSketch.ExitEdit()

	' Update the part
	oPartDoc.Update()

End Sub

' Helper function to check if parameter exists
Function ParameterExists(paramName As String) As Boolean
	Try
		Dim testParam = Parameter(paramName)
		Return True
	Catch
		Return False
	End Try
End Function

Since the Radius variable of that method says that it it a 'Variant' (Object type in vb.net), then we may be able to use a String there, instead of a Double.  If so, then we can probably just specify the radius as "0.25 in", and skip the units conversion.  So, we could convert the parameter's value to a String, then include the units specifier, to make sure it is understood as inches.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

chris
Advisor
Advisor

@WCrihfield This worked great! One more thing, now that this can work, is it possible to add functionality that would allow the radius to be added or left out?

Example: if all I want is the single line in the X axis, then I don't need the two vertical lines or the radius, but if I want the two vertical lines, then by default I would want the radius included.

 

Currently in my part I have these parameters:

 

Length      (X axis line length)

Length2     (negative Y direction line lengths

Radius       (User defined number)

Verticals    (True/False)

0 Likes
Message 5 of 5

chris
Advisor
Advisor

@WCrihfield No need to look at my last response, I was able to get it figured out, thank you for the assistance!

0 Likes