'//////////////////////////////////////////////////////////////////////////////
'//
'//  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.Windows.Forms

Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports SymBBAuto
Imports AcadmAuto
Imports GEAuto

Public Class SelectTargetType
    Dim acmApp As AcadmAuto.AcadmApplication
    Dim acadApp As AcadApplication = Library.COM.getActiveObject("AutoCAD.Application.26")
    Dim activeDoc As AcadDocument = acadApp.ActiveDocument
    Dim util As AcadUtility = activeDoc.Utility
    Private mcGeometry As McadObjectType

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Hide()
        Dim datumTgt As McadDatumTarget
        datumTgt = activeDoc.ModelSpace.AddCustomObject("AcmDatumTarget")

        If attachbtn.Checked Then
            CreateAttachDatumTarget(datumTgt)
        ElseIf nonebtn.Checked Then
            datumTgt.TargetType = McadTargetType.sbNoTarget
            SetDefValues(datumTgt)
            PlaceAndAttach(datumTgt)
        ElseIf pointbtn.Checked Then
            datumTgt.TargetType = McadTargetType.sbPoint
            SetDefValues(datumTgt)
            PlaceAndAttach(datumTgt)
        ElseIf linebtn.Checked Then
            CreateLineDatumTarget(datumTgt)
        ElseIf rectanglebtn.Checked Then
            CreateRectDatumtarget(datumTgt)
        ElseIf circlebtn.Checked Then
            CreateCircDatumtarget(datumTgt)
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub SetDefValues(ByRef datumTgt As McadDatumTarget)
        If datumTgt Is Nothing Then
            Return
        End If

        datumTgt.DimensionValue = "1"
        datumTgt.DatumValue = "A"
    End Sub

    Private Sub CreateAttachDatumTarget(ByRef datumTgt As McadDatumTarget)
        Dim selectObj As Object = Nothing
        Dim pickPt As Object = Nothing

        ' prompt select a datum target
        Try
            util.GetEntity(selectObj, pickPt, "Select datum target:")
        Catch
            MsgBox("No object selected.", vbExclamation, "warning")
            Me.Show()
            Exit Sub
        End Try

        ' return if nothing is selected
        If selectObj Is Nothing Then
            Me.Show()
            Exit Sub
        End If

        ' cast the selected object to datum target object
        Dim pDatumTgt As McadDatumTarget
        pDatumTgt = TryCast(selectObj, McadDatumTarget)

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

        ' attach the selected datum target object to the newly created datum target object
        datumTgt.TargetType = McadTargetType.sbAttach
        SetDefValues(datumTgt)
        datumTgt.SetTargetAttach(pDatumTgt, McadAttachPosition.sbAttachLeft)
    End Sub

    Private Sub PlaceAndAttach(ByRef datumTgt As McadDatumTarget)
        acmApp = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication.27")
        Dim acmUtil As AcadUtility
        acmUtil = acadApp.ActiveDocument.Utility

        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
            Dim mcGeometry As Object = Nothing
            pick = acmUtil.Pick("select object to attach:", mcGeometry)

            'Get selected attached object
            attachObj = acmUtil.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
        End Try

        SetupLeader(datumTgt)

        If Not (attachObj Is Nothing) Then
            datumTgt.AttachGeometry(attachObj, attachPt)
        End If
    End Sub
    Private Sub SetupLeader(ByRef datumTgt As McadDatumTarget)
        'Get leader start point
        Dim startPt As Object = Nothing
        Try
            startPt = util.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 = util.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)

        datumTgt.AddLeader(leaderPts)
    End Sub

    Private Sub CreateLineDatumTarget(ByRef datumTgt As McadDatumTarget)
        datumTgt.TargetType = McadTargetType.sbLine
        SetDefValues(datumTgt)

        ' pick a geometry to attach
        acmApp = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication.27")
        Dim acmUtil As McadUtility
        acmUtil = acmApp.ActiveDocument.Utility

        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 = acmUtil.Pick("select object to attach:", mcGeometry)

            'Get selected attached object
            attachObj = acmUtil.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
        End Try

        ' prompt to specify line start point and end point
        'Get line start point
        Dim startPt As Object = Nothing
        Try
            startPt = util.GetPoint(, "select first line point")
        Catch ex As SystemException
        End Try
        If (startPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        Dim linePts(0 To 2) As Double
        linePts(0) = startPt(0)
        linePts(1) = startPt(1)
        linePts(2) = startPt(2)
        datumTgt.TargetOrigin = linePts

        'Get line end point
        Dim endPt As Object = Nothing
        Try
            endPt = util.GetPoint(, "select second line point")
        Catch ex As SystemException
        End Try
        If (endPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        linePts(0) = endPt(0)
        linePts(1) = endPt(1)
        linePts(2) = endPt(2)
        datumTgt.LineEndPoint = linePts

        SetupLeader(datumTgt)

        If Not (attachObj Is Nothing) Then
            datumTgt.AttachGeometry(attachObj, attachPt)
        End If
    End Sub

    Private Sub CreateRectDatumtarget(ByRef datumTgt As McadDatumTarget)
        datumTgt.TargetType = McadTargetType.sbRectangular
        SetDefValues(datumTgt)

        ' pick a geometry to attach
        acmApp = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication.27")
        Dim acmUtil As McadUtility
        acmUtil = acmApp.ActiveDocument.Utility

        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 = acmUtil.Pick("select object to attach:", mcGeometry)

            'Get selected attached object
            attachObj = acmUtil.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
        End Try

        ' prompt to specify rectangle center point and corner point
        'Get rectangle center point
        Dim startPt As Object = Nothing
        Try
            startPt = util.GetPoint(, "select center point:")
        Catch ex As SystemException
        End Try
        If (startPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        Dim linePts(0 To 2) As Double
        linePts(0) = startPt(0)
        linePts(1) = startPt(1)
        linePts(2) = startPt(2)
        datumTgt.TargetOrigin = linePts

        'Get rectangle corner point
        Dim endPt As Object = Nothing
        Try
            endPt = util.GetPoint(, "select corner point")
        Catch ex As SystemException
        End Try
        If (endPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        linePts(0) = endPt(0)
        linePts(1) = endPt(1)
        linePts(2) = endPt(2)
        'datumTgt.r = linePts

        SetupLeader(datumTgt)
        If Not (attachObj Is Nothing) Then
            datumTgt.AttachGeometry(attachObj, attachPt)
        End If
    End Sub

    Private Sub CreateCircDatumtarget(ByRef datumTgt As McadDatumTarget)
        datumTgt.TargetType = McadTargetType.sbCircular
        SetDefValues(datumTgt)

        ' pick a geometry to attach
        acmApp = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication.27")
        Dim acmUtil As McadUtility
        acmUtil = acmApp.ActiveDocument.Utility

        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 = acmUtil.Pick("select object to attach:", mcGeometry)

            'Get selected attached object
            attachObj = acmUtil.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
        End Try

        ' prompt to specify circle center point and radius
        'Get circle center point
        Dim startPt As Object = Nothing
        Try
            startPt = util.GetPoint(, "select center point:")
        Catch ex As SystemException
        End Try
        If (startPt Is Nothing) Then
            Me.Show()
            Exit Sub
        End If

        Dim linePts(0 To 2) As Double
        linePts(0) = startPt(0)
        linePts(1) = startPt(1)
        linePts(2) = startPt(2)
        datumTgt.TargetOrigin = linePts

        'Get circle radius
        Dim radius As Double
        Try
            radius = util.GetDistance(startPt, "\nradius:")
        Catch ex As SystemException
        End Try
        datumTgt.CircleRadius = radius

        SetupLeader(datumTgt)
        If Not (attachObj Is Nothing) Then
            datumTgt.AttachGeometry(attachObj, attachPt)
        End If
    End Sub
End Class
