- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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)
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
Solved! Go to Solution.