'//////////////////////////////////////////////////////////////////////////////
'//
'//  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 System.Reflection
Imports System.Type

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Imports SymBBAuto.McadOrientation
Imports SymBBAuto.McadWeldSymbolType
Imports SymBBAuto
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

<Assembly: ExtensionApplication(GetType(netScaleArea.netScaleArea))>
<Assembly: CommandClass(GetType(netScaleArea.createCommand))>
Namespace netScaleArea
    Public Class netScaleArea
        Implements Autodesk.AutoCAD.Runtime.IExtensionApplication

        ' Declaration of global variables
        Dim symbb As McadSymbolBBMgr
        Dim acadApp As AcadApplication = Library.COM.getActiveObject("AutoCAD.Application.26")
        Dim ThisDrawing As AcadDocument = acadApp.ActiveDocument
        Private acAllViewports As AcRegenType

        Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize

        End Sub

        Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate

        End Sub

        Public Sub CreateScaleArea()
            symbb = ThisDrawing.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr.27")

            'Make sure we select scale area in the model space
            If ThisDrawing.ActiveSpace = AcActiveSpace.acPaperSpace Then
                ThisDrawing.ActiveSpace = AcActiveSpace.acModelSpace
            End If

            Dim closedCurveId As Long
            Dim objIds(1) As Object

            Dim plineEnt As AcadLWPolyline
            Dim dblVertices() As Double

            'setup initial points for Polyline
            ReDim dblVertices(7)
            dblVertices(0) = 1 : dblVertices(1) = 2
            dblVertices(2) = 2 : dblVertices(3) = 2
            dblVertices(4) = 2 : dblVertices(5) = 1
            dblVertices(6) = 1 : dblVertices(7) = 1

            'Draw Polyline and get its ID
            plineEnt = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblVertices)
            plineEnt.Closed = True
            closedCurveId = plineEnt.ObjectID
            plineEnt.Update()

            Dim varCenter(2) As Double
            Dim dblRadius As Double
            Dim cleEnt As AcadCircle

            varCenter(0) = 1.4 : varCenter(1) = 1.3 : varCenter(2) = 0
            dblRadius = 0.5

            'draw the Circle
            cleEnt = ThisDrawing.ModelSpace.AddCircle(varCenter, dblRadius)
            objIds(0) = cleEnt.ObjectID
            cleEnt.Update()

            Dim varStart(2) As Double
            Dim varEnd(2) As Double
            Dim lineEnt As AcadLine

            varStart(0) = 1.3 : varStart(1) = 1.3 : varStart(2) = 0
            varEnd(0) = 1.7 : varEnd(1) = 1.7 : varEnd(2) = 0

            'Draw the Line
            lineEnt = ThisDrawing.ModelSpace.AddLine(varStart, varEnd)
            objIds(1) = lineEnt.ObjectID
            lineEnt.Update()

            'Get the scale area manager object
            Dim samgr As McadScaleAreaMgr = symbb.ScaleAreaMgr

            'Create scale area object by attached object ID
            Dim sa As McadScaleArea = samgr.CreateScaleArea(closedCurveId)
            If sa Is Nothing Then
                MsgBox("Fail to create scale area")
                Exit Sub
            End If

            'Modify properties of scale area
            sa.ScaleType = McadScaleType.sbScalePf
            sa.TextScalingFactor = 1

            sa.SetBasicScale()

            'rescale selected objects
            If VarType(objIds(0)) = VariantType.Integer Then
                'In 32 bit OS, data type of ObjectID is Integer
                Dim objIdsInt(1) As Integer
                objIdsInt(0) = objIds(0)
                objIdsInt(1) = objIds(1)
                sa.RescaleObjects(objIdsInt)
            ElseIf VarType(objIds(0)) = VariantType.Long Then
                'In 64 bit OS, data type of ObjectID is Long
                Dim objIdsLong(1) As Long
                objIdsLong(0) = objIds(0)
                objIdsLong(1) = objIds(1)
                sa.RescaleObjects(objIdsLong)
            End If

            'Create viewport with specified position in the specified layout
            Dim viewPos(2) As Double
            viewPos(0) = 4 : viewPos(1) = 4 : viewPos(2) = 0
            sa.CreateViewPort("Layout1", viewPos)

            ThisDrawing.Regen(acAllViewports)
        End Sub

        Public Sub EditScaleArea()
            symbb = ThisDrawing.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr.27")

            'Make sure we select scale area in the model space
            If ThisDrawing.ActiveSpace = AcActiveSpace.acPaperSpace Then
                ThisDrawing.ActiveSpace = AcActiveSpace.acModelSpace
            End If

            'Allow user to pick on a object in the model space
            Dim objDrawingObject As AcadEntity = Nothing
            On Error Resume Next
            ThisDrawing.Utility.GetEntity(objDrawingObject, Nothing, "Select scale area to modify:")
            If objDrawingObject Is Nothing Then
                MsgBox("You did not pick an object")
                Exit Sub
            End If

            Dim borderId As Long
            borderId = objDrawingObject.ObjectID

            'Get the scale area manager object
            Dim samgr As McadScaleAreaMgr = symbb.ScaleAreaMgr

            'Get scale area object by specified scale area ID
            Dim sa As McadScaleArea = samgr.FindScaleArea(borderId, False)
            If sa Is Nothing Then
                MsgBox("Fail to find scale area")
                Exit Sub
            End If

            'Update scale type and reset scale value
            sa.ScaleType = McadScaleType.sbScaleDf
            sa.LengthScalingFactor = 0.5

            'Delete attached viewport
            sa.DeleteViewPort()

            'Move scale area
            Dim newPos(2) As Double
            newPos(0) = 3 : newPos(1) = 3 : newPos(2) = 0
            sa.MoveBorder(newPos)

            'If uncommenting the below, you can delete scale area as you need
            'samgr.DeleteScaleArea(sa)

            ThisDrawing.Regen(acAllViewports)
        End Sub
    End Class

    Public Class createCommand

        ' Declaration of global variables
        Dim sa As New netScaleArea

        <Autodesk.AutoCAD.Runtime.CommandMethod("CREATESCAREA")>
        Public Sub CreateScaleArea()
            ' Create Scale Area
            sa.CreateScaleArea()
        End Sub

        <Autodesk.AutoCAD.Runtime.CommandMethod("MODIFYSCAREA")>
        Public Sub ModifyScaleArea()
            ' Modify scale area
            sa.EditScaleArea()
        End Sub
    End Class

End Namespace