Message 1 of 2
Add item number to sketch symbol in drawing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here is the ilogic code generated by ChatGPT.
With the requirement that: I want to add itemnumber to sketch symbol when selecting object as line, circle, circle arc.
' Prompt the user to select an edge
Dim selectedEdge As Object = ThisApplication.CommandManager.Pick(kEdgeFilter, "Select an edge (Line, Circle, or Arc):")
If selectedEdge Is Nothing Then
MessageBox.Show("No edge was selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
' Ensure the selected object is a valid edge
Dim edge As Edge = TryCast(selectedEdge, Edge)
If edge Is Nothing Then
MessageBox.Show("The selected object is not a valid edge!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
' Get the active sheet and associated drawing view
Dim activeSheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet
Dim drawingCurve As DrawingCurve = edge.DrawingCurves(1)
Dim drawingView As DrawingView = drawingCurve.Parent
' Try to retrieve the Item Number from the BOM
Dim itemNumber As String = "N/A"
Dim occurrence As ComponentOccurrence = TryCast(edge.Parent, ComponentOccurrence)
If occurrence IsNot Nothing Then
Dim bomView As BOMView = occurrence.BOMView
If bomView IsNot Nothing Then
itemNumber = bomView.ItemNumber
End If
End If
' Determine the placement position of the symbol and leader based on the edge geometry
Dim symbolPosition As Point2d
Dim leaderPoint As Point2d
If TypeOf edge.Geometry Is LineSegment Then
' For straight lines: Use the midpoint
Dim line As LineSegment = CType(edge.Geometry, LineSegment)
Dim midPoint As Point = line.MidPoint
symbolPosition = drawingView.ModelToSheetSpace(midPoint)
leaderPoint = symbolPosition
ElseIf TypeOf edge.Geometry Is Circle Then
' For circles: Use the center for the symbol and a point on the circumference for the leader
Dim circle As Circle = CType(edge.Geometry, Circle)
Dim centerPoint As Point = circle.Center
Dim radiusVector As Vector = circle.Center.VectorTo(circle.StartPoint).ScaleBy(0.5)
Dim radiusPoint As Point = radiusVector.EndPoint
symbolPosition = drawingView.ModelToSheetSpace(centerPoint)
leaderPoint = drawingView.ModelToSheetSpace(radiusPoint)
ElseIf TypeOf edge.Geometry Is CircularArc Then
' For arcs: Use the midpoint of the arc
Dim arc As CircularArc = CType(edge.Geometry, CircularArc)
Dim evaluator As CurveEvaluator = arc.Evaluator
Dim midParameter As Double = (arc.StartParameter + arc.EndParameter) / 2
Dim midPoint As Point
evaluator.GetPointAtParam(midParameter, midPoint)
symbolPosition = drawingView.ModelToSheetSpace(midPoint)
leaderPoint = symbolPosition
Else
' Unsupported geometry
MessageBox.Show("Selected edge type is not supported! Only Line, Circle, and Arc are allowed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
' Add the sketch symbol to the sheet
Try
' Ensure the symbol definition exists
Dim symbolDef As SketchSymbolDefinition = activeSheet.SketchSymbolDefinitions.Item("ItemNumberSymbol")
Dim sketchSymbol As SketchSymbol = activeSheet.SketchSymbols.Add(symbolDef, symbolPosition)
' Update the symbol's text with the Item Number
sketchSymbol.TextBoxes.Item(1).Text = itemNumber
' Add a leader line pointing to the symbol
Dim leaderPoints As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
leaderPoints.Add(leaderPoint)
leaderPoints.Add(symbolPosition)
sketchSymbol.AddLeader(leaderPoints)
MessageBox.Show("Sketch symbol successfully added!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error adding the sketch symbol: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MessageBox.Show("Type name: " & TypeName(oEdge.Geometry))