'
'  (C) Copyright 2007-2008 by Autodesk, Inc.  All rights reserved.
'
' Permission to use, copy, modify, and distribute this software in
' object code form for any purpose and without fee is hereby granted, 
' provided that the above copyright notice appears in all copies and 
' that both that copyright notice and the limited warranty and
' restricted rights notice below appear in all supporting 
' documentation.
'
' AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
' AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
' MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
' DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
' UNINTERRUPTED OR ERROR FREE.
'
' Use, duplication, or disclosure by the U.S. Government is subject to 
' restrictions set forth in FAR 52.227-19 (Commercial Computer
' Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
' (Rights in Technical Data and Computer Software), as applicable.
'

Imports SymBBAuto
Imports SymBBAuto.McadTaperSlopeSymType
Imports SymBBAuto.McadOrientation
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.ApplicationServices
Imports AcadmAuto
Imports GEAuto

Public Class frmTaper
    Dim app As AcadApplication = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18")
    Dim activeDoc As AcadDocument = app.ActiveDocument

    'define and get the symbol manager
    Dim symbb As McadSymbolBBMgr = activeDoc.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr.3")
    Dim stdmgr As McadStandardMgr = symbb.StandardMgr
    Dim std As McadStandard = stdmgr.CurrentStandard
    Public taperStd As McadTaperStandard = std.TaperStandard

    Public taper As McadTaper = activeDoc.ModelSpace.AddCustomObject("AcmTaper")

    Dim symbolList(0 To 3) As String

    Private Sub frmTaper_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim acmApp As AcadmApplication = activeDoc.Application.GetInterfaceObject("AcadmAuto.AcadmApplication")
        Dim util As IMcadUtility = acmApp.ActiveDocument.Utility

        Me.Hide()
        Dim attachPt(0 To 2) As Double
        Dim attachObj As IMcadGeometry = Nothing

        Try
            'Allow user to pick on an object in the model space
            Dim pick As McadPick = util.Pick("select object to attach:", McadObjectType.mcGeometry)
            'Get selected attached object
            attachObj = util.GetObjectFromPick(pick)
            Dim pickPt As GePoint
            pickPt = pick.PickPoint
            attachPt(0) = pickPt.X
            attachPt(1) = pickPt.Y
            attachPt(2) = pickPt.Z
        Catch ex As Exception
        End Try

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

        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 Exception
            Me.Close()
            Exit Sub
        End Try

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

        'set Origin
        taper.Origin = attachPt

        'set to defaults
        taper.SetToDefaults()

        'add leader for this taper
        taper.AddLeader(leaderPts)

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

        'set all symbols
        symbolList(0) = "Taper 1"
        symbolList(1) = "Taper 2"
        symbolList(2) = "Slope 1"
        symbolList(3) = "Slope 2"

        GetSelectedSubType()

        'get display string
        txtDisplayStr.Text = taper.DisplayString

        'get orientation
        Dim dirIndx As McadOrientation = taper.Orientation
        cbDirection.SelectedIndex = dirIndx

        'get frame
        If taper.Inspect = True Then
            ckFrame.Checked = True
        Else
            ckFrame.Checked = False
        End If

        Me.Show()
    End Sub
    Private Sub GetSelectedSubType()
        'get offered symbols
        cbSubType.Items.Clear()
        Dim item As String
        Dim symbols() As Integer
        symbols = taperStd.OfferedSymbols
        Dim i As Integer = 0
        Dim count As Integer = 0 ' calculate symbols
        For i = LBound(symbols) To UBound(symbols)
            If symbols(i) = 1 Then
                count = count + 1
            End If
        Next i

        'When there are four offered symbols, all of symbolList should be displayed.
        If (count = 4) Then
            For i = LBound(symbolList) To UBound(symbolList)
                item = symbolList(i)
                cbSubType.Items.Add(item)
            Next
        Else 'When there are only three offered symbols, all of symbolList should be displayed except Taper 2.
            For i = LBound(symbolList) To UBound(symbolList)
                If i <> 1 Then
                    item = symbolList(i)
                    cbSubType.Items.Add(item)
                End If
            Next
        End If

        Dim type As McadTaperSlopeSymType = taper.SubSymbolType
        item = symbolList(type)

        'when current symbol is unchecked in Settings,select the first one in the sub type dropdown
        If (cbSubType.FindStringExact(item) = -1) Then
            cbSubType.SelectedIndex = 0
        Else
            cbSubType.Text = item
        End If
    End Sub

    Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
        Dim settingsDialog As New frmSettings
        settingsDialog.initialize(taperStd)
        Application.ShowModalDialog(settingsDialog)
    End Sub

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

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ApplyChanges()
        Me.Close()
    End Sub

    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        ApplyChanges()
    End Sub

    Private Sub ApplyChanges()
        'set display string
        taper.DisplayString = txtDisplayStr.Text

        'set sub symbol type
        taper.SubSymbolType = SymbolType()

        'set direction
        taper.Orientation = OrientationType()

        'set theoretically exact frame is on or off
        taper.Inspect = ckFrame.Checked

        'update drawing
        taper.Update()
        activeDoc.Regen(Common.AcRegenType.acActiveViewport)
    End Sub

    Private Function SymbolType()
        Select Case cbSubType.Text
            Case "Taper 1"
                Return sbTaper1
            Case "Taper 2"
                Return sbTaper2
            Case "Slope 1"
                Return sbSlope1
            Case "Slope 2"
                Return sbSlope2
            Case Else
                Return Nothing
        End Select
    End Function

    Private Function OrientationType()
        Select Case cbDirection.Text
            Case "Left"
                Return sbLeftSide
            Case "Right"
                Return sbRightSide
            Case Else
                Return Nothing
        End Select
    End Function
End Class