'//////////////////////////////////////////////////////////////////////////////
'//
'//  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 SymBBAuto
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public Class frmDetail
    Dim acadApp As AcadApplication = Library.COM.getActiveObject("AutoCAD.Application.26")
    Dim activeDoc As AcadDocument = acadApp.ActiveDocument
    Dim symbb As McadSymbolBBMgr = activeDoc.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr.27")

    Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Hide()

        ' Check the validity of user input for scale
        Dim scale As Double
        Try
            scale = CType(txtDetailScale.Text, Double)
        Catch
            MsgBox("Invalid input! Please input a numeric value for scale", vbExclamation, "warning")
            Me.Show()
            Exit Sub
        End Try

        'Create detail symbol in model space
        Dim detailSym As McadDetailSymbol
        detailSym = activeDoc.ModelSpace.AddCustomObject("AmdtDetailSym")

        'Set to default
        detailSym.SetToDefaults()

        'Get an entity to set detail source geometry
        Dim sourceGeom As AcadEntity = Nothing
        Dim pickPoint(0 To 2) As Double
        Try
            Call activeDoc.Utility.GetEntity(sourceGeom, pickPoint, "Select object: ")
        Catch ex As SystemException
        End Try

        If (sourceGeom Is Nothing) Then
            MsgBox("No object selected", vbExclamation, "warning")
            Me.Show()
            Exit Sub
        End If

        Try
            'Set source geometry
            Call detailSym.SetSourceGeometry(sourceGeom)
        Catch
            MsgBox("Invalid object selected! ", vbExclamation, "Error")
            Me.Show()
            Exit Sub
        End Try

        'Set detail scale
        detailSym.DetailScale = scale

        'Show or hide annotation leader
        detailSym.ShowAnnotationLeader = ckAnnoLeader.Checked

        'Set label pattern
        detailSym.LabelPattern = txtLabelPattern.Text

        'Set label name
        detailSym.LabelName = txtLabelName.Text

        'Get the center point for detail sym
        Dim centerPt As Object = Nothing
        Try
            centerPt = activeDoc.Utility.GetPoint(, "Center point for detail symbol: ")
        Catch ex As SystemException
        End Try

        If (centerPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        'Create label for the detail symbol
        Call detailSym.CreateLabelEntity(centerPt)

        detailSym.Update()
        Dim acAllViewports As AcRegenType = Nothing
        activeDoc.Regen(acAllViewports)
    End Sub

    Private Sub Init()
        'Get the standard manager object
        Dim stdmgr As McadStandardMgr
        stdmgr = symbb.StandardMgr

        'Get current standard
        Dim std As McadStandard
        std = stdmgr.CurrentStandard

        'Get detail symbol standard in the current standard
        Dim detailstd As McadDetailSymbolStandard
        detailstd = std.DetailSymbolStandard

        txtLabelPattern.Text = detailstd.LabelPattern()
        ckAnnoLeader.Checked = detailstd.ShowAnnotationLeader

        txtLabelName.Text = "A"
        txtDetailScale.Text = 2.0
    End Sub

    Protected Friend Sub New()
        ' This call is required by the Windows Form Designer.
        Try
            InitializeComponent()
            Call Init()
        Catch ex As System.SystemException
            MsgBox("Create object unsucessfully.", vbExclamation, "warning")
        End Try

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

End Class