'
'  (C) Copyright 2007 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 System.Runtime.InteropServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common.AcRegenType
Imports Autodesk.AutoCAD.Interop.Common.AcActiveSpace
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry

Imports AcadmAuto
Imports SymBBAuto

Public Class frmDetail
    Dim acadApp As AcadApplication = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18")
    Dim activeDoc As AcadDocument = acadApp.ActiveDocument
    Dim symbb As McadSymbolBBMgr = activeDoc.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr")

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