'//////////////////////////////////////////////////////////////////////////////
'//
'//  Copyright 2025 Autodesk, Inc.  All rights reserved.
'//
'//  Use of this software is subject to the terms of the Autodesk license 
'//  agreement provided at the time of installation or download, or which 
'//  otherwise accompanies this software in either electronic or hard copy form.   
'//
'//////////////////////////////////////////////////////////////////////////////
'

Imports System.Runtime.InteropServices

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.Interop

Imports GEAuto
Imports AcadmAuto
Imports SymBBAuto
Imports AcadmAuto.McadObjectType
Imports SymBBAuto.McadUndefinedSize
Imports SymBBAuto.McadUndefinedSizeDirection

Imports System.Text.RegularExpressions


Public Class frmEdge
    ' Declaration of global variables
    Dim acmApp As AcadmApplication
    Dim symbb As McadSymbolBBMgr
    Dim acadApp As AcadApplication = Library.COM.getActiveObject("AutoCAD.Application.26")
    Dim activeDoc As AcadDocument = acadApp.ActiveDocument
    Dim stdmgr As McadStandardMgr
    Dim edgeStd As McadEdgeStandard

    Public isSymSupported As Boolean

    Protected Friend Sub New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'define get the sybmbol manager
        Dim symmgr As McadSymbolBBMgr
        symmgr = activeDoc.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr.27")
        'get the standard manager
        stdmgr = symmgr.StandardMgr
        'define and get the current standard
        Dim std As McadStandard
        std = stdmgr.CurrentStandard

        'Exit if the current standard does not support edge symbol
        Call IssupportEdgeStd(std)
        If Not isSymSupported Then
            Exit Sub
        End If

        'get the edge standard from the current standard.
        edgeStd = std.EdgeStandard

        Call init()
    End Sub

    'check if the current standard support edge symbol
    Private Sub IssupportEdgeStd(ByRef std As McadStandard)
        isSymSupported = False
        Dim stdnames As String()
        stdnames = std.SymbolStandardsSupported
        For i As Integer = 0 To stdnames.Length - 1
            If String.Compare("AmdtEdgeStd", stdnames(i), StringComparison.OrdinalIgnoreCase) = 0 Then
                isSymSupported = True
                Exit Sub
            End If
        Next i
    End Sub

    Private Sub init()
        txtRevision.Text = edgeStd.Revision()

        If edgeStd.FullList = True Then
            rbFullList.Checked = True
        Else
            rbSimplified.Checked = True
        End If

        If edgeStd.AllAroundAllowed = False Then
            ckAllAround.Enabled = False
        End If

        ' This is supported only for DIN standard. 
        If edgeStd.StandardNumAllowed = False Then
            ckStandardNumber.Enabled = False
        End If

        cbBur.SelectedIndex = 0

        cbUndefSizeDirection.SelectedIndex = 0
    End Sub

    ' Create button click event handle
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        acmApp = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication.27")
        Dim util As McadUtility
        util = acmApp.ActiveDocument.Utility

        Me.Hide()

        Dim attachPt(0 To 2) As Double
        Dim attachObj As IMcadGeometry = Nothing
        Dim pick As McadPick

        'Allow user to pick on a object in modelspace
        Try
            pick = util.Pick("select object to attach:", mcGeometry)

            'Get selected attached object
            attachObj = util.GetObjectFromPick(pick)

            'Get pick point
            Dim pickPt As GePoint
            pickPt = pick.PickPoint
            attachPt(0) = pickPt.X
            attachPt(1) = pickPt.Y
            attachPt(2) = pickPt.Z
        Catch ex As System.SystemException
            MsgBox("No object selected.", vbExclamation, "warning")
        End Try

        'Get leader start point
        Dim startPt As Object = Nothing
        Try
            startPt = activeDoc.Utility.GetPoint(, "Start Point: ")
        Catch ex As SystemException
        End Try
        If (startPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        Dim leaderPts(5) As Double
        leaderPts(0) = startPt(0)
        leaderPts(1) = startPt(1)
        leaderPts(2) = startPt(2)

        'Get leader end point
        Dim endPt As Object = Nothing
        Try
            endPt = activeDoc.Utility.GetPoint(, "Next Point: ")
        Catch ex As SystemException
        End Try
        If (endPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        leaderPts(3) = endPt(0)
        leaderPts(4) = endPt(1)
        leaderPts(5) = endPt(2)

        'Create edge symbol
        Dim edge As McadEdge
        edge = activeDoc.ModelSpace.AddCustomObject("AmdtEdge")

        'Specifies the surface texture to automatically adjusted to the default position on the leader
        edge.Origin = attachPt

        'Set the properties of this edge symbol to the default values
        edge.SetToDefaults()

        'Add leader
        edge.AddLeader(leaderPts)

        'Attach symbol to object
        If Not (attachObj Is Nothing) Then
            edge.AttachGeometry(attachObj, attachPt)
        End If

        'Set properties of edge symbol
        Try
            'Specifies the value of the upper boundary for the edge symbol.
            edge.MaxValue = txtMaxValue.Text
            'Specifies the value of the lower boundary for the edge symbol.
            edge.MinValue = txtMinValue.Text
        Catch
            MsgBox("Invalid input for lower or upper boundary!", vbExclamation, "warning")
            Me.Show()
            Exit Sub
        End Try

        'Specifies the sign("+" or "-") for the edge symbol.
        edge.SignValue = SignValue()

        'Specifies the edge symbol follows new standard revision.
        edge.NewSymbolRev = True
        'Specifies the appearance of the majority symbol.
        edge.Majority = ckMajority.Checked
        'Specifies the all-around designation for the edge symbol.
        edge.AllAround = ckAllAround.Checked
        'Specifies if the edge symbol displays the standard number. 
        edge.StandardNumber = ckStandardNumber.Checked

        'Specifies if the direction of the edge symbol's size is defined.
        edge.Direction = ckDirection.Checked
        'Specifies the direction for the edge symbol's undefined size.
        edge.UndefsizeDirection = Direction()

        'Update symbol
        edge.Update()
        activeDoc.Regen(AcRegenType.acAllViewports)
    End Sub


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    Private Sub cbMajority_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckMajority.CheckedChanged
        Dim majority As System.Windows.Forms.CheckBox
        majority = sender
        If majority.Checked Then
            ckAllAround.Checked = False
            ckAllAround.Enabled = False
        Else
            ckAllAround.Enabled = True
        End If
    End Sub

    Private Sub rbSimplified_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSimplified.CheckedChanged
        edgeStd.FullList = False
    End Sub

    Private Sub rbFullList_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbFullList.CheckedChanged
        edgeStd.FullList = True
    End Sub

    Private Sub cbBur_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBur.SelectedIndexChanged
        Dim burValue As System.Windows.Forms.ComboBox
        burValue = sender
        ' if bur is set as "+" or "-", then disable upper and lower boundary
        If burValue.SelectedIndex = 0 Then
            txtMaxValue.Enabled = True
            txtMinValue.Enabled = True
            'disable direction of undefined size
            cbUndefSizeDirection.Enabled = False
        Else
            txtMaxValue.Text = ""
            txtMaxValue.Enabled = False
            txtMinValue.Text = ""
            txtMinValue.Enabled = False
            If ckDirection.Checked Then
                'enable direction of undefined size direction
                cbUndefSizeDirection.Enabled = True
            Else
                cbUndefSizeDirection.Enabled = False
            End If
        End If
    End Sub

    Private Function SignValue()
        'Declare the sign(\"+\" or \"-\") for the edge symbol the user selects
        Select Case cbBur.Text
            Case "None"
                Return sbDefSize
            Case "With bur"
                Return sbWithBurr
            Case "Without bur"
                Return sbWithoutBurr
            Case Else
                Return Nothing
        End Select

    End Function

    Private Function Direction()
        'Declare the direction for the edge symbol's undefined size the user selects
        Select Case cbUndefSizeDirection.Text
            Case "At top"
                Return sbTop
            Case "At side"
                Return sbSide
            Case Else
                Return Nothing
        End Select

    End Function

    Private Sub ckDirection_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckDirection.CheckedChanged
        If ckDirection.Checked And cbBur.SelectedIndex <> 0 Then
            'enable the direction for the edge symbol's undefined size
            cbUndefSizeDirection.Enabled = True
        Else
            'disable the direction for the edge symbol's undefined size
            cbUndefSizeDirection.Enabled = False
        End If
    End Sub
End Class
