Add item number to sketch symbol in drawing

Add item number to sketch symbol in drawing

tonythm
Advocate Advocate
153 Views
1 Reply
Message 1 of 2

Add item number to sketch symbol in drawing

tonythm
Advocate
Advocate

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))

 

0 Likes
154 Views
1 Reply
Reply (1)
Message 2 of 2

tonythm
Advocate
Advocate

Here is ilogic by Copilot.

Hope it hepls you.

' Get the active drawing document
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument

' Get the selected objects
Dim oSelectedObjects As ObjectsEnumerator = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select an object in the drawing:")

' Check if objects are selected
If oSelectedObjects.Count > 0 Then
    ' Get the first selected object
    Dim oSelectedObject As Object = oSelectedObjects.Item(1)

    ' Calculate the midpoint of the selected object (if possible)
    Dim oMidPoint As Point2d = Nothing
    If TypeOf oSelectedObject Is EdgeProxy Then
        ' If the object is an edge, calculate the midpoint of the edge
        Dim oEdge As EdgeProxy = oSelectedObject
        oMidPoint = ThisApplication.TransientGeometry.CreatePoint2d((oEdge.StartVertex.Geometry.X + oEdge.StopVertex.Geometry.X) / 2, _
                                                                    (oEdge.StartVertex.Geometry.Y + oEdge.StopVertex.Geometry.Y) / 2)
    ElseIf TypeOf oSelectedObject Is CircleProxy Then
        ' If the object is a circle, get the center of the circle
        Dim oCircle As CircleProxy = oSelectedObject
        oMidPoint = oCircle.Center
    ElseIf TypeOf oSelectedObject Is ArcProxy Then
        ' If the object is an arc, calculate the midpoint of the arc
        Dim oArc As ArcProxy = oSelectedObject
        oMidPoint = ThisApplication.TransientGeometry.CreatePoint2d((oArc.StartPoint.X + oArc.EndPoint.X) / 2, _
                                                                    (oArc.StartPoint.Y + oArc.EndPoint.Y) / 2)
    Else
        ' Object is not suitable, notify the user
        MessageBox.Show("The selected object is: " & TypeName(oSelectedObject) & ". Please select an edge, circle, or arc.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Return
    End If

    ' Create the point to place the Sketch Symbol 10mm to the right (positive x direction) of the midpoint
    Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oMidPoint.X + 10, oMidPoint.Y)

    ' Get the 3D model document from the selected object
    Dim oModelDoc As Document = oSelectedObject.Parent.Parent.Document

    ' Get the body name
    Dim textToDisplay As String = ""
    Dim oCompDef As ComponentDefinition = oModelDoc.ComponentDefinition
    If TypeOf oCompDef Is PartComponentDefinition Then
        Dim oPartCompDef As PartComponentDefinition = oCompDef
        Dim oBody As SurfaceBody = oPartCompDef.SurfaceBodies.Item(1)
        textToDisplay = "Body Name: " & oBody.Name
    Else
        MessageBox.Show("The selected object is not a solid body. Please select again.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Return
    End If

    ' Create the Sketch Symbol at the calculated point
    Dim oSheet As Sheet = oDoc.Sheets.Item(1)
    Dim oSketchSymbol As SketchSymbol = oSheet.SketchSymbols.Add(oSheet.SketchSymbolsDefinitions.Item("MySketchSymbol"), oPoint)

    ' Insert the information into the textbox of the Sketch Symbol
    Dim oTextBox As TextBox = oSketchSymbol.TextBoxes.Item(1)
    oTextBox.Text = textToDisplay

    ' Add Leader to the Sketch Symbol from the midpoint of the selected object
    Dim oLeader As Leader = oSketchSymbol.AddLeader(oMidPoint)
    oLeader.AddVertex(oPoint)
End If

 

Imports System.Windows.Forms

Module iLogicModule
    ' Function to show custom dialog
    Public Function ShowCustomDialog() As String
        Dim form As New Form With {
            .Width = 300,
            .Height = 150,
            .Text = "Choose Option"
        }

        ' Create "Manual" button
        Dim btnManual As New Button With {
            .Text = "Manual",
            .Left = 50,
            .Width = 75,
            .Top = 50
        }
        AddHandler btnManual.Click, Sub(sender, e)
                                        form.Tag = "Manual"
                                        form.Close()
                                    End Sub
        form.Controls.Add(btnManual)

        ' Create "Auto" button
        Dim btnAuto As New Button With {
            .Text = "Auto",
            .Left = 150,
            .Width = 75,
            .Top = 50
        }
        AddHandler btnAuto.Click, Sub(sender, e)
                                      form.Tag = "Auto"
                                      form.Close()
                                  End Sub
        form.Controls.Add(btnAuto)

        form.ShowDialog()
        Return form.Tag.ToString()
    End Function

    ' Main function
    Public Sub Main()
        Dim choice As String = ShowCustomDialog()

        ' Get the active drawing document
        Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument

        ' Get the selected objects
        Dim oSelectedObjects As ObjectsEnumerator = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select an object in the drawing:")

        ' Check if objects are selected
        If oSelectedObjects.Count > 0 Then
            ' Get the first selected object
            Dim oSelectedObject As Object = oSelectedObjects.Item(1)

            ' Calculate the midpoint of the selected object (if possible)
            Dim oMidPoint As Point2d = Nothing
            If TypeOf oSelectedObject Is EdgeProxy Then
                ' If the object is an edge, calculate the midpoint of the edge
                Dim oEdge As EdgeProxy = oSelectedObject
                oMidPoint = ThisApplication.TransientGeometry.CreatePoint2d((oEdge.StartVertex.Geometry.X + oEdge.StopVertex.Geometry.X) / 2, _
                                                                            (oEdge.StartVertex.Geometry.Y + oEdge.StopVertex.Geometry.Y) / 2)
            ElseIf TypeOf oSelectedObject Is CircleProxy Then
                ' If the object is a circle, get the center of the circle
                Dim oCircle As CircleProxy = oSelectedObject
                oMidPoint = oCircle.Center
            ElseIf TypeOf oSelectedObject Is ArcProxy Then
                ' If the object is an arc, calculate the midpoint of the arc
                Dim oArc As ArcProxy = oSelectedObject
                oMidPoint = ThisApplication.TransientGeometry.CreatePoint2d((oArc.StartPoint.X + oArc.EndPoint.X) / 2, _
                                                                            (oArc.StartPoint.Y + oArc.EndPoint.Y) / 2)
            Else
                ' Object is not suitable, notify the user
                MessageBox.Show("The selected object is: " & TypeName(oSelectedObject) & ". Please select an edge, circle, or arc.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            ' Create the point to place the Sketch Symbol 10mm to the right (positive x direction) of the midpoint
            Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oMidPoint.X + 10, oMidPoint.Y)

            ' Get the 3D model document from the selected object
            Dim oModelDoc As Document = oSelectedObject.Parent.Parent.Document

            ' Check the user's choice
            Dim textToDisplay As String = ""
            If choice = "Manual" Then
                ' Assign body name
                Dim oCompDef As ComponentDefinition = oModelDoc.ComponentDefinition
                If TypeOf oCompDef Is PartComponentDefinition Then
                    Dim oPartCompDef As PartComponentDefinition = oCompDef
                    Dim oBody As SurfaceBody = oPartCompDef.SurfaceBodies.Item(1)
                    textToDisplay = "Body Name: " & oBody.Name
                Else
                    MessageBox.Show("The selected object is not a solid body. Please select again.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Return
                End If
            ElseIf choice = "Auto" Then
                ' Get BOM from 3D model
                Dim oBOM As BOM = oModelDoc.ComponentDefinition.BOM
                oBOM.StructuredViewFirstLevelOnly = True

                ' Iterate through each item in BOM
                For Each oBOMRow As BOMRow In oBOM.BOMRows
                    ' Check if the object is a component or assembly
                    If oBOMRow.ComponentDefinitions(1).Document.FullDocumentName = oModelDoc.FullDocumentName Then
                        textToDisplay = "Item Number: " & oBOMRow.ItemNumber
                        Exit For
                    End If
                Next
            Else
                ' Invalid choice
                MessageBox.Show("Invalid choice. Please select 'Manual' or 'Auto'.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            ' Create the Sketch Symbol at the calculated point
            Dim oSheet As Sheet = oDoc.Sheets.Item(1)
            Dim oSketchSymbol As SketchSymbol = oSheet.SketchSymbols.Add(oSheet.SketchSymbolsDefinitions.Item("MySketchSymbol"), oPoint)

            ' Insert the information into the textbox of the Sketch Symbol
            Dim oTextBox As TextBox = oSketchSymbol.TextBoxes.Item(1)
            oTextBox.Text = textToDisplay

            ' Add Leader to the Sketch Symbol from the midpoint of the selected object
            Dim oLeader As Leader = oSketchSymbol.AddLeader(oMidPoint)
            oLeader.AddVertex(oPoint)
        End If
    End Sub
End Module

' Call the Main function to start the process
iLogicModule.Main()
0 Likes